<%@ page import="com.lightinit.hsdataplatformresdir.dictionary.DicModuleActionType" %>
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
    String basePath = request.getContextPath();
%>
<!DOCTYPE html>
<html>
<head>
    <jsp:include page="../../../common/admin/head.jsp"></jsp:include>
</head>
<body>
    <jsp:include page="../../../common/admin/body_pre.jsp"></jsp:include>
    <div class="row hidden-print">
        <div class="col-lg-12">
            <div class="ibox">
                <div class="ibox-title">
                    <h2>模块与功能管理</h2>
                    <ol class="breadcrumb">
                        <li>
                            <a href="javascript:void(0);" >配置中心</a>
                        </li>
                        <li class="active">
                            <strong>模块与功能管理</strong>
                        </li>
                    </ol>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <div class="row">
                <div class="col-md-7">
                    <div class="ibox">
                        <div class="ibox-content">
                            <div id="divTree"></div>
                        </div>
                    </div>
                </div>
                <div class="col-md-5">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="panel panel-primary">
                                <div class="panel-heading">
                                    编辑当前模块/功能
                                </div>
                                <div class="panel-body">
                                    <form id="formEdit" action="<%=basePath%>/admin/module_mgr/edit.action" method="post" class="form-horizontal">
                                        <input type="hidden" name="id" />
                                        <input type="hidden" name="moduleId" />
                                        <input type="hidden" name="objType" />
                                        <div class="form-group">
                                            <label class="col-md-2 control-label">名称：</label>
                                            <div class="col-md-9">
                                                <input type="text" name="objName" class="form-control" />
                                                <span class="help-block"></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-2 control-label">编码：</label>
                                            <div class="col-md-9">
                                                <input type="text" name="objCode" class="form-control" />
                                                <span class="help-block"></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-2 control-label">URL映射：</label>
                                            <div class="col-md-9">
                                                <input type="text" name="objUrl" class="form-control" data-toggle="tooltip" data-placement="top" title="多个映射使用“+”连接" />
                                                <span class="help-block"></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <div class="col-md-2"></div>
                                            <div class="col-md-9 text-right">
                                                <button type="button" class="btn btn-primary right" onclick='edit(this, "#divTree")'><i class="fa fa-check"></i>保存</button>
                                                <button type="button" class="btn btn-danger right" onclick='del(this, "#divTree")'><i class="fa fa-trash-o"></i>删除</button>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="panel panel-primary">
                                <div class="panel-heading">
                                    新增模块/功能
                                </div>
                                <div class="panel-body">
                                    <form id="formAdd" action="<%=basePath%>/admin/module_mgr/edit.action" method="post" class="form-horizontal">
                                        <input type="hidden" name="moduleId" />
                                        <div class="form-group">
                                            <label class="col-md-2 control-label">名称：</label>
                                            <div class="col-md-9">
                                                <select name="objType" class="form-control">
                                                    <c:forEach items="${dicModuleActionType}" var="item" >
                                                        <option value="${item.key}">${item.value}</option>
                                                    </c:forEach>
                                                </select>
                                                <span class="help-block"></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-2 control-label">名称：</label>
                                            <div class="col-md-9">
                                                <input type="text" name="objName" class="form-control" />
                                                <span class="help-block"></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-2 control-label">编码：</label>
                                            <div class="col-md-9">
                                                <input type="text" name="objCode" class="form-control" />
                                                <span class="help-block"></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label class="col-md-2 control-label">URL映射：</label>
                                            <div class="col-md-9">
                                                <input type="text" name="objUrl" class="form-control" data-toggle="tooltip" data-placement="top" title="多个映射使用“+”连接" />
                                                <span class="help-block"></span>
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <div class="col-md-2"></div>
                                            <div class="col-md-9 text-right">
                                                <button type="button" class="btn btn-primary right" onclick='add(this, "#divTree")'><i class="fa fa-check"></i>保存</button>
                                                <button type="button" class="btn btn-default right" onclick='resetForm("#formAdd")'><i class="fa fa-reply"></i>重置</button>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="clearfix"></div>
    <jsp:include page="../../../common/admin/body_post.jsp"></jsp:include>
    <script type="text/javascript">

        var allModuleList = ${allModuleList};
        var allActionList = ${allActionList};

        var defaultTreeViewSelectedFunction = function(event, node){
            var models;
            var isModule = node.tags && $.inArray("<%=DicModuleActionType.MODULE%>", node.tags) >= 0;
            if (isModule){
                models = $.grep(allModuleList, function (element, index) {
                    return element.id == node.id;
                });
            }else {
                models = $.grep(allActionList, function (element, index) {
                    return element.id == node.id;
                });
            }
            if (!models){
                $.showAlert({message: "未找到数据对象"});
                return;
            }

            var model = models[0];
            if (!model){
                $.showAlert({message: "未找到数据对象"});
                return;
            }

            var mContainer = $.getJqObj(this);
            var divForm1 = mContainer.closest(".row").children("div").eq(1).children("div").eq(0);
            var ctrlId = divForm1.find('input[name="id"]');
            var ctrlText = divForm1.find('input[name="objName"]');
            var ctrlObjType = divForm1.find('input[name="objType"]');
            var ctrlObjCode = divForm1.find('input[name="objCode"]');
            var ctrlObjUrl = divForm1.find('input[name="objUrl"]');
            ctrlId.val(node.id);
            ctrlText.val(node.text);
            ctrlObjType.val(node.tags[0]);
            if (isModule){
                ctrlObjCode.val(model.menucode);
                ctrlObjUrl.val(model.menuaction);
            } else {
                ctrlObjCode.val(model.actionCode);
                ctrlObjUrl.val(model.actionUrl);
            }
        };

        function resetForm(pForm){
            var jqForm = $.getJqObj(pForm);
            jqForm[0].reset();
        }

        var defaultTreeViewOptions = {
            levels: 1,
            showTags: true,
            onNodeSelected: defaultTreeViewSelectedFunction
        }

        function bindTree(pContainer, treeViewOptions, onBinded) {
            var container = $.getJqObj(pContainer);
            var divForm1 = container.closest(".row").children("div").eq(1).children("div").eq(0);
            var finalTreeViewOptions = $.extend(true, defaultTreeViewOptions, treeViewOptions);
            $.ajax({
                type : "get",
                url : "<%=basePath%>/admin/module_mgr/tree_data.action",
                success : function(returnData) {
                    if (!(returnData instanceof Array)){
                        $.showTip({ msg: "TreeView数据请求出错" });
                        return;
                    }
                    finalTreeViewOptions.data = returnData;
                    container.treeview(finalTreeViewOptions);
                    if (onBinded && typeof onBinded  == 'function'){
                        onBinded();
                    }
                },
                error : function() {
                    $.showTip({ msg: "TreeView数据请求出错" });
                }
            });
        }

        bindTree("#divTree");

        function refreshTree(divTree, resultState){
            var treeJqObj = $.getJqObj(divTree);
            var expandedNodes = treeJqObj.treeview('getExpanded');
            var selectedNodes = treeJqObj.treeview('getSelected');
            treeJqObj.treeview('remove');
            bindTree(divTree, null, function () {
                if (expandedNodes && expandedNodes instanceof Array){
                    for(var i = 0; i < expandedNodes.length; i++){
                        var expandedNode = expandedNodes[i];
                        treeJqObj.treeview('expandNode', [ expandedNode.nodeId, { silent: true } ]);
                    }
                }
                if (selectedNodes && selectedNodes instanceof Array && selectedNodes.length > 0){
                    var selectedNode = selectedNodes[0];
                    var node = treeJqObj.treeview('getNode', selectedNode.nodeId);
                    if (node.id == selectedNode.id){
                        treeJqObj.treeview('selectNode', [ selectedNode.nodeId, { silent: true } ]);
                        treeJqObj.treeview('expandNode', [ selectedNode.nodeId, { silent: true } ]);
                    }
                }
            });
        }

        function updateData(objType, model){
            var allObjList;
            if (objType == "<%=DicModuleActionType.MODULE%>"){
                allObjList = allModuleList;
            }else {
                allObjList = allActionList;
            }
            var isAdd = true;
            for (var i = 0; i < allObjList.length; i++){
                if (model.id == allObjList[i].id){
                    allObjList[i] = model;
                    isAdd = false;
                    break;
                }
            }
            if (isAdd){
                allObjList.push(model);
            }
        }

        function edit(sender, divTree) {
            var form = $(sender).closest("form");
            var treeJqObj = $.getJqObj(divTree);
            var selectedNodes = treeJqObj.treeview('getSelected');
            var selectedNode = selectedNodes && selectedNodes.length > 0 ? selectedNodes[0] : null;
            if (!selectedNode){
                $.showAlert({message:"请选中节点"});
                return;
            }
            form.find('input[name=id]').val(selectedNode.id);
            var objType = selectedNode.tags[0];

            var ajaxSubmitArgs = {
                form: form,
                ajaxSubmitOptions: {
                    onSuccess: function (resultState) {
                        updateData(objType, resultState.data);
                        refreshTree(divTree, resultState);
                    }
                }
            };
            $.ajaxValSubmit(ajaxSubmitArgs);
        }

        function add(sender, divTree) {
            var form = $(sender).closest("form");
            var treeJqObj = $.getJqObj(divTree);
            var selectedNodes = treeJqObj.treeview('getSelected');
            var selectedNode = selectedNodes && selectedNodes.length > 0 ? selectedNodes[0] : null;
            var moudleIdCtrl = form.find('input[name=moduleId]');
            if (selectedNode){
                var objTypeCtrl = form.find("select[name=objType]");
                var objType = objTypeCtrl.val();
                if (objType = "<%=DicModuleActionType.ACTION%>" && !(selectedNode.tags && $.inArray("<%=DicModuleActionType.MODULE%>", selectedNode.tags) >= 0)){
                    $.showAlert({message:"请选中模块进行新增功能操作"});
                    return;
                }else {
                    moudleIdCtrl.val(selectedNode.id);
                }
            }else {
                moudleIdCtrl.val("");
            }

            var ajaxSubmitArgs = {
                form: form,
                ajaxSubmitOptions: {
                    onSuccess: function (resultState) {
                        var objTypeCtrl = form.find("select[name=objType]");
                        var objType = objTypeCtrl.val();
                        updateData(objType, resultState.data);
                        refreshTree(divTree, resultState);
                        resetForm(form);
                    }
                }
            };
            $.ajaxValSubmit(ajaxSubmitArgs);
        }

        function del(sender, divTree) {
            var form = $(sender).closest("form");
            var textCtrl = form.find('input[name="objName"]');
            var treeJqObj = $.getJqObj(divTree);
            var selectedNodes = treeJqObj.treeview('getSelected');
            var selectedNode = selectedNodes && selectedNodes.length > 0 ? selectedNodes[0] : null;
            if (!selectedNode){
                $.showAlert({message:"请选中节点"});
                return;
            }

            var delUrl = "";
            if (selectedNode.tags && $.inArray("<%=DicModuleActionType.MODULE%>", selectedNode.tags) >= 0){
                delUrl = "<%=basePath%>/admin/module_mgr/delete_module.action/" + selectedNode.id
            }else {
                delUrl = "<%=basePath%>/admin/module_mgr/delete_action.action/" + selectedNode.id
            }

            var ajaxGetArgs = {
                ajaxGetOptions: {
                    url: delUrl,
                    onSuccess: function (resultState) {
                        refreshTree(divTree, resultState);
                        resetForm(form);
                    }
                }
            };
            $.delWithConfirm({ func: $.ajaxGet, funcArgs: ajaxGetArgs });
        }

        // 验证
        var validateOptions = {
            rules: {
                objName: "required"
            }
        }
        $("#formEdit").validate(validateOptions);
        optionAdd = jQuery.extend(true, {rules: {objType: 'required'}}, validateOptions);
        $("#formAdd").validate(optionAdd);

        $('[data-toggle="tooltip"]').tooltip();
    </script>
</body>
</html>