
let el = {
    tb: '#tb',
    useState: '#useState',
    edit: '#edit',
    delete: '#delete',
    export: '#export',
}

$(function () {
    initNecessaryDatas('pc/sys/factory/rest/listAll')
    initPageSelect();
    loadGrid();
});
/**
 * 初始化页面下拉框数据
 */
function initPageSelect() {
    $("#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',
    });

    $("#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}
        ]
    });
}

function loadGrid() {
    $('#tb').datagrid({
        url: g.ctx + "pc/tender/quotaManage/quotaManagement/rest/list",
        iconCls: 'icon-list',
        title: '',
        height: gridheight,
        width: gridwidth,
        singleSelect: true,
        pagination: true,
        idField: 'id',
        autoHeight: true,
        loadMsg: "数据加载中......",
        rownumbers: true,
        fitColumns: false,
        remoteSort: true,
        columns: [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'id',
                title: 'id',
                hidden: true
            },
            {
                field: 'auditStatus',
                title: '单据状态',
                width: '10%',
                align: 'left',
                formatter: function (value, row, index) {
                    switch (value) {
                        case 0:
                            return '<span style="color:green">新建</span>';
                        case 1:
                            return '<span style="color:blue">更新</span>';
                        case 2:
                            return '<span style="color:green">审批通过</span>';
                        case 3:
                            return '<span style="color:red">审批驳回</span>';
                        case 4:
                            return '<span style="color:#ff3338">失效</span>';
                        default:
                            break;
                    }
                }
            },
            {
                field: 'syncStatus',
                title: 'OA状态',
                width: '10%',
                align: 'left',
                formatter: function (value, row, index) {
                    // switch (value) {
                    //     case 0:
                    //         return '<span style="color:gray">待同步</span>';
                    //     case 1:
                    //         return '<span style="color:#ff3338">未同步</span>';
                    //     case 2:
                    //         return '<span style="color:#00e0ee">同步成功</span>';
                    //     case 3:
                    //         return '<span style="color:#372bff">变更中</span>';
                    //     case 4:
                    //         return '<span style="color:#ff3338">失效</span>';
                    //     default:
                    //         break;
                    // }

                    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: 'quotaCode',
                title: '单据号',
                width: '10%',
                align: 'left'
            },
            {
                field: 'processId',
                title: 'OA流程单号',
                width: '10%',
                align: 'left',
                formatter: function (value, row) {
                    return oaTrack(value, OaProcessType.QUOTA_PRICE)
                }
            },
            {
                field: 'factoryCode',
                title: '工厂',
                width: '20%',
                align: 'left',
                formatter: function (value) {
                    return getNecessaryDataName('pc/sys/factory/rest/listAll',value)
                }
            },
            {
                field: 'createTime',
                title: '创建时间',
                width: '10%',
                align: 'left',
                formatter: function (data) {
                    if (!data) {
                        return '';
                    }
                    return DateFormatMs(data)
                }
            },
            {
                field: 'creater',
                title: '创建人',
                width: '10%',
                align: 'left'
            },
            {
                field: 'auditTime',
                title: '审批时间',
                width: '10%',
                align: 'left',
                formatter: function (data) {
                    if (!data) {
                        return '';
                    }
                    return DateFormatMs(data)
                }
            },
            {
                field: 'auditPerson',
                title: '审批人',
                width: '10%',
                align: 'left'
            }
        ]],
        toolbar: [
        //     {
        //     text: '新增',
        //     iconCls: 'icon-add',
        //     hidden: true,
        //     handler: function () {
        //         AddOrEdit(0);
        //     }
        // },
            {
            text: '编辑',
            id: 'edit',
            iconCls: 'icon-edit',
            handler: function () {
                addOrEdit(1);
                setOperationLog("编辑按钮", "配额维护");
            }
        }, "-", {
            text: '删除',
            id: 'delete',
            hide: true,
            iconCls: 'icon-remove',
            handler: function () {
                delFactory();
            }
        }, "-", {
                text: '查看',
                id: 'edit',
                iconCls: 'icon-save',
                handler: function () {
                    look(4);
                    setOperationLog("查看按钮", "配额维护");
                }
        }, "-", {
            text: '同步OA',
            id: 'export',
            iconCls: 'icon-ok',
            handler: function () {
                submitOA();
            }
        }],
        onLoadSuccess: function () {
            displayButtonByUserRole("quotamanagement2");
            $(el.tb).datagrid('clearSelections');
        },
        onCheck: function (rowIndex) {
            var rows = $(el.tb).datagrid('getRows');
            var syncStatus = rows[rowIndex].syncStatus;
            //状态 0：新建 1：审批中 2：审批拒绝 3：审批通过
            switch (Number(syncStatus)) {
                case 0:
                    $(el.edit).linkbutton('enable');
                    $(el.delete).linkbutton('enable');
                    $(el.export).linkbutton('enable');
                    break;
                case 1:
                    $(el.edit).linkbutton('enable');
                    $(el.delete).linkbutton('enable');
                    $(el.export).linkbutton('disable');
                    break;
                case 2:
                    $(el.edit).linkbutton('enable');
                    $(el.delete).linkbutton('enable');
                    $(el.export).linkbutton('disable');
                    break;
                case 3:
                    $(el.edit).linkbutton('enable');
                    $(el.delete).linkbutton('enable');
                    $(el.export).linkbutton('enable');
                    break;
                case 4:
                    $(el.edit).linkbutton('enable');
                    $(el.delete).linkbutton('enable');
                    $(el.export).linkbutton('disable');
                    break;
                default:
                    break;
            }
        }
    });
}

