var dataTable = null;


//搜索框内容改变结果记录
var searchValue = "";

function valueChange(value) {
    searchValue = value;
    dataTable.search(value).draw();
}

function getMenuPloyList() {
    $.ajax({
        url: "/pqs9900/sysset/menuPloyList",
        type: "post",
        async: false,
        dataType: "json",
        success: function (data) {
            var tableData = '';

            if (data == null) {
                initTable([]);
                return;
            }

            for (var i = 0; i < data.body.length; i++) {
                tableData += '["' + data.body[i].roleName + '","'
                    + data.body[i].name + '","'
                    + data.body[i].updateTime + '","'
                    + data.body[i].des + '","' + '' + '","' + data.body[i].roleId + '","' + data.body[i].id + '"],';
            }

            tableData = tableData.substring(0, tableData.lastIndexOf(','));
            tableData = eval('[' + tableData + ']');
            initTable(tableData);
        }
    });
}

$(function () {
    getMenuPloyList();

    $("#addConfig").click(function () {
        topDlg(1);
        setRoleFunctionData(1, {});
    });
});

function topDlg(type) {
    var title = "新增配置";

    if (type == 2) {
        title = "编辑配置";
    }

    parent.layer.open({
        type: 2,
        title: title,
        shadeClose: false,
        shade: 0.3,
        area: ['60%', '100%'],
        maxmin: false,
        content: ['addConfig', 'no'],
        end: function () {
            layer.close();
        }
    });
}

function initTable(data) {
    var height = $(window).height() - 155;
    var length = Math.floor(height / 28);
    if (length < 1) {
        length = 1;
    }
    // var heightTemp = screen.height;
    // var length;
    // if (heightTemp === 768) {
    //     length = 10;
    // } else if (heightTemp === 1024) {
    //     length = 20;
    // } else if (heightTemp === 960) {
    //     length = 15;
    // } else if (heightTemp === 1050) {
    //     length = 18;
    // } else {
    //     length = 15;
    // }

    dataTable = $('#example').DataTable({
        data: data,
        lengthChange: false,
        info: false,
        paging: false,
        destroy: true,
        searching: true,
        ordering: false,
        scrollY: document.getElementById("right").offsetHeight - document.getElementById("search").offsetHeight - 3 - 42 + 'px',
        columns: [
            {"width": "15%"},
            {"width": "20%"},
            {"width": "15%"},
            {"width": "30%"},
            {"width": "20%"}
        ],
        dom: 'rtip',
        pageLength: length,
        language: {
            url: '/pqs9900/json/chine.json',
            "sEmptyTable": "暂无内容"
        },
        createdRow: function (row, data, index) {
            var $btnView = $('<button type="button" class="btn btn-danger ml10"><i class="glyphicon glyphicon-trash"></i>删除</button><button type="button" class="btn themeBGColor updateData ml10"><i class="glyphicon glyphicon-edit"></i>编辑</button>');
            $('td', row).eq(4).append($btnView);
        }
    });
    new $.fn.dataTable.Buttons(dataTable, {
        buttons: [
            {
                extend: 'csv',//导出格式
                title: '角色菜单模板',//导出csv名称
                exportOptions: {
                    columns: [0, 1, 2, 3],//导出需要的列
                    format: {
                        body: function (data) {//对导出内容的筛选 data 表格内容 rid 行索引 cid 列索引
                            return data;
                        }
                    }
                }
            }
        ]
    });

    modifyCss(dataTable);
}

function modifyCss(dataTable) {
    dataTable.buttons().container().css("display", "inline-block");
    dataTable.buttons().container().css("float", "right");
    dataTable.buttons().container().css("top", "4px");
    dataTable.buttons().container().find("a").prepend("<i class='glyphicon glyphicon-share'></i>");
    dataTable.buttons().container().appendTo('.header');
    dataTable.buttons().container().after($("#fliteData")[0]);
}

$('#example').on("click", ".updateData", function () {
    var item = dataTable.row($(this).closest('tr')).data();

    topDlg(2);
    setRoleFunctionData(2, item);

});

var roleFunctionData = {};

function setRoleFunctionData(type, item) {
    if (type == 1) {
        roleFunctionData.id = "";
    } else {
        roleFunctionData.roleId = item[5];
        roleFunctionData.id = item[6];
        roleFunctionData.name = item[1];
        roleFunctionData.des = item[3];
    }
}

function getRoleFunctionData() {
    return roleFunctionData;
}

$('#example').on("click", ".btn-danger", function () {
    var item = dataTable.row($(this).closest('tr')).data();

    layer.confirm('确认删除？', function (index) {
        layer.close(index);

        $.ajax({
            url: "/pqs9900/sysset/deleteMenuPloy",
            type: "post",
            async: false,
            dataType: "json",
            data: {
                id: item[6],
                roleId: item[5]
            },
            success: function (data) {
                if (data.code == 200) {
                    layer.msg("删除成功");
                    getMenuPloyList();
                } else {
                    layer.msg("删除失败");
                }
            }
        });
    });
});