let el = {
    tb: '#tb',
    dataPanel: '#dataPanel',
    planTb: '#plan_tb',
    costTb: '#cost_tb',
    bodyHeight: document.documentElement.clientHeight
};
var cacheData = {};
$(function () {
    $(el.dataPanel).panel({
        collapsible: true,
        onCollapse: function () {
            $(el.tb).datagrid('resize', {height: el.bodyHeight - 80});
        },
        onExpand: function () {
            $(el.tb).datagrid('resize', {height: el.bodyHeight - 350});
        }
    });
    initDict("po_item_type")
    loadGrid();
    initNecessaryDatas('pc/sys/factory/rest/listAll,pc/sys/tax/rest/listAll')
    initPlanGrid();
    initCostGrid();
});

function loadGrid() {
    var h = ((el.bodyHeight - $("#dataForm").height() - 330) * 0.75) <= 200 ? 200 : ((el.bodyHeight - $("#dataForm").height() - 330) * 0.75)
    $(el.tb).datagrid({
        url: g.ctx + 'pc/order/purchaseOrderDetail/rest/list?delFlag=0&orderId=' + $("#id").val()+'&supplierCode1='+supplierCode1,
        method: "GET",
        loadMsg: '数据远程载入中,请等待...',
        singleSelect: true,
        pagination: false,
        height: h,
        columns: [[
            {field: 'ck', checkbox: true},
            {
                field: 'delFlag', title: '是否删除',sortable: true,
                width: '62', align: 'left',
                formatter: function (value) {
                    if (value === 0) {
                        return "否";
                    } else if (value === 1) {
                        return "<span style='color: red;'>是</span>";
                    }
                }
            },
            {
                field: 'orderRow', title: '行项目',sortable: true,
                width: '50', align: 'left'
            },
            {
                field: 'materialCode', title: '物料编码',sortable: true,
                width: '120', align: 'left'
            },
            {
                field: 'materialName', title: '物料名称(品名)',sortable: true,
                width: '260', align: 'left'
                // , editor: {type: 'text'},
                // formatter: function (value) {
                //     debugger
                //     if (value) {
                //         return '<span style="display: inline-block;width: 260px;white-space: nowrap;text-overflow: ellipsis;overflow: hidden;" title="' + value + '">' + value + '</span>';
                //     }
                //     return '';
                // }
            },
            {
                field: 'middleClass', title: '物料中类',sortable: true,
                width: '90', align: 'left'
            },
            {
                field: 'rowTypeCode', title: '项目类别',sortable: true,
                width: '80', align: 'left',
                formatter: function (value) {
                    return getDicName("po_item_type", value);
                }
            },
            {
                field: 'subjectTypeCode',
                title: '科目类别',
                width: 80
            },
            {
                field: 'remark', title: '订单行备注',sortable: true,
                width: '100', align: 'left', editor: {type: 'text'}
            },
            // {
            //     field: 'specs', title: '规格',
            //     width: '90', align: 'left', editor: {type: 'text'}
            // },
            // {
            //     field: 'brand', title: '品牌',
            //     width: '90', align: 'left', editor: {type: 'text'}
            // },
            {
                field: 'unitName', title: '订单单位',sortable: true,
                width: '60', align: 'left',
            },
            {
                field: 'num', title: '订单数量',sortable: true,
                width: '90', align: 'left'
            },
            {
                field: 'replayNum', title: '回复数量',sortable: true,
                width: '90', align: 'left'
            },
            {
                field: 'noClearNum', title: '未清数量',sortable: true,
                width: '90', align: 'left'
            },
            {
                field: 'price', title: '含税单价',sortable: true,
                width: '90', align: 'left'
            },
            {
                field: 'taxCode', title: '税率',sortable: true,
                width: '80', align: 'left',
                formatter: function (value) {
                    return getNecessaryDataName("pc/sys/tax/rest/listAll", value);
                }
            },
            {
                field: 'sumTaxMoney', title: '含税金额',sortable: true,
                width: '80', align: 'left',
                editor: {type: 'numberbox', options: {precision: 2, editable: false}}
            },
            {
                field: 'originalRequireDate', title: '原始需求日期',sortable: true,
                width: '100', align: 'left',
                formatter: function (value) {
                    if (value) {
                        return new Date(value).format("yyyy-MM-dd");
                    }
                    return value;
                }
            },
            {
                field: 'requireDate', title: '需求日期',sortable: true,
                width: '100', align: 'left',
                formatter: function (value) {
                    if (value) {
                        return new Date(value).format("yyyy-MM-dd");
                    }
                    return value;
                }
            },


            {
                field: 'replayRequireDate',sortable: true,
                title: '回复交期',
                width: '100', align: 'left',
                formatter: function (value) {
                    if (value) {
                        return new Date(value).format("yyyy-MM-dd");
                    }
                    return value;
                }
            },

            {
                field: 'replayRemark', title: '回复说明',sortable: true,
                width: '100', align: 'left'
            },


            {
                field: 'factoryCode', title: '收货工厂',sortable: true,
                width: '80', align: 'left',
                formatter: function (value) {
                    return getNecessaryDataName('pc/sys/factory/rest/listAll', value)
                }
            },
            {
                field: 'stockCode', title: '库存地点',sortable: true,
                width: '60', align: 'left',

            },
            {
                field: 'defaultStock', title: '默认储位',sortable: true,
                width: '80', align: 'left',

            },
            {
                field: 'purchaseApplyDeatilId', title: '采购申请明细的id', hidden: true,sortable: true,
                width: '120', align: 'left'
            },

            {
                field: 'materialGroupName', title: '物料组',sortable: true,
                width: '60', align: 'left',

            },
            // {
            //     field: 'demandDepartment', title: '需求部门', hidden: isSupplier,
            //     width: '60', align: 'left', editor: {type: 'text'}
            // },
            {
                field: 'costCenterName', title: '成本中心',sortable: true,
                width: '100', align: 'left', hidden: el.isCost
                /* formatter: function (value) {
                     return getDicName("cost_center", value);
                 }*/
            },
            // {
            //     field: 'demandNum', title: '需求跟踪号',
            //     width: '90', align: 'left'
            // },
            {
                field: 'purchApplyNo', title: '采购申请号', hidden: isSupplier,sortable: true,
                width: '70', align: 'left', editor: {type: 'text'}
            },
            {
                field: 'purchApplyRow', title: '采购申请行', hidden: isSupplier,sortable: true,
                width: '70', align: 'left', editor: {type: 'text'}
            },
            {
                field: 'shouHuoDiZhi', title: '收货地址',sortable: true,
                width: '60', align: 'left'
            },
            {
                field: 'assetNo',sortable: true,
                title: '资产卡片号',
                width: 200
            },
            {
                field: 'returnFlag',
                title: '是否退货',sortable: true,
                width: 100,
                formatter: function (value) {
                    if (value === 'X') {
                        return "是"
                    } else {
                        return "否"
                    }
                }
            },
            {
                field: 'finishFlag', title: '交货已完成',sortable: true,
                width: '70', align: 'left',
                formatter: function (value) {
                    if (value == 'N') {
                        return "否"
                    } else if (value == 'Y') {
                        return "是"
                    }
                }
            },
        ]],
        onCheck: function (index, rowData) {
            loadPlanData(rowData);
            loadCostData(rowData);
        },
    });
}

