@{
    ViewBag.Title = "选择权限";
    Layout = "~/Views/Shared/_LayoutTable.cshtml";
}
<style>
    a {
        color:#000000;
    }
    a:hover,a:focus{
        color:#3d84db;
    }
</style>
<body>
    <div class="wrapper wrapper-content">
        <br />
        <div style="width:30%;height:100px; float: left">
            <div class="row" style="margin-left:2%;" id="tree">
            </div>
        </div>
        <div style="width:70%;float: left">
            <span id="btnSave" class="btn btn-default" onclick="On()"><i class="glyphicon glyphicon-saved"></i> 保存</span>
            <br />
            <table id="SelectGroupTable"></table>
        </div>
    </div>

    <script type="text/javascript">
    function On() {
        var data = $('#SelectGroupTable').bootstrapTable("getData");
        if (data.length < 1) {
            return;
        }
        layer.msg("保存成功", { icon: 1, time: 2000 });
    }
    function BindSelectGroupTable() {
        $('#SelectGroupTable').bootstrapTable({
            url: '../WLTManage/GetWoolinte_UserLimitType',
            method: 'post',
            toolbar: '',
            striped: true,                      //是否显示行间隔色
            pagination: true,                   //是否显示分页（*）
            sortOrder: "asc",                   //排序方式
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,                       //初始化加载第一页，默认第一页
            pageSize: 10,                       //每页的记录行数（*）
            pageList: [10, 30, 50],        //可供选择的每页的行数（*）
            minimumCountColumns: 2,  //最少允许的列数
            height: $(window).height() - 100,
            uniqueId: "",            //每一行的唯一标识，一般为主键列
            clickToSelect: true,              //是否启用点击选中行
            sortable: false,                     //是否启用排序
            silentSort: true,
            cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            search: false,                       //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
            strictSearch: false,
            paginationFirstText: '首页',
            paginationLastText: '末页',
            paginationPreText: '上一页',
            paginationNextText: '下一页',
            showColumns: false,            //是否显示所有的列
            showRefresh: false,              //是否显示刷新按钮
            showToggle: false,                     //是否显示详细视图和列表视图的切换按钮
             cardView: false,                    //是否显示详细视图
            detailView: false,                   //是否显示父子表
            queryParams: function (params) {
                return {
                    pagesize: params.limit,//页面大小
                    page: params.offset, //页码
                };
            },//传递参数（*）
            columns: [
                {
                    checkbox: true, field: 'che', title: "启/停", formatter: function (value, row, index) {
                        return row["LimitValue"] == 1 ? true : false;
                    }
                },{
                    title: "序号",width:50, formatter: function (value, row, index) {
                        return index+1;
                    }
                },
                { field: 'LimitName', title: '权限名称' }
            ], onCheck: function (row) {
                $.ajax({
                    url: "../WLTManage/UpdateLimitGroupSet",
                    data: {
                        LimitValue: "1",
                        LimitId: row["LimitId"],
                        Sort: row["Sort"],
                        LimitType: row["LimitType"]
                    },
                    datatype: "text",
                    type: "post",
                    error: function () {
                        layer.mag("操作失败", { cion: 2, time: 2000 });
                    }
                })
            }, onUncheck: function (row) {
                $.ajax({
                    url: "../WLTManage/UpdateLimitGroupSet",
                    data: {
                        LimitValue: "0",
                        LimitId: row["LimitId"],
                        Sort: row["Sort"],
                        LimitType: row["LimitType"]
                    },
                    datatype: "text",
                    type: "post",
                    error: function () {
                        layer.mag("操作失败", { cion: 2, time: 2000 });
                    }
                })
            }, onCheckAll: function (row) {
                var data = $('#SelectGroupTable').bootstrapTable("getData");
                if (data.length < 1) {
                    return;
                }
                var limitType = "";
                for (var i = 0; i < data.length; i++) {
                    if (i == data.length - 1) {
                        limitType += data[i]["LimitType"];
                    } else {
                        limitType += data[i]["LimitType"] + ",";
                    }

                }
                if (limitType) {
                    $.ajax({
                        url: "../WLTManage/UpdateLimitGroupSetList",
                        data: {
                            LimitValue: "1",
                            LimitId: data[0]["LimitId"],
                            LimitType: limitType
                        },
                        datatype: "text",
                        type: "post",
                        error: function () {

                        }
                    })
                }
            }, onUncheckAll: function (row) {
                var data = $('#SelectGroupTable').bootstrapTable("getData");
                if (data.length < 1) {
                    return;
                }
                var limitType = "";
                for (var i = 0; i < data.length; i++) {
                    if (i == data.length - 1) {
                        limitType += data[i]["LimitType"];
                    } else {
                        limitType += data[i]["LimitType"] + ",";
                    }

                }
                if (limitType) {
                    $.ajax({
                        url: "../WLTManage/UpdateLimitGroupSetList",
                        data: {
                            LimitValue: "0",
                            LimitId: data[0]["LimitId"],
                            LimitType: limitType
                        },
                        datatype: "text",
                        type: "post",
                        error: function () {

                        }
                    })
                }
            }
        });
    }
    $(function () {
        BindSelectGroupTable();
        $.ajax({
            url: "../WLTManage/GetTree",
            data: {},
            datatype: "json",
            type: "post",
            success: function (data) {
                layui.use('tree', function () {
                    layui.tree({
                        elem: '#tree',
                        nodes: data,
                        click: function (node) {
                            if (node.sort != undefined && node.sort != "") {
                                //var ementy = window.event.srcElement || window.event.target;
                                //ementy.style("color", "red");
                                var id = parent.$("#limitId").val();
                                $('#SelectGroupTable').bootstrapTable("refresh", {
                                    url: '../WLTManage/GetWoolinte_UserLimitType?sort=' + node.sort + "&LimitId=" + id
                                })
                            }
                        }
                    })
                });
            }
        })
        $(window).resize(function () {
            $('#SelectGroupTable').bootstrapTable('resetView');
        });
    });
    </script>
</body>