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

$(function () {
    initPageSelect();
    initDictName('moldform_status')
    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'
    // });
    // $("#useState").combobox({
    //     valueField: 'code',
    //     textField: 'name',
    //     panelHeight: 'auto',
    //     editable: false,
    //     data: getDictList('moldform_status')
    // })
    $("#useState").combobox({
        url: g.ctx + 'pc/sys/dict/getDicByType?type=moldform_status',
        panelHeight: 100,
        method: 'GET',
        valueField: 'code',
        textField: 'name',
        prompt: '-- 请选择 --',
        editable: false,
    });
}

function loadGrid() {
    $('#tb').datagrid({
        url: g.ctx + "pc/sys/mold/moldForm/rest/list",
        iconCls: 'icon-list',
        title: '',
        height: gridheight,
        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: 'moldCode',
                title: '智微编码',
                width: '120px',
                align: 'left'

            },
            {
                field: 'moldName',
                title: '名称',
                width: '170px',
                align: 'left'

            },
            {
                field: 'supplierMouldNo',
                title: '制造商模号',
                width: '110px',
                align: 'left'
            },
            {
                field: 'supplierCode',
                title: '制造商编码',
                width: '85px',
                align: 'left'
            },
            {
                field: 'supplierName',
                title: '制造商名称',
                width: '180px',
                align: 'left'
            },
            {
                field: 'useTimes',
                title: '寿命次数',
                width: '80px',
                align: 'left'
            },
            {
                field: 'dosage',
                title: '单次产出数量',
                width: '80px',
                align: 'left'
            },
            {
                field: 'moveSupplierCode',
                title: '移模制造商编码',
                width: '85px',
                align: 'left'
            },
            {
                field: 'moveSupplierName',
                title: '移模制造商名称',
                width: '180px',
                align: 'left'
            },
            // {
            //     field: 'position',
            //     title: '移模后位置',
            //     width: '180px',
            //     align: 'left'
            // },
            {
                field: 'moveCreateTime',
                title: '移模时间',
                width: '110px',
                align: 'left',
                formatter: function (value, row, index) {
                    return DateFormatMs(value);
                }

            },
            {
                field: 'moldMoveTimes',
                title: '移模次数',
                width: '80px',
                align: 'left'
            },
            {
                field: 'isCallPolice',
                title: '寿命次数预警',
                width: '80px',
                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>';
                        default:
                            return '';
                    }
                }
            },
            {
                field: 'policeTime',
                title: '寿命次数预警时间',
                width: '110px',
                align: 'left',
                formatter: function (value, row, index) {
                    return DateFormatMs(value);
                }

            },
            // {
            //     field: 'quantity',
            //     title: '分摊次数',
            //     width: '80px',
            //     align: 'left'
            // },
            // {
            //     field: 'isQuantityPolice',
            //     title: '分摊次数预警',
            //     width: '80px',
            //     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>';
            //             default:
            //                 return '';
            //         }
            //     }
            // },
            // {
            //     field: 'quantityPoliceTime',
            //     title: '分摊次数预警时间',
            //     width: '110px',
            //     align: 'left',
            //     formatter: function (value, row, index) {
            //         return DateFormatMs(value);
            //     }
            //
            // },
            {
                field: 'actualUseTimes',
                title: '实际使用次数',
                width: '80px',
                align: 'left'
            },
            {
                field: 'useState',
                title: '使用状态',
                width: '80px',
                align: 'left',
                exportEnum:getDictList('moldform_status'),
                formatter: function (value) {
                    return getDicName('moldform_status',value);
                }
            },
            {
                field: 'fixTimes',
                title: '维修次数',
                width: '80px',
                align: 'left'
            },
            {
                field: 'maintenanceTimes',
                title: '保养次数',
                width: '80px',
                align: 'left'
            },
            {
                field: 'notes',
                title: '备注',
                width: '250px',
                align: 'left',
                formatter: function(value, row, index) {
                    return '<a href="#" style="color: black" title="' + value + '" class="note">' + value + '</a>';
                }
            },
            {
                field: 'deleteNote',
                title: '删除意见',
                width: '250px',
                align: 'left',
                formatter: function(value, row, index) {
                    return '<a href="#" style="color: black" title="' + value + '" class="note">' + value + '</a>';
                }
            },
            // {
            //     field: 'attachment',
            //     title: '附件',
            //     width: '250px',
            //     align: 'left',
            //     formatter: function(value, row, index) {
            //         return '<a href="#" style="color: black" title="' + value + '" class="attachment">' + value + '</a>';
            //     }
            // },
            {
                field: 'attachment',
                title: '附件',
                width: '120px',
                align: 'left',
                exportHidden:true,
                // formatter: function (value, row ,rec) {
                //     var sty = "";
                //     if (!row.attachment) {
                //         sty = "color: #0C0C0C";
                //     }
                //     return '<a href="#" style="' + sty + '" onclick="uploadFilesDialog(\'' + row.attachment + '\')">上传/下载</a>';
                // }
                formatter: function (value, row ,rec) {
                    //var row = $("#tb").datagrid("getChecked");

                    var sty = "";
                    if (!row.attachment) {
                        sty = "color: #0C0C0C";
                    }
                    var fileIds = row.attachment
                    var id = row.id
                    return '<a href="#" style="' + sty + '" onclick="onSiteAuthenticationFilesLookupDialog(\'' + id + '\',\'' + fileIds + '\')">上传/下载</a>';
                }
            },
            {
                field: 'createrName',
                title: '创建人',
                width: '100px',
                align: 'left'
            },
            {
                field: 'createTime',
                title: '创建日期',
                width: '110px',
                align: 'left',
                formatter: function (value, row, index) {
                    return DateFormatMs(value);
                }

            }
        ]],
        toolbar: ["-", {
            text: '新增',
            id: 'add',
            iconCls: 'icon-add',
            hidden: true,
            handler: function () {
                addOrEdit(0);
            }
        }, "-", {
            text: '变更',
            id: 'edit',
            iconCls: 'icon-edit',
            handler: function () {
                addOrEdit(1);
                setOperationLog("变更按钮", "模具列表");

            }
        }, "-", {
            text: '删除',
            id: 'delete',
            hide: true,
            iconCls: 'icon-cancel',
            handler: function () {
                delFactory();
            }
        }, "-", {
            text: '移模',
            id: 'moldMove',
            hide: true,
            iconCls: 'icon-remove',
                handler: function () {
                    moveMold();
            }
            },"-", {
            text: '查看',
            id: 'look',
            iconCls: 'icon-search',
            handler: function () {
                look(1);
                setOperationLog("查看按钮", "模具列表");
            }
        }, "-", {
            text: '导出',
            id: 'export',
            hide: true,
            iconCls: 'icon-reload',
            handler: function () {
                var params = searchParams();
                openExportDialog('pc/sys/mold/moldForm/rest/export','tb',params);
                setOperationLog("导出按钮", "模具列表");
            }
        }
        ],
        onLoadSuccess: function () {
            displayButtonByUserRole("Mold List");
            $(el.tb).datagrid('clearSelections');
        },
    });
}


