/**
 *
 * @author BOGON
 * @version 1.0
 */

let el = {
    tb: '#tb',
};
$(function () {
    loadDataGride();
});
var userDialog = undefined;
var approvalDialog = undefined;
var submitFlag = 0

function loadDataGride() {
    $('#tb').datagrid(
        {
            height: document.documentElement.clientHeight - $("#searchForm").outerHeight() - 10,
            url: g.ctx + 'pc/supplier/supplierQualification/rest/changeList',
            method: "GET",
            singleSelect: true,
            pagination: true,
            autoHeight: true,
            loadMsg: "数据加载中......",
            rownumbers: true,
            fitColumns: false,
            nowrap: true,
            remoteSort: true,
            striped: true,
            page: 1,
            pageSize: 20,
            columns: [[
                {field: 'ck', checkbox: true},
                {
                    field: 'state', title: itablei8n.status,
                    width: '75', align: 'left',
                    formatter: function (value) {
                        if (value === 0) {
                            return '<span style="color: grey;">' + statusi18n.xj + '</span>';
                        } else if (value === 1) {
                            return '<span style="color: blue;">审批中</span>';
                        } else if (value === 2) {
                            return '<span style="color: green;">' + statusi18n.tg + '</span>';
                        } else if (value === 3) {
                            return '<span style="color: blue;">商务审批中</span>';
                        } else if (value === 5) {
                            return '<span style="color: blue;">SQE审批中</span>';
                        } else if (value === 4) {
                            return '<span style="color: red;">' + statusi18n.bh + '</span>';
                        }
                    }
                },
                {
                    field: 'changeType', title: '变更类型',
                    width: '60', align: 'left',
                },
                {
                    field: 'code', title: supplieri18n.sqdh,
                    width: '120', align: 'left'
                },

                {
                    field: 'supplierCode', title: itableTitlei18n.gysbm,
                    width: '120', align: 'left', hidden: isSupplier
                },
                {
                    field: 'supplierName', title: itableTitlei18n.gysmc,
                    width: '200', align: 'left', hidden: isSupplier
                },
                {
                    field: 'supplementCode', title: '补录编码',
                    width: '120', align: 'left',
                },
                {
                    field: 'createName', title: itableTitlei18n.cjr,
                    width: '150', align: 'left'
                },
                {
                    field: 'createTime', title: itableTitlei18n.cjsj,
                    width: '150', align: 'left'
                },
                {
                    field: 'approverName', title: "商务审核人",
                    width: '150', align: 'left'
                },
                {
                    field: 'remark', title: "商务审批意见",
                    width: '300', align: 'left'
                },
                {
                    field: 'sqeApproverName', title: "SQE审核人",
                    width: '150', align: 'left'
                },
                {
                    field: 'sqeRemark', title: "SQE审批意见",
                    width: '300', align: 'left'
                }
            ]],
            toolbar: [
                "-",
                {
                    text: itablei8n.newAdd, id: 'btn_add', iconCls: 'icon-add',
                    handler: function () {
                        detailDialog(undefined, 0)
                    }
                },
                "-",
                {
                    text: itablei8n.edit, id: 'btn_edit', iconCls: 'icon-edit',
                    handler: function () {
                        var row = $('#tb').datagrid('getSelected');
                        if (!row) {
                            topWin.layer.msg(itablei8n.plselect);
                            return;
                        }
                        if (row.state !== 0 && row.state !== 4) {
                            topWin.layer.msg(tipsi18n.xjhwtgkbj);
                            return;
                        }
                        detailDialog(row.id, 0)
                    }
                },
                "-",
                {
                    text: itablei8n.find, id: 'btn_lookup', iconCls: 'icon-large-smartart',
                    handler: function () {
                        var row = $('#tb').datagrid('getSelected');
                        if (!row) {
                            topWin.layer.msg(itablei8n.plselect);
                            return;
                        }
                        detailDialog(row.id, 1)
                    }
                },
                "-",
                {
                    text: itablei8n.sc, id: 'btn_del', iconCls: 'icon-remove',
                    handler: function () {
                        var row = $('#tb').datagrid('getSelected');
                        if (!row) {
                            topWin.layer.msg(itablei8n.plselect);
                            return;
                        }
                        if (row.state !== 0 && row.state !== 4) {
                            topWin.layer.msg(tipsi18n.xjhwtgkbj);
                            return;
                        }
                        topWin.layer.confirm(tipsi18n.sfsc, {icon: 3},function (index) {
                            $.ajax({
                                url: g.ctx+'pc/supplier/supplierQualification/rest/remove',
                                type: 'POST',
                                data: {id: row.id},
                                success: function (data) {
                                    topWin.layer.alert("删除成功", {icon: 1}, function (idx) {
                                        topWin.layer.close(idx);
                                        $('#tb').datagrid('reload');
                                    });
                                },
                                error: function (data) {
                                    topWin.layer.msg(data.responseJSON.message, {icon: 2});
                                }
                            })
                        })
                    }
                },
                "-",
                {
                    text: "商务审批", id: 'btn_publish', iconCls: 'icon-redo',
                    handler: function () {
                        var row = $('#tb').datagrid('getSelected');
                        if (!row) {
                            topWin.layer.msg(itablei8n.plselect);
                            return;
                        }
                        if (row.state !== 3) {
                            topWin.layer.msg(tipsi18n.xjhwtgktj);
                            return;
                        }
                        detailDialog(row.id, 2)
                    }
                }, "-",
                {
                    text: "SQE审批", id: 'btn_sqe', iconCls: 'icon-redo',
                    handler: function () {
                        var row = $('#tb').datagrid('getSelected');
                        if (!row) {
                            topWin.layer.msg(itablei8n.plselect);
                            return;
                        }
                        if (row.state !== 5) {
                            topWin.layer.msg(tipsi18n.xjhwtgktj);
                            return;
                        }
                        detailDialog(row.id, 3)
                    }
                }
            ],
            onLoadSuccess: function () {
                displayButtonByUserRole('qualifications change Manage');
            }
        }
    )
}

