var el = {
    searchForm: '#searchForm',

};
$(function () {
    var status = [
        {'text': '', 'value': ''},
        {
            'text': '草稿',
            // 'text': pcn.cg,
            'value': '1'
        },
        {
            'text': '待SQE受理',
            // 'text': pcn.d_sqe,
            'value': '2'
        },
        {
            'text': '审批中',
            // 'text': pcn.jjsl,
            'value': '3'
        },
        {
            'text': '审批拒绝',
            // 'text': pcn.ysl,
            'value': '4'
        },
        {
            'text': '审批同意',
            // 'text': pcn.ysl,
            'value': '5'
        },
        {
            'text': '废弃',
            // 'text': pcn.ysl,
            'value': '6'
        },
    ];

    $('#status').combobox({
        textField: 'text',
        valueField: 'value',
        panelHeight: 'auto',
        data: status,
        editable: false
    });

    loadGrid();
})


function loadGrid() {
    $('#pcnListTab').datagrid({
        iconCls: 'icon-list',
        title: '',
        height: gridheight - 23,
        width: gridwidth,
        url: g.ctx+'pc/order/pcn/rest/queryPcnByPage',
        pagination: true,
        singleSelect: true,
        idField: 'id',
        fitColumns: false,
        autoHeight: true,
        loadMsg: commonMsg.loadData,
        fit: false,
        page: 1,
        pageSize: 10,
        columns: [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'id',
                title: 'id',
                hidden: true
            }, {
                field: 'status',
                title: commonTxt.status,
                align: 'center',
                width:120,
                formatter: function (status) {
                    if (status == 1) {
                        return '<span style="color:orange">  草稿  </span>';
                    } else if (status == 2) {
                        return '<span style="color: blue">  待SQE受理  </span>';
                    } else if (status == 3) {
                        return '<span style="color:blue">  审批中  </span>';
                    } else if (status == 4) {
                        return '<span style="color: red">  审批拒绝  </span>';
                    }else if (status == 5) {
                        return '<span style="color: green">  审批同意  </span>';
                    }else if (status == 6) {
                        return '<span style="color: saddlebrown">  废弃  </span>';
                    }
                }
            },
            {
                field: 'oaProcessId',
                title: "OA流程单号",
                width:150,
                align: 'center',
                formatter: function (value) {
                    return oaTrack(value,OaProcessType.PCN_CHANGE);
                }
            },
            {
                field: 'changeNotice',
                title: '审批结论',
                width: '120px',
                align: 'left'
            },
            {
                field: 'changeNoticeRemarks',
                title: '审批意见',
                width: '120px',
                align: 'left'
            },

            {
                field: 'supplierNo',
                title: materiaTable.supplierCode,
                width:150,
                align: 'center'
            },

            {
                field: 'supplierName',
                title: materiaTable.supplierName,
                width:200,
                align: 'center'
            },
            {
                field: 'changeReason',
                width:250,
                title: pcn.changeReason,
                align: 'center'

            },
            // {
            //     field: 'changeContent',
            //     width:250,
            //     title: pcn.changeContent,
            //     align: 'center'
            //
            // },
            {
                field: 'createTime',
                // title: '创建时间',
                title: pcn.createTime,
                align: 'center',
                formatter: function (createTime) {
                    if (createTime == '' || createTime == null || createTime == undefined)
                        return '';
                    return DateTimeFormatMs(createTime)
                }

            }
            ,
            {
                field: 'modifyTime',
                title: pcn.modifyTime,
                align: 'center',
                formatter: function (modifyTime) {
                    if (modifyTime == '' || modifyTime == null || modifyTime == undefined)
                        return '';
                    return DateTimeFormatMs(modifyTime)
                }
            },
            {
                field: 'sqeUserName',
                title: pcn.sqe,
                width:150,
                align: 'center'
            }
        ]],
        toolbar: [
            "-", {
                id: 'add',
                // text: '新增',
                text: buttonI18n.add,
                iconCls: 'icon-add',
                handler: function () {
                    add();
                }
            },
            "-", {
                id: 'edit',
                // text: '编辑',
                text: buttonI18n.edit,
                iconCls: 'icon-edit',
                handler: function () {
                    editPcn();
                }
            },
            "-", {
                id: 'del',
                //text: '删除',
                text: buttonI18n.delete,
                iconCls: 'icon-remove',
                handler: function () {
                    remove();
                }
            },
            "-", {
                id: 'viewDetail',
                // text: '查看详情',
                text: pcn.viewDetail,
                iconCls: 'icon-search',
                handler: function () {
                    viewPcnDetail();
                    setOperationLog("查看详情按钮", "PCN变更申请");

                }
            },
            "-", {
                id: 'agree',
                //text: '同意',
                text: pcn.agree,
                iconCls: 'icon-confirm',
                handler: function () {
                    agree();
                }

            },"-", {
                id: 'sqeExamine',
                //text: 'sqe审批',
                text: pcn.sqeExamine,
                iconCls: 'icon-confirm',
                handler: function () {
                    examine();
                }

            }
        ],
        onLoadSuccess: function () {
            displayButtonByUserRole("PCNMANAGER");
            $('#pcnListTab').datagrid('clearSelections');
        },
    });
}