function onSiteAuthenticationFilesLookupDialog (id, fileIds) {
    const uploadOptions = {
        title: '模具附件上传',
        width:'40%',
        height:'40%',
        fileIds: fileIds
    }
    if (id != undefined) {
        uploadOptions.callBack = function (fileIds) {
            $.ajax({
                url: g.ctx + 'pc/sys/mold/moldForm/rest/saveOnFile',
                method: "POST",
                data: {fileIds: fileIds.join(','), id: id},
                success: function (data) {
                    topWin.layer.alert("保存成功", {icon: 1}, function (idx) {
                        $("#tb").datagrid("reload")
                        topWin.layer.close(idx);
                    });
                },
                error: function (data) {
                    topWin.layer.alert(data.responseJSON.message, {icon: 2});
                }
            });
        }
    }
    openFileUploadDialog("模具附件上传", uploadOptions)
}

/**
 * 附件上传
 **/
function uploadFilesDialog(val){

    openFileUploadDialog('uploadFilesDialog',{
        title:'附件上传',
        fileIds : val,
        lookFlag: false,
        height: '75%',
        callBack : function (fileIds) {
            returnAddRequirefileUpload(fileIds.join(','))
        },
    });
}
function returnAddRequirefileUpload(ids) {

    var rows = $('#tb').datagrid('getSelected');
    var inx = $('#tb').datagrid('getRowIndex', rows);
    $('#tb').datagrid('endEdit', inx);
    $('#tb').datagrid('updateRow', {
        index: inx,
        row: {
            attachment: ids
        }
    })}
function moveMold() {
    var id= '';
    var cbox = $('#tb').datagrid('getSelected');
    if (cbox === null || cbox === undefined) {
        layer.msg("请选中一条记录");
        return;
    }
    if(cbox.deleteFlag === 1){
        topWin.layer.msg("只能操作删除状态为“否”的数据!")
        return
    }
    id = cbox.id;
    var moldFormDialog = openDialog('moldForm', g.ctx + 'pc/sys/mold/moldForm/moldMove/index?id=' + id, {
        title: '选择供应商',
        width: '900px',
        height: '520px',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            moldFormDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.close,
                iconCls: 'icon-cancel',
                handler: function () {
                    moldFormDialog.dialog('close');
                }
            }
        ]
    });
}
function addOrEdit(flag) {
    var id= '';
    if(flag===1){
        var cbox = $('#tb').datagrid('getSelected');
        if (cbox === null || cbox === undefined) {
            topWin.layer.msg("请选中一条记录");
            return;
        }
        if(cbox.deleteFlag === 1){
            topWin.layer.msg("只能操作删除状态为“否”的数据!")
            return
        }
        id = cbox.id;
    }



    var moldFormDialog = openDialog('moldForm', g.ctx + 'pc/sys/mold/moldForm/detail?id=' + id +'&flag=' + flag, {
        title: flag===0?'新增':'编辑',
        width: '900px',
        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 look(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/look?id=' + id +'&flag=' + flag, {
        title: '查看',
        width: '900px',
        height: '520px',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            moldFormDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.close,
                iconCls: 'icon-cancel',
                handler: function () {
                    moldFormDialog.dialog('close');
                }
            }
        ]
    });
}

function delFactory() {
    var id= '';

        var cbox = $('#tb').datagrid('getSelected');
        if (cbox === null || cbox === undefined) {
            layer.msg("请选中一条记录");
            return;
        }
    if(cbox.deleteFlag === 1){
        topWin.layer.msg("只能操作删除状态为“否”的数据!")
        return
    }
        id = cbox.id;


    var moldFormDialog = openDialog('moldForm', g.ctx + 'pc/sys/mold/moldForm/del?id=' + id, {
        title: '删除',
        width: '950px',
        height: '200px',
        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');
                }
            }
        ]
    });
}