<h2>{{ $title }} <a href="javascript:history.go(-1);" class="layui-btn layui-btn-primary layui-layer-close"
                    style="position:relative;">返回</a></h2>
<hr/>

<form class="layui-form">
    <div class="layui-form-item">
         <input type="hidden" name="staff_num" id="staff_num" value="{{ $staff_num }}">
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">全部选择</label>
        <div class="layui-input-block">
            <input type="checkbox" lay-filter="xuanze" lay-skin="switch" lay-text="全选|全不选">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选择权限</label>
        <div class="layui-input-block">
            <div id="LAY-auth-tree-index"></div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" type="submit" lay-submit lay-filter="LAY-auth-tree-submit">提交</button>
            <button class="layui-btn layui-btn-default" type="reset">重置</button>
        </div>
    </div>
</form>

<script type="text/javascript">
    layui.extend({
        authtree: '/js/admin/layui/extends/authtree',
    });
    layui.use(['jquery', 'authtree', 'form', 'layer'], function () {
        var $ = layui.jquery;
        var authtree = layui.authtree;
        var form = layui.form;
        var layer = layui.layer;
        // 初始化
        var trees = authtree.listConvert(@json($menusAll), {
            primaryKey: 'id'
            , startPid: 0
            , parentKey: 'parent_id'
            , nameKey: 'title'
            , valueKey: 'id'
            , checkedKey: eval(<?php echo json_encode($checkedId);?>)
        });

        // 渲染时传入渲染目标ID，树形结构数据（具体结构看样例，checked表示默认选中），以及input表单的名字
        authtree.render('#LAY-auth-tree-index', trees, {
            inputname: 'authids[]'
            , layfilter: 'lay-check-auth'
            // ,autoclose: false
            // ,autochecked: false
            // ,openchecked: true
            , openall: true
            , autowidth: true
        });

        form.on('switch(zhankai)', function (data) {
            if (data.elem.checked) {
                authtree.showAll('#LAY-auth-tree-index');
            } else {
                authtree.closeAll('#LAY-auth-tree-index');
            }
        });

        form.on('switch(xuanze)', function (data) {
            if (data.elem.checked) {
                authtree.checkAll('#LAY-auth-tree-index');
            } else {
                authtree.uncheckAll('#LAY-auth-tree-index');
            }
        });

        form.on('submit(LAY-auth-tree-submit)', function (obj) {
            var authids = authtree.getChecked('#LAY-auth-tree-index');
            console.log('Choosed authids is', authids);
            var staff_num = $('#staff_num').val();
            $.post('/admin/users/roles/edit',{'staff_num':staff_num,'mine_id':authids},function(res){
                if (res.code === 0) {
                    layer.msg('提交成功！');
                    location.href = '/admin/users/list';
                }
            })
            return false;
        });

    });

</script>