<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@ page isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
    String basePath = request.getContextPath();
%>
<jsp:include page="../../../common/admin/modal_content_pre.jsp"><jsp:param name="modal_title" value="新增"/></jsp:include>
<div class="alert alert-danger" role="alert">
    <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
    <span class="sr-only">注意:</span>
    子角色权限的最大范围为父级角色权限，子角色默认拥将有其父级角色所有权限，子角色可以在父角色的权限范围内进一步缩小权限范围。
</div>
<div id="divTree"></div>
<jsp:include page="../../../common/admin/modal_content_post.jsp"></jsp:include>
<jsp:include page="../../../common/admin/modal_footer_pre.jsp"></jsp:include>
<button type="button" class="btn btn-default" data-dismiss="modal"><i class="fa fa-close"></i>取消</button>
<button type="button" id="btnSave" class="btn btn-primary"><i class="fa fa-check"></i>保存</button>
<jsp:include page="../../../common/admin/modal_footer_post.jsp"></jsp:include>
<script type="text/javascript">
    $("#btnSave").click(function() {
        var checked = $('#divTree').treeview('getChecked');
        var selectedModuleIdArr = new Array();
        var selectedActionIdArr = new Array();
        for(var i = 0; i < checked.length; i++){
            if (checked[i].tags && $.inArray("功能", checked[i].tags) >= 0){
                selectedActionIdArr.push(checked[i].id);
            }else {
                selectedModuleIdArr.push(checked[i].id);
            }
        }
        var jsonData = {
            roleId: ${busiModel.id},
            selectedModuleIds: selectedModuleIdArr,
            selectedActionIds: selectedActionIdArr
        };
        $.ajax({
            type : "post",
            url : "<%=basePath%>/admin/role_mgr/auth.action",
            contentType: "application/json;charset=UTF-8",
            data: JSON.stringify(jsonData),
            success : function(resultState) {
                if (resultState.stateCode == 200) {
                    $.showTip({ msg: resultState.msg });
                    var jqContainer = $.getJqObj(wysharpDefaultModalContainerSelector);
                    jqContainer.modal('hide');
                } else if (resultState.stateCode == 500) {
                    $.showAlert({message: "系统错误"});
                } else {
                    // 显示错误信息
                    if (resultState.msg && resultState.msg != ""){
                        $.showAlert({message: resultState.msg});
                    }
                }
            },
            error : function() {
                $.showTip({ msg: "系统错误" });
            }
        });
    });

    function checkParentNodeRecursively(node){
        var container = $.getJqObj("#divTree");
        var parentNode = container.treeview('getParent', node);
        if (!parentNode || parentNode instanceof jQuery){
            return;
        }
        container.treeview('checkNode', parentNode);
        checkParentNodeRecursively(parentNode);
    }

    function getChildNodeRecursively(node) {
        var ts = [];
        if (node.nodes) {
            for (x in node.nodes) {
                ts.push(node.nodes[x].nodeId);
                if (node.nodes[x].nodes) {
                    var getNodeDieDai = getChildNodeRecursively(node.nodes[x]);
                    for (j in getNodeDieDai) {
                        ts.push(getNodeDieDai[j]);
                    }
                }
            }
        } else {
            ts.push(node.nodeId);
        }
        return ts;
    }

    function uncheckChildrenRecursively(node){
        var container = $.getJqObj("#divTree");
        var parentNode = container.treeview('getParent', node);
        if (!parentNode || parentNode instanceof jQuery){
            return;
        }
        container.treeview('checkNode', parentNode);
        uncheckChildrenRecursively(parentNode);
    }

    var defaultTreeViewOptions = {
        showCheckbox: true,
        showTags: true,
        onNodeChecked: function (event, node) {
            checkParentNodeRecursively(node);
        },
        onNodeUnchecked: function (event, node) {
            var childNodes = getChildNodeRecursively(node);
            $.getJqObj("#divTree").treeview("uncheckNode",[childNodes,{silent: true}]);
        }
    }

    function bindTree(pContainer, treeViewOptions, onBinded) {
        var container = $.getJqObj(pContainer);
        var finalTreeViewOptions = $.extend(true, defaultTreeViewOptions, treeViewOptions);
        var requestData = {roleId: ${busiModel.id}};
        var selectedModuleIdArr = null;
        <c:if test="${selectedModuleList != null && selectedModuleList.size() > 0}">
            selectedModuleIdArr = [
                <c:forEach var="item" items="${selectedModuleList}" varStatus="vs">
                    ${item.moduleid},
                </c:forEach>
            ];
            requestData.selectedModuleIds = selectedModuleIdArr;
        </c:if>
        var selectedActionIdArr = null;
        <c:if test="${selectedActionList != null && selectedActionList.size() > 0}">
            selectedActionIdArr = [
                <c:forEach var="item" items="${selectedActionList}" varStatus="vs">
                    ${item.actionId},
                </c:forEach>
            ];
            requestData.selectedActionIds = selectedActionIdArr;
        </c:if>
        $.ajax({
            type : "post",
            url : "<%=basePath%>/admin/common/module_tree_data.action",
            contentType: "application/json;charset=UTF-8",
            data: JSON.stringify(requestData),
            success : function(returnData) {
                if (!(returnData instanceof Array)){
                    $.showTip({ msg: "TreeView数据请求出错" });
                    return;
                }
                finalTreeViewOptions.data = returnData;
                container.treeview(finalTreeViewOptions);
                /*container.treeview('expandAll');*/
                if (onBinded && typeof onBinded  == 'function'){
                    onBinded();
                }
            },
            error : function() {
                $.showTip({ msg: "TreeView数据请求出错" });
            }
        });
    }

    bindTree("#divTree");
</script>