var loading;
var resultData;
var height = $(window).height() - 140;
$(function () {
    initselect()
    initTableData()

})


$("#query").click(function () {
    initTableData()
});

$("#batchImport").click(function () {
    var op = layer.open({
        type: 1,
        title: '批量导入监测点评级',
        shadeClose: true,
        resize: false,
        area: ['20%', '32%'],
        content: '<div style="padding: 15px"><button class="btn btn-primary" id="import">选择文件</input></div>' +
            '<div style="position:absolute;bottom:10px;width: 100%;text-align: center" ><button style="margin: 0px auto" class="btn btn-primary" id="upfile">提交</input></div>',
        success: function () {
            layui.use('upload', function () {
                var upload = layui.upload;

                //执行实例
                var uploadInst = upload.render({
                    elem: '#import' //绑定元素
                    , url: '/pqs9900/lineWeightMark/batchImportLineWeight' //上传接口
                    , accept: 'file' //普通文件
                    , exts: 'xls|excel|xlsx|csv' //只允许上传压缩文件
                    , auto: false
                    , bindAction: '#upfile' //
                    , done: function (res) {
                        if (res.code == 200) {
                            layer.msg(res.message, {icon: 1}, function () {
                                layer.close(op);
                                initTableData()
                            })

                        } else {
                            layer.msg(res.message, {icon: 2})
                        }

                    }
                    , error: function () {
                        //请求异常回调
                    }
                });
            })
        }
    });

});


function initTableData() {

    let devFlag = $("#devFlag").val()
    let lineGrade = $("#lineGrade").val()

    $.ajax({
        type: "get",
        url: "/pqs9900/lineWeightMark/getLineMarkWeight",
        dataType: 'json',
        data: {devFlag: devFlag, lineGrade: lineGrade},
        beforeSend: function () {
            loading = ityzl_SHOW_LOAD_LAYER();
        },
        success: function (data) {
            if (data.code === 500) {
                layer.msg(data.message, {icon: 2, time: 2000})
                resultData = null;
            } else {
                resultData = data.body;
            }
            initTable(resultData);
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER(loading);
        }

    })
}

//实例化表格
function initTable(data) {

    dataTable = $("#lineLedger").DataTable();
    dataTable.clear().draw();
    dataTable = $('#lineLedger').DataTable({
        order: [],
        columnDefs: [
            {
                orderable: false,
                targets: [0]
            }
        ],
        columns: [
            {width: "3%", data: null},
            {width: "7%", data: "province"},
            {width: "7%", data: "gdName"},
            {width: "15%", data: "subName"},
            {width: "10%", data: "devName"},
            {width: "12%", data: "lineName"},
            {
                width: "5%", data: "devflag", render: function (data) {
                    if (data == 0) {
                        return "投运"
                    } else if (data == 1) {
                        return "检修"
                    } else {
                        return "退运"
                    }
                }
            },

            {width: "3%", data: "lineGrade"},
            {width: "3%", data: "communFeeMark"},
            {width: "3%", data: "assetBelong"},
            {width: "3%", data: "serviceMark"},
            {width: "3%", data: "agentMark"},
            {width: "3%", data: "companyMark"},
            {width: "3%", data: "userMark"},
            {width: "3%", data: "lineTypeMark"},
            {width: "3%", data: "businessMark"},
            {
                width: "10%", render: function (data, type, full, meta) {
                    return "<button type='button' class='btn themeBGColor'  onclick='gradeOperate(\"" + full.lineIndex + "\",\"" + full.communFeeMark + "\",\"" + full.assetBelong + "\",\"" + full.serviceMark + "\",\"" + full.agentMark + "\",\"" + full.companyMark + "\",\"" + full.userMark + "\",\"" + full.lineTypeMark + "\",\"" + full.businessMark + "\")' >评分</button>"
                }
            }
        ],
        autoWidth: false,
        destroy: true,
        data: data,
        lengthChange: false,
        searching: true,
        dom: 'rtip',
        scrollY: height,
        scrollX: true,
        lengthMenu: [50],
        language: {
            url: '/pqs9900/json/chine.json'
        },
        createdRow: function (row, data, index) {
            if (data.devflag == 0) {
                $('td', row).eq(6).css('color', '#00ff04')
            } else if (data.devflag == 1) {
                $('td', row).eq(6).css('color', '#edca1c')
            } else if (data.devflag == 2) {
                $('td', row).eq(6).css('color', '#ED1C24')
            }

        },
        fnDrawCallback: function (oTable) {
            $("#lineLedger_info").addClass("fl");
            $("#lineLedger_paginate").addClass("fr");
            $("#lineLedger_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 = $("#lineLedger").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',
                text: '导出',//导出csv名称
                title: '监测点评分权重' + new Date().getTime(),
                exportOptions: {
                    columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,14,15],
                    format: {
                        body: function (data, rID, cID) {
                            if(cID == 0){
                                return rID+1;
                            }
                            return data;
                        }
                    }
                }
            }
        ]
    });


    modifyCss(dataTable);
    dataTable.on('order.dt search.dt',
        function () {
            dataTable.column(0, {
                search: 'applied',
                order: 'applied'
            }).nodes().each(function (cell, i) {
                cell.innerHTML = i + 1;
            });
        }).draw();

}

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

