<%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page import="com.lightinit.hsdataplatform.dictionary.DicResourceCatelogType" %>
<%@ 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 action="<%=basePath%>/admin/dictionary_mgr/edit.action" method="post" class="form-horizontal">
                                        <input type="hidden" name="id" />
                                        <input type="hidden" name="groupId" />
                                        <div class="form-group">
                                            <label class="col-md-2 control-label">字典名称：</label>
                                            <div class="col-md-9">
                                                <input type="text" name="dicName" class="form-control" />
                                            </div>
                                            <span class="field-validation-valid help-block"></span>
                                        </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 action="<%=basePath%>/admin/dictionary_mgr/edit.action" method="post" class="form-horizontal">
                                        <input type="hidden" name="groupId" />
                                        <div class="form-group">
                                            <label class="col-md-2 control-label">字典名称：</label>
                                            <div class="col-md-9">
                                                <input type="text" name="dicName" class="form-control" />
                                            </div>
                                            <span class="field-validation-valid help-block"></span>
                                        </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>
                                            </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 defaultTreeViewSelectedFunction = function(event, node){
            var mContainer = $.getJqObj(this);
            var divForm1 = mContainer.closest(".row").children("div").eq(1).children("div").eq(0);
            var ctrlId = divForm1.find('input[name="id"]');
            ctrlId.val(node.id);
            var ctrlText = divForm1.find('input[name="dicName"]');
            ctrlText.val(node.text);
            var ctrlGroupId = divForm1.find('input[name="groupId"]');
            var parentNode = mContainer.treeview('getParent', node);
            if (parentNode && !(parentNode instanceof jQuery)){
                ctrlGroupId.val(parentNode.id);
            }else {
                ctrlGroupId.val("");
            }
        };

        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/dictionary_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 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 textValue = form.find('input[name=dicName]').val();
            if (textValue == null || textValue == ""){
                $.showAlert({message:"字典名称不能为空"});
                return;
            }
            var formSerialize = $(sender).closest("form").serialize();

            var ajaxSubmitArgs = {
                form: form,
                ajaxSubmitOptions: {
                    onSuccess: function (resultState) {
                        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 groupCtrl = form.find('input[name=groupId]');
            if (selectedNode){
                var parentNode = treeJqObj.treeview('getParent', selectedNode);
                if (parentNode && !(parentNode instanceof jQuery)){
                    $.showAlert({message:"请选中顶级节点进行新增操作"});
                    return;
                } else{
                    groupCtrl.val(selectedNode.id);
                }
            }else {
                groupCtrl.val("");
            }
            var textCtrl = form.find('input[name="dicName"]');
            var textValue = textCtrl.val();
            if (textValue == null || textValue == ""){
                $.showAlert({message:"字典名称不能为空"});
                return;
            }
            var formSerialize = $(sender).closest("form").serialize();

            var ajaxSubmitArgs = {
                form: form,
                ajaxSubmitOptions: {
                    onSuccess: function (resultState) {
                        refreshTree(divTree, resultState);
                        textCtrl.val("");
                    }
                }
            };
            $.ajaxValSubmit(ajaxSubmitArgs);
        }

        function del(sender, divTree) {
            var form = $(sender).closest("form");
            var textCtrl = form.find('input[name="dicName"]');
            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 = "";
            var parentNode = treeJqObj.treeview('getParent', selectedNode);
            if (parentNode && !(parentNode instanceof jQuery)){
                delUrl = "<%=basePath%>/admin/dictionary_mgr/delete_dic.action/" + selectedNode.id
            }else {
                delUrl = "<%=basePath%>/admin/dictionary_mgr/delete_dic_group.action/" + selectedNode.id
            }

            var ajaxGetArgs = {
                ajaxGetOptions: {
                    url: delUrl,
                    onSuccess: function (resultState) {
                        refreshTree(divTree, resultState);
                        textCtrl.val("");
                    }
                }
            };
            $.delWithConfirm({ func: $.ajaxGet, funcArgs: ajaxGetArgs });
        }
    </script>
</body>
</html>