<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
	<th:block th:include="include :: header('修改角色')" />
	<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="${user}">
			<input id="userId" name="userId" type="hidden" th:field="*{userId}"/>

			<div class="form-group">
				<label class="col-sm-3 control-label">菜单权限</label>
				<div class="col-sm-8">
					<input type="checkbox" id="highTuc" th:value="${authorInfo.authorId}"><label for="highTuc">TUC事业群</label>
					<div id="menuTrees" class="ztree"></div>
				</div>
			</div>
		</form>
	</div>
	<th:block th:include="include :: footer" />
	<th:block th:include="include :: ztree-js" />
	<script th:inline="javascript">

		var tucCheck = [[${tucCheck}]];
		$("#highTuc").prop('checked',tucCheck);

	     $(function() {
			var url = ctx + "system/author/tree/" + $("#userId").val();
			var options = {
				id: "menuTrees",
		        url: url,
		        check: { enable: true},
		        expandLevel: 0,
				onCheck:function (event,treeId,treeNode) {
					// console.log(treeNode);
					// console.log(treeNode.getParentNode());
				}
		    };
			$.tree.init(options);
		});
	
		$("#form-role-edit").validate({
			onkeyup: false,
			rules:{
				roleName:{
					remote: {
		                url: ctx + "system/role/checkRoleNameUnique",
		                type: "post",
		                dataType: "json",
		                data: {
							"roleId": function() {
							    return $("#roleId").val();
							},
							"roleName": function() {
							    return $.common.trim($("#roleName").val());
							}
		                },
		                dataFilter: function(data, type) {
		                	return $.validate.unique(data);
		                }
		            }
				},
				roleKey:{
					remote: {
		                url: ctx + "system/role/checkRoleKeyUnique",
		                type: "post",
		                dataType: "json",
		                data: {
							"roleId": function() {
								return $("#roleId").val();
							},
							"roleKey": function() {
							    return $.common.trim($("#roleKey").val());
							}
		                },
		                dataFilter: function(data, type) {
		                	return $.validate.unique(data);
		                }
		            }
				},
				roleSort:{
					digits:true
				},
			},
			messages: {
		        "roleName": {
		            remote: "角色名称已经存在"
		        },
		        "roleKey": {
		            remote: "角色权限已经存在"
		        }
		    },
		    focusCleanup: true
		});

		function edit() {
			var userId = $("input[name='userId']").val();
			// console.log($.tree)
			// console.log($.tree.getNodes()[0].getCheckStatus())
			var authorIds='';
			if($("#highTuc").is(":checked")){
				authorIds=$('#highTuc:checked').val()+","
			}
			authorIds += $.tree.getCheckedNodesRole();
			if($.tree.getCheckedNodesRole()==''){
				$.modal.alertWarning("至少勾选一个事业部")
				return
			}
			$.ajax({
				cache : true,
				type : "POST",
				url : ctx + "system/author/tree/edit",
				data : {
					"userId": userId,
					"authorIds": authorIds
				},
				async : false,
				error : function(request) {
					$.modal.alertError("系统错误");
				},
				success : function(data) {
					$.operate.successCallback(data);
				}
			});
		}
		
		function submitHandler() {
	        if ($.validate.form()) {
	        	edit();
	        }
	    }
	</script>
</body>
</html>
