
let el = {
    tb: '#tb',
    id: '#id',
    useState: '#useState',
}

$(function () {
    initDict('currency,payment_method');
    initNecessaryDatas('pc/sys/factory/rest/listAll');
    initPageSelect();
    loadGrid();
});
/**
 * 初始化页面下拉框数据
 */
function initPageSelect() {
    $("#auditStatus").combobox({
        valueField: 'value',
        textField: 'label',
        editable: false,
        panelHeight: 'auto',
        value: -1,
        data: [
            {label: '全部', value: -1},
            {label: '新建', value: 0},
            {label: '待审批', value: 1},
            {label: '审批通过', value: 2},
            {label: '审批驳回', value: 3},
            {label: '失效', value: 4}
        ]
    });
    $("#syncStatus").combobox({
        valueField: 'value',
        textField: 'label',
        data: [
            {label: '全部', value: -1},
            {label: '新建', value: 0},
            {label: '未同步', value: 1},
            {label: '同步成功', value: 2},
            {label: '变更中', value: 3},
            {label: '失效', value: 4}
        ],
        value: -1,
        editable: false,
        panelHeight: 'auto',
    });
    // 初始化使用状态下拉框
    $(el.useState).combobox({
        valueField: 'value',
        textField: 'label',
        value: -1,
        data: [
            {label: '全部', value: -1},
            {label: '使用', value: 0},
            {label: '未使用', value: 1}
        ],
        editable: false,
        prompt: '-- 请选择 --',
        panelHeight: 'auto'
    });
}

