<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <base th:href="${#request.getContextPath() + '/'} ">
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="assets/fonts/font-awesome/css/font-awesome.css"/>
    <link rel="stylesheet" href="assets/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="assets/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="assets/js/vendor/zTree3/css/metroStyle/metroStyle.css"/>
    <link rel="stylesheet" href="assets/layuiadmin/style/common.css" media="all">
    <style>
        .layui-btn a i {color: #FFF}
        .layui-card-header {overflow: auto}
    </style>
</head>
<body>
<div class="layui-anim layui-anim-fadein">
    <div class="layui-card">
        <div class="layui-card-header"></div>
        <div class="layui-card-body left-tree-wrap">
            <div id="leftTreeWrap">
                <ul id="leftTree" class="ztree"></ul>
            </div>
        </div>
    </div>
</div>
<script src="assets/layuiadmin/layui/layui.js"></script>
<script>
    var common;
    layui.config({
        base: 'assets/layuiadmin/'
    }).extend({
        index: 'lib/index'
    }).use(['index', 'ztree', 'common'], function() {
        common = layui.common;
        var $ = layui.$;
        var $win = $(window);
        var resizeTimeout = null;
        var questionTypeIds = top.restAjax.params(window.location.href).questionTypeIds;
        top.dialog.dialogData.selectedNodes = [];
        var selectedNodesMap = {};
        var parentId = 0;

        // 初始化大小
        function initSize() {
            $('#leftTreeWrap').css({
                height: $win.height() - 45,
                overflow: 'auto'
            });
        }

        // 初始化树
        function initThree() {
            var setting = {
                async: {
                    enable: true,
                    autoLoad: false,
                    type: 'get',
                    url: top.restAjax.path('api/question-type/list-ztree', []),
                    autoParam: ['id'],
                    otherParam: {},
                    dataFilter: function (treeId, parentNode, childNodes) {
                        if (!childNodes) return null;
                        for (var i = 0, l = childNodes.length; i < l; i++) {
                            childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
                        }
                        return childNodes;
                    }
                },
                callback: {
                    onClick: function (event, treeId, treeNode) {
                        parentId = treeNode.id;
                        addSelected(treeNode.id, treeNode.name, false)
                        return false;
                    }
                },
            };
            var zTree = $.fn.zTree.init($("#leftTree"), setting);
            zTree.addNodes(null, {
                id: '0',
                pId: '-1',
                name: top.dataMessage.tree.rootName,
                url: 'javascript:void(0);',
                isParent: 'true'
            });
            common.refreshTree('leftTree');
        }
        initSize();
        initThree();

        // 初始化训中
        function initSelectedNodes() {
            if(!questionTypeIds) {
                return;
            }
            top.restAjax.get(top.restAjax.path('api/question-type/list/{questionTypeIds}', [questionTypeIds]), {}, null, function(code, data) {
                for(var i = 0, item; item = data[i++];) {
                    selectedNodesMap[item.questionTypeId] = {
                        id: item.questionTypeId,
                        name: item.typeName
                    }
                }
                refreshSelectedNode();
            }, function(code, data) {
                top.dialog.msg(data.msg);
            })
        }
        initSelectedNodes();

        /**
         * 刷新节点
         */
        function refreshSelectedNode() {
            top.dialog.dialogData.selectedNodes = [];
            $('.layui-card-header').empty();
            var nodeSpans = ''
            for(var k in selectedNodesMap) {
                var selectedNode = selectedNodesMap[k];
                nodeSpans += '<span class="layui-btn layui-btn-xs">'+ selectedNode.name +' <a href="javascript:void(0)" class="remove-selected" data-id="'+ selectedNode.id +'"><i class="fa fa-times"></i></a></span>';
                top.dialog.dialogData.selectedNodes.push({
                    id: selectedNode.id,
                    name: selectedNode.name
                })
            }
            $('.layui-card-header').append(nodeSpans);
        }

        // 追加选中
        function addSelected(nodeId, nodeName, isSingle) {
            if(nodeId == 0) {
                return;
            }
            if(isSingle) {
                // 单选
                selectedNodesMap = {};
            }
            selectedNodesMap[nodeId] = {
                id: nodeId,
                name: nodeName
            }
            refreshSelectedNode();
        }

        // 删除选中
        $(document).on('click', '.remove-selected', function() {
            var id = this.dataset.id;
            delete selectedNodesMap[id];
            refreshSelectedNode();
        })

        // 事件 - 页面变化
        $win.on('resize', function() {
            clearTimeout(resizeTimeout);
            resizeTimeout = setTimeout(function() {
                initSize();
            }, 500);
        });
    });
</script>
</body>
</html>