<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>新增批次</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="format-detection" content="telephone=no">
		<link rel="stylesheet" type="text/css" href="../../layui/css/layui.css" />
		<link rel="stylesheet" type="text/css" href="../../css/common.css" />
		<link rel="stylesheet" type="text/css" href="../../css/inputTags.css" />
		<style type="text/css">
			.ui-required:before {
				content: "*";
				color: red;
				vertical-align: middle;
			}

			#roleSelectViwe .layui-form-item {
				margin-bottom: 0
			}

			#roleSelectViwe .layui-input-inline {
				width: calc(100% - 110px);
				margin-right: 0;
			}

			.region {
				padding-left: 10px;
				height: 38px;
				line-height: 38px;
				background-color: #fff;
				border-radius: 2px;
				border: 1px solid #e6e6e6;
				width: calc(100% - 132px) !important;
			}

			.userTip {
				color: #666;
				font-size: 12px;
			}

			.userTip b {
				color: #333;
			}

			.my-middle {
				line-height: 36px;

			}

			.my-button {
				width: 60%;
				margin-left: 20%;
				height: 40px;
				text-align: center;
				line-height: 40px;
				border: 1px dashed;
				border-radius: 5px;
			}

			.my-inline {
				display: inline-block;
				float: left;
				text-align: center;
				height: 38px;
				line-height: 38px;
				width: 10%;
			}

			.my-images-inline {
				display: inline-block;
				width: 104px;
				height: 104px;
				border: 1px dashed;
				border-radius: 5px;
				padding: 4px;
				vertical-align: top;
				margin: 4px;
			}

			.my-img {
				width: 100%;
				height: 100%;
			}

			.my-images-add {
				display: inline-block;
				text-align: center;
				line-height: 103px;
				font-size: 32px;
				cursor: pointer;
				background-color: #fbfbfb;
			}

			.my-del {
				background-color: black;
				color: white;
			}

			.button-delImage.button-delAssayImage {
				position: relative;
				left: -2px;
				top: -106px;
			}
		</style>
	</head>

	<body>
		<form class="layui-form" action="" lay-filter="addForm">
			<div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
				<ul class="layui-tab-title">
					<li class="layui-this" lay-id="1">基础信息</li>
					<li lay-id="2">原料生产</li>
					<li lay-id="3">加工流通</li>
					<li lay-id="4">检疫检验</li>
				</ul>
				<div class="layui-tab-content">
					<div class="layui-tab-item layui-show">
						<div class="layui-form-item">
							<label class="layui-form-label ui-required">产品分类</label>
							<div class="layui-input-block my-middle" id="goodsName">

							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label ui-required">批次编码</label>
							<div class="layui-input-block">
								<input type="text" name="goodsBatchNo" lay-verify="required" maxlength="20" placeholder="请输入批次编码" autocomplete="off" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label ui-required">产品规格</label>
							<div class="layui-input-block">
								<input type="text" name="goodsUnit" lay-verify="required" maxlength="20" placeholder="请输入产品规格" autocomplete="off" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label ui-required">生产地</label>
							<div class="layui-input-block">
								<select name="fkFieldGuid" lay-verify="required" lay-filter="roles" id="rolesView"></select>
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label ui-required">生产日期</label>
							<div class="layui-input-block">
								<input type="text" name="productDateStr" id="goodsProductDate" lay-verify="required" maxlength="20" placeholder="请输入生产日期" autocomplete="off" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label ui-required">保质期</label>
							<div class="layui-input-block">
								<input type="text" name="goodsExpired" lay-verify="" maxlength="10" placeholder="请输入保质期" autocomplete="off" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label ui-required">产品标签</label>
							<div class="layui-input-block">
								<div class="tags" id="tags">
									<input type="text" id="inputTags" placeholder="回车生成标签" autocomplete="off">
								</div>
							</div>
						</div>
						<!-- <div class="layui-form-item">
							<label class="layui-form-label">销售网址</label>
							<div class="layui-input-block">
								<input type="text" name="goodsSaleUrl" lay-verify="" maxlength="300" placeholder="请输入在线销售网址" autocomplete="off"
								 class="layui-input">
							</div>
						</div> -->
						<div class="layui-card">
							<div class="layui-card-header">自定义信息</div>
							<div class="layui-card-body">
								<div id="auxListDiv"></div>
								<div class="layui-form-item">
									<div class="my-button my-button-product">+添加</div>
								</div>
							</div>
						</div>
						<div class="layui-card">
							<div class="layui-card-header">产品图片视频</div>
							<div class="layui-card-body">
								<div id="imageListDiv"></div>
								图片视频上传完毕后请记得按保存哦
							</div>
						</div>
					</div>

					<div class="layui-tab-item">
						<div class="layui-card">
							<div class="layui-card-header">原料信息</div>
							<div class="layui-card-body">
								<div style="display: flex;">
									<input class="layui-input" style="margin-right: 15px;" type="text" name="rawMaterial" />
									<button type="button" class="layui-btn" id="rawMaterialInfoAdd">新增</button>
								</div>
								<style type="text/css">
									#rawMaterialInfo li {
										display: inline-block;
										border: 1px solid rgba(0, 0, 0, .2);
										background: rgba(0, 0, 0, .1);
										border-radius: .2em;
										padding: .3em .6em;
										margin: 8px;
									}

									#rawMaterialInfo li i:hover {
										cursor: pointer;
									}
								</style>
								<script type="text/html" id='rawMaterialInfoTpl'>
								{{# layui.each(d,function(i,item){ }}
									<li>
										<div style="display: flex;align-items: center;">
											<span>{{item.inputsProcessName}}</span>
											{{#if ("view" !== location.searchObj().event){}}
											<i data-bind="{{i||0}}" class="layui-icon layui-icon-close" style="margin-left: 5px;"></i>
											{{#} }}
										</div>
									</li>
									{{# });}}
								</script>
								<ol id="rawMaterialInfo"></ol>
							</div>
						</div>
						<div class="layui-card">
							<div class="layui-card-header">生产过程信息</div>
							<div class="layui-card-body">
								<div id="produceProcessInfoForm">
									<div class="layui-input-inline">
										<label class="layui-form-label">执行项</label>
										<div class="layui-input-inline">
											<input class="layui-input" style="margin-right: 15px;" type="text" name="processItem" />
										</div>
									</div>
									<div class="layui-input-inline">
										<label class="layui-form-label">执行人</label>
										<div class="layui-input-inline">
											<input class="layui-input" style="margin-right: 15px;" type="text" name="processUser" />
										</div>
									</div>
									<div class="layui-input-inline">
										<label class="layui-form-label">开始日期</label>
										<div class="layui-input-inline">
											<input class="layui-input" style="margin-right: 15px;" type="text" name="processTime" id="produceProcessInfoForm_processTime" />
										</div>
									</div>
									<div class="layui-input-inline">
										<label class="layui-form-label">执行描述</label>
										<div class="layui-input-inline">
											<input class="layui-input" style="margin-right: 15px;" type="text" name="processDesc" />
										</div>
									</div>
									<input type="hidden" name="processType" value="1" />
									<button type="button" class="layui-btn" id='produceProcessInfoAdd'>新增</button>
									<table class="layui-hide" id="produceProcessInfo" lay-filter="produceProcessInfo"></table>
								</div>
							</div>
						</div>
					</div>

					<div class="layui-tab-item" id="processingCirculationInfoForm">
						<div class="layui-input-inline">
							<label class="layui-form-label">执行项</label>
							<div class="layui-input-inline">
								<input class="layui-input" style="margin-right: 15px;" type="text" name="processItem" />
							</div>
						</div>
						<div class="layui-input-inline">
							<label class="layui-form-label">执行人</label>
							<div class="layui-input-inline">
								<input class="layui-input" style="margin-right: 15px;" type="text" name="processUser" />
							</div>
						</div>
						<div class="layui-input-inline">
							<label class="layui-form-label">开始日期</label>
							<div class="layui-input-inline">
								<input class="layui-input" style="margin-right: 15px;" type="text" name="processTime" id="processingCirculationInfoForm_processTime" />
							</div>
						</div>
						<div class="layui-input-inline">
							<label class="layui-form-label">执行描述</label>
							<div class="layui-input-inline">
								<input class="layui-input" style="margin-right: 15px;" type="text" name="processDesc" />
							</div>
						</div>
						<input type="hidden" name="processType" value="2" />
						<button type="button" class="layui-btn" id='processingCirculationInfoAdd'>新增</button>
						<table class="layui-hide" id="processingCirculationInfo" lay-filter="processingCirculationInfo"></table>
					</div>
					<div class="layui-tab-item">
						<div class="layui-card">
							<div class="layui-card-header">检验信息</div>
							<div class="layui-card-body">
								<div class="layui-form-item">
									<label class="layui-form-label">检验单位：</label>
									<div class="layui-input-block">
										<input type="text" name="assayDepartment" lay-verify="" maxlength="10" placeholder="请输入检验单位" autocomplete="off" class="layui-input">
									</div>
								</div>
								<div id="assayAuxListDiv"></div>
								<div class="layui-form-item">
									<div class="my-button my-button-assay">+添加</div>
								</div>
							</div>
						</div>
						<div class="layui-card">
							<div class="layui-card-header">检验报告</div>
							<div class="layui-card-body">
								<div id="assayImageListDiv"></div>
								图片视频上传完毕后请记得按保存哦
							</div>
						</div>
					</div>
				</div>
				<div class="layui-form-item" style="text-align: center;padding: 25px;">
					<button class="layui-btn" lay-submit lay-filter="formDemo">保存</button>
					<button type="reset" class="layui-btn layui-btn-primary close-win">取消</button>
				</div>
			</div>
		</form>

		<script type="text/html" id="typeHtmlTpl">
			<option value="">请选择</option>
		{{# if(!d.list){d.list = []}; layui.each(d.list, function(index, item){ }}
		<option value="{{item.guid}}" oneMap="{{item.oneMap}}">{{item.fieldName}}</option>
		{{# }); }}
	</script>
		<script type="text/html" id="auxHtmlTpl">
		{{# if(!d.list){d.list = []}; layui.each(d.list, function(index, item){ }}
			<div class="layui-form-item">
				<label class="layui-form-label">自定义项{{index+1}}：</label>
				<div class="my-inline" style="width: 20%">
					<input type="text" name="auxItme" maxlength="20" lay-verify="required" placeholder="标题" autocomplete="off" class="layui-input" value="{{item.auxItme}}">
				</div>
				<div class="my-inline">--</div>
				<div class="my-inline" style="width: 40%">
					<input type="text" name="auxItmeVal" maxlength="100" lay-verify="required" placeholder="项" autocomplete="off" class="layui-input" value="{{item.auxItmeVal}}">
				</div>
				<div class="my-inline">
					<i class="layui-icon my-del button-delAux" data-bind="{{index}}" title="删除" data-icon="&#x1006;">&#x1006;</i>
				</div>
			</div>
			{{# }); }}
		</script>
		<script type="text/html" id="imageHtmlTpl">
			<div class="layui-form-item">
			{{# if(!d.list){d.list = []}; layui.each(d.list, function(index, item){ }}
			<div class="my-images-inline">
				{{#if ("jpg|jpeg|png|gif|bmp".indexOf(item.substring(item.lastIndexOf(".")+1))==-1){}}
				<video class="my-img" src="{{window.hywa.config.href}}/{{item}}" autoplay loop></video>
				{{#}else{}}
				<img class="my-img" src="{{window.hywa.config.href}}/{{item}}">
				{{#} }}
				<i class="layui-icon my-del button-delImage" data-bind="{{index}}" title="删除" data-icon="&#x1006;">&#x1006;</i>
			</div>
			{{# }); }}
			<div class="my-images-inline my-images-add" id="imageUploadBtn">+</div>
		</div>
	</script>
		<script type="text/html" id="assayAuxHtmlTpl">
		{{# if(!d.list){d.list = []}; layui.each(d.list, function(index, item){ }}
			<div class="layui-form-item">
				<label class="layui-form-label">{{#if(index==0){}}检验数据：{{#} }}</label>
				<div class="my-inline" style="width: 20%">
					<input type="text" name="auxItme2" maxlength="20" placeholder="检验项名" autocomplete="off" class="layui-input" value="{{item.auxItme}}">
				</div>
				<div class="my-inline">--</div>
				<div class="my-inline" style="width: 40%">
					<input type="text" name="auxItmeVal2" maxlength="100" placeholder="数值和单位" autocomplete="off" class="layui-input" value="{{item.auxItmeVal}}">
				</div>
				<div class="my-inline">
					<i class="layui-icon my-del button-delAssay" data-bind="{{index}}" title="删除" data-icon="&#x1006;">&#x1006;</i>
				</div>
			</div>
			{{# }); }}
		</script>
		<script type="text/html" id="assayImageHtmlTpl">
			<div class="layui-form-item">
			{{# if(!d.list){d.list = []}; layui.each(d.list, function(index, item){ }}
			<div class="my-images-inline">
				{{#if ("jpg|jpeg|png|gif|bmp".indexOf(item.substring(item.lastIndexOf(".")+1))==-1){}}
				<video class="my-img" src="{{window.hywa.config.href}}/{{item}}" autoplay loop></video>
				{{#}else{}}
				<img class="my-img" src="{{window.hywa.config.href}}/{{item}}">
				{{#} }}
				<i class="layui-icon my-del button-delAssayImage" data-bind="{{index}}" title="删除" data-icon="&#x1006;">&#x1006;</i>
			</div>
			{{# }); }}
			<div class="my-images-inline my-images-add" id="assayImageUploadBtn">+</div>
		</div>
	</script>
		<script type="text/javascript">
			function loadJS(url) {
				document.write("<script src='" + url + "?ver=" + new Date().getTime() + "'><\/script>")
			}
			loadJS("../../layui/layui.js");
			loadJS("../../js/ajaxhook.min.js");
			loadJS("../../js/config.js");
		</script>
		<script type="text/javascript">
			layui.config({
				base: "../../js/layuiPlugins/"
			}).use(["form", 'layer', 'laytpl', 'jquery', 'laydate', 'element', 'upload', 'table', 'inputTags'], function () {				
				var form = layui.form,
					layer = layui.layer,
					laytpl = layui.laytpl,
					$ = layui.jquery,
					laydate = layui.laydate,
					element = layui.element,
					upload = layui.upload,
					inputTags = layui.inputTags
				table = layui.table;
				laydate.render({
					elem: '#goodsProductDate' //指定元素
				});
				laydate.render({
					type: 'datetime',
					elem: '#produceProcessInfoForm_processTime' //指定元素
				});
				laydate.render({
					type: 'datetime',
					elem: '#processingCirculationInfoForm_processTime' //指定元素
				});
				var funcName = "pdinfo";
				var rolesViewOnemapParam = "";
				var formData = {
					guid: (JSON.stringify(location.searchObj()) != "{}") ? location.searchObj().guid || null : null, //带参传入ID
					current: {
						auxList: [],
						imagesList: [],
						assayInfoForm: {
							imagesList: [],
							auxList: []
						}
					}, //当前表单数据
					select_data: "select_" + funcName, //查询用户
					save_data: (location.searchObj().guid ? 'edit' : 'add') + '_' + funcName, //查询用户
					formLayFilter: "addForm", //表单容器
					closeModal: function () { //关闭当前窗口
						parent.layer.close(parent.layer.getFrameIndex(window.name));
					}
				};

				var option = {
					elem: '#imageUploadBtn',
					url: 'uploadImage',
					accept: "file",
					exts: "jpg|jpeg|png|gif|bmp|mp4|ogg|webm|avi|wma|rmvb|rm|flash|3gp|flv",
					size: 10240, //kb
					before: function () {
						uploadMsg = layer.msg('正在上传中……', {
							icon: 16,
							fixed: false,
							time: 0
						});
					},
					done: function (res) {
						layer.close(uploadMsg);
						formData.current.imagesList.push(res.data[0]);
						imageTpl({
							elem: "#imageListDiv",
							list: formData.current.imagesList
						});
					},
					error: function () { }
				};
				var assayOption = {
					elem: '#assayImageUploadBtn',
					url: 'uploadImage',
					accept: "file",
					exts: "jpg|jpeg|png|gif|bmp|mp4|ogg|webm|avi|wma|rmvb|rm|flash|3gp|flv",
					size: 10240, //kb
					before: function () {
						uploadMsg = layer.msg('正在上传中……', {
							icon: 16,
							fixed: false,
							time: 0
						});
					},
					done: function (res) {
						layer.close(uploadMsg);
						formData.current.assayInfoForm.imagesList.push(res.data[0]);
						assayImageTpl({
							elem: "#assayImageListDiv",
							list: formData.current.assayInfoForm.imagesList
						});
					},
					error: function () { }
				};
				//自定义项列表模板
				function auxTpl(obj) {
					laytpl(auxHtmlTpl.innerHTML).render(obj, function (html) {
						$(obj.elem).html(html);
						$(".button-delAux").on("click", function (e) {
							var sortno = e.target.getAttribute("data-bind");
							for (var i = formData.current.auxList.length - 1; i >= 0; i--) {
								if ($("input[name='auxItme']")[i]) {
									formData.current.auxList[i].auxItme = $("input[name='auxItme']")[i].value;
									formData.current.auxList[i].auxItmeVal = $("input[name='auxItmeVal']")[i].value;
								}
							}
							formData.current.auxList.splice(sortno, 1);
							auxTpl({
								elem: "#auxListDiv",
								list: formData.current.auxList
							});
						});
					});
				}
				//检验数据列表模板
				function assayTpl(obj) {
					laytpl(assayAuxHtmlTpl.innerHTML).render(obj, function (html) {
						$(obj.elem).html(html);
						$(".button-delAssay").on("click", function (e) {
							var sortno = e.target.getAttribute("data-bind");
							var index = formData.current.assayInfoForm.auxList.length;
							for (var i = index - 1; i >= 0; i--) {
								if ($("input[name='auxItme2']")[i]) {
									formData.current.assayInfoForm.auxList[i].auxItme = $("input[name='auxItme2']")[i].value;
									formData.current.assayInfoForm.auxList[i].auxItmeVal = $("input[name='auxItmeVal2']")[i].value;
								}
							}
							formData.current.assayInfoForm.auxList.splice(sortno, 1);
							assayTpl({
								elem: "#assayAuxListDiv",
								list: formData.current.assayInfoForm.auxList
							});
						});
					});
				}
				//下拉列表模板
				function typeTpl(obj) {
					laytpl(typeHtmlTpl.innerHTML).render(obj, function (html) {
						$(obj.elem).html(html);
						form.render();
					});
				}
				//图片列表模板
				function imageTpl(obj) {
					laytpl(imageHtmlTpl.innerHTML).render(obj, function (html) {
						$(obj.elem).html(html);
						upload.render(option);
						$(".button-delImage").on("click", function (e) {
							formData.current.imagesList.splice(e.target.getAttribute("data-bind"), 1);
							imageTpl({
								elem: "#imageListDiv",
								list: formData.current.imagesList
							});
						});
					});
				}
				//检验图片列表模板
				function assayImageTpl(obj) {
					laytpl(assayImageHtmlTpl.innerHTML).render(obj, function (html) {
						$(obj.elem).html(html);
						upload.render(assayOption);
						$(".button-delAssayImage").on("click", function (e) {
							formData.current.assayInfoForm.imagesList.splice(e.target.getAttribute("data-bind"), 1);
							assayImageTpl({
								elem: "#assayImageListDiv",
								list: formData.current.assayInfoForm.imagesList
							});
						});
					});
				}
				//原料信息模板
				function rawMaterial(data) {
					laytpl(rawMaterialInfoTpl.innerHTML).render(data, function (html) {
						$('#rawMaterialInfo').html(html);
					});
				}
				$('#rawMaterialInfo').on("click", 'li i', function (e) { //删除
					if (formData.guid) {
						$.ajax({
							url: 'del_rawMaterialInfo',
							data: {
								guid: formData.current.inputsFormList[e.target.getAttribute("data-bind")].guid
							},
							success: function (res) {

							}
						});
					}
					formData.current.inputsFormList.splice(e.target.getAttribute("data-bind"), 1);
					rawMaterial(formData.current.inputsFormList);
				});
				$("#rawMaterialInfoAdd").on('click', function () {
					let val = $('input[name="rawMaterial"]').val();
					if (val) {
						formData.current.inputsFormList = formData.current.inputsFormList || [];
						formData.current.inputsFormList.push({
							inputsProcessName: val
						});
						rawMaterial(formData.current.inputsFormList);
						if (formData.guid) {
							formData.submitInfo('add_rawMaterialInfo', {
								fkGoodsBatchGuid: formData.guid,
								inputsProcessName: val
							}, function () {
								formData.getCurrent();
							});
						}
					}
				});

				//生产过程信息
				formData.current.materialList = formData.current.materialList || [];
				//编号
				for (let i in formData.current.materialList) {
					formData.current.materialList[i].index = i;
				}
				let produceProcessInfoOptions = {
					id: "produceProcessInfo",
					elem: '#produceProcessInfo',
					data: formData.current.materialList,
					cols: [
						[{
							field: 'processItem',
							title: '执行项',
							edit: 'text',
							event: 'edit',
							align: 'center'
						}, {
							field: 'processTime',
							title: '开始时间',
							edit: 'time',
							align: 'center',
							event: 'edit'
						}, {
							field: 'processUser',
							title: '执行人',
							edit: 'text',
							align: 'center',
							event: 'edit'
						}, {
							field: 'processDesc',
							title: '执行描述',
							edit: 'text',
							align: 'center',
							event: 'edit'
						}, {
							field: 'processImgs',
							title: '上传过程照片',
							// toolbar: `<div><a href="javascript:void(0);" lay-event="processImgsPreview">{{d.processImgs || ''}}</a></div>`,
							toolbar: ("view" !== location.searchObj().event) ? `<div>
								<a class="layui-btn layui-btn-xs" lay-event="upload">上传</a>
								<a target="_blank" href="{{window.hywa.config.href}}/{{d.processImgs || ''}}" lay-event="aHref">{{d.processImgs || ''}}</a>
							</div>` : `<div>
								<a target="_blank" href="{{window.hywa.config.href}}/{{d.processImgs || ''}}" lay-event="aHref">{{d.processImgs || ''}}</a>
							</div>`,
							align: 'center'
						}, {
							title: '操作',
							toolbar: ("view" !== location.searchObj().event) ? `<div>
								<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" id="produceProcessInfoDeeBtn">删除</a>
							</div>` : `<div>&nbsp;</div>`,
							align: 'center'
						}]
					],
					page: true
				};
				let produceProcessInfoTable = table.render(produceProcessInfoOptions);
				//监听工具条
				table.on('tool(produceProcessInfo)', function (obj) {
					var data = obj.data;
					if (obj.event === 'detail') {
						layer.msg('ID：' + data.id + ' 的查看操作');
					} else if (obj.event === 'del') {
						layer.confirm('真的删除行么', function (index) {
							obj.del();
							formData.current.materialList.map(function (item, index) {
								item.index == obj.data.index && formData.current.materialList.splice(index, 1);
							});
							produceProcessInfoOptions.data = formData.current.materialList;
							// 刷新表格数据
							produceProcessInfoTable.reload(produceProcessInfoOptions);

							if (formData.guid) {
								$.ajax({
									url: 'del_produceProcessInfo',
									data: {
										guid: data.guid
									},
									success: function (res) {
									}
								});
							}
							layer.close(index);
						});
					} else if (obj.event === 'upload') {
						let upelem = document.createElement('input'),
							uploadMsg = 0;
						upload.render({
							elem: upelem, //绑定元素
							url: 'uploadImage',
							accept: "file",
							exts: "jpg|jpeg|png|gif|bmp|mp4|ogg|webm|avi|wma|rmvb|rm|flash|3gp|flv|mp3",
							size: 10240,
							before: function () {
								uploadMsg = layer.msg('正在上传中……', {
									icon: 16,
									fixed: false,
									time: 0
								});
							},
							done: function (res) {
								layer.close(uploadMsg);
								if (!(res.code - 0)) {
									let a = obj.tr[0].querySelector('a[lay-event="aHref"]');
									a.href = window.hywa.config.href + '/' + res.data[0];
									a.innerHTML = res.data[0];
									let ggObj = {};
									formData.current.materialList.map(function (item, index) {
										if (item.index == obj.data.index) {
											formData.current.materialList[index].processImgs = res.data[0];
											ggObj = formData.current.materialList[index];
										}
									});
									if (formData.guid) {
										formData.submitInfo('edit_produceProcessInfo', ggObj, function () {
											formData.getCurrent();
										});
									}
								}
							},
							error: function () {

							}
						});
						upelem.click();
					} else if (obj.event === 'processImgsPreview') {
						let imageSrc = ((obj.data || {}).processImgs || '');
						if (imageSrc.indexOf("downloadDocument") != -1) {
							imageSrc = imageSrc;
						} else {
							imageSrc = window.hywa.config.href + imageSrc;
						}
						let processImgsPreviewDialog = '<div class="dialog-role f-dsn" id="processImgsPreviewDialog"><div style="text-align: center"><img id="processImgsPreviewDialogimg" src="' + imageSrc + '" style="width: 550px; height: 520px; margin: 10px;" /></div></div>';
						layer.open({
							type: 1,
							title: "图片预览",
							fixed: true,
							area: ['600px', '600px'],
							shadeClose: true,
							scrollbar: false,
							content: processImgsPreviewDialog
						});
					} else {
						return;
					}
				});
				table.on('edit(produceProcessInfo)', function (obj) {
					//修改后响应
					let ggObj = {};
					formData.current.materialList.map(function (item, index) {
						if (item.index == obj.data.index) {
							formData.current.materialList[index] = obj.data;
							ggObj = formData.current.materialList[index];
						}
					});
					if (formData.guid) {
						formData.submitInfo('edit_produceProcessInfo', ggObj, function () {
							formData.getCurrent();
						});
					}
				});
				//监听新增
				$('#produceProcessInfoAdd').on('click', function () {
					let input = $("#produceProcessInfoForm")[0].querySelectorAll('input');
					let obj = {};
					let ver = [];
					layui.each(input, function (i, item) {
						if (item.name) {
							obj[item.name] = item.value;
							ver.push(item.value);
						}
					});
					for (let i = 0; i < ver.length; i++) {
						if (!ver[i]) {
							layer.msg("全部都需要填写噢", {
								icon: 2
							});
							return false;
						}
					}
					if (formData.guid) {
						obj.fkGoodsBatchGuid = formData.guid;
						formData.submitInfo('add_produceProcessInfo', obj, function () {
							formData.getCurrent();
						});
					}
					formData.current.materialList.push(obj);
					//编号
					for (let i in formData.current.materialList) {
						formData.current.materialList[i].index = i;
					}
					table.reload("produceProcessInfo", {
						data: formData.current.materialList
					});
				});
				//加工流通
				formData.current.productList = formData.current.productList || [];
				//编号
				for (let i in formData.current.productList) {
					formData.current.productList[i].index = i;
				}
				table.render({
					id: "processingCirculationInfo",
					elem: '#processingCirculationInfo',
					data: formData.current.productList,
					cols: [
						[{
							field: 'processItem',
							title: '执行项',
							edit: 'text',
							event: 'edit',
							align: 'center'
						}, {
							field: 'processTime',
							title: '开始时间',
							edit: 'time',
							align: 'center',
							event: 'edit'
						}, {
							field: 'processUser',
							title: '执行人',
							edit: 'text',
							align: 'center',
							event: 'edit'
						}, {
							field: 'processDesc',
							title: '执行描述',
							edit: 'text',
							align: 'center',
							event: 'edit'
						}, {
							field: 'processImgs',
							title: '上传过程照片',
							toolbar: ("view" !== location.searchObj().event) ? `<div>
								<a class="layui-btn layui-btn-xs" lay-event="upload">上传</a>
								<a target="_blank" href="{{window.hywa.config.href}}/{{d.processImgs || ''}}" lay-event="aHref">{{d.processImgs || ''}}</a>
							</div>` : `<div>
								<a target="_blank" href="{{window.hywa.config.href}}/{{d.processImgs || ''}}" lay-event="aHref">{{d.processImgs || ''}}</a>
							</div>`,
							align: 'center'
						}, {
							title: '操作',
							toolbar: ("view" !== location.searchObj().event) ? `<div>
								<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
							</div>` : `<div>&nbsp;</div>`,
							align: 'center'
						}]
					],
					page: true
				});
				//监听工具条
				table.on('tool(processingCirculationInfo)', function (obj) {
					var data = obj.data;
					if (obj.event === 'detail') {
						layer.msg('ID：' + data.id + ' 的查看操作');
					} else if (obj.event === 'del') {
						layer.confirm('真的删除行么', function (index) {
							obj.del();
							formData.current.productList.map(function (item, index) {
								item.index == obj.data.index && formData.current.productList.splice(index, 1);
							});
							if (formData.guid) {
								$.ajax({
									url: 'del_processingCirculationInfo',
									data: {
										guid: data.guid
									},
									success: function (res) {

									}
								});
							}
							layer.close(index);
						});
					} else if (obj.event === 'upload') {
						let upelem = document.createElement('input'),
							uploadMsg = 0;
						upload.render({
							elem: upelem, //绑定元素
							url: 'uploadImage',
							accept: "file",
							exts: "jpg|jpeg|png|gif|bmp|mp4|ogg|webm|avi|wma|rmvb|rm|flash|3gp|flv|mp3",
							size: 10240,
							before: function () {
								uploadMsg = layer.msg('正在上传中……', {
									icon: 16,
									fixed: false,
									time: 0
								});
							},
							done: function (res) {
								layer.close(uploadMsg);
								if (!(res.code - 0)) {
									let a = obj.tr[0].querySelector('a[lay-event="aHref"]');
									a.href = window.hywa.config.href + '/' + res.data[0];
									a.innerHTML = res.data[0];
									let ggObj = {};
									formData.current.productList.map(function (item, index) {
										if (item.index == obj.data.index) {
											formData.current.productList[index].processImgs = res.data[0];
											ggObj = formData.current.productList[index];
										}
									});
									if (formData.guid) {
										formData.submitInfo('edit_processingCirculationInfo', ggObj, function () {
											formData.getCurrent();
										});
									}
								}
							},
							error: function () {

							}
						});
						upelem.click();
					}
				});
				table.on('edit(processingCirculationInfo)', function (obj) {
					//修改后响应
					let ggObj = {};
					formData.current.productList.map(function (item, index) {
						if (item.index == obj.data.index) {
							formData.current.productList[index] = obj.data;
							ggObj = formData.current.productList[index];
						}
					});
					if (formData.guid) {
						formData.submitInfo('edit_processingCirculationInfo', ggObj, function () {
							formData.getCurrent();
						});
					}
				});
				//监听新增
				$('#processingCirculationInfoAdd').on('click', function () {
					let input = $("#processingCirculationInfoForm")[0].querySelectorAll('input');
					let obj = {};
					let ver = [];
					layui.each(input, function (i, item) {
						if (item.name) {
							obj[item.name] = item.value;
							ver.push(item.value);
						}
					});
					for (let i = 0; i < ver.length; i++) {
						if (!ver[i]) {
							layer.msg("全部都需要填写噢", {
								icon: 2
							});
							return false;
						}
					}
					if (formData.guid) {
						obj.fkGoodsBatchGuid = formData.guid;
						formData.submitInfo('add_processingCirculationInfo', obj, function () {
							formData.getCurrent();
						});
					}
					formData.current.productList.push(obj);
					//编号
					for (let i in formData.current.productList) {
						formData.current.productList[i].index = i;
					}
					table.reload("processingCirculationInfo", {
						data: formData.current.productList
					});
				});
				//初始化
				formData.main = function () {
					var _this = this;
					$("#goodsName").html(location.searchObj().pdName);
					this.current.fkGoodsGuid = location.searchObj().pid;
					this.current.goodsName = location.searchObj().pdName;
					$.ajax({
						url: "regionList",
						method: 'post',
						contentType: "application/json",
						data: JSON.stringify({
							page: 1,
							limit: 100
						}),
						success: function (res) {
							typeTpl({
								elem: "#rolesView",
								list: res.data
							});
							form.val(_this.formLayFilter, _this.current);
						}
					});
					if (this.guid) {
						_this.getCurrent(_this.guid); //获取并初始化表单角色信息
					} else {
						_this.initialization();
						imageTpl({
							elem: "#imageListDiv",
							list: _this.current.imagesList
						});
						assayImageTpl({
							elem: "#assayImageListDiv",
							list: _this.current.assayInfoForm.imagesList
						});
					}
					if (location.searchObj().event == "view") {
						var el = document.getElementsByTagName("INPUT");
						for (var i = 0; i < el.length; i++) {
							el[i].readOnly = true;
						}
						el = document.getElementsByTagName("SELECT");
						for (var i = 0; i < el.length; i++) {
							el[i].disabled = true;
						}
						el = document.getElementsByTagName("checkbox");
						for (var i = 0; i < el.length; i++) {
							el[i].disabled = true;
						}
						el = document.getElementsByTagName("button");
						for (var i = 0; i < el.length; i++) {
							if (el[i].innerText.replace(" ", "") == "保存") {
								el[i].style.display = "none";
							}
						}
					}
					return this;
				};

				//初始化
				formData.initialization = function () {
					//标签初始化
					let labelList = formData.current.labelList;
					let productTags = []
					if (labelList) {
						for (let i = 0; i < labelList.length; i++) {
							productTags.push(labelList[i].labelName)
						}
					}
					var _this = this;
					//监听提交
					form.on('submit(formDemo)', function (obj) {
						//必须return false;
						return _this.fusionData(obj.field).submitInfo(); //获取选中权限变数组,提交变化===下拉
					});
					//关闭窗口
					$(".close-win").on("click", function () {
						_this.closeModal();
					});
					$(".my-button-product").on("click", function () {
						_this.current.auxList.push({
							i: _this.current.auxList.length + 1,
							auxItme: "",
							auxItmeVal: ""
						}); //数据库里把item写成itme
						for (var i = _this.current.auxList.length - 1; i >= 0; i--) {
							if ($("input[name='auxItme']")[i]) {
								_this.current.auxList[i].auxItme = $("input[name='auxItme']")[i].value;
								_this.current.auxList[i].auxItmeVal = $("input[name='auxItmeVal']")[i].value;
							}
						}
						auxTpl({
							elem: "#auxListDiv",
							list: _this.current.auxList
						});
					});
					$(".my-button-assay").on("click", function () {
						_this.current.assayInfoForm.auxList.push({
							i: _this.current.assayInfoForm.auxList.length + 1,
							auxItme: "",
							auxItmeVal: ""
						}); //数据库里把item写成itme
						for (var i = _this.current.assayInfoForm.auxList.length - 1; i >= 0; i--) {
							if ($("input[name='auxItme2']")[i]) {
								_this.current.assayInfoForm.auxList[i].auxItme = $("input[name='auxItme2']")[i].value;
								_this.current.assayInfoForm.auxList[i].auxItmeVal = $("input[name='auxItmeVal2']")[i].value;
							}
						}
						assayTpl({
							elem: "#assayAuxListDiv",
							list: _this.current.assayInfoForm.auxList
						});
					});
					inputTags.render({
						elem: '#inputTags',//定义输入框input对象
						content: productTags,//默认标签
						aldaBtn: true,//是否开启获取所有数据的按钮
						done: function (value) { //回车后的回调
							formData.current.labelNames = value.toString();
						},
						del: function (value) {
							formData.current.labelNames = value.toString();
						}
					})
					return _this;
				};
				//获取当前信息
				formData.getCurrent = function (id) {
					var _this = this;
					$.ajax({
						url: this.select_data,
						method: 'get',
						data: {
							guid: id || this.guid //当前ID
						},
						success: function (res) {
							try {
								res.data[0].productDateStr = res.data[0].goodsProductDate.substring(0, 10);
								if (!res.data[0].auxList)
									res.data[0].auxList = [];
								if (!res.data[0].imagesList)
									res.data[0].imagesList = [];
								if (!res.data[0].assayInfoForm)
									res.data[0].assayInfoForm = {};
								if (!res.data[0].assayInfoForm.auxList)
									res.data[0].assayInfoForm.auxList = [];
								if (!res.data[0].assayInfoForm.imagesList)
									res.data[0].assayInfoForm.imagesList = [];
								res.data[0].assayDepartment = res.data[0].assayInfoForm.assayDepartment;
							} catch (e) {
								//TODO handle the exception
							}

							$("#goodsName").html(res.data[0].goodsName);
							_this.current = res.data[0];
							auxTpl({
								elem: "#auxListDiv",
								list: res.data[0].auxList
							});
							imageTpl({
								elem: "#imageListDiv",
								list: res.data[0].imagesList
							});
							assayTpl({
								elem: "#assayAuxListDiv",
								list: res.data[0].assayInfoForm.auxList
							});
							assayImageTpl({
								elem: "#assayImageListDiv",
								list: res.data[0].assayInfoForm.imagesList
							});
							_this.current.inputsFormList = _this.current.inputsFormList || [];
							rawMaterial(_this.current.inputsFormList);
							//生产过程信息
							_this.current.materialList = formData.current.materialList || [];
							//编号
							for (let i in _this.current.materialList) {
								_this.current.materialList[i].index = i;
							}
							table.reload("produceProcessInfo", {
								data: _this.current.materialList
							});
							//加工流通信息
							_this.current.productList = formData.current.productList || [];
							//编号
							for (let i in _this.current.productList) {
								_this.current.productList[i].index = i;
							}
							table.reload("processingCirculationInfo", {
								data: _this.current.productList
							});
							_this.initialization().formVal();
						}
					});
					return _this;
				};
				//表单赋值
				formData.formVal = function () {
					var _this = this;
					form.val(this.formLayFilter, this.current);
					return this;
				};
				//合并表单内容
				formData.fusionData = function (obj) {
					for (var i = this.current.auxList.length - 1; i >= 0; i--) {
						this.current.auxList[i].auxItme = $("input[name='auxItme']")[i].value;
						this.current.auxList[i].auxItmeVal = $("input[name='auxItmeVal']")[i].value;
						if (this.current.auxList[i].auxItme == "" && this.current.auxList[i].auxItmeVal == "")
							this.current.auxList.splice(i, 1);
					}
					for (var i = this.current.assayInfoForm.auxList.length - 1; i >= 0; i--) {
						this.current.assayInfoForm.auxList[i].auxItme = $("input[name='auxItme2']")[i].value;
						this.current.assayInfoForm.auxList[i].auxItmeVal = $("input[name='auxItmeVal2']")[i].value;
						if (this.current.assayInfoForm.auxList[i].auxItme == "" && this.current.assayInfoForm.auxList[i].auxItmeVal ==
							"")
							this.current.assayInfoForm.auxList.splice(i, 1);
					}
					this.current = $.extend(this.current, obj);
					this.current.goodsBatchImages = this.current.imagesList.join(",");
					this.current.assayInfoForm.assayImgs = this.current.assayInfoForm.imagesList.join(",");
					this.current.assayInfoForm.assayDepartment = this.current.assayDepartment;
					return this;
				};
				//提交新增修改
				formData.submitInfo = function (url, data, fun) {
					var _this = this;
					$.ajax({
						type: 'POST',
						url: url || this.save_data, //+'?userId='+this.guid
						data: JSON.stringify(data || this.current),
						contentType: 'application/json',
						success: function (res) {

							if (!(res.code - 0)) {
								parent.layer.msg(res.msg, {
									icon: 1
								});
								typeof fun === 'function' && fun();
								url || _this.closeModal();
							} else {
								layer.msg(res.msg, {
									icon: 2
								});
							}
						}
					});
					return false;
				};
				formData.main();
				//验证规则
				form.verify({
					nickname: function (value) {
						if (!new RegExp("^[a-zA-Z][a-zA-Z0-9_]*$").test(value)) {
							return '账号必须以英文字母开头，只能包含英文字母、数字、下划线';
						}
						if (/(^\_)|(\__)|(\_+$)/.test(value)) {
							return '账号首尾不能出现下划线\'_\'';
						}
					},
					checkName: function (value) { },
					phoneNumber: function (value) {
						if (value) {
							if (!(/^1[3|4|5|7|8]\d{9}$/).test(value)) {
								return '请输入正确的手机号码';
							}
						}
						return false;
					},
					password: function (value) {
						if (location.searchObj().guid) {
							if (value) {
								if (!(/^[\S]{6,12}$/).test(value)) {
									return '密码必须6到12位，且不能出现空格';
								}
							}
							return false;
						}
						if (!(/^[\S]{6,12}$/).test(value)) {
							return '密码必须6到12位，且不能出现空格';
						}
					},
					resspaword: function (value) {
						var pass = $('#L_pass').val();
						if (value != pass) {
							return '两次密码不一致';
						}
					},
					ageLength: [
						/^[0-9]{0,2}$/,
						'年龄必须0到2位，且只能是数字'
					],
					jobLength: [
						/^[a-z\A-Z\u4e00-\u9fa5_]{0,10}$/,
						'职务必须是0-10位,且不包含特殊字符与数字'
					],
					workUnit: [
						/^[0-9\a-z\A-Z\u4e00-\u9fa5_]{0,25}$/,
						'工作单位必须是0-25位,且不包含特殊字符'
					],
					vcode: [
						/^[\w]{4,5}$/,
						'验证码不符合规则'
					]
				});

				form.on('select(roles)', function (data) {
					rolesViewOnemapParam = $("#rolesView option[value='" + data.value + "']").attr('oneMap');
				});

				/*element.on('tab(docDemoTabBrief)', function (data) {
					if ('2' == this.getAttribute('lay-id')) {
						if (rolesViewOnemapParam && null != rolesViewOnemapParam) {
							let req = {
								orchardId: rolesViewOnemapParam,
								pageNo: 1,
								pageSize: 10
							};
							$.ajax({
								type: 'get',
								url: 'getOrchardList' + '?orchardId=' + rolesViewOnemapParam + '&pageNo=1&pageSize=10',
								success: function (res) {
									formData.current.materialList = res.data;
									formData.current.materialList.forEach(value => {
										value.processType = 1;
									});
									table.reload("produceProcessInfo", {
										data: formData.current.materialList
									});
								}
							});
						}
					}
				});*/

			});
		</script>
	</body>

</html>