//评分操作
var op2;
var lineId;

function gradeOperate(lineIndex,communFeeMark,assetBelong,serviceMark,agentMark,companyMark,userMark,lineTypeMark,businessMark) {
    lineId = lineIndex;

    op2 = layer.open({
        type: 1,
        title: "权重评分",
        shadeClose: true,
        resize: false,
        area: ['30%', '70%'],
        content: '<div class="form-inline" style="padding: 5px;text-align: center">' +
        '<div class="col-xs-4 col-sm-4"><label style="margin-top: 4px" class="form-group">通讯费用评分：</label></div><input placeholder="0~100" id="communFeeMark" class="form-control"></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-4 col-sm-4"><label style="margin-top: 4px" class="form-group">资产归属：</label></div><input placeholder="0~100" id="assetBelong" class="form-control"></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-4 col-sm-4"><label style="margin-top: 4px" class="form-group">服务条款评分：</label></div><input placeholder="0~100" id="serviceMark" class="form-control"></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-4 col-sm-4"><label style="margin-top: 4px" class="form-group">中间户数据需求：</label></div><input placeholder="0~100" id="agentMark" class="form-control"></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-4 col-sm-4"><label style="margin-top: 4px" class="form-group">公司数据需求：</label></div><input placeholder="0~100" id="companyMark" class="form-control"></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-4 col-sm-4"><label style="margin-top: 4px" class="form-group">用户需求：</label></div><input placeholder="0~100" id="userMark" class="form-control"></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-4 col-sm-4"><label style="margin-top: 4px" class="form-group">监测点类型：</label></div><input placeholder="0~100" id="lineTypeMark" class="form-control"></div>' +
            '<div class="form-inline" style="padding: 5px;text-align: center">' +
            '<div class="col-xs-4 col-sm-4"><label style="margin-top: 4px" class="form-group">行业重要度：</label></div><input placeholder="0~100" id="businessMark" class="form-control"></div>' +
            '<div class="form-inline mt15" style="text-align: center"><button id="submit" class="btn btn-primary">提交</button></div>',
        success: function () {

            $("#communFeeMark").val(communFeeMark=='null' ? '':communFeeMark)
            $("#assetBelong").val(assetBelong=='null' ? '':assetBelong)
            $("#serviceMark").val(serviceMark=='null' ? '':serviceMark)
            $("#agentMark").val(agentMark=='null' ? '':agentMark)
            $("#companyMark").val(companyMark=='null' ? '':companyMark)
            $("#userMark").val(userMark=='null' ? '':userMark)
            $("#lineTypeMark").val(lineTypeMark=='null' ? '':lineTypeMark)
            $("#businessMark").val(businessMark=='null' ? '':businessMark)
        }
    })
}

