<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<title>添加角色</title>
	<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>
</head>

<body>
	<form class="layui-form" action="" style="padding: 20px 30px 0 0;">
		<div id="areaSelect"></div>
		<div class="layui-form-item layui-layer-btn">
			<div class="layui-input-block">
				<button type="reset" class="layui-btn layui-btn-primary" id="closeWin">取消</button>
				<button class="layui-btn" lay-submit lay-filter="formDemo">保存</button>
			</div>
		</div>
	</form>
	<script type="text/javascript">
		require('js/layuiPlugins/layui-xtree.js');
		layui.config({
			base: "../../../admin/js/layuiPlugins/"
		}).use(["form", "tree", "table", 'cascade'], function () {
			var form = layui.form,
				$ = layui.$,
				cascade = layui.cascade,
				table = layui.table;
			form.verify({
				namelength: [
					/^.{2,8}$/,
					"长度限制在2-8个字符噢"
				],
			});
			var region = {
				id: (JSON.stringify(location.searchObj()) != "{}") ? location.searchObj().id || null : null, //带参传入ID
				currentData: {}, //当前角色对象
				infoUrl: "get_roleInfo", //角色信息接口
				areaPort: {//地区，参数名:接口
					provinceId: 'getProvince',
					cityId: 'getCity',
					countyId: 'getCounty'
				},
				addChange: "regionFormUrl", //新增修改接口
				closeModal: function () { //关闭当前窗口
					parent.layer.close(parent.layer.getFrameIndex(window.name));
				}
			};
			//初始化
			region.main = function () {
				var _this = this;
				//初始化地区
				_this.areaCascade = cascade.render({
					title: "请选择",
					elem: '#areaSelect',
					url: _this.areaPort
				}).val({
					provinceId: location.searchObj().provinceId,
					cityId: location.searchObj().cityId,
					countyId: '-1000',
				});
				if (this.id) {
					_this.getCurrent(_this.id); //获取并初始化表单角色信息
				} else {

				}
				return this;
			};
			//获取当前信息
			region.getCurrent = function (id) {
				var _this = this;
				_this.currentData = location.searchObj();
				_this.formVal();
				$.ajax({
					url: this.infoUrl,
					data: {
						id: id || this.id //当前角色ID
					},
					success: function (res) {
						
						_this.currentData = res.data[0];
						_this.formVal();
					}
				});
				return this;
			};
			//表单赋值
			region.formVal = function () {
				form.val(this.formLayFilter, this.currentData);
				this.areaCascade.val(this.currentData);
				return this;
			};
			//合并表单内容
			region.fusionData = function (obj) {
				this.currentData = $.extend(this.currentData, obj);
				return this;
			};
			//提交新增修改
			region.submitInfo = function () {
				var _this = this;
				// if (this.currentData.provinceId && !this.currentData.cityId) {
				// 	this.currentData.regionId = this.currentData.provinceId
				// } else if (this.currentData.cityId && !this.currentData.countyId) {
				// 	this.currentData.regionId = this.currentData.cityId
				// } else if (this.currentData.countyId) {
				// 	this.currentData.regionId = this.currentData.countyId
				// }
				console.log("sdfsdafadasda", this.currentData);
				$.ajax({
					type: this.id ? 'PUT' : 'POST',
					url: this.addChange + '?id=' + this.currentData.id,
					data: JSON.stringify(this.currentData),
					contentType: 'application/json',
					success: function (res) {
						
						if (!(res.code - 0)) {
							parent.layer.msg(res.msg, {
								icon: 1
							});
							_this.closeModal();
						}
					}
				});
				return false;
			};
			//监听提交
			form.on('submit(formDemo)', function (obj) {
				//必须return false;
				return region.fusionData(obj.field).submitInfo();
			});
			//关闭窗口
			$("#closeWin").on("click", function () {
				region.closeModal();
			});
			region.main();
		});
	</script>
</body>

</html>