
let el = {
    tb: '#tb'
}

$(function () {
    loadGrid();
});

function loadGrid() {
    $('#tb').datagrid({
        url: g.ctx + "pc/sys/factory/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: 'factory',
                title: '工厂编码',
                width: '15%',
                align: 'left'

            },
            {
                field: 'name',
                title: '工厂名称',
                width: '15%',
                align: 'left'

            },
            {
                field: 'companyCode',
                title: '公司编码',
                width: '15%',
                align: 'left'
            },
            {
                field: 'companyName',
                title: '公司名称',
                width: '15%',
                align: 'left'
            },
            {
                field: 'contacts',
                title: '联系人',
                width: '15%',
                align: 'left'
            },
            {
                field: 'receivingPhone',
                title: '收货联系人电话',
                width: '15%',
                align: 'left'
            },
            {
                field: 'qualityer',
                title: '品质',
                width: '5%',
                align: 'left'
            },
            {
                field: 'approvaler',
                title: '核准',
                width: '5%',
                align: 'left'
            },
            {
                field: 'address',
                title: '收货地址',
                width: '15%',
                align: 'left',
                formatter: function(value, row, index) {
                    return '<a href="#" style="color: black" title="' + value + '" class="address">' + value + '</a>';
                }
            },
            {
                field: 'createTime',
                title: '创建日期',
                width: '15%',
                align: 'left',
                formatter: function (value, row, index) {
                    return DateFormatMs(value);
                }

            },
            {
                field: 'lastUpdateTime',
                title: '最后修改时间',
                width: '15%',
                align: 'left',
                formatter: function (value, row, index) {
                    return DateFormatMs(value);
                }

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


function delFactory() {
    var cbox = $('#tb').datagrid('getSelected');
    if (cbox === null || cbox === undefined) {
        layer.msg("请选中一条记录");
        return;
    }

    var factory = cbox.factory;

    parent.layer.confirm('是否删除？', function (index) {
        var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        var url = g.ctx + 'pc/sys/factory/rest/remove';
        $.ajax({
            url: url,
            method: "post",
            data: {factoryCode: factory},
            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);
            }
        });
    });
}

//查询
function dataSearch() {
    var queryParams = $(el.tb).datagrid('options').queryParams;
    queryParams['factoryCode'] = $.trim($('#factoryCode').val());
    queryParams['factoryName'] = $.trim($('#factoryName').val());
    $(el.tb).datagrid('reload');
}

//重置
function resetSearch() {
    $('#factoryCode').val('');
    $('#factoryName').val('');
    dataSearch();
}