function loadGrid() {
    $('#tb').datagrid({
        url: g.ctx + "pc/tender/quotaManage/quotaManagement/rest/bid/list",
        iconCls: 'icon-list',
        title: '',
        height: gridheight,
        width: gridwidth,
        singleSelect: true,
        pagination: true,
        idField: 'id',
        autoHeight: true,
        loadMsg: "数据加载中......",
        rownumbers: true,
        queryParams: {
            bidQuotaId:$(el.id).val(),
        },
        fitColumns: false,
        remoteSort: true,
        columns: [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'id',
                hidden: true
            },
            {
                field: "quotaCode",
                title: "配额编码",
                width: 120
            },
            // {
            //     field: 'sapQuotaCode',
            //     title: 'sap配额编码',
            //     width: 120,
            //     align: 'left'
            //
            // },
            // {
            //     field: 'sapQuotaNumber',
            //     title: 'sap配额项目号',
            //     width: 100,
            //     align: 'left'
            //
            // },

            {
                field: 'syncStatus',
                title: 'OA状态',
                width: 170,
                align: 'left',
                exportEnum:[
                    {code:4,codeWithDesc:'OA审批驳回'},
                    {code:3,codeWithDesc:'OA审批拒绝'},
                    {code:2,codeWithDesc:'OA审批通过'},
                    {code:1,codeWithDesc:'OA审批中'},
                    {code:0,codeWithDesc:'待提交OA审批'}
                ],
                formatter: function (value, row, index) {
                    if (value === 0) {
                        return "待提交OA审批"
                    } else if (value === 1) {
                        return '<a style="color: blue">OA审批中</a>'
                    } else if (value === 2) {
                        return '<a style="color: green">OA审批通过</a>'
                    } else if (value === 3) {
                        return '<a style="color: red">OA审批拒绝</a>'
                    } else if (value === 4) {
                        return '<a style="color: grey">OA审批驳回</a>'
                    }
                }

            },
            {
                field: 'auditStatus',
                title: '单据状态',
                width: 100,
                align: 'left',
                exportEnum:[
                    {code:4,codeWithDesc:'失效'},
                    {code:3,codeWithDesc:'审批驳回'},
                    {code:2,codeWithDesc:'审批通过'},
                    {code:1,codeWithDesc:'更新'},
                    {code:0,codeWithDesc:'新建'}
                ],
                formatter: function (value, row, index) {
                    switch (value) {
                        case 0:
                            return '<span style="color:green">新建</span>';
                            break;
                        case 1:
                            return '<span style="color:blue">更新</span>';
                        case 2:
                            return '<span style="color:#00e0ee">审批通过</span>';
                            break;
                        case 3:
                            return '<span style="color:#372bff">审批驳回</span>';
                            break;
                        case 4:
                            return '<span style="color:#ff3338">失效</span>';
                            break;
                        default:
                            break;
                    }
                }
            },
            {
                field: 'creater',
                title: '创建人',
                width: 100,
                align: 'left'
            },
            // {
            //     field: 'auditor',
            //     title: '审批人',
            //     width: 100,
            //     align: 'left'
            //
            // }, {
            //     field: 'auditTime',
            //     title: '审批时间',
            //     width: 100,
            //     align: 'left',
            //     exportDateFormatter:'date',
            //     formatter: function (value, row, index) {
            //         if (value == '') {
            //             return '';
            //         }
            //         return moment(value).format("YYYY-MM-DD");
            //     }
            //
            // },
            // {
            //     field: 'validFlag',
            //     title: '是否有效',
            //     width: 100,
            //     align: 'left',
            //     exportEnum:[
            //         {code:1,codeWithDesc:'作废'},
            //         {code:0,codeWithDesc:'有效'}
            //     ],
            //     formatter: function (value, row, index) {
            //         if (value == '') {
            //             return '';
            //         }
            //         return value == 1 ? "作废" : "有效"
            //     }
            //
            // },
            {
                field: 'factoryCode',
                title: '工厂',
                width: 100,
                align: 'left',
                exportEnum:getNecessaryDataList('pc/sys/factory/rest/listAll'),
                formatter:function (value) {
                    return getNecessaryDataName('pc/sys/factory/rest/listAll',value);
                }
            },
            {
                field: 'materialCode',
                title: '物料编码',
                width: 100,
                align: 'left'
            },
            {
                field: 'materialName',
                title: '物料名称',
                width: 100,
                align: 'left'
            },
            {
                field: 'supplierCode',
                title: '供应商编码',
                width: 100,
                align: 'left'
            },
            {
                field: 'supplierName',
                title: '供应商名称',
                width: 100,
                align: 'left'
            },
            {
                field: 'quota',
                title: '配额',
                width: 100,
                align: 'left'
            },
            {
                field: 'reason',
                title: '原因',
                width: 100,
                align: 'left'
            },
            {
                field: 'effectDate',
                title: '生效日期',
                width: 100,
                align: 'left',
                exportDateFormatter:'date',
                formatter: function (value, row, index) {
                    if (value) {
                        return moment(value).format("YYYY-MM-DD");
                    }
                    return '';
                }

            }, {
                field: 'invalidDate',
                title: '失效日期',
                width: 100,
                align: 'left',
                exportDateFormatter:'date',
                formatter: function (value, row, index) {
                    if (value) {
                        return moment(value).format("YYYY-MM-DD");
                    }
                    return '';
                }

            },
            // {
            //     field: 'createTime',
            //     title: '创建日期',
            //     width: 220,
            //     // exportDateFormatter:'dateTime',
            //     formatter: function (value) {
            //         if (value) {
            //             return moment(value).format('YYYY-MM-DD HH:mm:SS');
            //         }
            //         return '';
            //     }
            // }
            {
                field: 'procurementType',
                title: '采购类型',
                width: 100,
                align: 'left',
                exportEnum:[
                    {code:0,codeWithDesc:'标准'},
                    {code:2,codeWithDesc:'寄售'},
                    {code:3,codeWithDesc:'标准委外'}
                ],
                formatter: function (value, row) {
                    if (Number(value) === 0) {
                        return "标准"
                    } else if (Number(value) === 2) {
                        return '寄售'
                    } else if (Number(value) === 3) {
                        return '标准委外'
                    }
                }
            },
             {
                field: 'moq',
                title: '最小订货量',
                width: '7%',
                align: 'left'
            }, {
                field: 'mpq',
                title: '最小包装量',
                width: '7%',
                align: 'left'
            },
            /*  {
                field: 'lastQuota',
                title: '上次配额',
                width: '5.8%',
                align: 'left'
              },*/
            {
                field: 'paymentMethod',
                title: '付款条件',
                width: '8%',
                align: 'left',
                exportEnum:getDictList('payment_method'),
                formatter: function (value) {
                    return getDicName('payment_method',value);
                }
            },
            {
                field: 'leadTime',
                title: '货期',
                width: '8%',
                align: 'left'
            },
            {
                field: 'processId',
                title: 'OA审批流程单号',
                width: 200,
                align: 'left',
                formatter: function (value, row) {
                    return oaTrack(value, OaProcessType.QUOTA_PRICE)
                }
            },
            // {
            //     field: 'lastQuarterInMoney',
            //     title: '上季度入库金额',
            //     width: 100,
            //     align: 'left'
            // },
            // {
            //     field: 'moldNum',
            //     title: '模具套数',
            //     width: 100,
            //     align: 'left'
            // },
            // {
            //     field: 'cavity',
            //     title: '穴数',
            //     width: 100,
            //     align: 'left'
            // },
            // {
            //     field: 'capacity',
            //     title: '产能',
            //     width: 100,
            //     align: 'left'
            // },
            // {
            //     field: 'commitmentAnnex',
            //     title: '承诺书附件id集合',
            //     width: 100,
            //     align: 'left'
            // },
        ]],
        toolbar: [ {
            text: '导出',
            iconCls: 'icon-print',
            id: 'export',
            handler: function () {
                openExportDialog('pc/tender/quotaManage/quotaManagement/rest/export','tb',{bidQuotaId:$(el.id).val()});
                setOperationLog("导出按钮", "配额清单");

            }
        }, "-",
            {
                text: '期初导入',
                iconCls: 'icon-import',
                id: 'btn_import',
                handler: function () {
                    quotaImport();
                }
            }, "-",
            {
                text: '查看',
                id: 'look',
                iconCls: 'icon-find',
                handler: function () {
                    addOrEdit(2);
                    setOperationLog("查看按钮", "配额清单");

                }
            }
            ],
        onLoadSuccess: function () {
            displayButtonByUserRole("BIDQUOTA");
            $(el.tb).datagrid('clearSelections');
        },
    });
}

