var dataTable = null;


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

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

function setTableData(data) {
    var tableData = '';

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

    for (var i = 0; i < data.length; i++) {
        tableData += '["' + data[i].name + '","'
            + data[i].path + '","'
            + data[i].menuName + '","'
            + data[i].updateTime + '","'
            + data[i].menuDes + '","' + '' + '","' + data[i].guid + '","' + data[i].parentNode + '","' + data[i].menuType +
            '"],';
    }

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

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": "10%"},
            {"width": "20%"},
            {"width": "10%"},
            {"width": "15%"},
            {"width": "20%"},
            {"width": "30%"}
        ],
        dom: 'rtip',
        pageLength: length,
        language: {
            url: '/pqs9900/json/chine.json',
            "sEmptyTable": "暂无内容"
        },
        createdRow: function (row, data, index) {
            var $btnView = $('<button type="button" data-toggle="modal" data-target="#addModal" class="btn themeBGColor btn-view"><i class="glyphicon glyphicon-plus"></i>新增节点</button><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(5).append($btnView);
        }
    });
    new $.fn.dataTable.Buttons(dataTable, {
        buttons: [
            {
                extend: 'csv',//导出格式
                title: '菜单资源',//导出csv名称
                exportOptions: {
                    columns: [0, 1, 2, 3, 4],//导出需要的列
                    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", ".btn-view", function () {
    var item = dataTable.row($(this).closest('tr')).data();
    popus(1);
    setData(1, item);
});

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

    if (item[8] === "#") { //资源类型为#
        layer.msg("根节点无法编辑！");
        return;
    }

    popus(2);
    setData(2, item);
});

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

    if (item[8] === "#") { //资源类型为#
        layer.msg("根节点无法删除！");
        return;
    }

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

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

function popus(type) {
    var title = '新增资源';

    if (type == 2) {
        title = '编辑资源';
    }

    layer.open({
        type: 2,
        title: title,
        shadeClose: false,
        shade: 0.3,
        area: ['370px', '420px'],
        maxmin: false,
        content: ['addChildMenu', 'no'],
        end: function () {
            layer.close();
        }
    });
}

var data = {};

function setData(type, item) {
    if (type == 1) {
        data.name = '';
        data.parentName = item[0];
        data.guid = item[6];
        data.id = "";
    } else if (type == 2) {
        data.name = item[0];
        data.parentGuid = item[7];  //父节点的guid
        data.des = item[4];
        data.path = item[1];
        data.menuType = item[8];
        data.id = item[6];
    }
}

function getMenuData() {
    return data;
}

function reloadTable() {
    getTreeData();
}

$(function () {
    $("#menuConfig").click(function () {
        layer.open({
            type: 2,
            title: '资源配置',
            shadeClose: false,
            shade: 0.3,
            area: ['100%', '100%'],
            maxmin: false,
            content: ['menuConfig', 'no'],
            end: function () {
                layer.close();
            },
            success: function (layero, index) {
                var iframeWin = window[layero.find('iframe')[0]['name']];
                dd = iframeWin.getRoleFunctionData();
                reloadMenuConfig = iframeWin.getMenuPloyList;
            }
        });
    });
});

var dd;
var reloadMenuConfig;

function getMenuConfigData() {
    return dd;
}