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

var page = {
    submitFlag: 0,
    initData: function () {
        $("#tb").datagrid({
            height: document.documentElement.clientHeight - $("#searchForm").outerHeight() - 10,
            url: g.ctx + 'pc/supplier/supplierQualification/rest/applyList',
            method: "GET",
            singleSelect: true,
            pagination: true,
            autoHeight: true,
            loadMsg: "数据加载中......",
            rownumbers: true,
            fitColumns: false,
            nowrap: true,
            remoteSort: true,
            striped: true,
            page: 1,
            pageSize: 10,
            queryParams: {},
            columns: [[
                {
                    field: 'status',
                    title: '状态',
                    width: '10%',
                    formatter: function (value, row) {
                        if (row.status === 1) {
                            return '<font color="#66cc00">新建</font>'
                        }
                        if (row.status === 2) {
                            return '<font color="#ff7f50">待供应商处理</font>'
                        }
                    }
                },
                {
                    field: 'applyNo',
                    title: '补录编码',
                    width: '20%'
                },
                {
                    field: 'createName',
                    title: '创建人',
                    width: '20%'
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    width: '20%',
                    formatter: function (value) {
                        if (value) {
                            return moment(new Date(value)).format('YYYY-MM-DD HH:mm:ss');
                        }
                    }
                }
            ]],
            toolbar: [
                {
                    text: '新增',
                    id: 'btn_add',
                    iconCls: 'icon-add',
                    handler: function () {
                        page.detailDialog();
                    }
                }, {
                    text: '编辑',
                    id: 'btn_edit',
                    iconCls: 'icon-edit',
                    handler: function () {
                        var row = $('#tb').datagrid('getChecked');
                        if (row.length <= 0) {
                            topWin.layer.msg("请选中一条记录");
                            return;
                        }
                        if(row[0].status === 2){
                            topWin.layer.msg("该记录已提交，不能编辑");
                            return;
                        }
                        page.detailDialog(row[0].id);
                    }
                }, {
                    text: '查看',
                    id: 'btn_look',
                    iconCls: 'icon-find',
                    handler: function () {
                        var row = $('#tb').datagrid('getChecked');
                        if (row.length <= 0) {
                            topWin.layer.msg("请选中一条记录");
                            return;
                        }
                        page.detailDialog(row[0].id, 1);
                    }
                },
                {
                    text: '提交',
                    id: 'btn_ok',
                    iconCls: 'icon-ok',
                    handler: function () {
                        page.updateStatus();
                    }
                },
                {
                    text: '删除',
                    id: 'btn_waste',
                    iconCls: 'icon-remove',
                    handler: function () {
                        page.updateRemoveStatus();
                    }
                }
            ],
            onLoadSuccess: function () {
                displayButtonByUserRole('SupplierQualificationApply');
            }
        });
    },
    detailDialog: function (id, lookupFlag) {
        let title = ''
        let btns = [{
            text: itablei8n.save, handler: function () {
                topWin.frames['qualificationApplyEditDialog'].submitForm(function () {
                    qualificationApplyEditDialog.dialog('close');
                    $('#tb').datagrid('reload');
                });
            }
        }, {
            text: itablei8n.close, handler: function () {
                qualificationApplyEditDialog.dialog('close');
            }
        }]
        if (id != undefined && lookupFlag != undefined) {
            title = '查看'
            btns.shift()
        } else if (id != undefined) {
            title = '编辑'
        } else {
            title = '新增'
        }
        let url = g.ctx + 'pc/supplier/supplierQualification/page/apply/detail?lookupFlag=' + (lookupFlag == undefined ? '0' : '1') + '&id=' + (id == undefined ? '' : id);
        var qualificationApplyEditDialog = openDialog('qualificationApplyEditDialog', url, {
            title: title,
            width: '750px',
            height: '100%',
            cache: false,
            modal: true,
            border: false,
            draggable: true,
            shadow: false,
            onClose: function () {
                qualificationApplyEditDialog.dialog('destroy');
            },
            buttons: btns
        });
    },
    updateStatus: function () {
        if (page.submitFlag === 1) {
            return
        }
        let row = $('#tb').datagrid('getChecked');
        if (row.length <= 0) {
            topWin.layer.msg("请选中一条记录");
            return;
        }
        if (row[0].status === 2) {
            topWin.layer.msg("已提交无需重复提交");
            return;
        }
        topWin.layer.confirm('是否提交?',{icon: 3}, function (index) {
            page.submitFlag = 1
            $.ajax({
                url: g.ctx + "pc/supplier/supplierQualification/rest/apply/submit",
                data: {id: row[0].id},
                type: "POST",
                complete: function () {
                    page.submitFlag = 0
                },
                success: function (data) {
                    topWin.layer.alert('操作成功', {icon: 1}, function (idx) {
                        topWin.layer.close(idx);
                        $('#tb').datagrid('reload');
                    });
                },
                error: function (data) {
                    topWin.layer.alert(data.responseJSON.message, {icon: 2});
                }
            })
        })
    },
    updateRemoveStatus: function () {
        if (page.submitFlag === 1) {
            return
        }
        let row = $('#tb').datagrid('getChecked');
        if (row.length <= 0) {
            topWin.layer.msg("请选中一条记录");
            return;
        }
        // if (row[0].status === 2) {
        //     topWin.layer.msg("已提交无需重复提交");
        //     return;
        // }
        topWin.layer.confirm('是否删除?',{icon: 2}, function (index) {
            page.submitFlag = 1
            $.ajax({
                url: g.ctx + "pc/supplier/supplierQualification/rest/apply/updateRemoveStatus",
                data: {id: row[0].id},
                type: "POST",
                complete: function () {
                    page.submitFlag = 0
                },
                success: function (data) {
                    topWin.layer.alert('操作成功', {icon: 1}, function (idx) {
                        topWin.layer.close(idx);
                        $('#tb').datagrid('reload');
                    });
                },
                error: function (data) {
                    topWin.layer.alert(data.responseJSON.message, {icon: 2});
                }
            })
        })
    }
}
$(function () {
    page.initData()
})