<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header(#{system.role.dataScope.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-edit" th:object="${role}">
		<input id="roleId" name="roleId" type="hidden" th:field="*{roleId}"/>
		<div class="form-group">
			<label class="col-sm-3 control-label" th:text="#{system.role.add.roleName}"></label>
			<div class="col-sm-8">
				<input class="form-control" type="text" name="roleName" id="roleName" th:field="*{roleName}" readonly="true"/>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-3 control-label" th:text="#{system.role.add.roleKey}"></label>
			<div class="col-sm-8">
				<input class="form-control" type="text" name="roleKey" id="roleKey" th:field="*{roleKey}" readonly="true">
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-3 control-label" th:text="#{system.role.dataScope.dataScope}"></label>
			<div class="col-sm-8">
				<select id="dataScope" name="dataScope" class="form-control m-b">
					<option value="1" th:field="*{dataScope}" th:text="#{system.role.dataScope.all}"></option>
					<option value="2" th:field="*{dataScope}" th:text="#{system.role.dataScope.custom}"></option>
					<option value="3" th:field="*{dataScope}" th:text="#{system.role.dataScope.dept}"></option>
					<option value="4" th:field="*{dataScope}" th:text="#{system.role.dataScope.deptAndChild}"></option>
					<option value="5" th:field="*{dataScope}" th:text="#{system.role.dataScope.self}"></option>
				</select>
				<span class="help-block m-b-none"><i class="fa fa-info-circle"></i> <span th:text="#{system.role.dataScope.help}"></span></span>
			</div>
		</div>
		<div class="form-group" id="authDataScope" th:style="'display:' + @{(*{dataScope=='2'} ? 'block' : 'none')} + ''">
			<label class="col-sm-3 control-label" th:text="#{system.role.dataScope.dataPermission}"></label>
			<div class="col-sm-8">
				<label class="check-box">
					<input type="checkbox" value="1" checked><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="deptTrees" 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/role/deptTreeData?roleId=" + $("#roleId").val();
		var options = {
			id: "deptTrees",
			url: url,
			check: { enable: true, nocheckInherit: true, chkboxType: { "Y": "ps", "N": "ps" } },
			expandLevel: 2
		};
		$.tree.init(options);
	});

	$('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()) {
			edit();
		}
	}

	function edit() {
		var roleId = $("input[name='roleId']").val();
		var roleName = $("input[name='roleName']").val();
		var roleKey = $("input[name='roleKey']").val();
		var dataScope = $("#dataScope").val();
		var deptIds = $.tree.getCheckedNodes();
		$.ajax({
			cache : true,
			type : "POST",
			url : ctx + "system/role/authDataScope",
			data : {
				"roleId": roleId,
				"roleName": roleName,
				"roleKey": roleKey,
				"dataScope": dataScope,
				"deptIds": deptIds
			},
			async : false,
			error : function(request) {
				$.modal.alertError(i18n("system.error"));
			},
			success : function(data) {
				$.operate.successCallback(data);
			}
		});
	}

	$("#dataScope").change(function(event){
		var dataScope = $(event.target).val();
		dataScopeVisible(dataScope);
	});

	function dataScopeVisible(dataScope) {
		if (dataScope == 2) {
			$("#authDataScope").show();
		} else {
			$._tree.checkAllNodes(false);
			$("#authDataScope").hide();
		}
	}
</script>
</body>
</html>