$(function () {
    getMonitorType();

    getData(1);
    initValue("area");
    initValue("voltage");
    initValue("factory");
    initValue("type");
    initValue("appear");
    initValue("nature");
    setCSS();

    $('#area').on('changed.bs.select', function (e, clickedIndex, isSelected, previousValue) {
        if (clickedIndex != null) {
            var value = document.getElementById("area").options[clickedIndex].value;

            if ((value === '-1' && previousValue.length > 0 && previousValue[0] != '-1') || (value != '-1' && previousValue[0] === '-1')) { //清空并重新选中
                $('#area').selectpicker('deselectAll');
                $('#area').selectpicker('val', value);
                $('#area').selectpicker('refresh');
            }
        }
    });

    $('#factory').on('changed.bs.select', function (e, clickedIndex, isSelected, previousValue) {
        if (clickedIndex != null) {
            var value = document.getElementById("factory").options[clickedIndex].value;

            if ((value === '-1' && previousValue.length > 0 && previousValue[0] != '-1') || (value != '-1' && previousValue[0] === '-1')) { //清空并重新选中
                $('#factory').selectpicker('deselectAll');
                $('#factory').selectpicker('val', value);
                $('#factory').selectpicker('refresh');
            }
        }
    });

    $('#voltage').on('changed.bs.select', function (e, clickedIndex, isSelected, previousValue) {
        if (clickedIndex != null) {
            var value = document.getElementById("voltage").options[clickedIndex].value;

            if ((value === '-1' && previousValue.length > 0 && previousValue[0] != '-1') || (value != '-1' && previousValue[0] === '-1')) { //清空并重新选中
                $('#voltage').selectpicker('deselectAll');
                $('#voltage').selectpicker('val', value);
                $('#voltage').selectpicker('refresh');
            }
        }
    });

    $('#type').on('changed.bs.select', function (e, clickedIndex, isSelected, previousValue) {
        if (clickedIndex != null) {
            var value = document.getElementById("type").options[clickedIndex].value;

            if ((value === '-1' && previousValue.length > 0 && previousValue[0] != '-1') || (value != '-1' && previousValue[0] === '-1')) { //清空并重新选中
                $('#type').selectpicker('deselectAll');
                $('#type').selectpicker('val', value);
                $('#type').selectpicker('refresh');
            }
        }
    });

    $('#query').click(function () {
        getData(0);
    });

    $("#export").click(function () {
        window.location.href = "/pqs9000/area/exportExcel";
    });
});

function setCSS() {
    //$('.bootstrap-select').find('button').eq(0).addClass('form-control');dropdown-toggle
    $('.dropdown-toggle').addClass('form-control');
}

function getOptions(id) {
    var data = $('#' + id).selectpicker('val');
    var array = '';

    for (var i = 0; i < data.length; i++) {
        array += data[i] + ',';
    }

    return array;
}

function getData(b) {
    if (b === 0) {
        var area = getOptions('area');
        var voltage = getOptions('voltage');
        var factory = getOptions('factory');
        var type = getOptions('type');
        var appear = $('#appear').selectpicker('val');
        var nature = $('#nature').selectpicker('val');
    }

    $.ajax({
        type: "POST",
        url: "/pqs9000/area/getledger",
        dataType: 'json',
        data: {
            area: area,
            voltage: voltage,
            factory: factory,
            type: type,
            appear: appear,
            nature: nature
        },
        success: function (data) {
            if (data.code === 500) {
                initTable([]);
            }

            if (b === 1) {
                initOption(data.body);
            }
            translateData(data.body);
        }
    })
}

function optionObj(index, name) {
    this.index = index;
    this.name = name;
}

