<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<script src="../../../admin/index.js" door type="text/javascript" charset="utf-8"></script>
		<!--下拉选择模板-->
		<script type="text/html" id="selectTpl">
			<option value="">{{d.name}}</option>
		    {{# if(!d.list){d.list = []}}} {{# for(var i=0;i<d.list.length;i++){}} 
				<option value="{{d.list[i].id}}">{{d.list[i].name}}</option>
			{{#}}}
		</script>
	</head>

	<body>
		<form class="layui-form" action="" lay-filter="addForm" style="padding: 20px 30px 0 0">
			<div class="layui-form-item">
				<label class="layui-form-label ui-required">姓名</label>
				<div class="layui-input-block">
					<input type="text" name="name" lay-verify="required|nickname" maxlength="10" placeholder="请输入姓名" 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="phone" lay-verify="required|phone" maxlength="11" placeholder="请输入手机号" 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="password" id="L_pass" name="pwd" lay-verify="password" maxlength="12" autocomplete="off" placeholder="请输入密码" 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="password" lay-verify="resspaword" autocomplete="off" maxlength="12" placeholder="请再次输入密码" class="layui-input">
				</div>
			</div>
			<div class="layui-form-item layui-hide">
				<button class="layui-btn" lay-submit lay-filter="formSubmit" id="formSubmit">保存</button>
			</div>
		</form>
		<script type="text/javascript">
			layui.config({
				base: "../../../admin/js/layuiPlugins/"
			}).use(["form", 'layer', 'laytpl', 'layedit', ], function() {
				var form = layui.form,
					$ = layui.$,
					layer = layui.layer,
					laytpl = layui.laytpl,
					layedit = layui.layedit;
				var object = {
					id: (JSON.stringify(location.searchObj()) != "{}") ? location.searchObj().id || null : null, //带参传入ID
					currentData: {},
					urls: {
						formUrl: "riderAddFormUrl"
					},
					formLayFilter: "addForm", //表单容器
					closeModal: function() { //关闭当前窗口
						parent.layer.close(parent.layer.getFrameIndex(window.name));
					}
				};

				//初始化
				object.main = function() {
					var _this = this;
					if (_this.id) {
						_this.getCurrent(_this.id);
					} else {
						_this.initialization();
					}

					form.on('submit(formSubmit)', function(obj) {
						return _this.mergeData(obj.field).submitInfo();
					});
					return _this;
				};

				//初始化
				object.initialization = function() {
					var _this = this;
					return _this;
				};
				//合并表单内容
				object.mergeData = function(obj) {
					this.currentData = $.extend(this.currentData, obj);
					if (this.currentData.id) {
						delete this.currentData.password;
					}
					return this;
				};
				//获取当前信息
				object.getCurrent = function(id) {
					var _this = this;
					_this.currentData = {
						id: _this.id,
						name: location.searchObj().name || '',
						phone: location.searchObj().phone || ''
					}
					_this.formVal();
					return this;
				};
				//表单赋值
				object.formVal = function() {
					var _this = this;
					form.val(_this.formLayFilter, _this.currentData);
					return _this;
				};
				//提交新增修改
				object.submitInfo = function() {
					var _this = this;
					$.ajax({
						type: _this.id ? 'PUT' : 'POST',
						url: _this.urls.formUrl,
						data: JSON.stringify(_this.currentData),
						contentType: 'application/json',
						success: function(res) {
							if (res.code == 0) {
								parent.layer.msg(res.msg, {
									icon: 1
								});
								parent.layui.table.reload('tableData');
								_this.closeModal();
							} else {
								layer.msg(res.msg, {
									icon: 2
								});
							}
						}
					});
					return false;
				};
				object.main();

				//验证规则
				form.verify({
					nickname: function(value) {
						if (/(^\_)|(\__)|(\_+$)/.test(value)) {
							return '用户名首尾不能出现下划线\'_\'';
						}
					},
					password: function(value) {
						if (location.searchObj().id) {
							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 '两次密码不一致';
						}
					},
				});
			});
		</script>
	</body>

</html>