function moveMold() {
    var moldFormDialog = openDialog('moldForm', g.ctx + 'pc/sys/mold/moldForm/moldMove/index', {
        title: '移模',
        width: '900px',
        height: '520px',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            moldFormDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.close,
                iconCls: 'icon-cancel',
                handler: function () {
                    moldFormDialog.dialog('close');
                }
            }
        ]
    });
}
/**
 * 新增配额
 */
function addOrEdit(flag) {
    var id= '';
    if(flag===1){
        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: flag===0?'新增':'编辑',
        width: '100%',
        height: '100%',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            quotaManagementDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.save,
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['quotaManagement'].saveQuota(function () {
                        topWin.layer.msg('数据保存成功', {icon: 1});
                        quotaManagementDialog.dialog('close');
                        $(el.tb).datagrid('reload');
                    });
                }
            },
            {
                text: itablei8n.close,
                iconCls: 'icon-cancel',
                handler: function () {
                    quotaManagementDialog.dialog('close');
                }
            }
        ]
    });
}


/**
 * 查看
 */
function look(flag) {
    var id= '';

        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 AddOrEdit(flag) {
    var id= '';
    if(flag===1){
        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, {
        title: flag===0?'新增':'编辑',
        width: '1300px',
        height: '800px',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            quotaManagementDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.save,
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['moldForm'].saveOrUpdate(function () {
                        topWin.layer.msg('数据保存成功', {icon: 1});
                        quotaManagementDialog.dialog('close');
                        $(el.tb).datagrid('reload');
                    });
                }
            },
            {
                text: itablei8n.close,
                iconCls: 'icon-cancel',
                handler: function () {
                    quotaManagementDialog.dialog('close');
                }
            }
        ]
    });
}
// 提交OA审批：支持批量
function submitOA() {
    let cBox = $('#tb').datagrid('getSelected');
    if (!cBox) {
        layer.msg("请选中一条记录");
        return false;
    }
    let syncStatus = cBox.syncStatus;
    if(Number(syncStatus)!==0){
        layer.msg("只有待提交OA审批状态可以同步OA");
        return false;
    }
    var quotaCode = cBox.quotaCode;
    parent.layer.confirm('是否确认提交？', function (index) {
        var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        var url= g.ctx + 'pc/tender/quotaManage/quotaManagement/rest/pushOa';
        $.ajax({
            url: url,
            method: "post",
            data: {quotaCode: quotaCode},
            success: function (data) {
                parent.layer.close(index);
                $(el.tb).datagrid('reload');
            },
            complete: function(data) {
                top.layer.close(loading);
            },
            error: function (data) {
                parent.layer.alert(JSON.parse(data.responseText).message,{icon: 2, shadeClose: true, title: '温馨提示'});
            }
        });
    });

}
// function delFactory() {
//     var cbox = $('#tb').datagrid('getSelected');
//     if (cbox === null || cbox === undefined) {
//         layer.msg("请选中一条记录");
//         return;
//     }
//
//     var moldForm = cbox.moldCode;
//
//     parent.layer.confirm('是否删除？', function (index) {
//         var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
//         var url = g.ctx + 'pc/sys/mold/moldForm/rest/remove';
//         $.ajax({
//             url: url,
//             method: "post",
//             data: {moldCode: moldForm},
//             success: function (data) {
//                 parent.layer.close(index);
//                 $(el.tb).datagrid('reload');
//             },
//             complete: function(data) {
//                 top.layer.close(loading);
//             },
//             error: function (data) {
//                 layer.close(index);
//                 layer.alert(JSON.parse(data.responseText).message);
//             }
//         });
//     });
// }