let idParam, objParam, fileList = new Array(), imgListUp = new Array(); let form, laydate, layer, upload, table, util; let pageNum = 1, tableIns; // 定义分页 let jjDataArr = []; // 新增需求计划外机具的数据 let jjDetailArr = []; // 机具明细数据 function setParams(obj) { objParam = JSON.parse(obj); idParam = objParam.proId; $('#proName').html(objParam.proName); $('#planNum').html(objParam.planNum); layui.use(['form', 'layer', 'laydate', 'upload', 'table'], function () { form = layui.form; layer = layui.layer; laydate = layui.laydate; upload = layui.upload; table = layui.table; util = layui.util; laydate.render({ elem: '#createDay' }); form.verify(); form.on('submit(formData)', function (data) { submitApply(data); }); form.render(); let uploadObj = upload.render({ elem: '#test2', multiple: true, dataType: "json", exts: 'jpg|png|jpeg|doc|docx|pdf|xlsx|xls', acceptMime: 'image/jpg,image/png,image/jpeg,application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', number: 5, //最大上传数量 size: 1024 * 10, //最大文件大小,单位k auto: false, //是否自动上传 ,默认为true bindAction: '#hideUpload', //绑定的按钮 choose: function (obj) { uploadObj.config.elem.next()[0].value = ''; obj.preview(function (index, file, result) { console.log(file); $('#uploader-list').append( '
' + '

x

' + handleFileType(index, file, result) + '
' ); let map = new Map();//将选择的图片索引和图片写成对象存入集合 map.index = index; map.file = file; fileList.push(map); }); } }); initTable(); }); } // 设置文件类型 function handleFileType(index, file, result) { let html = '', img = ''; if (file.ext === 'doc' || file.ext === 'docx') { img = '../../../images/docx.png'; } else if (file.ext === 'xls' || file.ext === 'xlsx') { img = '../../../images/xlsx.png'; } else if (file.ext === 'pdf') { img = '../../../images/PDF.png'; } else { return ''; } html += '
' + '' + '

' + file.name + '

