<!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>
	<style>
		.ui-required:before {
			content: "*";
			color: red;
			vertical-align: middle;
		}
	</style>
</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" required lay-verify="required|namelength|ajaxRoleName" 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 type="text" name="description" maxlength="80" lay-verify="required|descriptionlength" autocomplete="off" class="layui-textarea"></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" 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 role = {
				id: (JSON.stringify(location.searchObj()) != "{}") ? location.searchObj().id || null : null, //带参传入ID
				currentRole: {}, //当前角色对象
				roleUrl: "roleByid", //角色信息接口
				areaPort: {//地区，参数名:接口
					provinceId: 'getProvince',
					cityId: 'getCity',
					countyId: 'getCounty'
				},
				roleChange: "roleFormUrl", //新增修改接口
				competence: "get_roleMenu", //权限接口
				formLayFilter: "addForm", //表单名
				menuTree: "menus", //菜单树容器
				roleTreeObj: null, //权限树对象
				menuTreeTable: "#roleDataList", //权限表格容器
				menuTableDataId: "roleDataList", //表格数据ID
				limit: 10000, //初始条数
				roleDataList: [ //标题栏
					{
						type: 'checkbox'
					},
					{
						field: 'id',
						title: 'ID',
						align: 'center',
						event: 'list_li',
						sort: true //排序
					}, {
						field: 'name',
						title: '名称',
						align: 'center',
						event: 'list_li',
						sort: true //排序
					}, {
						field: 'description',
						title: '描述',
						event: 'list_li',
						align: 'center'
					}
				], //设置表头
				closeModal: function () { //关闭当前窗口
					parent.layer.close(parent.layer.getFrameIndex(window.name));
				}
			};
			//初始化
			role.main = function () {
				var _this = this;
				if (this.id) {
					_this.getCurrent(_this.id); //获取并初始化表单角色信息
				} else {

				}
				return this;
			};
			//获取当前信息
			role.getCurrent = function (id) {
				var _this = this;
				$.ajax({
					url: this.roleUrl,
					data: {
						id: id || this.id //当前角色ID
					},
					success: function (res) {
						
						_this.currentRole = res.data[0];
						_this.formVal();
					}
				});
				return this;
			};
			//表单赋值
			role.formVal = function () {
				form.val(this.formLayFilter, this.currentRole);
				return this;
			};
			//合并表单内容
			role.fusionData = function (obj) {
				this.currentRole = $.extend(this.currentRole, obj);
				return this;
			};
			//提交新增修改
			role.submitInfo = function () {
				var _this = this;
				this.currentRole.regionId = location.searchObj().regionId;
				this.currentRole.status = '';
				
				$.ajax({
					type: this.id ? 'PUT' : 'POST',
					url: this.roleChange + '?id=' + this.currentRole.id,
					data: JSON.stringify(this.currentRole),
					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 role.fusionData(obj.field).submitInfo();
			});
			//关闭窗口
			$("#closeWin").on("click", function () {
				role.closeModal();
			});
			role.main();
		});
	</script>
</body>

</html>