function initOption(data) {
    //区域
    for (var i = 0; i < data.length; i++) {
        $('#area').append("<option value='" + data[i].areaIndex + "'>" + data[i].areaName + "</option>");
    }

    //电压等级、终端厂家、干扰源类型终
    var voltage = [];
    var factory = [];
    var type = [];
    for (var i = 0; i < data.length; i++) {
        for (var j = 0; j < data[i].list.length; j++) {
            if (data[i].list[j].name === '电压等级') {
                for (var k = 0; k < data[i].list[j].list.length; k++) {
                    if (voltage.length === 0) {
                        $('#voltage').append("<option value='" + data[i].list[j].list[k].dicIndex + "'>" + data[i].list[j].list[k].dicName + "</option>");
                        voltage.push(new optionObj(data[i].list[j].list[k].dicIndex, data[i].list[j].list[k].dicName));
                    }

                    for (var l = 0; l < voltage.length; l++) {
                        if (voltage[l].index != data[i].list[j].list[k].dicIndex) {
                            if (l == voltage.length - 1) {
                                $('#voltage').append("<option value='" + data[i].list[j].list[k].dicIndex + "'>" + data[i].list[j].list[k].dicName + "</option>");
                                voltage.push(new optionObj(data[i].list[j].list[k].dicIndex, data[i].list[j].list[k].dicName));
                            }

                        } else {
                            break;
                        }
                    }
                }
            } else if (data[i].list[j].name === '终端厂家') {
                for (var k = 0; k < data[i].list[j].list.length; k++) {
                    if (factory.length === 0) {
                        $('#factory').append("<option value='" + data[i].list[j].list[k].dicIndex + "'>" + data[i].list[j].list[k].dicName + "</option>");
                        factory.push(new optionObj(data[i].list[j].list[k].dicIndex, data[i].list[j].list[k].dicName));
                    }

                    for (var l = 0; l < factory.length; l++) {
                        if (factory[l].index != data[i].list[j].list[k].dicIndex) {
                            if (l == factory.length - 1) {
                                $('#factory').append("<option value='" + data[i].list[j].list[k].dicIndex + "'>" + data[i].list[j].list[k].dicName + "</option>");
                                factory.push(new optionObj(data[i].list[j].list[k].dicIndex, data[i].list[j].list[k].dicName));
                            }
                        } else {
                            break;
                        }
                    }
                }
            } else if (data[i].list[j].name === '干扰源类型') {
                for (var k = 0; k < data[i].list[j].list.length; k++) {
                    if (type.length === 0) {
                        $('#type').append("<option value='" + data[i].list[j].list[k].dicIndex + "'>" + data[i].list[j].list[k].dicName + "</option>");
                        type.push(new optionObj(data[i].list[j].list[k].dicIndex, data[i].list[j].list[k].dicName));
                    }

                    for (var l = 0; l < type.length; l++) {
                        if (type[l].index != data[i].list[j].list[k].dicIndex) {
                            if (l == type.length - 1) {
                                $('#type').append("<option value='" + data[i].list[j].list[k].dicIndex + "'>" + data[i].list[j].list[k].dicName + "</option>");
                                type.push(new optionObj(data[i].list[j].list[k].dicIndex, data[i].list[j].list[k].dicName));
                            }
                        } else {
                            break;
                        }
                    }
                }
            }
        }
    }

    $('#area').selectpicker('refresh');
    $('#voltage').selectpicker('refresh');
    $('#factory').selectpicker('refresh');
    $('#type').selectpicker('refresh');
}

function initValue(id) {
    $("#" + id).selectpicker('val', '-1');
}

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

    for (var i = 0; i < data.length; i++) {
        for (var j = 0; j < data[i].list.length; j++) {
            for (var k = 0; k < data[i].list[j].list.length; k++) {
                tableData += '["","' + data[i].areaName + '", "' + data[i].list[j].name + '","'
                    + data[i].list[j].list[k].dicName + '","' + data[i].list[j].list[k].gdCount + '","' + data[i].list[j].list[k].subCount + '","'
                    + data[i].list[j].list[k].lineCount + '","';

                if (k === 0) {
                    var n = 0;

                    if (j === 0) {
                        for (var l = 0; l < data[i].list.length; l++) {
                            for (var m = 0; m < data[i].list[l].list.length; m++) {
                                n++;
                            }
                        }
                    }

                    tableData += n + '","' + data[i].list[j].list.length + '"],';
                } else {
                    tableData += '0","0"],'; //前面表示所有行，后面表示自己的行
                }
            }
        }
    }

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

    initTable(tableData);
}

function initTable(data) {
    $('#tongji').dataTable({
        "columns": [
            {"title": "序号"},
            {"title": "区域"},
            {"title": "类型"},
            {"title": "分类型"},
            {"title": "变电站个数"},
            {"title": "终端个数"},
            {"title": "监测点个数"}
        ],
        paging: false,
        scrollY: ($(window).height() * 0.83),
        lengthChange: false,
        language: {
            url: '/pqs9000/json/chine.json',
        },
        searching: false,
        info: false,
        ordering: false,
        destroy: true,
        data: data,
        fnDrawCallback: function () {
            this.api().column(0).nodes().each(function (cell, i) {
                cell.innerHTML = i + 1;
            });
        },
        columnDefs: [{
            targets: 1,
            createdCell: function (td, cellData, rowData, row, col) {
                var rowspan = rowData[7];

                if (rowspan > 1) {
                    $(td).attr('rowspan', rowspan)
                }
                if (rowspan == 0) {
                    $(td).remove();
                }
            }
        }, {
            targets: 2,
            createdCell: function (td, cellData, rowData, row, col) {
                var rowspan = rowData[8];

                if (rowspan > 1) {
                    $(td).attr('rowspan', rowspan)
                }
                if (rowspan == 0) {
                    $(td).remove();
                }
            }
        }]
    });
}

let areaName;
function getMonitorType(){
    $.ajax({
        type: "POST",
        url: "/pqs9000/theme/getThemeInfo",
        async: false,
        data: {},
        dataType: 'json',
        success: function (data) {
            areaName = data.body.name;
            var name = data.body.name + "上报";
            var name2 = "非" + data.body.name + "上报";
            loadColor = data.body.value;
            var option = $("<option value='0'>" + name + "</option>");
            var option2 = $("<option value='1'>" + name2 + "</option>");
            $('#appear').append(option);
            $('#appear').append(option2);
        }
    })
}