function initPlanGrid() {
    $(el.planTb).edatagrid({
        method: "GET",
        loadMsg: '数据远程载入中,请等待...',
        singleSelect: false,
        autoSave: true,
        pagination: false,
        rownumbers: true,
        height: 250,
        columns: [[
            {
                field: 'delFlag', title: '是否删除',
                width: '105', align: 'left',
                formatter: function (value, row) {
                    if (value === 1) {
                        return '<span style="color: red;">是</span>'
                    }
                    return '否'
                }
            },
            {
                field: 'purchaseOrder', title: '采购订单号',
                width: '105', align: 'left'
            },
            {
                field: 'lineItem', title: '采购订单行号',
                width: '90', align: 'left'
            },
            {
                field: 'scheduleLine', title: '计划行号',
                width: '90', align: 'left'
            },
            {
                field: 'lastRequireDate', title: '需求日期',
                width: '90', align: 'left',
                formatter: function (value) {
                    if (value) {
                        return new Date(value).format("yyyy-MM-dd");
                    }
                    return value;
                }
            },
            {
                field: 'scheduleDeliveryDate', title: '计划交货日期',
                width: '110', align: 'left',
                formatter: function (value, row) {
                    if (value) {
                        return new Date(value).format("yyyy-MM-dd");
                    }
                    return value;
                }
            },
            {
                field: 'scheduleDeliveryQuantity', title: '计划交货数量',
                width: '90', align: 'left',
                formatter: function (value, row) {
                    if (value) {
                        return value;
                    }
                    return "";
                }
            },
            {
                field: 'purchaseRequest', title: '采购申请',
                width: '90', align: 'left'
            }, {
                field: 'purchaseRequestLine', title: '采购申请行号',
                width: '90', align: 'left'
            },
            {
                field: 'noClearNum', title: '未清数量',
                width: '90', align: 'left',
                formatter: function (value, row) {
                    return Number(row.scheduleDeliveryQuantity)-Number(row.receivedQuantity)
                }
            }, {
                field: 'receivedQuantity', title: '已收货数量',
                width: '90', align: 'left'
            }, {
                field: 'look', title: '组件查看',
                width: '90', align: 'left',
                formatter: function (index, row) {
                    return '<a href="javascript:void(0)" onclick="openComponentDialog(\'' + row.purchaseOrder + '\',\'' + row.lineItem + '\',\'' + row.scheduleLine + '\')">组件查看</a>';
                }
            }, {
                field: 'day1',
                title: 'day1',
                width: 120
            },
            {
                field: 'day2',
                title: 'day2',
                width: 120
            },
            {
                field: 'day3',
                title: 'day3',
                width: 120
            }, {
                field: 'day4',
                title: 'day4',
                width: 120
            }, {
                field: 'day5',
                title: 'day5',
                width: 120
            },
            {
                field: 'day6',
                title: 'day6',
                width: 120
            },
            {
                field: 'day7',
                title: 'day7',
                width: 120
            },
            {
                field: 'week2',
                title: 'week2',
                width: 120
            },
            {
                field: 'week3',
                title: 'week3',
                width: 120
            },
            {
                field: 'week4',
                title: 'week4',
                width: 120
            }, {
                field: 'week5',
                title: 'week5',
                width: 120
            }, {
                field: 'week6',
                title: 'week6',
                width: 120
            },
            {
                field: 'week7',
                title: 'week7',
                width: 120
            },
            {
                field: 'week8',
                title: 'week8',
                width: 120
            },
            {
                field: 'month1',
                title: 'month1',
                width: 120
            },
            {
                field: 'month2',
                title: 'month2',
                width: 120
            },
            {
                field: 'month3',
                title: 'month3',
                width: 120
            }, {
                field: 'month4',
                title: 'month4',
                width: 120
            }
        ]]
    });
}

