/**
 * 数据字典
 */
var typeName = $("#sel").val();
var i;
var dataTable = null;
var tyIndex = typeId;
var resultData = null;

/**
 * 获取通讯字典的index
 * @param index
 */
function splitTree() {
    var index;
    index = getname();
    return index;
}

//添加通用字典
$("#addType").click(function () {
    layer.open({
        type: 2,
        title: '新增通用字典',
        shadeClose: true,
        resize: false,
        shade: 0.3,
        area: ['20%', '53%'],
        content: 'addTypeData', //iframe的url
        scrollbar: false,
        end: function () {
            parent.close();
        }
    });
});
$(document).ready(function () {
    //添加数据字典
    $("#add").click(function () {
        layer.open({
            type: 2,
            title: '新增字典',
            shadeClose: true,
            resize: false,
            shade: 0.3,
            area: ['25%', '60%'],
            content: 'addData', //iframe的url
            scrollbar: false,
            end: function () {
                parent.close();
            }
        });
    });
    //模糊查询
    $("#selRoleUserName").click(function () {
        typeName = $("#sel").val();
        lo();
    });
});
lo();

function lo() {
    //查询所有
    $.ajax({
        type: "post",
        url: "/pqs9900/dic/seleAll",
        data: {
            name: typeName
        },
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_LAYER();
        },
        success: function (data) {
            initTable(data);
            resultData = data;
            startQuery();
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER(i);
        }
    })
}

function initTable(data) {
    var height = $(window).height() - 238;
    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 = $("#dicTyId").DataTable();
    dataTable.clear().draw();
    dataTable = $('#dicTyId').DataTable({
        order: [],
        columnDefs: [
            {
                orderable: false,
                targets: [4, 5]
            }
        ],
        lengthChange: false,
        bDestroy: true,
        data: data.body,
        destroy: true,
        searching: true,
        dom: 'rtip',
        pageLength: length,
        "columns": [
            {"width": "15%"},
            {"width": "8%"},
            {"width": "15%"},
            {"width": "10%"},
            {"width": "6%"},
            {"width": "12%"}
        ],
        data: data.body,
        language: {
            url: '/pqs9900/json/chine.json'
        },
        'fnDrawCallback': function (oTable) {
            $("#dicTyId_paginate").append("<div style='float:right;padding-top:4px;padding-left:10px'>到第 <input type='text' id='changePage' class='input-text' style='width:50px;height:28px;'> 页  <button class='btn btn-primary' id='dataTable-btn'>确定</button></div>");
            var table = $("#dicTyId").dataTable();
            $('#dataTable-btn').click(function (e) {
                if ($("#changePage").val() && $("#changePage").val() > 0) {
                    var redirectpage = $("#changePage").val() - 1;
                } else {
                    var redirectpage = 0;
                }
                table.fnPageChange(redirectpage);
            });
        }
    });
    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);
}

//修改通用字典
$("#updateType").click(function () {
    var username = $("#username").val();
    confirmIndentity(username, function () {
        $("#dicTypeIndex").val(splitTree());
        var dicTypeIndex = typeId;
        if (dicTypeIndex != 0) {
            layer.open({
                type: 2,
                title: '修改通用字典',
                shadeClose: true,
                resize: false,
                shade: 0.3,
                area: ['25%', '53%'],
                content: 'updateType', //iframe的url
                scrollbar: false,
                end: function () {
                    layer.closeAll();
                }
            });
        } else {
            layer.msg('未选择字典类型！', {icon: 2, time: 1000});
        }
    })
})
//删除字典类型
$("#deleteType").click(function () {
    var username = $("#username").val();
    confirmIndentity(username, function () {
        if (typeId == 0) {
            layer.msg('未选择字典类型！', {icon: 2, time: 1000});
        } else {
            layer.confirm('确定删除？', function (index) {
                $.ajax({
                    method: "post",
                    url: "/pqs9900/type/delete.do",
                    data: {"dicTypeIndex": typeId},
                    success: function (data) {
                        if (data == 1) {
                            layer.msg('已删除!', {icon: 1, time: 1000}, function () {
                                getTree();
                                cc(splitTree());
                            });
                        } else if (data == 2) {
                            layer.msg('删除失败！该通用字典下还有其他信息', {icon: 2, time: 1000});
                        } else {
                            layer.msg('删除失败！', {icon: 2, time: 1000});
                        }
                    }
                })
            })
        }
    })
})

function updateDataByIndex(index) {
    var username = $("#username").val();
    confirmIndentity(username, function () {
        $("#dicTypeIndex").val(index);
        var dicTypeIndex = index;
        layer.open({
            type: 2,
            title: '修改数据字典',
            shadeClose: true,
            resize: false,
            shade: 0.3,
            area: ['28%', '60%'],
            content: 'updateData', //iframe的url
            scrollbar: false,
            end: function () {
                close();
            }
        });
    })
}

function deleteDataByIndex(index) {
    var username = $("#username").val();
    confirmIndentity(username, function () {
        var dicIndex = index;
        layer.confirm('确定删除？', function (index) {
            $.ajax({
                type: "POST",
                url: "/pqs9900/dic/deleteDataByIndex",
                dataType: "text",
                data: {
                    index: dicIndex
                },
                success: function (data) {
                    var info = $.parseJSON(data);
                    if (info.code == 500) {
                        //删除字典异常、或者失败
                        layer.msg(info.message, {
                            icon: 5,
                            time: 1000
                        }, function () {
                            if (splitTree() == "") {
                                lo();
                            } else {
                                cc(splitTree());
                            }
                        });
                    } else {
                        //删除字典成功
                        layer.msg(info.message, {
                            icon: 6,
                            time: 1000
                        }, function () {
                            if (splitTree() == "") {
                                lo();
                            } else {
                                cc(splitTree());
                            }
                        });
                    }
                }

            });
            layer.close(index);
        })
    })
}

//搜索表中的数据
function searchResult(value) {
    dataTable.search(value).draw();
}

//特殊处理，表头紧缩问题--start
var it = null;

function startQuery() {
    //实时刷新时间单位为毫秒
    it = setInterval('refreshQuery()', 100);

}

/* 刷新查询 */
function refreshQuery() {
    var width = $("#dicTyId").width();
    if (width > 100) {
        initTable(resultData);
        window.clearInterval(it)
    }
}

//特殊处理，表头紧缩问题--end

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]);
}