<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>管辖区域</title>
    <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="../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md6">
            <div class="layui-card">

                <div class="layui-card-body">
                    <form class="layui-form" action="" lay-filter="component-form-element">
                        <div id="id_classes" class="demo-tree"></div>
                        <button class="layui-btn" id="id_save" lay-submit lay-filter="component-form-element"
                                style="display: none">保存
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="../../layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'tree',], function () {
        var $ = layui.$
            , admin = layui.admin
            , form = layui.form
            , tree = layui.tree;

        var id = layui.view.getParameterByName('id');
        var urlTree = '/option/school_tree/'
        admin.req({
            url: urlTree
            , done: function (res) {
                tree.render({
                    elem: '#id_classes'
                    , id: 'classesID'
                    , data: res.data
                    , showCheckbox: true
                });
            }
        });

        form.on('submit(component-form-element)', function (data) {
            var classes = []
            var checkedData = tree.getChecked('classesID');
            for (var p in checkedData) {
                for (var c in checkedData[p].children) {
                    for (var t in checkedData[p].children[c].children) {
                        classes.push(checkedData[p].children[c].children[t]['id'])
                    }
                }
            }

            if (classes.length === 0) {
                layer.msg("请选择班级", {icon: 2});
                return false
            }
            else if (classes.length > 1) {
                layer.msg("只能选择一个班级", {icon: 2});
                return false
            }
            admin.req({
                url: '/option/student/' + id +'/change_classes/'
                , data: {classes: JSON.stringify(classes)}
                , type: 'post'
                , done: function (res) {
                    parent.layui.onSubmitChild(res);
                }
            });
            return false;
        });

        parent.layui.submitChild = function () {
            $("#id_save").click();
        };
    });
</script>
</body>
</html>
