/**
 * 采购订单行明细数据选择
 *
 * Created by likui on 2019/9/26.
 */
'use strict';
var el = {
    itemNo: '#itemNo',
    orderCode: '#orderCode',
    materialCode: '#materialCode',
    supplierCode: '#supplierCode',
    singleSelect: '#singleSelect',
    factoryCode: '#factoryCode',
    dockerCode: '#dockerCode',
    searchForm: "#searchForm",
    tb: '#tb',
    bodyHeight: document.documentElement.clientHeight,
};

$(function () {
    var itemNo = $(el.itemNo).val();
    var orderCode = $(el.orderCode).val();
    var materialCode = $(el.materialCode).val();
    var supplierCode = $(el.supplierCode).val();
    var factoryCode = $(el.factoryCode).val();
    initTable(itemNo, orderCode, materialCode, supplierCode, factoryCode);
});


function initTable(itemNo, orderCode, materialCode, supplierCode, factoryCode) {
    let searchHeight = $(el.searchForm).outerHeight();
    let pager = initGridPagerNew(el.bodyHeight - searchHeight, 37, 40);

    $(el.tb).edatagrid({
        loadMsg: itablei8n.dataWati,
        url: g.ctx+'pc/order/purchaseOrderDetail/rest/list',
        queryParams: {
            itemNo: itemNo,
            orderCode: orderCode,
            materialCode: materialCode,
            supplierCode: supplierCode,
            factoryCode: factoryCode
        },
        singleSelect: $(el.singleSelect).val() === "1",
        pagination: true,
        height: el.bodyHeight - searchHeight,
        width: '100%',
        checkbox: true,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        rownumbers: true,
        columns: [
            [
                {
                    field: 'createTime',
                    title: '创建时间',
                    width: 150,
                    sortable: true,
                    formatter: function (value) {
                        return DateTimeFormatMs(value);
                    }
                },
                {
                    field: 'orderCode',
                    title: 'SAP订单号',
                    width: 80
                },
                {
                    field: 'materialCode',
                    title: '物料编码',
                    width: 100
                },
                {
                    field: 'materialName',
                    title: '物料描述',
                    width: 150
                },
                {
                    field: 'specs',
                    title: '规格',
                    width: 80
                },
                {
                    field: 'brand',
                    title: '品牌',
                    width: 80
                },
                {
                    field: 'num',
                    title: '下单数量',
                    width: 80
                },
                {
                    field: 'noClearNum',
                    title: '未清数量',
                    width: 80
                },
                {
                    field: 'unitName',
                    title: '采购单位',
                    width: 80
                },
                {
                    field: 'price',
                    title: '单价',
                    width: 80
                },
                {
                    field: 'orderTaxAmount',
                    title: '订单头总金额',
                    width: 90
                },
                {
                    field: 'requireDate',
                    title: '需求日期',
                    width: 100,
                    sortable: true
                },
                {
                    field: 'supplierErpCode',
                    title: '供应商编码',
                    width: 80
                },
                {
                    field: 'supplierName',
                    title: '供应商名称',
                    width: 200
                },
                {
                    field: 'zjjcd',
                    title: '紧急程度',
                    width: 100,
                    formatter: function (val) {
                        if (val == 'T特急') {
                            return "<span style='color: red'>" + val + "</span>"
                        } else if (val == '加急') {
                            return "<span style='color: rebeccapurple'>" + val + "</span>"
                        } else {
                            return "<span style='color: black'>" + val + "</span>"
                        }
                    }
                },
                {
                    field: 'createName',
                    title: '创建人',
                    width: 80
                },
                {
                    field: 'state',
                    title: '订单状态',
                    width: 80,
                    formatter: function (value) {
                        if (!value) {
                            return ""
                        }
                        if (value === 1) {
                            return "新建"
                        }
                        if (value === 2) {
                            return "订单审批中"
                        }
                        if (value === 3) {
                            return "订单审批驳回"
                        }
                        if (value === 4) {
                            return "GMP审批中"
                        }
                        if (value === 5) {
                            return "GMP审批驳回"
                        }
                        if (value === 6) {
                            return "待供应商确认"
                        }
                        if (value === 7) {
                            return "确认完成"
                        }
                        if (value === 8) {
                            return "收货完成"
                        }
                        if (value === 9) {
                            return "删除"
                        }
                        if (value === 10) {
                            return "订单审批通过"
                        }
                    }
                },
                {
                    field: 'delFlag',
                    title: '删除标识',
                    width: 80,
                    formatter: function (value) {
                        if (value == 0) {
                            return "未删除"
                        }
                        if (value == 1) {
                            return "<span style=\"color: red;\">已删除</span>"
                        }
                    }
                },
                {
                    field: 'markType',
                    title: '<span style="color: lightskyblue;">计划标识</span>',
                    width: 80,
                    editor: {
                        type: 'combobox',
                        options: {
                            valueField: 'itemValue', textField: 'itemName',
                            data: [{'itemValue': '1', 'itemName': '提货'}, {
                                'itemValue': '2',
                                'itemName': '备货'
                            }],
                            prompt: '请选择计划类型', panelHeight: '100',
                            editable: false
                        }
                    },
                    formatter: function (value) {
                        if (!value) {
                            return "";
                        } else if (value == '1') {
                            return "<span style='color: blue'>提货</span>";
                        } else {
                            return "<span>备货</span>";
                        }

                    }
                },
                {
                    field: 'markCount',
                    title: '<span style="color: lightskyblue;">计划数量</span>',
                    width: 80,
                    editor: {
                        type: 'numberbox',
                        options: {min: 0, precision: 3}
                    }
                },
                {
                    field: 'markDate',
                    title: '<span style="color: lightskyblue;">计划交货日期</span>',
                    width: 80,
                    sortable: true,
                    editor: {
                        type: 'datebox',
                        options: {showSeconds: false, editable: false}
                    }
                },
                {
                    field: 'markUpdateTime',
                    title: '计划修改日期',
                    width: 120,
                    sortable: true,
                    formatter: function (value) {
                        if (!value) {
                            return "";
                        }
                        return DateFormatMs(value);
                    }
                },
                {
                    field: 'replayRequireDate',
                    title: '回复交期',
                    width: 80,
                    sortable: true,
                    formatter: function (value) {
                        if (!value) {
                            return "";
                        }
                        return DateFormatMs(value);
                    }
                },
                {
                    field: 'replayNum',
                    title: '回复数量',
                    width: 80
                },
                {
                    field: 'orderRemark',
                    title: '单头备注',
                    width: 145,
                    formatter: function (value) {
                        if (!value) {
                            return '';
                        }
                        return '<span title="' + value + '">' + value + '</span>';
                    }
                },
                {
                    field: 'orderRow',
                    title: '订单行号',
                    width: 60
                },
                {
                    field: 'paymentMethod',
                    title: '付款条款编码',
                    width: 150
                },
                {
                    field: 'paymentMethodName',
                    title: '付款条款名称',
                    width: 150
                },
                {
                    field: 'mrp',
                    title: 'MRP控制者',
                    width: 100
                },
                {
                    field: 'sapOrderNo',
                    title: '工单单号',
                    width: 110
                },
                {
                    field: 'orderTypeCode',
                    title: '订单类型',
                    width: 80
                },
                {
                    field: 'purchaseGroupCode',
                    title: '采购组编码',
                    width: 100
                },
                {
                    field: 'purchaseGroupName', title: '采购组', width: 120,
                    formatter: function (value, row) {
                        if (!value) {
                            return "";
                        }
                        return row.purchaseGroupCode + "-" + value;
                    }
                },


                {
                    field: 'warningFlag',
                    title: '告警',
                    width: 80,
                    formatter: function (value) {
                        if (value == '1') {
                            return "<span style='color: red'>是</span>"
                        } else {
                            return "<span>否</span>"
                        }
                    }
                },
                {
                    field: 'demandNum',
                    title: '需求跟踪号',
                    width: 80
                },

                {
                    field: 'replayRemark',
                    title: '回复备注',
                    width: 80
                },
                {
                    field: 'currencyName',
                    title: '币种',
                    width: 80
                },
                {
                    field: 'taxAmount',
                    title: '行总金额',
                    width: 100
                },

                {
                    field: 'taxName',
                    title: '税率',
                    width: 150
                },
                {
                    field: 'factoryCode',
                    title: '工厂编码',
                    width: 100
                },
                {
                    field: 'factoryName',
                    title: '工厂名称',
                    width: 100
                },
                {
                    field: 'stockCode',
                    title: '库存地点',
                    width: 80
                },
                {
                    field: 'defaultStock',
                    title: '默认储位',
                    width: 80
                },
                {
                    field: 'materialGroupCode',
                    title: '物料组编码',
                    width: 100
                },
                {
                    field: 'materialGroupName',
                    title: '物料组',
                    width: 200
                },
                {
                    field: 'companyCode',
                    title: '公司编码',
                    width: 70
                },
                {
                    field: 'companyName',
                    title: '公司',
                    width: 200
                }
            ]
        ],
    });
}


/**
 * 选择单个采购订单明细数据
 */
function chooseSingleDetail(callback) {
    var row = $(el.tb).edatagrid('getSelected');
    if (!row) {
        parent.layer.msg('请选择数据', {icon: 2});
        return;
    }
    callback(row);
}

/**
 * 选择多个
 * @param callback
 */
function chooseMuliteDetails(callback) {
    var rows = $(el.tb).edatagrid('getSelections');
    if (rows.length <= 0) {
        parent.layer.msg('请选择数据', {icon: 2});
        return;
    }
    for (let i = 0; i < rows.length; i++) {
        var row = rows[i];
        row.materialName = row.materialDesc;
        row.unclearNum = row.sapUnclearedQuantity;
        row.factory = row.factoryCode;
    }
    callback(rows);
}


