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

$(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() {
    $('#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',
                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: 'supplierCode',
                title: '制造商编码',
                width: '85px',
                align: 'left'
            },
            {
                field: 'supplierName',
                title: '制造商名称',
                width: '180px',
                align: 'left'
            },
            {
                field: 'actualUseTimes',
                title: '实际使用次数',
                width: '80px',
                align: 'left'
            },
            {
                field: 'useTimes',
                title: '寿命次数',
                width: '80px',
                align: 'left'
            },
            {
                field: 'quantity',
                title: '分摊次数',
                width: '80px',
                align: 'left'
            },
            {
                field: 'isCallPolice',
                title: '是否报警',
                width: '80px',
                align: 'left',
                formatter: function (value) {
                    switch (value) {
                        case 0:
                            return '<span>否</span>';
                        case 1:
                            return '<span style="color: red;">是</span>';
                        default:
                            return '';
                    }
                }
            },
            {
                field: 'useState',
                title: '使用状态',
                width: '80px',
                align: 'left',
                formatter: function (value) {
                    switch (value) {
                        case 0:
                            return '使用';
                        case 1:
                            return '未使用';
                        default:
                            return '';
                    }
                }
            },
            {
                field: 'fixTimes',
                title: '维修次数',
                width: '80px',
                align: 'left'
            },
            {
                field: 'maintenanceTimes',
                title: '保养次数',
                width: '80px',
                align: 'left'
            },
            {
                field: 'dosage',
                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: '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',
                // 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);
        //     }
        // }, "-", {
        //     text: '删除',
        //     id: 'delete',
        //     hide: true,
        //     iconCls: 'icon-remove',
        //     handler: function () {
        //         delFactory();
        //     }
        // }, "-", {
        //     text: '移模',
        //     id: 'moldMove',
        //     hide: true,
        //     iconCls: 'icon-remove',
        //         handler: function () {
        //             moveMold();
        //     }
        //     }],
    });
}


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;
    }
    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) {
            layer.msg("请选中一条记录");
            return;
        }
        id = cbox.id;
    }

    var moldFormDialog = openDialog('moldForm', g.ctx + 'pc/sys/mold/moldForm/detail?id=' + id, {
        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 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);
            }
        });
    });
}


/**
 * 选择模具
 * @param call  选择模具数据的回调
 */
function chooseRows(call) {
    // if (single) {
        var rows = $(el.tb).datagrid('getChecked');
        if (rows.length > 1) {
            topWin.layer.msg("请选中一条记录");
            return;
        }
        call(rows[0]);
    // }
    // else {
    //     var rows = $(el.tb).datagrid('getSelections');
    //     if (rows === null || rows === undefined) {
    //         topWin.layer.msg("请选中一条记录");
    //         return;
    //     }
    //     call(rows);
    // }

}