<!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="../../forstcss/common.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-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 {
			position: relative;
			left: -2px;
			top: -106px;
		}
	</style>
	<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}}">{{item.fieldName}}</option>
		{{# }); }}
	</script>
</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">基础信息</li>
				<li>自定义信息</li>
				<li>图片视频</li>
			</ul>
			<div class="layui-tab-content">
				<input type="hidden" id="oneMap" value="">
				<div class="layui-tab-item layui-show">
					<div class="layui-form-item" style="display:none;">
						<label class="layui-form-label ui-required">产地名称</label>
						<div class="layui-input-block">
							<select name="guid" class="layui-input search-input" 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="fieldName" maxlength="32" lay-verify="required" 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="saleGoodsRange" maxlength="32" lay-verify="required" 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="fieldArea" maxlength="50" lay-verify="required|length50" placeholder="请输入产地面积"
								autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label ui-required">产地地址</label>
						<div id="address" class="layui-input-block">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">详细地址</label>
						<div class="layui-input-block">
							<input type="text" name="fieldAddress" maxlength="100" 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">
							<textarea name="remark" maxlength="1000" placeholder="请输入产地简介" autocomplete="off"
								class="layui-textarea" rows="4"></textarea>
						</div>
					</div>
					<div class="layui-form-item layui-layer-btn">
						<div class="layui-input-block">
							<button type="reset" class="layui-btn layui-btn-primary close-win">取消</button>
							<button class="layui-btn" lay-submit lay-filter="formDemo">保存</button>
						</div>
					</div>
				</div>
				<div class="layui-tab-item">
					<div id="auxListDiv"></div>
					<div class="layui-form-item">
						<div class="my-button">+添加</div>
					</div>
					<div class="layui-form-item layui-layer-btn">
						<div class="layui-input-block">
							<button type="reset" class="layui-btn layui-btn-primary close-win">取消</button>
							<button class="layui-btn" lay-submit lay-filter="formDemo">保存</button>
						</div>
					</div>
				</div>
				<div class="layui-tab-item">
					<div id="imageListDiv"></div>
					图片视频上传完毕后请记得按保存哦
					<div class="layui-form-item layui-layer-btn">
						<div class="layui-input-block">
							<button type="reset" class="layui-btn layui-btn-primary close-win">取消</button>
							<button class="layui-btn" lay-submit lay-filter="formDemo">保存</button>
						</div>
					</div>
				</div>
			</div>
		</div>
	</form>
	<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/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', 'cascade', 'element', 'upload'], function () {
			var form = layui.form,
				layer = layui.layer,
				laytpl = layui.laytpl,
				cascade = layui.cascade,
				$ = layui.jquery,
				element = layui.element,
				upload = layui.upload;
			var funcName = "region";
			var formData = {
				originList: [],
				guid: (JSON.stringify(location.searchObj()) != "{}") ? location.searchObj().guid || null : null, //带参传入ID
				current: { auxList: [], imagesList: [] }, //当前表单数据
				select_data: "select_" + funcName, //查询
				select_regionNameList: 'rnlist_' + 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|mp3",
				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 () {
				}
			};
			//初始化
			formData.main = function () {
				var _this = this;
				if (this.guid) {
					_this.getCurrent(_this.guid); //获取并初始化表单角色信息
				} else {
					_this.initialization();
					imageTpl({
						elem: "#imageListDiv",
						list: _this.current.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;
			};

			//产地名称选择
			form.on('select(roles)', function (data) {
				var _this = this;
				var guid = data.value;
				if (guid !== "") {
					let arr = formData.originList;
					if (arr.length != 0) {
						let result = arr.filter(item => {
							return item['guid'] == data.value
						})
						formData.current['fieldName'] = result[0]['fieldName'];
						formData.current['saleGoodsRange'] = result[0]['saleGoodsRange'];
						formData.current['fieldArea'] = result[0]['fieldArea'];
						formData.current['fieldAddress'] = result[0]['fieldAddress'];
						formData.current['remark'] = result[0]['remark'];
						formData.current['guid'] = result[0]['guid'];
						formData.current['oneMap'] = result[0]['oneMap'];
						console.info(result[0]['oneMap']);
						form.val(formData.formLayFilter, formData.current);
					}
				} else {
					formData.current['fieldName'] = '';
					formData.current['saleGoodsRange'] = '';
					formData.current['fieldArea'] = '';
					formData.current['fieldAddress'] = '';
					formData.current['remark'] = '';
					formData.current['guid'] = '';
					formData.current['oneMap'] = '';
					form.val(formData.formLayFilter, formData.current);
				}
			})

			//类型下拉列表模板
			function SelectTpl(obj) {
				laytpl(typeHtmlTpl.innerHTML).render(obj, function (html) {
					$(obj.elem).html(html);
					form.render();
				});
			}

			//初始化
			formData.initialization = function () {
				var _this = this;

				//获取产地名称列表
				$.ajax({
					url: this.select_regionNameList,
					method: 'get',
					data: {
						customerId: '229'
					},
					success: function (res) {
						formData.originList = res.data
						SelectTpl({
							elem: "#rolesView",
							list: res.data
						});
						form.val(_this.formLayFilter, _this.current);
					}
				});

				//初始化地区
				cascade.render({
					elem: "#address",
					hostParm: {
						idName: 'parentId'
					},
					url: {
						provinceId: 'areaSearch',
						cityId: 'areaSearch',
						countyId: 'areaSearch'
					},
					listParamName: {
						valueName: 'code',
						name: 'name'
					},
					width: {
						provinceId: '8em',
						cityId: '8em',
						countyId: '8em',
						townId: '8em'
					}
				}).val({
					provinceId: this.current.provinceId,
					cityId: this.current.cityId,
					countyId: this.current.countyId
				});

				//监听提交
				form.on('submit(formDemo)', function (obj) {
					//必须return false;
					return _this.fusionData(obj.field).submitInfo(); //获取选中权限变数组,提交变化===下拉
				});

				//关闭窗口
				$(".close-win").on("click", function () {
					_this.closeModal();
				});

				$(".my-button").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;
						}
					}
					typeTpl({
						elem: "#auxListDiv",
						list: _this.current.auxList
					});
				});
				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) {
						if (!res.data[0].auxList)
							res.data[0].auxList = [];
						if (!res.data[0].imagesList)
							res.data[0].imagesList = [];
						_this.current = res.data[0];

						typeTpl({
							elem: "#auxListDiv",
							list: res.data[0].auxList
						});
						imageTpl({
							elem: "#imageListDiv",
							list: res.data[0].imagesList
						});

						//禁止编辑基础信息
						if (formData.save_data === 'edit_region') {
							$("#rolesView").attr("disabled", 'disabled')
						}
						_this.initialization().formVal();
					}
				});
				return _this;
			};
			//表单赋值
			formData.formVal = function () {
				var _this = this;
				form.val(this.formLayFilter, this.current);
				return this;
			};
			//合并表单内容
			formData.fusionData = function (obj) {
				var _this = this;
				try {
					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);
					}
					_this.current.fieldImages = _this.current.imagesList.join(",");
					this.current = $.extend(this.current, obj);
				} catch (e) {
					console.log(e)
				}

				return this;
			};
			//提交新增修改
			formData.submitInfo = function () {
				var _this = this;
				if (!this.current.countyId || this.current.countyId == "") {
					layer.msg("必须选择省市县地址");
					return false;
				}
				this.current.fkPostionId = this.current.countyId;
				if ("edit_region" === this.save_data) {
					this.current.guid = _this.guid;
				}
				$.ajax({
					type: 'POST',
					url: this.save_data,//+'?userId='+this.guid
					data: JSON.stringify(this.current),
					contentType: 'application/json',
					success: function (res) {
						if (!(res.code - 0)) {
							parent.layer.msg(res.msg, {
								icon: 1
							});
							_this.closeModal();
						} else {
							layer.msg(res.msg, {
								icon: 2
							});
						}
					}
				});
				return false;
			};
			//自定义项列表模板
			function typeTpl(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);
						typeTpl({
							elem: "#auxListDiv",
							list: formData.current.auxList
						});
					});
				});
			}

			//图片列表模板
			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
						});
					});
				});
			}

			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}$/,
					'验证码不符合规则'
				],
				length50: [
					/^.{0,50}$/,
					"长度必须0-50个字符"
				]
			});
		});
	</script>
</body>

</html>