let el = {
    tb: '#tb'
}

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

function loadGrid() {
    $('#tb').datagrid({
        url: g.ctx + "pc/order/appointmentSupplier/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: 'status',
                title: '启用状态',
                width: '100',
                align: 'left',
                formatter: function (data) {
                    if (Number(data) === 1)
                        return "<span style='color:green;'>启用</span>";
                    else
                        return "<span style='color: red'>禁用</span>"
                }
            },
            {
                field: 'supplierCode',
                title: '供应商编码',
                width: '100',
                align: 'left',
            },
            {
                field: 'supplierName',
                title: '供应商名称',
                width: '250',
                align: 'left',
            },
            {
                field: 'createTime',
                title: '创建时间',
                width: '160',
                align: 'left',
                formatter: function (value) {
                    return DateFormatMs(value);
                }
            },
            {
                field: 'warehouse',
                title: '是否送仓库',
                width: '80',
                align: 'left',
                formatter: function (value) {
                    if (value === 0) {
                        return '否'
                    } else {
                        return '是';
                    }
                }
            },
        ]],
        toolbar: [
            "-", {
                text: '新增',
                iconCls: 'icon-add',
                id:'btn-add',
                handler: function () {
                    addOrEdit(0);
                }
            }, "-", {
                text: '编辑',
                id:'btn-edit',
                iconCls: 'icon-edit',
                handler: function () {
                    addOrEdit(1);
                }
            }, "-", {
                text: '删除',
                id:'btn-remove',
                iconCls: 'icon-remove',
                handler: function () {
                    delAppointment();
                }
            }, "-", {
                text: '启用|禁用',
                id:'btn-open_down',
                iconCls: 'icon-import',
                handler: function () {
                    updateStatus();
                }
            }],
        // onLoadSuccess: function () {
        //     displayButtonByUserRole('appointmentSupplier');
        // }
    });
}

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

/**
 * 删除
 */
function delAppointment() {
    let cbox = $('#tb').datagrid('getSelected');
    if (cbox === null || cbox === undefined) {
        layer.msg("请选中一条记录");
        return;
    }

    let id = cbox.id;

    parent.layer.confirm('是否删除？', function (index) {
        let loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        let url = g.ctx + 'pc/order/appointmentSupplier/rest/remove';
        $.ajax({
            url: url,
            method: "post",
            data: {id: id, deleteFlag: 1},
            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 updateStatus() {
    var row = $(el.tb).datagrid('getSelected');
    if (!row) {
        layer.msg('请选择查看的数据!');
        return;
    }

    $.post(g.ctx+'pc/order/appointmentSupplier/rest/updateStatus', {id: row.id}, function () {
        $(el.tb).datagrid('load');
        parent.layer.msg('保存成功');
    }).error(function (data) {
        let msg = JSON.parse(data['responseText'])['message'];
        layer.msg('保存失败！ ' + msg);
    }).complete(function () {
        parent.layer.closeAll('loading');
    })
}


//查询
function dataSearch() {
    var queryParams = $(el.tb).datagrid('options').queryParams;
    queryParams['supplierCode'] = $.trim($('#supplierCode').val());
    queryParams['supplierName'] = $.trim($('#supplierName').val());
    queryParams['status'] = $.trim($('#status').combobox('getValue'));
    queryParams['warehouse'] = $.trim($('#warehouse').combobox('getValue'));
    $(el.tb).datagrid('reload');
}

//重置
function resetSearch() {
    $('#supplierCode').val('');
    $('#supplierName').val('');
    $('#status').combobox('setValue', -1);
    $('#warehouse').combobox('setValue', -1);
    dataSearch();
}