let el = {
    tb: '#tb',
    useState: '#useState',
    //moldId: '#id',
    form: '#form',
}

$(function () {

    initPageSelect();
    loadGrid();

});

/**
 * 初始化页面下拉框数据
 */
function initPageSelect() {
    // 初始化使用状态下拉框
    $(el.useState).combobox({
        valueField: 'value',
        textField: 'label',
        value: -1,
        data: [
            {label: '全部', value: -1},
            {label: '使用', value: 0},
            {label: '未使用', value: 1}
        ],
        editable: false,
        prompt: '-- 请选择 --',
        panelHeight: 'auto'
    });
}

function loadGrid() {
    var formData = $(el.form).serializeJson();
    console.log(JSON.stringify(formData))
    var moldId = formData.id
    $('#tb').datagrid({
        //url: g.ctx + "pc/sys/mold/moldForm/rest/moldMove/list",
        url: g.ctx + "pc/sys/mold/moldForm/rest/moldMove/list?moldId=" + moldId,
        iconCls: 'icon-list',
        title: '',
        height: gridheight + 100,
        width: gridwidth,
        singleSelect: true,
        pagination: true,
        idField: 'id',
        autoHeight: true,
        loadMsg: "数据加载中......",
        rownumbers: true,
        fitColumns: false,
        remoteSort: true,
        columns: [[
            {
                field: 'ck',
                checkbox: true
            },
            {
                field: 'deleteFlag',
                title: '是否删除',
                width: '120px',
                align: 'left',
                exportEnum: [
                    {code: 1, codeWithDesc: '是'},
                    {code: 0, codeWithDesc: '否'},
                ],
                formatter: function (value) {
                    switch (value) {
                        case 0:
                            return '<span>否</span>';
                        case 1:
                            return '<span style="color: red;">是</span>';
                    }
                }
            },
            {
                field: 'supplierCode',
                title: '供应商编码',
                width: '15%',
                align: 'left'
            },
            {
                field: 'supplierName',
                title: '供应商名称',
                width: '15%',
                align: 'left'
            },
            {
                field: 'createTime',
                title: '移模时间',
                width: '15%',
                align: 'left',
                formatter: function (value, row, index) {
                    return DateFormatMs(value);
                }

            },
            {
                field: 'createrName',
                title: '操作人',
                width: '15%',
                align: 'left'
            },
            {
                field: 'createDate',
                title: '创建时间',
                width: '15%',
                align: 'left',
                formatter: function (value, row, index) {
                    return DateFormatMs(value);
                }
            },
            // {
            //     field: 'createDate',
            //     title: '移模次数',
            //     width: '15%',
            //     align: 'left',
            // },
            // {
            //     field: 'position',
            //     title: '移模后位置',
            //     width: '15%',
            //     align: 'left'
            // },
            {
                field: 'notes',
                title: '备注',
                width: '15%',
                align: 'left'
            },
            {
                field: 'deleteNote',
                title: '删除意见',
                width: '250px',
                align: 'left',
                formatter: function(value, row, index) {
                    return '<a href="#" style="color: black" title="' + value + '" class="note">' + value + '</a>';
                }
            },

        ]],
        toolbar: ["-", {
            text: '选择供应商',
            iconCls: 'icon-add',
            hidden: true,
            handler: function () {
                addOrEdit(0);
            }
        }, "-", {
            text: '删除',
            hide: true,
            iconCls: 'icon-remove',
            handler: function () {
                delFactory0();
            }
        }]
    });
}

function addOrEdit(flag) {
    var formData = $(el.form).serializeJson();
    var moldId = formData.id
    var id = '';
    var moldMoveDialog = openDialog('moldMove', g.ctx + 'pc/sys/mold/moldForm/moldMove/detail?moldId=' + moldId, {
        title: '新增',
        width: '900px',
        height: '520px',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            moldMoveDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.save,
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['moldMove'].saveOrUpdate(function () {
                        topWin.layer.msg('数据保存成功', {icon: 1});
                        moldMoveDialog.dialog('close');
                        $(el.tb).datagrid('reload');
                    });
                }
            },
            {
                text: itablei8n.close,
                iconCls: 'icon-cancel',
                handler: function () {
                    moldMoveDialog.dialog('close');
                }
            }
        ]
    });
}


function delFactory0() {
    var id = '';
    var cbox = $('#tb').datagrid('getSelected');
    if (cbox === null || cbox === undefined) {
        layer.msg("请选中一条记录");
        return;
    }
     var moldId = cbox.id;
    // var formData = $(el.form).serializeJson();
    // var moldId = formData.id
    // var id = '';
    var moldMoveDialog = openDialog('moldMove', g.ctx + 'pc/sys/mold/moldForm/moldMove/del?moldId=' + moldId, {
        title: '删除',
        width: '950px',
        height: '200px',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            moldMoveDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.save,
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['moldMove'].saveOrUpdate(function () {
                        topWin.layer.msg('数据保存成功', {icon: 1});
                        moldMoveDialog.dialog('close');
                        $(el.tb).datagrid('reload');
                    });
                }
            },
            {
                text: itablei8n.close,
                iconCls: 'icon-cancel',
                handler: function () {
                    moldMoveDialog.dialog('close');
                }
            }
        ]
    });
    // var cbox = $('#tb').datagrid('getSelected');
    // if (cbox === null || cbox === undefined) {
    //     layer.msg("请选中一条记录");
    //     return;
    // }
    //
    // var moldMove = cbox.id;
    //
    // 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/moldMove/remove';
    //     $.ajax({
    //         url: url,
    //         method: "post",
    //         data: {id: moldMove},
    //         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);
    //         }
    //     });
    // });
}