function refreshLoad() {
    $('#tb').datagrid('reload');
}

function detailDialog(id, type) {
    let title = ''
    let btns = [{
        text: itablei8n.save, handler: function () {
            topWin.frames['qualificationChangeEditDialog'].submitForm(function () {
                qualificationChangeEditDialog.dialog('close');
                $('#tb').datagrid('reload');
            });
        }
    }, {
        text: itablei8n.close, handler: function () {
            qualificationChangeEditDialog.dialog('close');
        }
    }]
    if (id !== undefined && type !== undefined && type === 1) {
        title = '查看'
        btns.shift()
    } else if (id !== undefined && type !== undefined && type === 2) {
        title = '商务审批'
        btns.shift()
        btns.unshift({
            text: '驳回', handler: function () {
                if (submitFlag === 1) {
                    return
                }
                var remark = topWin.frames['qualificationChangeEditDialog'].getRemark();
                if (!remark) {
                    topWin.layer.msg("请输入审批意见")
                    return;
                }
                submitFlag = 1
                $.ajax({
                    url: g.ctx + "pc/supplier/supplierQualification/rest/changeBusinessReject",
                    type: "POST",
                    data: {
                        id: id,
                        remark: remark
                    },
                    complete: function () {
                        submitFlag = 0
                    },
                    success: function (data) {
                        topWin.layer.alert("保存成功", {icon: 1}, function (idx) {
                            topWin.layer.close(idx);
                            qualificationChangeEditDialog.dialog('close');
                            $('#tb').datagrid('reload');
                        });
                    },
                    error: function (data) {
                        topWin.layer.msg(data.responseJSON.message, {icon: 2});
                    }
                });
            }
        })
        btns.unshift({
            text: '通过', handler: function () {
                topWin.frames['qualificationChangeEditDialog'].changeBusinessPass(function(){
                    qualificationChangeEditDialog.dialog('close');
                    $('#tb').datagrid('reload');
                });

            }
        })
    } else if (id !== undefined && type !== undefined && type === 3) {
        title = 'SQE审批'
        btns.shift()
        btns.unshift({
            text: '驳回', handler: function () {
                if (submitFlag === 1) {
                    return
                }
                var remark = topWin.frames['qualificationChangeEditDialog'].getSqeRemark();
                if (!remark) {
                    topWin.layer.msg("请输入审批意见")
                    return;
                }
                submitFlag = 1
                $.ajax({
                    url: g.ctx + "pc/supplier/supplierQualification/rest/changeSqeReject",
                    type: "POST",
                    data: {
                        id: id,
                        remark: remark
                    },
                    complete: function () {
                        submitFlag = 0
                    },
                    success: function (data) {
                        topWin.layer.alert("保存成功", {icon: 1}, function (idx) {
                            topWin.layer.close(idx);
                            qualificationChangeEditDialog.dialog('close');
                            $('#tb').datagrid('reload');
                        });
                    },
                    error: function (data) {
                        topWin.layer.msg(data.responseJSON.message, {icon: 2});
                    }
                });
            }
        })
        btns.unshift({
            text: '通过', handler: function () {
                var remark = topWin.frames['qualificationChangeEditDialog'].getSqeRemark();
                if (!remark) {
                    topWin.layer.msg("请输入审批意见")
                    return;
                }
                submitFlag = 1
                $.ajax({
                    url: g.ctx + "pc/supplier/supplierQualification/rest/changeSqePass",
                    type: "POST",
                    data: {
                        id: id,
                        remark: remark
                    },
                    complete: function () {
                        submitFlag = 0
                    },
                    success: function (data) {
                        topWin.layer.alert("保存成功", {icon: 1}, function (idx) {
                            topWin.layer.close(idx);
                            qualificationChangeEditDialog.dialog('close');
                            $('#tb').datagrid('reload');
                        });
                    },
                    error: function (data) {
                        topWin.layer.msg(data.responseJSON.message, {icon: 2});
                    }
                });
            }
        })
    } else if (id !== undefined) {
        title = '编辑'
    } else {
        title = '新增'
    }
    let url = g.ctx + 'pc/supplier/supplierQualification/page/change/detail?type=' + type + '&id=' + (id === undefined ? '' : id);
    var qualificationChangeEditDialog = openDialog('qualificationChangeEditDialog', url, {
        title: title,
        width: '80%',
        height: '80%',
        cache: false,
        modal: true,
        border: false,
        draggable: true,
        shadow: false,
        onClose: function () {
            qualificationChangeEditDialog.dialog('destroy');
        },
        buttons: btns
    });
}