//查询
function searchPcn() {
    var queryParams = $('#searchForm').serializeJson();
    queryParams.supplierNo = queryParams.supplierCode;
    $('#pcnListTab').datagrid('load', queryParams);
}


function resetSearch() {
    $("#supplierCode").val('');
    $("#supplierName").val('');
    $("#status").combobox('setValue', '');
    var queryParams = $('#searchForm').serializeJson();
    $('#pcnListTab').datagrid('load', queryParams);
};


/**
 * 新增
 */
function add() {
    var url = g.ctx+'pc/order/pcn/page/addpage'

    var addPcnManagerDialog = openDialog('addPcnManager', url, {
        title: '新增',
        width: '80%',
        height:'90%',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            addPcnManagerDialog.dialog('destroy');
        },
        buttons: [
            {
                text:'暂存',
                iconCls:'icon-save',
                handler:function() {
                    topWin.frames['addPcnManager'].submitForm(1,function (data) {
                        addPcnManagerDialog.dialog('close');
                        $('#pcnListTab').datagrid('reload');
                    });
                }
            },
            {
                text:'提交',
                iconCls:'icon-save',
                handler:function() {
                    topWin.frames['addPcnManager'].submitForm(2,function (data) {
                        addPcnManagerDialog.dialog('close');
                        $('#pcnListTab').datagrid('reload');
                    });
                }
            },
            {
                text:'关闭',
                iconCls:'icon-cancel',
                handler:function() {
                    addPcnManagerDialog.dialog('close');
                }
            }
        ]
    });

}




/**
 * 编辑
 */
function editPcn() {
    var rows = $('#pcnListTab').datagrid('getSelections');
    var cbox = $('#pcnListTab').datagrid('getSelected');
    if (!cbox || rows.length > 1) {
        // layer.msg("请选中一条记录");
        layer.msg(commonMsg.msg1);
        return;
    }
    if (cbox.status != 1 && cbox.status != 4) {
        layer.msg(pcn.msg4);
        return;
    }

    if(cbox.changeNotice == "不同意变更"){
        layer.msg("该流程SQE已审批不同意变更，不允许进行重新编辑提交！");
        return;
    }

    var url = g.ctx+'pc/order/pcn/page/editPcn?id=' + cbox.id+"&viewType=";
    var editPcnManagerDialog = openDialog('editPcnManager', url, {
        title: '修改',
        width: '90%',
        height:'100%',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            editPcnManagerDialog.dialog('destroy');
        },
        buttons: [
            {
                text:'暂存',
                iconCls:'icon-save',
                handler:function() {
                    topWin.frames['editPcnManager'].submitForm(1,function (data) {
                        editPcnManagerDialog.dialog('close');
                        $('#pcnListTab').datagrid('reload');
                    });
                }
            },
            {
                text:'提交',
                iconCls:'icon-save',
                handler:function() {
                    topWin.frames['editPcnManager'].submitForm(2,function (data) {
                        editPcnManagerDialog.dialog('close');
                        $('#pcnListTab').datagrid('reload');
                    });
                }
            },
            {
                text:'关闭',
                iconCls:'icon-cancel',
                handler:function() {
                    editPcnManagerDialog.dialog('close');
                }
            }
        ]
    });

}


/**
 * 删除
 */
function remove() {
    var cbox = $('#pcnListTab').datagrid('getSelected');
    if (!cbox) {
        // layer.msg("请选中一条记录");
        layer.msg(commonMsg.msg1);
        return;
    }
    if (cbox.status != 1) {
        layer.msg("只有新建状态数据可以删除！");
        return;
    }

    // layer.confirm('是否确认删除该记录？', function () {
    topWin.layer.confirm(pcn.msg2, function () {
        $.ajax({
            url: g.ctx+'pc/order/pcn/rest/del',
            method: "get",
            data: {id: cbox.id},
            statusCode: {
                200: function () {
                    $('#pcnListTab').datagrid('reload');
                    topWin.layer.closeAll('dialog');
                },
                500: function () {
                    topWin.layer.alert(commonMsg.opt_fail_msg);
                },
                400: function () {
                    topWin.layer.alert(commonMsg.opt_fail_msg);
                }
            }
        });

    })
}


/**
 * 受理
 */
function accept() {
    var cbox = $('#pcnListTab').datagrid('getSelected');
    if (!cbox) {
        //layer.msg("请选中一条记录");
        layer.msg(commonMsg.msg1);
        return;
    }
    if (cbox.status != 2 && cbox.status != 3) {
        // layer.msg("只有待SQE审核和已拒绝的记录才能受理");
        layer.msg(pcn.msg3);
        return;
    }

    parent.layer.open({
        // title: "受理",
        title: pcn.accept,
        type: 2,
        area: ["600px", '400px'],
        content: g.ctx+"pc/order/pcn/page/pcnAcceptPage?id=" + cbox.id,
        skin: 'layui-layer-molv', //样式类名
    });
}