' + '
'; return html; } // 删除文件 $(document).on("click", ".file-iteme .handle", function (event) { imgListUp.splice(0, imgListUp.length); let index = $(this).next().attr('data-index'); $.each(fileList, function (inx, ele) { //对比删除文件索引 //将未删除的存入新集合 if (index != ele.index) { imgListUp.push(ele); } }); $(this).parent().remove(); //将新文件集合替换老集合 fileList.splice(0, fileList.length); $.each(imgListUp, function (inx, ele) { fileList.push(ele) }); }); // 查询/重置 function queryTable(type) { if (type === 1) { reloadTable(1); } else if (type === 2) { $('#name').val(''); $('#module').val(''); layui.form.render(); reloadTable(1); } } // 重载表格 function reloadTable(pageNum) { table.reload("currentTableId", { page: { curr: pageNum ? pageNum : 1, }, where: { encryptedData: JSON.stringify({ 'name': $('#name').val(), 'module': $('#module').val(), 'proId': idParam }), }, }, ); } // 表格数据 function initTable() { tableIns = table.render({ elem: "#currentTableId", id: 'currentTableId', height: "full", headers: { authorization: sessionStorage.getItem("gz-token"), }, url: dataUrl + "backstage/planOut/getPorInfoDetail", where: { encryptedData: JSON.stringify({ 'name': $('#name').val(), 'module': $('#module').val(), 'proId': idParam }), }, request: { pageName: 'pageNum', limitName: 'pageSize' }, parseData: function (res) { // res 即为原始返回的数据 if (jjDetailArr && res.list) { $.each(res.list, function (index, item) { $.each(jjDetailArr, function (index2, item2) { if (item.id === item2.id) { item.num = item2.num; item.tzNum = item2.tzNum; item.outId = item2.outId; } }); }); } return { "code": 0, // 解析接口状态 "msg": '获取成功', // 解析提示文本 "count": res.total, // 解析数据长度 "data": res.list // 解析数据列表 }; }, cols: [ [ { width: 80, title: "序号", align: "center", templet: function (d) { return d.LAY_NUM; }, }, { field: "type", width: 150, title: "物机类型", unresize: true, align: "center", }, { field: "name", width: 150, title: "物机名称", unresize: true, align: "center", }, { field: "module", width: 120, title: "规格", unresize: true, align: "center", }, { field: "unit", title: "单位", width: 120, unresize: true, align: "center", }, { field: "needNum", width: 120, title: "需要量", unresize: true, align: "center", templet: function (d) { return setNumColor(d.needNum, 1); }, }, { field: "fhNum", width: 120, title: "已发货量", unresize: true, align: "center", templet: function (d) { return setNumColor(d.fhNum, 2); }, }, { field: "diff", width: 120, title: "差缺量", unresize: true, align: "center", templet: function (d) { return setNumColor(d.diff, 3); }, }, { field: "num", width: 150, title: " * 本次发货量", unresize: true, align: "center", edit: 'text', style: 'outline: 1px solid #e6e6e6;outline-offset: -5px;' }, { field: "tzNum", width: 150, title: "调整量", unresize: true, align: "center", edit: 'text', style: 'outline: 1px solid #e6e6e6;outline-offset: -5px;' }, { field: "outId", width: 250, title: "备注", unresize: true, align: "center", edit: 'textarea', style: 'outline: 1px solid #e6e6e6;outline-offset: -5px;' }, ], ], limits: [10, 15, 20, 25, 50, 100], limit: 10, page: true, done: function (res, curr, count) { pageNum = tableIns.config.page.curr; $("#tips").suspensionTips({ "content": "1.如需单个机具进度达到100%,请填写不发货的数量;2.若填写不发货数量,请填写备注不发货原因及附件说明", position: "top", width: 300 }); }, }); table.on('edit(currentTableId)', function (obj) { console.log(obj); var field = obj.field; // 得到修改的字段 var value = obj.value // 得到修改后的值 var oldValue = obj.oldValue // 得到修改前的值 -- v2.8.0 新增 var data = obj.data // 得到所在行所有键值 var col = obj.getCol(); // 得到当前列的表头配置属性 -- v2.8.0 新增 if (field === 'num' || field === 'tzNum') { // 本次发货量/调整量 if (value) { const regex = /^(0|[1-9]\d{0,6})$/; if (!regex.test(value)) { obj.reedit(); return layer.msg('格式不正确,最大输入6位数,且为正整数!', { icon: 7 }) } } } else if (field === 'outId') { // 备注 if (value) { if (value.length > 255) { obj.reedit(); return layer.msg('备注最多输入255位!', { icon: 7 }) } } } // 显示 - 仅用于演示 layer.msg('[ID: ' + data.id + '] ' + field + ' 字段更改值为:' + util.escape(value)); let id = obj.data.id; let num = obj.data.num; let tzNum = obj.data.tzNum; let outId = obj.data.outId; let type = obj.data.type; let name = obj.data.name; let module = obj.data.module; let objParam = { id: id, num: num, tzNum: tzNum, outId: outId, type: type, name: name, module: module, remarks: outId } updateOrAddObject(id, objParam); }); } // 根据id 判断对象数组的对象是否存在 如果存在则替换,不存在则添加 function updateOrAddObject(id, newObject) { const index = jjDetailArr.findIndex(obj => obj.id === id); if (index !== -1) { // 如果对象存在,则替换它 jjDetailArr[index] = newObject; } else { // 如果对象不存在,则添加到数组 jjDetailArr.push(newObject); } } function saveData2() { $('#formSubmit').trigger('click') } function setNumColor(value, type) { // 1.需要量 2.已发货量 3.差缺量 let color = '#66b1ff'; if (type === 1) { color = '#66b1ff'; } else if (type === 2) { color = '#19be6b'; } else if (type === 3) { color = '#ff9900'; } return '' + setNullValue(value) + ""; } // 提交 function submitApply(data) { // 校验发货附件是否上传 if (fileList.length === 0) { return layer.msg('请上传发货附件', { icon: 7 }); } // 校验机具发货数量 if (jjDetailArr.length === 0) { return layer.msg('未填写发货数量', { icon: 7 }); } // 校验调整量发生变化是否填写备注 for (let i = 0; i < jjDetailArr.length; i++) { let obj = jjDetailArr[i]; if(obj.tzNum > 0 && !obj.remarks){ return layer.msg(obj.type+'-' + obj.name+'-' + obj.module + ',已修改调整量,请填写备注', { icon: 7 }); } } data.field.proId = idParam; data.field.list = jjDetailArr; data.field.addList = jjDataArr; let formData = new FormData(); //遍历最终文件集合 for (let i = 0; i < fileList.length; i++) { formData.append("file[]", fileList[i].file) } formData.append('params', JSON.stringify(data.field)); console.log(JSON.stringify(data.field)); let loadingMsg = layer.msg('正在提交保存,请稍等...', { icon: 16, shade: 0.01, time: '0' }); console.log(JSON.stringify(data.field)); let url = dataUrl + 'backstage/planOut/insertProOutPutInfo'; ajaxRequestByUploadFile(url, formData, function () { $('.save').addClass("layui-btn-disabled").attr("disabled", true); $('.cancel').addClass("layui-btn-disabled").attr("disabled", true); }, function (result) { console.log(result); layer.close(loadingMsg); $('.save').removeClass("layui-btn-disabled").attr("disabled", false); $('.cancel').removeClass("layui-btn-disabled").attr("disabled", false); if (result.code === 200) { parent.layer.msg(result.msg, { icon: 1 }); closePage(1); } else { layer.msg(result.msg, { icon: 2 }); } }, function (xhr, status, error) { layer.close(loadingMsg); // 关闭提示层 layer.msg('服务异常,请稍后重试', { icon: 16, scrollbar: false, time: 2000 }); $('.save').removeClass("layui-btn-disabled").attr("disabled", false); $('.cancel').removeClass("layui-btn-disabled").attr("disabled", false); errorFn(xhr, status, error) }, null); } // 处理数字 function handleNum(value) { if (!value) { return null; } const regex = /^(0|[1-9]\d{0,6})$/; if (!regex.test(value)) { return layer.msg('格式不正确,最大输入6位数,且为正整数!', { icon: 7 }) } return null; } // 详情 function openDetail() { alert('详情'); } // 新增需求计划外机具 function addJjData() { let params = '-1'; if (jjDataArr && jjDataArr.length > 0) { params = JSON.stringify(jjDataArr); } openIframeByParam("addJjData", '新增需求计划外机具', "./add_jj_data.html", '72%', '80%', params); } function addJjDatas(data) { console.log('新增的数据:' + data); jjDataArr = JSON.parse(data); } // 关闭页面 function closePage(type) { let index = parent.layer.getFrameIndex(window.name); // 先得到当前 iframe层的索引 parent.layer.close(index); // 再执行关闭 if (type == 1) { window.parent.reloadData(); } }