
layui.use(['table', 'jquery'], function () {
    var table = layui.table;
    var form = layui.form,
       layer = parent.layer === undefined ? layui.layer : parent.layer,
       $ = layui.jquery;
    //监听表格复选框选择
    table.on('checkbox(demo)', function (obj) {
        console.log(obj)
    });
    //监听工具条
    table.on('tool(demo)', function (obj) {
        var data = obj.data;
        if (obj.event === 'edit') {
            layer.open({
                type: 2,
                anim: 4,
                scrollbar: false,
                content: '/SystemManage/RoleEdit/?roleId=' + data.F_RoleId,
                area: ['800px', '400px'],
                id: 'RoleEdit',
                title: data.F_RoleName
                , end: function () {
                    RefushTableList();

                }
            });
        }

        else if (obj.event === 'accredit') {
            layer.open({
                type: 2,
                content: '/SystemManage/MenuTree/?rootCode=' + data.F_RoleId,
                area: ['500px', '500px'],
                id: 'MenuTree',
                title: '授权'

            })
        }
        else if (obj.event === 'delete') {
            //layer.msg('ID：' + data.ProjectKey + ' 的查看操作');
            layer.confirm('确认要删除该行数据么？', function (index) {
                $.ajax({
                    url: "/SystemManage/DeleteRoleData",
                    type: "get",
                    async: false,//同步请求
                    contentType: "application/json",
                    dataType: "text",
                    cache: false,
                    data: "roleId=" + data.F_RoleId,
                    success: function (thisResult, result) {

                        if (thisResult == "True") {
                            layer.open({
                                content: '删除成功'
                                 , icon: 1
                                 , btn: ['确定']
                                 , yes: function (index, layero) {

                                     layer.close(index);
                                     RefushTableList();

                                 }
                            });
                        } else {
                            layui.layer.alert("删除失败!", {
                                icon: 2
                            });

                        }
                    },
                    error: function (error) {
                        return false;
                    }
                });
                layer.close(index);
            });
        }
    });
    var $ = layui.$, active = {
        //新增
        rowAdd: function () {
            layer.open({
                type: 2,
                anim: 4,
                scrollbar: false,
                content: '/SystemManage/RoleEdit/',
                area: ['800px', '400px'],
                id: 'RoleEdit',
                title: '新增角色'
             , end: function () {

                 RefushTableList();
             }
            });

        }
        //编辑
        , rowEdit: function () {

            var checkStatus = table.checkStatus('recordtable')
                   , data = checkStatus.data;
            if (data.length == 0) {
                layui.layer.alert("请先选中要操作的数据行！", {
                    icon: 3
                });
            }
            else {
                layer.open({
                    type: 2,
                    anim: 4,
                    scrollbar: false,
                    content: '/SystemManage/RoleEdit/?roleId=' + data[0].F_RoleId,
                    area: ['800px', '400px'],
                    id: 'RoleEdit',
                    title: '编辑角色'
                        , end: function () {

                            RefushTableList();

                        }
                });
            }
        }
        //删除
        , rowDelete: function () {

            var checkStatus = table.checkStatus('recordtable')
                   , data = checkStatus.data;
            if (data.length == 0) {
                layui.layer.alert("请先选中要操作的数据行！", {
                    icon: 3
                });
            }
            else {

                layer.confirm('确认要删除该行数据么？', function (index) {

                    var strId = "";
                    for (var i = 0; i < data.length; i++) {
                        if (i == 0) {
                            strId += +data[i].F_RoleId;
                        }
                        else {
                            strId += "," + data[i].F_RoleId;
                        }
                    }

                    $.ajax({
                        url: "/SystemManage/DeleteRoleData",
                        type: "get",
                        async: false,//同步请求
                        contentType: "application/json",
                        dataType: "text",
                        cache: false,
                        data: "roleId=" + strId,
                        success: function (thisResult, result) {

                            if (thisResult == "True") {
                                layer.open({
                                    content: '删除成功'
                                     , icon: 1
                                     , btn: ['确定']
                                     , yes: function (index, layero) {

                                         layer.close(index);
                                         RefushTableList();

                                     }
                                });
                            } else {
                                layui.layer.alert("删除失败!", {
                                    icon: 2
                                });

                            }
                        },
                        error: function (error) {
                            return false;
                        }
                    });
                    layer.close(index);
                });
            }

        }
    };

    $('.layui-btn-group .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    //刷新表
    function RefushTableList() {
        //执行重载
        table.reload('recordtable', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
          , where: {
              NowDateTime: new Date()

          }
        });
    }


});