/**
 * 同意
 */
function agree() {
    var cbox = $('#pcnListTab').datagrid('getSelected');
    if (!cbox) {
        layer.msg("请选中一条记录");
        return;
    }
    if (cbox.status != 4 && cbox.status != 5) {
        layer.msg("只有已受理或者拒绝同意的记录才能确认");
        return;
    }

    var url = g.ctx+"pc/order/pcn/page/pcnAgreePage?id=" + cbox.id;
    var agreeDialog = openDialog('agree', url, {
        title: claimModel.confirm,
        width: '600px',
        height:'400px',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            agreeDialog.dialog('destroy');
        },
        buttons: [
            {
                text:'提交',
                iconCls:'icon-save',
                handler:function() {
                    topWin.frames['agree'].submit(function (data) {
                        agreeDialog.dialog('close');
                        $('#pcnListTab').datagrid('reload');
                    });
                }
            },
            {
                text:'关闭',
                iconCls:'icon-cancel',
                handler:function() {
                    agreeDialog.dialog('close');
                }
            }
        ]
    });
}

/**
 * 查看详情
 */
function viewPcnDetail() {
    var rows = $('#pcnListTab').datagrid('getSelections');
    var cbox = $('#pcnListTab').datagrid('getSelected');
    if (!cbox || rows.length > 1) {
        //layer.msg("请选中一条记录");
        layer.msg(commonMsg.msg1);
        return;
    }

    var url = g.ctx+'pc/order/pcn/page/viewDetail?id=' + cbox.id+"&viewType=";
    var viewDialog = openDialog('view', url, {
        title: pcn.viewDetail,
        width: '90%',
        height:'100%',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            viewDialog.dialog('destroy');
        },
        buttons: [
            {
                text:'关闭',
                iconCls:'icon-cancel',
                handler:function() {
                    viewDialog.dialog('close');
                }
            }
        ]
    });
    // var url = '/pc/order/pcn/page/viewDetail?id=' + cbox.id+"&viewType="+viewType;
    // parent.layer.open({
    //     skin: 'layui-layer-molv', //样式类名
    //     // title: "查看详情",
    //     title: pcn.viewDetail,
    //     type: 2,
    //     area: ["1200px", '600px'],
    //     content: url
    // });
}

/**
 * sqe审批
 */
function examine() {
    var rows = $('#pcnListTab').datagrid('getSelections');
    var cbox = $('#pcnListTab').datagrid('getSelected');
    if (!cbox || rows.length > 1) {
        //layer.msg("请选中一条记录");
        layer.msg(commonMsg.msg1);
        return;
    }
    if (cbox.status != 2) {
        layer.msg(pcn.msg5);
        return;
    }

    var url = g.ctx+'pc/order/pcn/page/sqeExamine?id=' + cbox.id+"&viewType=";
    var examineDialog = openDialog('view', url, {
        title: pcn.sqeExamine,
        width: '90%',
        height:'100%',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            examineDialog.dialog('destroy');
        },
        buttons: [
            {
                text:'驳回',
                iconCls:'icon-cancel',
                handler:function() {
                    topWin.frames['view'].submitForm(4,function (data) {
                        examineDialog.dialog('close');
                        $('#pcnListTab').datagrid('reload');
                    });
                }
            },
            {
                text:'通过',
                iconCls:'icon-ok',
                handler:function() {
                    topWin.frames['view'].submitForm(3,function (data) {
                        examineDialog.dialog('close');
                        $('#pcnListTab').datagrid('reload');
                    });
                }
            },
            // {
            //     text:'关闭',
            //     iconCls:'icon-cancel',
            //     handler:function() {
            //         viewDialog.dialog('close');
            //     }
            // }
        ]
    });
    // var url = '/pc/order/pcn/page/viewDetail?id=' + cbox.id+"&viewType="+viewType;
    // parent.layer.open({
    //     skin: 'layui-layer-molv', //样式类名
    //     // title: "查看详情",
    //     title: pcn.viewDetail,
    //     type: 2,
    //     area: ["1200px", '600px'],
    //     content: url
    // });
}

function downLoad() {
    var rows = $('#pcnListTab').datagrid('getSelections');
    var cbox = $('#pcnListTab').datagrid('getSelected');
    if (!cbox || rows.length > 1) {
        // layer.msg("请选中一条记录");
        layer.msg(commonMsg.msg1);
        return;
    }

    var exportConfirm = layer.confirm('确认生成并下载此pcn申请单么?', {
        // btn: ['下载', '返回']
        btn: [buttonI18n.download, buttonI18n.return]
    }, function () {

        window.location.href = g.ctx+"pcn/downLoad?id=" + cbox.id;
        layer.close(exportConfirm);
    });

}


window.top.pcnList = {
    "closeAndRefreshPage": function () {
        parent.layer.closeAll();
        var queryParams = $('#searchForm').serializeJson();
        $('#pcnListTab').datagrid('load', queryParams);
    }
}