<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header(#{system.role.add.title})" />
	<th:block th:include="include :: ztree-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
	<form class="form-horizontal m" id="form-role-add">
		<div class="form-group">
			<label class="col-sm-3 control-label is-required" th:text="#{system.role.add.roleName}"></label>
			<div class="col-sm-8">
				<input class="form-control" type="text" name="roleName" id="roleName" required>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-3 control-label is-required" th:text="#{system.role.add.roleKey}"></label>
			<div class="col-sm-8">
				<input class="form-control" type="text" name="roleKey" id="roleKey" required>
				<span class="help-block m-b-none"><i class="fa fa-info-circle"></i> <span th:text="#{system.role.add.roleKey.help}"></span></span>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-3 control-label is-required" th:text="#{system.role.add.roleSort}"></label>
			<div class="col-sm-8">
				<input class="form-control" type="text" name="roleSort" id="roleSort" required>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-3 control-label" th:text="#{system.role.add.status}"></label>
			<div class="col-sm-8">
				<label class="toggle-switch switch-solid">
					<input type="checkbox" id="status" checked>
					<span></span>
				</label>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-3 control-label" th:text="#{system.role.add.remark}"></label>
			<div class="col-sm-8">
				<input id="remark" name="remark" class="form-control" type="text">
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-3 control-label" th:text="#{system.role.add.menuPermission}"></label>
			<div class="col-sm-8">
				<label class="check-box">
					<input type="checkbox" value="1"><span th:text="#{system.role.add.expandCollapse}"></span></label>
				<label class="check-box">
					<input type="checkbox" value="2"><span th:text="#{system.role.add.selectAll}"></span></label>
				<label class="check-box">
					<input type="checkbox" value="3" checked><span th:text="#{system.role.add.parentChildLink}"></span></label>
				<div id="menuTrees" class="ztree ztree-border"></div>
			</div>
		</div>
	</form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: ztree-js" />
<script type="text/javascript">
	$(function() {
		var url = ctx + "system/menu/roleMenuTreeData";
		var options = {
			id: "menuTrees",
			url: url,
			check: { enable: true },
			expandLevel: 0
		};
		$.tree.init(options);
	});

	$("#form-role-add").validate({
		rules:{
			onkeyup: false,
			roleName:{
				remote: {
					url: ctx + "system/role/checkRoleNameUnique",
					type: "post",
					dataType: "json",
					data: {
						"roleName" : function() {
							return $.common.trim($("#roleName").val());
						}
					}
				}
			},
			roleKey:{
				remote: {
					url: ctx + "system/role/checkRoleKeyUnique",
					type: "post",
					dataType: "json",
					data: {
						"roleKey" : function() {
							return $.common.trim($("#roleKey").val());
						}
					}
				}
			},
			roleSort:{
				digits:true
			},
		},
		messages: {
			"roleName": {
				remote: i18n("system.role.add.validate.roleNameExists")
			},
			"roleKey": {
				remote: i18n("system.role.add.validate.roleKeyExists")
			}
		},
		focusCleanup: true
	});

	$('input').on('ifChanged', function(obj){
		var type = $(this).val();
		var checked = obj.currentTarget.checked;
		if (type == 1) {
			if (checked) {
				$._tree.expandAll(true);
			} else {
				$._tree.expandAll(false);
			}
		} else if (type == "2") {
			if (checked) {
				$._tree.checkAllNodes(true);
			} else {
				$._tree.checkAllNodes(false);
			}
		} else if (type == "3") {
			if (checked) {
				$._tree.setting.check.chkboxType = { "Y": "ps", "N": "ps" };
			} else {
				$._tree.setting.check.chkboxType = { "Y": "", "N": "" };
			}
		}
	})

	function submitHandler() {
		if ($.validate.form()) {
			add();
		}
	}

	function add() {
		var roleName = $("input[name='roleName']").val();
		var roleKey = $("input[name='roleKey']").val();
		var roleSort = $("input[name='roleSort']").val();
		var status = $("input[id='status']").is(':checked') == true ? 0 : 1;
		var remark = $("input[name='remark']").val();
		var menuIds = $.tree.getCheckedNodes();
		$.ajax({
			cache : true,
			type : "POST",
			url : ctx + "system/role/add",
			data : {
				"roleName": roleName,
				"roleKey": roleKey,
				"roleSort": roleSort,
				"status": status,
				"remark": remark,
				"menuIds": menuIds
			},
			async : false,
			error : function(request) {
				$.modal.alertError(i18n("system.error"));
			},
			success : function(data) {
				$.operate.successCallback(data);
			}
		});
	}
</script>
</body>
</html>