{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="{% static '/lib/layui/css/layui.css' %}">
    <link rel="stylesheet" href="{% static '/lib/css/reset.css' %}">
    <style>
        .breadcrumbs {
            padding: 12px 0 12px 30px;
            border-bottom: 1px solid #dbdbdb;
        }

        .layui-breadcrumb>* {
            font-size: 16px;
        }

        .fixedDiv {
            position: fixed;
            width: 100%;
            z-index: 999;
            box-shadow: 0px 3px 5px 0px #ececec;
            background: #fff;
        }

        /* 发布类型 */

        .widget-title {
            position: relative;
            padding-left: 10px;
            font-size: 16px;
            padding: 5px 0 5px 10px;
        }

        .widget-title::after {
            position: absolute;
            content: '';
            left: 0;
            top: 9px;
            height: 15px;
            border-left: 5px solid #23a3ee;
            padding-left: 10px;
            font-size: 16px;
            padding: 5px 0 5px 10px;
        }

        .widget-title h2 {
            font-weight: 700;
            line-height: 33px;
        }

        .widget-title h2 span {
            font-size: 13px;
            font-weight: 100;
            margin-left: 20px;
            color: #333;
        }

        /*  */

        span.layui-tree-icon {
            height: 16px;
            line-height: 12px;
            width: 16px;
        }



        /* 上传详情 */

        .uploadContent {
            /* border-top: 7px solid #f0f0f0; */
            padding: 25px;
        }



        /* 返回 */

        .headerPlace {
            font-size: 22px;
            color: #418fbb;
            cursor: pointer;
            margin-bottom: 12px;
        }

        .headerPlace img {
            vertical-align: middle;
            /* margin-right: 10px; */
            margin-top: -4px;
        }

        .allotInnerDiv {
            margin-top: 12px;
        }
    </style>
</head>

<body>
    <div class="content">
        <div class="floatNav">
            <!-- 导航 -->
            <div class="breadcrumbs">
                <span class="layui-breadcrumb">
                    <a target="BoardRight">首页</a>
                    <a>
                        <cite>权限</cite>
                    </a>
                </span>
            </div>
        </div>

        <!-- 内容 -->
        <div>
            <!-- 发布详情 -->
            <div class="uploadContent" id="uploadContent">

                <div class="headerPlace">
                    <span onclick="goback()">
                        <img src="{% static 'imgs/gobackIcon (1).png'%}" alt=""> 返回
                    </span>
                </div>
                <div class="widget-title">
                    <h2>权限分配
                        <span id="Uptime"></span>
                    </h2>
                </div>
                <div style="margin-bottom: 20px;">
                    <button type="button" class="layui-btn  layui-btn-normal layui-btn-sm" onclick="getLimit()">保存权限</button>
                    <button type="button" class="layui-btn  layui-btn-normal layui-btn-sm" onclick="getLimit()">重置</button>
                </div>
                <div>
                    <div class="qx">
                        <div id="test1"></div>
                    </div>
                    <div>
                    </div>
                </div>

                <!-- 本页所需数据 -->
                <input type="hidden" value="{{uname}}" id="uname">

                <!-- 上页所需数据 -->
                <input type="hidden" value="{{f_name}}" id="f_name">
                <input type="hidden" value="{{currpage}}" id="currPage">
            </div>
        </div>
    </div>


    <script src="{% static '/lib/js/jquery-2.1.4.min.js'%}"></script>
    <script src="{% static '/lib/layui/layui.js'%}"></script>
    <script src="{% static '/js/backstageNet/common.js'%}"></script>
    <script>
        layui.use(['element'], function () {
            var element = layui.element;
        })
        var navList = [
            {
                "id": 1,
                "title": "首页",
                "icon": "&#xe626;",
                "url": "map",
                "children": []
            },
            {
                "id": 2,
                "title": "设备分配",
                "icon": "&#xe609;",
                "url": "equipallot",
                "disabled": "true",
                "children": []
            },
            {
                "id": 3,
                "title": "信息发布",
                "icon": "&#xe765;",
                "url": "",
                "children": [
                    {
                        "id": 3.1,
                        "title": "新闻资讯",
                        "url": "news_pub",
                    },
                    {
                        "id": 3.2,
                        "title": "视频资讯",
                        "url": "video_pub",
                    },
                    {
                        "id": 3.2,
                        "title": "产品展厅",
                        "url": "equip_pub",
                    },
                    {
                        "id": 3.3,
                        "title": "四季采摘",
                        "url": "garden_pub",
                    },
                    {
                        "id": 3.4,
                        "title": "民宿导览",
                        "url": "dorm_pub",
                    },
                    {
                        "id": 3.5,
                        "title": "旅游景点",
                        "url": "sightsee_pub",
                    },
                ]
            },
            {
                "id": 4,
                "title": "我的发布",
                "icon": "&#xe609;",
                "url": "my_pub",
                "disabled": "true",
                "children": []
            },
            {
                "id": 5,
                "title": "视频监控",
                "icon": "&#xe610;",
                "url": "jkview",
                "children": []
            },
            {
                "id": 6,
                "title": "传感及控制",
                "icon": "&#xe654;",
                "url": "",
                "children": [
                    {
                        "id": 6.1,
                        "title": "控制模块",
                        "url": "qxzcontrol",
                    },
                    {
                        "id": 6.2,
                        "title": "趋势分析",
                        "url": "qxzchart",
                    },
                    {
                        "id": 6.3,
                        "title": "数据统计",
                        "url": "qxz_status",
                    }
                ]
            },
            {
                "id": 7,
                "title": "预警中心",
                "icon": "&#xe634;",
                "url": "",
                "children": [
                    {
                        "id": 7.1,
                        "title": "阈值设置",
                        "url": "qxz_warning_view",
                    },
                    {
                        "id": 7.2,
                        "title": "生产方式设置",
                        "url": "productionset",
                    },
                    {
                        "id": 7.3,
                        "title": "预警记录",
                        "url": "qxz_warning_list",
                    },
                ]
            },
            {
                "id": 8,
                "title": "开心农场",
                "icon": "&#xe603;",
                "url": "",
                "children": [
                    {
                        "id": 8.1,
                        "title": "我的农场",
                        "url": "person_farm_edit",
                    },
                    {
                        "id": 8.2,
                        "title": "农场管理",
                        "url": "garden_manage_view",
                    },
                    {
                        "id": 8.3,
                        "title": "人员管理",
                        "url": "garden_user_view",
                    },
                    {
                        "id": 8.4,
                        "title": "库存管理",
                        "url": "repertory_view",  //物料管理    
                    },
                    {
                        "id": 8.5,
                        "title": "种植管理",
                        "url": "cropcount",
                    },
                    {
                        "id": 8.6,
                        "title": "入库管理",
                        "url": "pick",
                    },
                    {
                        "id": 8.7,
                        "title": "销售管理",
                        "url": "sell_view",
                    }
                ]
            },
            {
                "id": 9,
                "title": "数据中心",
                "icon": "&#xe603;",
                "url": "",
                "children": [
                    {
                        "id": 9.1,
                        "title": "产业数据",
                        "url": "industrydata",
                    },
                    {
                        "id": 9.2,
                        "title": "类型分析",
                        "url": "type",
                    },
                    {
                        "id": 9.3,
                        "title": "对比分析",
                        "url": "contrast",
                    },
                ]
            },
            {
                "id": 10,
                "title": "通知公告",
                "icon": "&#xe765;",
                "url": "",
                "disabled": "true",
                "children": [
                    {
                        "id": 10.1,
                        "title": "发布公告",
                        "url": "policy_pub",
                        "disabled": "true",
                    },
                    {
                        "id": 10.2,
                        "title": "查看公告",
                        "url": "policy_list_view",
                        "disabled": "true",
                    },
                ]
            },
            {
                "id": 11,
                "title": "信息审核",
                "icon": "&#xe765;",
                "url": "",
                "disabled": "true",
                "children": [
                    {
                        "id": 11.1,
                        "title": "新闻资讯",
                        "url": "news_check",
                        "disabled": "true",
                    },

                    {
                        "id": 11.2,
                        "title": "视频资讯",
                        "url": "video_check",
                        "disabled": "true",
                    },
                    {
                        "id": 11.3,
                        "title": "产品展厅",
                        "url": "equip_check",
                        "disabled": "true",
                    },
                    {
                        "id": 11.4,
                        "title": "四季采摘",
                        "url": "garden_check",
                        "disabled": "true",
                    },
                    {
                        "id": 11.5,
                        "title": "民宿导览",
                        "url": "dorm_check",
                        "disabled": "true",
                    },
                    {
                        "id": 11.6,
                        "title": "旅游景点",
                        "url": "sightsee_check",
                        "disabled": "true",
                    },
                    {
                        "id": 11.7,
                        "title": "个人农场",
                        "url": "farm_check",
                        "disabled": "true",
                    },
                ]
            },
            {
                "id": 12,
                "title": "权限",
                "icon": "&#xe765;",
                "url": "limit_view",
                "disabled": "true",
                "children": []
            },
            {
                "id": 13,
                "title": "系统设置",
                "icon": "&#xe765;",
                "url": "",
                "disabled": "true",
                "children": [
                    {
                        "id": 13.1,
                        "title": "用户管理",
                        "url": "person_view",
                        "disabled": "true",
                    },
                    {
                        "id": 13.2,
                        "title": "修改密码",
                        "url": "policy_list_view",
                        "disabled": "true",
                    }
                ]
            },
			{
                "id": 14,
                "title": "专家诊断",
                "icon": "&#xe765;",
                "url": "",
                "disabled": "true",
                "children": [
                    {
                        "id": 14.1,
                        "title": "在线专家",
                        "url": "expert",
                        "disabled": "true",
                    },
                    {
                        "id": 14.2,
                        "title": "知识库",
                        "url": "learning_depot",
                        "disabled": "true",
                    }
                ]
            },
        ]

        $.ajax({
            url: 'limit',
            type: 'post',
            data: {
                uname: $('#uname').val()
            },
            // dataType: "json",
            success: function (data) {
                if (data) {
                    var newNavList = eval('(' + data + ')');
                    layui.use(['tree'], function () {
                        var tree = layui.tree;

                        var topicId = []
                        for (var i = 0; i < newNavList.length; i++) {
                            if (newNavList[i].children.length) {
                                var currChildren = newNavList[i].children;
                                for (var j = 0; j < currChildren.length; j++) {
                                    topicId.push(currChildren[j].id);
                                    // topicId.push(parseInt(currChildren[j].id));
                                }
                            } else {
                                topicId.push(newNavList[i].id);
                                // topicId.push(parseInt(newNavList[i].id));
                            }
                            if (i == newNavList.length - 1) {
                                //基本演示
                                tree.render({
                                    elem: '#test1'
                                    , data: navList
                                    , showCheckbox: true  //是否显示复选框
                                    , id: 'demoId1'
                                    , isJump: true //是否允许点击节点时弹出新窗口跳转
                                    , click: function (obj) {
                                        var data = obj.data;  //获取当前点击的节点数据
                                        // layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
                                    }
                                });
                                tree.setChecked('demoId1', topicId); //勾选指定节点
                                console.log(topicId)

                            }
                        }


                    })
                } else {
                    layui.use(['tree'], function () {
                        var tree = layui.tree;
                        tree.render({
                            elem: '#test1'
                            , data: navList
                            , showCheckbox: true  //是否显示复选框
                            , id: 'demoId1'
                            , isJump: true //是否允许点击节点时弹出新窗口跳转
                            , click: function (obj) {
                                var data = obj.data;  //获取当前点击的节点数据
                                // layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
                            }
                        });
                    })
                }
            },
            error: function (type) {
                console.log(type)
            }
        })


        function getLimit() {
            layui.use(['tree'], function () {
                var tree = layui.tree;
                var checkData = tree.getChecked('demoId1');
                console.log(checkData)
                if (checkData.length) {
                    $.ajax({
                        url: 'limit_view',
                        type: 'post',
                        data: {
                            uname: $('#uname').val(),
                            limit: JSON.stringify(checkData)
                        },
                        success: function (data) {
                            if (data) {
                                layui.use(['layer'], function () {
                                    var layer = layui.layer;
                                    layer.msg('设置成功！', { icon: 1, time: 500 }, function () {
                                        goback()
                                    })
                                })
                            } else {
                                layui.use(['layer'], function () {
                                    var layer = layui.layer;
                                    layer.msg('设置失败！', { icon: 2 })
                                })
                            }
                        },
                        error: function (type) {
                            console.log(type)
                        }
                    })
                } else {
                    layui.use(['layer'], function () {
                        var layer = layui.layer;
                        layer.msg('必须选择一项', { icon: 2 })
                    })
                }

            })
        }









        windowAddMouseWheel();
        function windowAddMouseWheel() {
            var scrollFunc = function (e) {
                e = e || window.event;
                var top = $(window).scrollTop();
                if (e.wheelDelta) {  //判断浏览器IE，谷歌滑轮事件
                    if (e.wheelDelta > 0) { //当滑轮向上滚动时
                        if (top < 60) {
                            if ($(".floatNav").hasClass("fixedDiv")) {
                                $(".floatNav").removeClass("fixedDiv");
                                $(window).scrollTop(0)
                            }
                        }
                    }
                    if (e.wheelDelta < 0) { //当滑轮向下滚动时
                        if (top >= 60) {
                            $(".floatNav").addClass("fixedDiv");
                        }
                    }
                } else if (e.detail) {  //Firefox滑轮事件
                    if (e.detail > 0) { //当滑轮向上滚动时
                        if (top < 60) {
                            if ($(".floatNav").hasClass("fixedDiv")) {
                                $(".floatNav").removeClass("fixedDiv");
                                $(window).scrollTop(0)
                            }
                        }
                    }
                    if (e.detail < 0) { //当滑轮向下滚动时
                        if (top >= 60) {
                            $(".floatNav").addClass("fixedDiv");
                        }
                    }
                }
            };
            //给页面绑定滑轮滚动事件
            if (document.addEventListener) {
                document.addEventListener('DOMMouseScroll', scrollFunc, false);
            }
            //滚动滑轮触发scrollFunc方法
            window.onmousewheel = document.onmousewheel = scrollFunc;
        }

        // 返回
        function goback() {
            var currPage = $('#currPage').val();
            var f_name = $('#f_name').val();
            window.location.href = 'limit_view?uname=' + f_name + '&page=' + currPage;
        }

    </script>
</body>

</html>