<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta name="renderer" content="webkit">
    <!-- 移动设备 viewport -->
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <jsp:include page="../../../common.jsp" />
    <title>编辑</title>
</head>
<body>
<div class="layui-fluid">

    <div style="margin-bottom: 20px;" id="test1"></div>

    <form action="" class="layui-form" lay-filter="form">
        <input autocomplete="off" class="layui-input layui-hide" name="id" value="${entity.id}" type="text">

        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-fluid" lay-filter="demo1" lay-submit type="submit">保存</button>
        </div>

    </form>
</div>
</body>

<script>
    let layer;
    let treeData = []
    let tree
    let form
    layui.use(['tree', 'layer'], function () {
        tree = layui.tree;
        layer = layui.layer;
        treeInit()
    });

    layui.use(['form'], function () {
        let form = layui.form;

        form.on("submit(demo1)", function (d) {
            let data = d.field;
            let resultArr = [];
            getAllIdList(tree.getChecked('test1'), resultArr);
            data.menuIds = resultArr;
            $.ajax({
                url: ctx + '/roleInfo/saveConfig'
                , type: 'POST'
                , data: data
                , success: function (data) {
                    if (data.code === 200) {
                        parent.layer.msg("保存成功")
                        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index);
                        parent.location.reload();
                    } else {
                        parent.layer.msg("保存失败")
                    }
                }
            });

            return false;
        })
    })

    function getAllIdList(nodeList, resultArr) {
        if (Array.isArray(nodeList)) {
            nodeList.forEach(function(item) {
                resultArr.push(item.id);
                if (item.children) {
                    getAllIdList(item.children, resultArr);
                }
            })
        } else {
            resultArr.push(nodeList);
            if (nodeList.children) {
                getAllIdList(nodeList.children, resultArr);
            }
        }
    }

    function treeInit() {
        $.ajax({
            url: ctx + "/menuInfo/getRoleConfigMenuTree?roleId=${entity.id}"
            , type: 'GET'
            , success: function (d) {
                if (d.code === 200) {
                    treeData = d.data;
                    treeData[0].spread = true;


                    //渲染
                    tree.render({
                        elem: '#test1'  //绑定元素
                        , id: 'test1'
                        , showCheckbox: true
                        , data: treeData
                    });
                }
            }
        })
    }
</script>
</html>