function initCostGrid() {
    $(el.costTb).edatagrid({
        method: "GET",
        loadMsg: '数据远程载入中,请等待...',
        singleSelect: false,
        autoSave: true,
        pagination: false,
        rownumbers: true,
        height: 250,
        columns: [[
            {
                field: 'purchaseOrder', title: '采购订单号',
                width: '105', align: 'left'
            },
            {
                field: 'lineItem', title: '采购订单行号',
                width: '90', align: 'left'
            }, {
                field: 'generalLedgerAccount', title: '总账科目',
                width: '90', align: 'left'
            }, {
                field: 'costCenter', title: '成本中心',
                width: '90', align: 'left'

            }, {
                field: 'accountAssignmentNumber', title: '科目分配序号',
                width: '90', align: 'left'
            }, {
                field: 'assetCode', title: '资产编码',
                width: '90', align: 'left'
            }, {
                field: 'order_number', title: '订单',
                width: '90', align: 'left'
            }
        ]]
    });
}

function loadPlanData(rowData) {
    if (cacheData[rowData.orderCode + rowData.orderRow] !== undefined) {
        $(el.planTb).datagrid('loadData', cacheData[rowData.orderCode + rowData.orderRow]);
    } else {
        let load = topWin.layer.load(0);
        $.ajax({
            url: g.ctx + 'pc/order/purchaseOrderDetail/rest/planList',
            method: "GET",
            data: {orderCode: rowData.orderCode, row: rowData.orderRow},
            complete: function () {
                topWin.layer.close(load)
            },
            success: function (data) {
                $(el.planTb).datagrid('loadData', data.rows);
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    }

}

function loadCostData(rowData) {
    if (cacheData[rowData.orderCode + rowData.orderRow] !== undefined) {
        $(el.costTb).datagrid('loadData', cacheData[rowData.orderCode + rowData.orderRow]);
    } else {
        let load = topWin.layer.load(0);
        $.ajax({
            url: g.ctx + 'pc/order/purchaseOrderDetail/rest/costList',
            method: "GET",
            data: {orderCode: rowData.orderCode, row: rowData.orderRow},
            complete: function () {
                topWin.layer.close(load)
            },
            success: function (data) {
                $(el.costTb).datagrid('loadData', data.rows);
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    }

}

function openComponentDialog(purchaseOrder, row, scheduleLine) {
    let url = g.ctx + 'pc/order/purchaseOrderDetail/page/componentList?orderCode=' + purchaseOrder + "&row=" + row + "&planLine=" + scheduleLine;
    var componentDialog = openDialog('componentDialog', url, {
            title: "查看组件数据",
            width: '800px',
            height: '600px',
            cache: false,
            modal: true,
            border: false,
            draggable: false,
            shadow: false,
            onClose: function () {
                componentDialog.dialog('destroy');
            },
            buttons: [
                {
                    text: itablei8n.close,
                    handler: function () {
                        componentDialog.dialog('close');
                    }
                }
            ]
        }
    );
}