var el = {
    form : '#form',
    attachment: '#attachment',
    qualityNotice: '#qualityNotice',
    emptyTd: '.empty-td',
    qualityNoticeData: '.quality_notice_data',
    qualityNoticeCode: '#qualityNoticeCode',
    suppllierName: '#suppllierName',
    badReason: '#badReason',
    problemSource: '#problemSource',
    userIdMap: {},
}


/**
 * 初始化页面特殊输入框
 */
$(function () {
    // 初始化不合格原因下拉框
    $(el.badReason).combobox({
        url: g.ctx + 'pc/sys/dict/getDicByType?type=reasons_for_nonconformity',
        panelHeight: 260,
        method: 'GET',
        valueField: 'code',
        textField: 'name',
        prompt: '-- 请选择 --',
        editable: false
    });
    // 初始化问题类型下拉框
    $(el.problemSource).combobox({
        url: g.ctx + 'pc/sys/dict/getDicByType?type=problem_source',
        panelHeight: 260,
        method: 'GET',
        valueField: 'code',
        textField: 'name',
        prompt: '-- 请选择 --',
        editable: false
    });
    // 初始化公司下拉框
    $('#companyCode').combobox({
        valueField: 'companyCode',
        textField: 'dataPrimary',
        url: g.ctx + 'pc/sys/company/rest/listAll',
        editable: false,
        prompt: '--- 请选择 ---',
        panelHeight: 'auto',
        panelWidth: 300,
        onSelect:function (data) {
            $('#companyName').textbox('setValue',data.companyName);
        }
    });
    loadLog();

});

/**
 * 初始化审批日志
 */
function loadLog() {
    $('#tbLog').datagrid({
        loadMsg: '数据远程载入中,请等待...',
        singleSelect: true,
        url: 'pc/spe/qualityManage/launchedQualityManage/rest/findMergeByPage',
        pagination: false,
        checkbox: true,
        rownumbers: true,
        fitColumns: true,
        idFiled: "id",
        // height: gridheight,
        width: gridwidth,
        queryParams: {
            page: 1,
            rows: 10,
            id: $("#id").val(),
            mergeFlag: $("#merge_flag").val()
        },
        onLoadSuccess: function () {
        },
        columns: [[
            {
                field: 'noticeOrderId',
                hidden: true
            },
            {
                field: 'examineNodeName',
                title: '审核节点',
                width: 130
            },
            {
                field: 'examineStatus',
                title: '审核状态',
                width: 120,
                formatter: function (value) {
                    if (value === 1) {
                        return '<span style="color: green;">审批通过</span>';
                    }
                    if (value === 0) {
                        return '<span style="color: red;">拒绝</span>';
                    }
                    return '';
                }
            },
            {
                field: 'examineUserCode',
                title: '审核人工号',
                width: 130
            },
            {
                field: 'examineUserName',
                title: '审核人姓名',
                width: 140
            },
            {
                field: 'examineView',
                title: '审核意见',
                width: 300
            },
            {
                field: 'examineTime',
                title: '审核时间',
                width: 140,
                formatter: function (value) {
                    if (value) {
                        return moment(value).format('YYYY-MM-DD HH:mm:ss');
                    }
                    return '';
                }
            }
        ]]
    });
}

/**
 * @param call
 * 编辑和保存
 */

function saveOrUpdate(call) {
    if($(el.form).form('validate')){
        var formData = $(el.form).serializeJson();
        var attachData = $(el.attachment).attr('data-fileIds')
        formData.attachment = attachData
        var batch_num = $("#batchNum").val();
        var bad_num = $("#badNum").val();
        if (parseInt(bad_num) > parseInt(batch_num)) {
            layer.msg("不合格数量不能大于检验批数量");
            return false;
        }
        var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        $.ajax({
            contentType: 'application/json',
            url: g.ctx + 'pc/spe/qualityManage/launchedQualityManage/rest/saveOrUpdate',
            method: 'post',
            data: JSON.stringify(formData),
            success: function (data) {
                call();
            },
            complete: function(data) {
                top.layer.close(loading);
            },
            error: function (data) {
                layer.alert(JSON.parse(data.responseText).message);
            }
        });
    }else{
        layer.msg("红色框为必输项，请填写！");
    }
}


