var supIndex = $("#supIndex").val();
var obj_ser = new Object();
$(document).ready(function () {
    getData();
})

function getData() {
    $.ajax({
        type: "POST",
        dataType: "json",
        data: {
            supIndex: supIndex
        },
        url: "/pqs9300/technical/getTechnicalByID",
        success: function (data) {
            if (data.code == 200) {
                var option;
                var deptsname = data.body.deptName;
                var deptsindex = data.body.depts;
                var eventdate = data.body.eventDate;
                var type = data.body.type;
                var overRun = data.body.overRunLog;

                option = option + "<option selected value='"
                    + deptsindex + "'>"
                    + deptsname + "</option>";

                $("#depts").append(option);
                $("#startTime").val(formatDate(eventdate));
                $("#type").val(type);

                $("#startTime").attr("disabled", true);
                $("#depts").attr("disabled", true);
                $("#type").attr("disabled", true);
                initTable(overRun);
            } else {
                layer.msg('获取技术监督详情失败！', {icon: 2, time: 1000});
            }
        }
    })
}

function formatDate(now) {
    var date = new Date(now);// 时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date
            .getMonth() + 1)
        + '-';
    var D = change(date.getDate());
    return Y + M + D;
}

function change(t) {
    if (t < 10) {
        return "0" + t;
    } else {
        return t;
    }
}

function queryOverRun() {
    var startTime = $("#startTime").val();
    var depts = $("#depts").val();
    var type = $("#type").val();

    $.ajax({
        type: "POST",
        dataType: "json",
        data: {
            startTime: startTime,
            deptIndex: depts,
            type: type
        },
        url: "/pqs9300/technical/getTechnicalDetail",
        success: function (data) {
            var resultData;
            if (data.code === 500) {
                layer.msg(data.message, {icon: 2, time: 2000})
                resultData = null;
            } else if (data.body.length == 0) {
                resultData = null;
            } else {
                resultData = data.body;
            }
            initTable(resultData);
        }
    })
}

function initTable(data) {
    var dataTable = null;
    var height = $(window).height() - 140;
    dataTable = $("#vdlist").DataTable();
    dataTable.clear().draw();
    dataTable = $("#vdlist").DataTable({
        order: [],
        columnDefs: [
            {
                orderable: false,
                targets: [0]
            }
        ],
        columns: [
            {width: "9%", data: "updateTime"},
            {width: "12%", data: "gdName"},
            {width: "12%", data: "subName"},
            {width: "12%", data: "lineName"},
            {width: "8%", data: "overRunTimes"},
            {
                width: "35%", data: "overRunDescribe", render: function (data, type, full, meta) {
                    var value = data;
                    if (data.length > 45) {
                        value = data.substring(0, 44) + "...";
                        value = "<span title=" + data + ">" + value + "</span>";
                    }
                    return value;
                }
            },
            {
                width: "12%", data: "lineIndex",
                render: function (data, type, full, meta) {
                    var bxck = "", report = "";
                    bxck = "<button type='button' class='btn themeBGColor' style='margin-right:10px;' href='javascript:void(0);'  onclick='viewWave(\"" + data + "\",\"" + full.lineName + "\",\"" + full.overRunDescribe + "\")' >趋势图</button>";
                    report = "<button type='button' class='btn themeBGColor' style='margin-right:10px;' href='javascript:void(0);'  onclick='viewExcel(\"" + data + "\")' >日报</button>";

                    return bxck + report;
                }
            }
        ],
        destroy: true,
        data: data,
        lengthChange: false,
        searching: true,
        dom: 'rtip',
        scrollY: height,
        scrollX: false,
        lengthMenu: [50],
        language: {
            url: '/pqs9300/json/chine.json'
        },
        fnDrawCallback: function (oTable) {
            $("#vdlist_info").addClass("fl");
            $("#vdlist_paginate").addClass("fr");
            $("#vdlist_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 = $("#vdlist").dataTable();
            $('#dataTable-btn').click(function (e) {
                if ($("#changePage").val() && $("#changePage").val() > 0) {
                    var redirectpage = $("#changePage").val() - 1;
                } else {
                    var redirectpage = 0;
                }
                table.fnPageChange(redirectpage);
            });
        }
    });
}


//实现全选功能
$("#all_checked").click(function () {
    $('[name=test]:checkbox').prop('checked', this.checked);//checked为true时为默认显示的状态
});

//实现反选功能
$("#checkrev").click(function () {
    $('[name=test]:checkbox').each(function () {
        this.checked = !this.checked;
    });
});

//波形
function viewWave(lineIndex, lineName, overRunDescribe) {
    obj_ser.lineIndex = lineIndex;
    obj_ser.lineName = lineName;
    obj_ser.startTime = $("#startTime").val();
    obj_ser.overRunDescribe = overRunDescribe;

    parent.layer.open({
        type: 2,
        title: '趋势图',
        maxmin: false,
        area: ['100%', '100%'],
        content: '/pqs9300/technical/viewWave',
        scrollbar: false ,
        success: function (layero, index) {
            $(layero).find("iframe")[0].contentWindow.getObj(
                obj_ser.lineIndex, obj_ser.lineName, obj_ser.startTime,obj_ser.overRunDescribe);
        }
    });
}

function getObj() {
    return obj_ser;
}

//日报
function viewExcel(lineIndex) {
    var startTime = $("#startTime").val();
    parent.layer.open({
        type: 2,
        title: "EXCEL报表",
        maxmin: false,
        area: ['100%', '100%'],
        content: 'excel?lineIndex=' + lineIndex + '&time=' + startTime,
        scrollbar: false
    })
}