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

$(function () {
    loadGrid();
});
function loadGrid() {
    $('#tb').datagrid({
        url: g.ctx + "pc/sys/company/rest/list",
        iconCls: 'icon-list',
        title: '',
        height: gridheight + 20,
        width: gridwidth,
        singleSelect: true,
        pagination: true,
        idField: 'id',
        autoHeight: true,
        loadMsg: "数据加载中......",
        rownumbers: true,
        fitColumns: false,
        remoteSort: true,
        columns: [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'companyCode',
                title: '公司编码',
                width: '15%',
                align: 'left'

            },
            {
                field: 'companyName',
                title: '公司名称',
                width: '15%',
                align: 'left'

            },
            {
                field: 'taxNo',
                title: '税号',
                width: '15%',
                align: 'left'

            },
            {
                field: 'country',
                title: '国家',
                width: '15%',
                align: 'left'
            },
            {
                field: 'currencyCode',
                title: '货币',
                width: '15%',
                align: 'left'
            },
            {
                field: 'address',
                title: '地址',
                width: '15%',
                align: 'left'
            },
            {
                field: 'legal',
                title: '法人',
                width: '15%',
                align: 'left'
            },
            {
                field: 'tel',
                title: '电话',
                width: '15%',
                align: 'left',
            },
            {
                field: 'bankName',
                title: '开户银行名称',
                width: '15%',
                align: 'left'
            },
            {
                field: 'bankAccount',
                title: '开户银行账号',
                width: '15%',
                align: 'left'
            }
        ]],
        // toolbar: ["-", {
        //     text: '新增',
        //     iconCls: 'icon-add',
        //     hidden: true,
        //     handler: function () {
        //         addOrEdit(0);
        //     }
        // }, "-", {
        //     text: '编辑',
        //     iconCls: 'icon-edit',
        //     handler: function () {
        //         addOrEdit(1);
        //     }
        // }, "-", {
        //     text: '删除',
        //     hide: true,
        //     iconCls: 'icon-remove',
        //     handler: function () {
        //         delFactory();
        //     }
        // }]
    });
}


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 moldFormDialog = openDialog('moldForm', g.ctx + 'pc/sys/mold/moldForm/detail?id=' + id, {
        title: flag===0?'新增':'编辑',
        width: '700px',
        height: '520px',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            moldFormDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.save,
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['moldForm'].saveOrUpdate(function () {
                        topWin.layer.msg('数据保存成功', {icon: 1});
                        moldFormDialog.dialog('close');
                        $(el.tb).datagrid('reload');
                    });
                }
            },
            {
                text: itablei8n.close,
                iconCls: 'icon-cancel',
                handler: function () {
                    moldFormDialog.dialog('close');
                }
            }
        ]
    });
}


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);
            }
        });
    });
}