@{
    ViewBag.Title = "权限设置";
    Layout = "~/Views/Shared/_LayoutTable.cshtml";
}

@section head{
    <style>
        /*body .layui-tree-skin-shihuang .layui-tree-branch {
            color: #9999CC;
        }*/
    </style>
}
<body>
    @*<div style="width:20%;height:100%;background-color:white; float:left">
        <div></div>
        <ul id="demo1"></ul>
    </div>
   
    <div style="width:5%;float:left;height:100%;background-color:white;">&nbsp;</div>
    <div style="width:75%;height:100%;background-color:white; float:left">
        <table class="table table-bordered table-striped table-hover">
            <tr>
                <td style="text-align:left;width:180px">
                    快速设置权限组：
                </td>
                <td>
                    @{
                        if (ViewBag.Fast != null && ViewBag.Fast.Rows.Count > 0)
                        {
                            for (int i = 0; i < ViewBag.Fast.Rows.Count; i++)
                            {
                                <button id="btnFast" style="margin:1px 3px" class="btn btn-default btn-sm" onclick="FastSet('@ViewBag.Fast.Rows[i]["Id"]')"><i class="glyphicon glyphicon-flash"></i> @ViewBag.Fast.Rows[i]["LimitName"]</button>
                            }
                        }
                    }
                </td>
            </tr>
            <tr>
                <td colspan="2" align="left">
                    <input id="chkAll" name="chkAll" type="checkbox" onclick="chkAll()" /> 开启所有权限
                </td>
            </tr>
        </table>
        <table id="WLTRightsTable"></table>
    </div>*@


    <div class="container-fluid">
        <div class="row-fluid">
            <div class="col-md-12" style="margin-top:5px">
                <table class="table table-bordered table-striped table-hover">
                    <tr>
                        <td style="text-align:left;width:180px">
                            快速设置权限组：
                        </td>
                        <td>
                            @{
                                if (ViewBag.Fast != null && ViewBag.Fast.Rows.Count > 0)
                                {
                                    for (int i = 0; i < ViewBag.Fast.Rows.Count; i++)
                                    {
                                        <button id="btnFast" style="margin:1px 3px" class="btn btn-default btn-sm" onclick="FastSet('@ViewBag.Fast.Rows[i]["Id"]')"><i class="glyphicon glyphicon-flash"></i> @ViewBag.Fast.Rows[i]["LimitName"]</button>
                                    }
                                }
                            }
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" align="left">
                            <input id="chkAll" name="chkAll" type="checkbox" onclick="chkAll()" /> 开启所有权限
                        </td>
                    </tr>
                </table>
            </div>
            <div class="col-md-3" style="margin-top:30px;padding:0">
                <ul id="demo1" style="padding:0;background-color:rgb(248, 248, 248)"></ul>
            </div>
            <div class="col-md-9">
                <table id="WLTRightsTable"></table>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        
        function chkAll()
        {
            var obj = document.getElementById("chkAll");
            if (obj.checked == true) {
                var selval = "all" + ":" + '@ViewBag.LoginID' + ":" + "1";
                $.ajax({
                    url: "../WLTManage/UpdateWLTRightsSetting",
                    dataType: "text",
                    data: { selval: selval },
                    type: "post",
                    success: function (data) {//删除成功后刷新table
                        layer.msg("启用成功", { time: 2000 });
                        $('#WLTRightsTable').bootstrapTable("refresh");
                    },
                    error: function (data) {
                    }
                })
            }
            else {
                var selval = "all" + ":" + '@ViewBag.LoginID' + ":" + "0";
                $.ajax({
                    url: "../WLTManage/UpdateWLTRightsSetting",
                    dataType: "text",
                    data: { selval: selval },
                    type: "post",
                    success: function (data) {//删除成功后刷新table
                        layer.msg("取消成功", { time: 2000 });
                        $('#WLTRightsTable').bootstrapTable("refresh");
                    },
                    error: function (data) {
                    }
                })
            }
        }

        $(function () {
            $('#WLTRightsTable').bootstrapTable({
                url: '../WLTManage/GetWLTRightsTableList?LoginID=' + '@ViewBag.LoginID',         //请求后台的URL（*）
                method: 'post',                      //请求方式（*）
                toolbar: '#toolbar',                //工具按钮用哪个容器
                striped: true,                      //是否显示行间隔色
                pagination: true,                   //是否显示分页（*）
                sortOrder: "asc",                   //排序方式
                sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
                pageNumber: 1,                       //初始化加载第一页，默认第一页
                pageSize: 10,                       //每页的记录行数（*）
                pageList: [10, 30, 50],        //可供选择的每页的行数（*）
                minimumCountColumns: 2,  //最少允许的列数
                //height: "100%",               //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
                uniqueId: "",            //每一行的唯一标识，一般为主键列
                clickToSelect: true,              //是否启用点击选中行
                sortable: true,                     //是否启用排序
                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', formatter: function (value, row, index) {
                            if (row["USERLIMITVALUE"] == "1") {
                                return true;
                            } else {
                                return false;
                            }
                        }
                    },
                    {
                        field: "LIMITNAME", title: "权限名称"
                    },
                    { field: 'USERLIMITVALUE', title: 'USERLIMITVALUE' },
                { field: 'LIMITTYPE', title: 'LIMITTYPE' }
                ],
                onCheck: function (row) {
                    var selval = "";
                    selval = row.LIMITTYPE + ":" + '@ViewBag.LoginID' + ":" + "1";
                    $.ajax({
                        url: "../WLTManage/UpdateWLTRightsSetting",
                        dataType: "text",
                        data: { selval: selval },
                        type: "post",
                        success: function (data) {//删除成功后刷新table
                            var obj1 = document.getElementById("chkAll");
                            var value1 = obj1.checked;
                            obj1.checked = false;// 设置复选框为选中状态
                            layer.msg("启用成功", { time: 2000 });
                        },
                        error: function (data) {
                        }
                    })
                },
                onUncheck: function (row) {
                    var selval = "";
                    selval = row.LIMITTYPE + ":" + '@ViewBag.LoginID' + ":" + "0";
                    $.ajax({
                        url: "../WLTManage/UpdateWLTRightsSetting",
                        dataType: "text",
                        data: { selval: selval },
                        type: "post",
                        success: function (data) {//删除成功后刷新table
                            var obj1 = document.getElementById("chkAll");
                            var value1 = obj1.checked;
                            obj1.checked = false;// 设置复选框为选中状态
                            layer.msg("取消成功", { time: 2000 });
                        },
                        error: function (data) {
                        }
                    })
                }
                    , onCheckAll: function (rows) {
                        var nsel = "";
                        for (var i = 0; i < rows.length; i++) {
                            if (i == rows.length - 1)
                            { nsel += rows[i].LIMITTYPE + ":" + '@ViewBag.LoginID' + ":" + "1"; }
                            else
                            { nsel += rows[i].LIMITTYPE + ":" + '@ViewBag.LoginID' + ":" + "1" + ","; }
                        }
                        $.ajax({
                            url: "../WLTManage/UpdateWLTRightsSetting",
                            dataType: "text",
                            data: { selval: nsel },
                            type: "post",
                            success: function (data) {//删除成功后刷新table
                                var obj1 = document.getElementById("chkAll");
                                var value1 = obj1.checked;
                                obj1.checked = false;// 设置复选框为选中状态
                                layer.msg("启用成功", { time: 2000 });
                            },
                            error: function (data) {
                            }
                        })
                    }, onUncheckAll: function (rows) {
                        var nsel = "";
                        for (var i = 0; i < rows.length; i++) {
                            if (i == rows.length - 1)
                            { nsel += rows[i].LIMITTYPE + ":" + '@ViewBag.LoginID' + ":" + "0"; }
                            else
                            { nsel += rows[i].LIMITTYPE + ":" + '@ViewBag.LoginID' + ":" + "0" + ","; }
                        }
                        $.ajax({
                            url: "../WLTManage/UpdateWLTRightsSetting",
                            dataType: "text",
                            data: { selval: nsel },
                            type: "post",
                            success: function (data) {//删除成功后刷新table
                                var obj1 = document.getElementById("chkAll");
                                var value1 = obj1.checked;
                                obj1.checked = false;// 设置复选框为选中状态
                                layer.msg("取消成功", { time: 2000 });
                            },
                            error: function (data) {
                                layer.msg(data, { icon: 5, time: 2000 });
                            }
                        })
                    }

            });
            $('#WLTRightsTable').bootstrapTable('hideColumn', 'USERLIMITVALUE');
            $('#WLTRightsTable').bootstrapTable('hideColumn', 'LIMITTYPE');
        });
        function FastSet(id) {//快速设置权限组
            $.ajax({
                url: "../WLTManage/FastRightsGroupSetting",
                dataType: "text",
                data: { id: id, LoginID: '@ViewBag.LoginID' },
                type: "post",
                success: function (data) {//删除成功后刷新table
                    if (data == "权限组设置成功！") {
                        var obj1 = document.getElementById("chkAll");
                        var value1 = obj1.checked;
                        obj1.checked = false;// 设置复选框为选中状态
                        layer.msg(data, { time: 2000 });
                        $('#WLTRightsTable').bootstrapTable("refresh");
                    } else {
                        layer.msg(data, { time: 2000 });
                    }
                },
                error: function (data) {
                    layer.msg(data, { icon: 5, time: 2000 });
                }
            })
        }
    </script>

    <script type="text/javascript">
        var version = '@ViewBag.WLTVERSION';
        var mobilename = '@ViewBag.mobilename';
        var mobilevalue = '@ViewBag.mobilevalue';
        var sername = '@ViewBag.sername';
        var servalue = '@ViewBag.servalue';
        var mbname = mobilename.split(',');//手机版权限name
        var mbval = mobilevalue.split(',');//手机版权限val
        var srname = sername.split(',');//服务端name
        var srval = servalue.split(',');//服务端val

        layui.use(['tree', 'layer'], function () {
            var layer = layui.layer
            , $ = layui.jquery;

            //生成一个模拟树
            var createTree = function (node, start) {
                node = node || function () {
                    var arr = [{ name: "物联通手机版权限", spread: true }, { name: "物联通服务端权限", spread: true }];
                    if (version == "WLTGY") {
                        arr = [{ name: "果易手机版权限", spread: true }, { name: "果易服务端权限", spread: true }];
                    }
                    return arr;
                }();
                layui.each(node, function (index, item) {
                    if (version == "WLTGY") {
                        if (item.name == "果易手机版权限") {
                            var child = [];
                            for (var i = 0; i < mbname.length; i++) {
                                child.push({
                                    name: mbname[i],
                                    val: mbval[i]
                                });
                            }
                            node[index].children = child;
                        }
                        if (item.name == "果易服务端权限") {
                            var child = [];
                            for (var i = 0; i < srname.length; i++) {
                                child.push({
                                    name: srname[i],
                                    val: srval[i]
                                });
                            }
                            node[index].children = child;
                        }
                    } else {
                        if (item.name == "物联通手机版权限") {
                            var child = [];
                            for (var i = 0; i < mbname.length; i++) {
                                child.push({
                                    name: mbname[i],
                                    val: mbval[i]
                                });
                            }
                            node[index].children = child;
                        }
                        if (item.name == "物联通服务端权限") {
                            var child = [];
                            for (var i = 0; i < srname.length; i++) {
                                child.push({
                                    name: srname[i],
                                    val: srval[i]
                                });
                            }
                            node[index].children = child;
                        }
                    }
                });
                return node;
            };

            layui.tree({
                skin: 'shihuang',
                elem: '#demo1', //指定元素
                nodes: createTree(),
                click: function (node) {
                    var parid = node.val;
                    $('#WLTRightsTable').bootstrapTable('refresh', {
                        url: '../WLTManage/GetWLTRightsTableList?LoginID=' + '@ViewBag.LoginID' + '&parid=' + parid
                    })
                }
            });
        });
    </script>
</body>