//提交操作
$("body").on('click', "#submit", function () {
    let communFeeMark = $("#communFeeMark").val()
    let assetBelong = $("#assetBelong").val()
    let serviceMark = $("#serviceMark").val()
    let agentMark = $("#agentMark").val()
    let companyMark = $("#companyMark").val()
    let userMark = $("#userMark").val()
    let lineTypeMark = $("#lineTypeMark").val()
    let businessMark = $("#businessMark").val()

    let zz = /^((?!0)\d{1,2}|100)$/;
    if (!zz.test(communFeeMark)) {
        layer.tips('请输入0~100数字', '#communFeeMark', {
            tips: [1, 'red'] //还可配置颜色
        });
        return
    }
    if (!zz.test(assetBelong)) {
        layer.tips('请输入0~100数字', '#assetBelong', {
            tips: [1, 'red'] //还可配置颜色
        });
        return
    }
    if (!zz.test(serviceMark)) {
        layer.tips('请输入0~100数字', '#serviceMark', {
            tips: [1, 'red'] //还可配置颜色
        });
        return
    }
    if (!zz.test(agentMark)) {
        layer.tips('请输入0~100数字', '#agentMark', {
            tips: [1, 'red'] //还可配置颜色
        });
        return
    }
    if (!zz.test(companyMark)) {
        layer.tips('请输入0~100数字', '#companyMark', {
            tips: [1, 'red'] //还可配置颜色
        });
        return
    }
    if (!zz.test(userMark)) {
        layer.tips('请输入0~100数字', '#userMark', {
            tips: [1, 'red'] //还可配置颜色
        });
        return
    }
    if (!zz.test(lineTypeMark)) {
        layer.tips('请输入0~100数字', '#lineTypeMark', {
            tips: [1, 'red'] //还可配置颜色
        });
        return
    }
    if (!zz.test(businessMark)) {
        layer.tips('请输入0~100数字', '#businessMark', {
            tips: [1, 'red'] //还可配置颜色
        });
        return
    }


        $.ajax({
        type: "POST",
        url: "/pqs9900/lineWeightMark/singleOperate",
        data: JSON.stringify({lineIndex: lineId,
            communFeeMark: communFeeMark,
            assetBelong:assetBelong,
            serviceMark: serviceMark,companyMark: companyMark,
            agentMark: agentMark,userMark: userMark,
            lineTypeMark: lineTypeMark,businessMark: businessMark}),
        dataType: 'json',
        contentType: "application/json;charset=UTF-8",
        beforeSend: function () {
            loading = ityzl_SHOW_LOAD_LAYER()
        },
        success: function (data) {
            if (data.code == 200) {
                layer.msg(data.message, {icon: 1, time: 2000}, function () {
                    layer.close(op2);
                    initTableData()
                })
            } else {
                layer.msg(data.message, {icon: 2})
            }

            ityzl_CLOSE_LOAD_LAYER(loading)
        }
    })
})


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

$(document).on("click", "#templateExport", function () {
    var str = "序号,省份,供电公司,所属变电站,装置名称,监测点名称,通讯费用,资产归属,服务条款,中间户数据需求,公司数据需求,用户数据需求,监测点类型,行业重要度";


    var aaaa = "data:text/csv;charset=gbk,\ufeff" + str;
    var link = document.createElement("a");
    link.setAttribute("href", aaaa);
    link.setAttribute("download", "监测点权重模板" + ".csv");
    link.click();
});

$(document).on("click", "#stopLineMark", function () {
    $.ajax({
        method: "get",
        url: "/pqs9900/lineMark/batchUpdateLineMarkStop",
        dataType: 'json',
        success: function (data) {
            if (data.code == 200) {
                layer.msg(data.message, {icon: 1, time: 2000}, function () {
                    initTableData()
                })

            } else {
                layer.msg(data.message, {icon: 2})
            }

        }


    })
});

function initselect() {
    var option = "<option value=''>全部</option>";
    $.ajax({
        method: "post",
        url: "/pqs9900/device/getselect",
        data: {
            dictypeName: '终端等级'
        },
        dataType: 'json',
        async: false,
        success: function (data) {
            for (var i = 0; i < data.length; i++) {

                option = option + "<option value='"
                    + data[i].dicLeave + "'>"
                    + data[i].dicName + "</option>";

            }
            $("#lineGrade").empty()
            $("#lineGrade").append(option)
        }
    })
}