function searchSupplier(){
    var userOrgyDialog = openDialog('userOrgyDialog', g.ctx + 'pc/supplier/supplierSelect?single=1', {
        title: "选择供应商",
        width: '800px',
        height: '500px',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            userOrgyDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.confirm,
                handler: function () {
                    topWin.frames['userOrgyDialog'].chooseRow(function (cbox) {console.log(cbox)
                        $("#supplierId").val(cbox.id);
                        $('#supplierCode').textbox('setValue', cbox.supplierCode);
                        $('#supplierName').textbox('setValue', cbox.supplierName);
                        userOrgyDialog.dialog('close');
                    });
                }
            },
            {
                text: itablei8n.close,
                handler: function () {
                    userOrgyDialog.dialog('close');
                }
            }
        ]
    });
}

/**
 * 选择物料页面
 */
function selectMaterial() {
    // let url = g.ctx + 'pc/sys/product/select/dialog?factoryCode=' + el.factoryCode;
    // let title = common_layer_title.select_material_title;
    // openLayerWin(title, url, '1000px', '560px');

    // let factoryCode = $("#factoryCode").combobox('getValue');
    // if(!factoryCode){
    //     layer.alert('请先选择工厂', {icon: 0});
    //     return false;
    // }
    var selectMaterialElement = openDialog("selectMaterialDialog", g.ctx + 'pc/sys/product/select/dialog?single=1', {
        title: '选择物料',
        width: '80%',
        height: '75%',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            selectMaterialElement.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.confirm,
                handler: function () {
                    topWin.frames['selectMaterialDialog'].chooseRows(function (cbox) {console.log(cbox)
                        //$("#supplierId").val(cbox.id);
                        $("#materialCode").textbox('setValue',cbox.materialCode);
                        $('#materialName').textbox('setValue',cbox.materialName);
                        selectMaterialElement.dialog('close');
                    });
                }
            },
            {
                text: itablei8n.close,
                handler: function () {
                    selectMaterialElement.dialog('close');
                }
            }
        ]


    });
}

/**
 * 选择部门
 */
function doSearch() {
    var userOrgyDialog = openDialog('userOrgyDialog', g.ctx + 'pc/sys/org/orgTree', {
        title: "组织部门",
        width: '400px',
        height: '700px',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            userOrgyDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.save,
                handler: function () {
                    topWin.frames['userOrgyDialog'].confirm(function (cbox) {
                        $("#materialUseDeptCode").val(cbox.code);
                        $('#materialUseDeptName').textbox('setValue', cbox.name);
                        // setOrgValue(cbox.id, cbox.name, cbox.code);
                        userOrgyDialog.dialog('close');
                    });
                }
            },
            {
                text: itablei8n.close,
                handler: function () {
                    userOrgyDialog.dialog('close');
                }
            }
        ]
    });
}

/**
 * 选择用户
 */
function userSearch () {
    var userDialogElement = openDialog("userDialog", g.ctx + "pc/sys/user/index?select=1&single=0", {
        title: "用户选择",
        width: '80%',
        height: '80%',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            userDialogElement.dialog('destroy');
        },
        buttons: [{
            text: itablei8n.save,
            handler: function () {
                topWin.frames['userDialog'].chooseRow(function (rows) {
                    //数组得用rows[0]
                    $('#mqeMceName').textbox('setValue', rows[0].username);
                    userDialogElement.dialog('close');
                });
            }
        }, {
            text: '关闭', iconCls: 'icon-cancel', handler: function () {
                userDialogElement.dialog('close');
            }
        }]
    })
}