function addOrEdit(flag) {
    var id= '';
    // if(flag===2){
        var cbox = $('#tb').datagrid('getSelected');
        if (cbox === null || cbox === undefined) {
            layer.msg("请选中一条记录");
            return;
        }
        id = cbox.id;
    // }
    var quotaManagementDialog = openDialog('quotaManagement', g.ctx + 'pc/tender/quotaManage/quotaManagement/detail?id=' + id + "&flag=" + flag + "&quotaCode=" + cbox.quotaCode, {
        title: '查看',
        width: '100%',
        height: '100%',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            quotaManagementDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.close,
                iconCls: 'icon-cancel',
                handler: function () {
                    quotaManagementDialog.dialog('close');
                }
            }
        ]
    });
}
function quotaImport(){
    var importDialog = openImportExcelDialog('static/excel/配额导入模板.xlsx','配额期初导入',function(file){
        let formData = new FormData();
        formData.append("file", file);
        var loading = topWin.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        $.ajax({
            url: g.ctx + 'pc/tender/quotaManage/quotaManagement/rest/import',
            method: "POST",
            data: formData,
            contentType: false,
            processData: false,
            success: function (data) {

                if (data === 'ok') {
                    topWin.layer.alert("导入成功", {icon: 1}, function (idx) {
                        $(el.tb).datagrid('reload');
                        $(el.tb).datagrid('clearSelections');
                        topWin.layer.close(idx);
                        importDialog.dialog('close');
                    });
                } else {
                    topWin.layer.confirm('部分导入成功,是否下载未成功数据?', function (index) {
                        $(el.tb).datagrid('reload');
                        $(el.tb).datagrid('clearSelections');
                        topWin.layer.close(index);
                        window.location.href = g.ctx + 'pc/sys/file/rest/download/' + data
                    });
                }
            },
            complete: function(data) {
                topWin.layer.close(loading);
            },
            error: function (data) {
                topWin.layer.alert(JSON.parse(data.responseText).message,{icon: 2, shadeClose: true, title: '温馨提示'});
            }
        });
    });
}


