@{
    ViewBag.Title = "权限设置";
    Layout = "~/Views/Shared/_LayoutTable.cshtml";
}
<style>
</style>
<div style="width:20%;height:100%;background-color:white; float:left">
    <div>&nbsp;</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">
                <button id="btnSave" class="btn btn-default btn-sm" onclick="Save()"><i class="glyphicon glyphicon-saved"></i> 保 存</button>
            </td>
        </tr>
    </table>
    <table id="RightsTable"></table>
</div>
<script type="text/javascript">
    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: "物联通手机版权限"}, { name: "物联通服务端权限"}];
                return arr;
            }();
            layui.each(node, function (index, item) {
                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;
                $('#RightsTable').bootstrapTable('refresh', {
                    url: '../DealerEdition/GetRightsTableList?LoginID=' + '@ViewBag.LoginID' + '&parid=' + parid
                })
            }
        });
    });
</script>
<script type="text/javascript">
    $(function () {
        $('#RightsTable').bootstrapTable({
            url: '../DealerEdition/GetRightsTableList?LoginID=' + '@ViewBag.LoginID',         //请求后台的URL（*）
            method: 'post',                      //请求方式（*）
            toolbar: '',                //工具按钮用哪个容器
            striped: true,                      //是否显示行间隔色
            pagination: false,                   //是否显示分页（*）
            sortOrder: "asc",                   //排序方式
            minimumCountColumns: 2,  //最少允许的列数
            sidePagination: "server",
            //height: "636",               //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
            uniqueId: "",            //每一行的唯一标识，一般为主键列
            clickToSelect: true,              //是否启用点击选中行
            sortable: true,                     //是否启用排序
            silentSort: true,
            cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            search: false,                       //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
            strictSearch: false,
            showColumns: false,            //是否显示所有的列
            showRefresh: false,              //是否显示刷新按钮
            showToggle: false,                     //是否显示详细视图和列表视图的切换按钮
             cardView: false,                    //是否显示详细视图
            detailView: false,                   //是否显示父子表
            columns: [
                {
                    checkbox: true, field: 'che', formatter: function (value, row, index) {
                        if (row["LimitValue"] == "1") {
                            return true;
                        } else {
                            return false;
                        }
                    }
                },
                {
                    field: "LimitName", title: "权限名称"
                },
                { field: 'LimitValue', title: 'LimitValue' },
            { field: 'LimitType', title: 'LimitType' }
            ],
            onCheck: function (row) {
                var selval = "";
                selval = row.LimitType + ":" + '@ViewBag.LoginID' + ":" + "1";
                $.ajax({
                    url: "../DealerEdition/UpdateDealerRights",
                    dataType: "text",
                    data: { selval: selval },
                    type: "post",
                    success: function (data) {//删除成功后刷新table
                    },
                    error: function (data) {
                        layer.msg(data, { icon: 5, time: 2000 });
                    }
                })
            },
            onUncheck: function (row) {
                var selval = "";
                selval = row.LimitType + ":" + '@ViewBag.LoginID' + ":" + "0";
                $.ajax({
                    url: "../DealerEdition/UpdateDealerRights",
                    dataType: "text",
                    data: { selval: selval },
                    type: "post",
                    success: function (data) {//删除成功后刷新table
                    },
                    error: function (data) {
                        layer.msg(data, { icon: 5, time: 2000 });
                    }
                })
            }
            , 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: "../DealerEdition/UpdateDealerRights",
                    dataType: "text",
                    data: { selval: nsel },
                    type: "post",
                    success: function (data) {//删除成功后刷新table
                    },
                    error: function (data) {
                        layer.msg(data, { icon: 5, time: 2000 });
                    }
                })
            }, 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: "../DealerEdition/UpdateDealerRights",
                    dataType: "text",
                    data: { selval: nsel },
                    type: "post",
                    success: function (data) {//删除成功后刷新table
                    },
                    error: function (data) {
                        layer.msg(data, { icon: 5, time: 2000 });
                    }
                })
            }
        });
        $('#RightsTable').bootstrapTable('hideColumn', 'LimitValue');
        $('#RightsTable').bootstrapTable('hideColumn', 'LimitType');
    });
    function Save() {
        var data = $('#RightsTable').bootstrapTable('getData');
        for (var i = 0; i < data.length; i++) {
            if (data[i]["che"]) {
                data[i]["LimitValue"] = "1";
            } else {
                data[i]["LimitValue"] = "0";
            }

        }
        var nsel = "";
        for (var i = 0; i < data.length; i++) {
            if (i == data.length - 1)
            { nsel += data[i].LimitType + ":" + '@ViewBag.LoginID' + ":" + data[i].LimitValue; }
            else
            { nsel += data[i].LimitType + ":" + '@ViewBag.LoginID' + ":" + data[i].LimitValue + ","; }
        }
        $.ajax({
            url: "../DealerEdition/UpdateDealerRights",
            dataType: "text",
            data: { selval: nsel },
            type: "post",
            success: function (data) {//删除成功后刷新table
                if (data == "保存成功！") {
                    layer.msg(data, { time: 2000 });
                } else {
                    layer.msg(data, { time: 2000 });
                }
            },
            error: function (data) {
                layer.msg(data, { icon: 5, time: 2000 });
            }
        })
    }
</script>