/**
 * 终端运行管理
 */
var dataTable = null;
var resultData = null;
var area = null;
$(document).ready(function () {
    // $.ajax({
    //     type: "POST",
    //     url: "/pqs9000/user/getAreasInfo",
    //     dataType:'json',
    //     success:function (data) {
    //         $("#area").val(data[0].name);
    //         $("#area").attr("index", data[0].id);
    //         areaTree = $.fn.zTree.init($("#areaTree"), settingAreaTree, data);
    //         //初始化页面内容
    //         area = $area.eq(0).attr("index");
    //         load();
    //     }
    // })
    // getGdnameG();
    load();
});
$("#query").click(function () {
    //area = $area.eq(0).attr("index");
    load();
});

function load() {
    var startTime = $("#startTime").val();
    var endTime = $("#endTime").val();
    var type = $("#transactionType").val();
    var progress = $("#transactionFlow").val();
    var i;
    $.ajax({
        method: "post",
        url: "/pqs9300/technical/getTechnicalInfo",
        data: {
            startTime: startTime,
            endTime: endTime,
            progress: progress,
            supType: type
        },
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_LAYER();
        },
        success: function (data) {
            if (data == "") {
                resultData = null;
                //没有数据
                //initTable(null);
            } else {
                var info = data.body;
                if (info.code == 500) {
                    resultData = null;
                    //没有数据
                    initTable(null);
                } else {
                    resultData = info;
                    initTable(info);
                }
            }
            // startQuery();
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER(i);

            $("#lineIndex").val(null);
        }
    });
}

function initTable(data) {
    var height = $(window).height() - 160;
    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 = $("#runtimeid").DataTable();
    dataTable.clear().draw();
    dataTable = $('#runtimeid').DataTable({
        order: [[5, 'desc']],
        columnDefs: [
            {
                orderable: false,
                targets: [0, 13]
            }
        ],
        columns: [
            {
                width: "2%", data: "supIndex",
                render: function (data, type, full, meta) {
                    if (full.progress == 0) {
                        return "<input type ='checkbox' name='test' class='icheckbox_minimal' value='" + data + "'>";
                    } else {
                        return "<input type ='checkbox' name='disable' class='icheckbox_minimal' disabled >";
                    }
                }
            },
            {
                width: "6%", data: "supName", render: function (data, type, full, meta) {
                    var value = data;
                    if (value.length > 9) {
                        value = data.substring(0, 8) + "...";
                        value = "<span title=" + data + ">" + value + "</span>";
                    }
                    return value;
                }
            },
            {
                width: "5%", data: "supType", render: function (data, type, full, meta) {
                    if (data == 0) {
                        return "预警单";
                    } else {
                        return "告警单";
                    }
                }
            },
            {width: "5%", data: "userName"},
            {width: "5%", data: "deptName"},
            {
                width: "7%", data: "creatTime", render: function (data, type, full, meta) {
                    var date = new Date(data);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
                    var Y = date.getFullYear() + '-';
                    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
                    var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
                    var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours() + ':';
                    var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes() + ':';
                    var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
                    return Y + M + D + h + m + s;
                }
            },
            {
                width: "7%", data: "modifyTime", render: function (data, type, full, meta) {
                    var date = new Date(data);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
                    var Y = date.getFullYear() + '-';
                    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
                    var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
                    var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours() + ':';
                    var m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes() + ':';
                    var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
                    return Y + M + D + h + m + s;
                }
            },
            {
                width: "5%", data: "progress", render: function (data, type, full, meta) {
                    var tet = "";
                    if (data == 0) {
                        tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='detailTech(\"" + full.supIndex + "\",\"" + data + "\")'>" + '未开始' + "</a>"
                    } else if (data == 1) {
                        tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='checkTransaction(\"" + full.supIndex + "\")'>" + '下发' + "</a>"
                    } else if (data == 2) {
                        tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='detailTech(\"" + full.supIndex + "\",\"" + data + "\")'>" + '详情' + "</a>"
                    } else if (data == 3) {
                        tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='detailTech(\"" + full.supIndex + "\",\"" + data + "\")'>" + '详情' + "</a>"
                    } else if (data == 4) {
                        tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='detailTech(\"" + full.supIndex + "\",\"" + data + "\")'>" + '详情' + "</a>"
                    } else if (data == 5) {
                        tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='detailTech(\"" + full.supIndex + "\",\"" + data + "\")'>" + '详情' + "</a>"
                    } else if (data == 6) {
                        tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='detailTech(\"" + full.supIndex + "\",\"" + data + "\")'>" + '详情' + "</a>"
                    }
                    return tet;
                }
            },
            {
                width: "5%", data: "progress", render: function (data, type, full, meta) {
                    var tet = "";
                    if (data == 0) {
                        tet = "<a>" + '/' + "</a>"
                    } else if (data == 1) {
                        tet = "<a>" + '/' + "</a>"
                    } else if (data == 2) {
                        tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='excuteNextStep(\"" + full.supIndex + "\",\"" + data + "\")'>" + '上传' + "</a>"
                    } else if (data == 3) {
                        tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='detailTech(\"" + full.supIndex + "\",\"" + data + "\")'>" + '详情' + "</a>"
                    } else if (data == 4) {
                        tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='detailTech(\"" + full.supIndex + "\",\"" + data + "\")'>" + '详情' + "</a>"
                    } else if (data == 5) {
                        tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='detailTech(\"" + full.supIndex + "\",\"" + data + "\")'>" + '详情' + "</a>"
                    } else if (data == 6) {
                        tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='detailTech(\"" + full.supIndex + "\",\"" + data + "\")'>" + '详情' + "</a>"
                    }
                    return tet;
                }
            },
            {
                width: "4%", data: "progress", render: function (data, type, full, meta) {
                    var tet = "";
                    if (full.supType == 0) {
                        tet = "-"
                    } else {
                        if (data == 0) {
                            tet = "<a>" + '/' + "</a>"
                        } else if (data == 1) {
                            tet = "<a>" + '/' + "</a>"
                        } else if (data == 2) {
                            tet = "<a>" + '/' + "</a>"
                        } else if (data == 3) {
                            tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='excuteNextStep(\"" + full.supIndex + "\",\"" + data + "\")'>" + '上传' + "</a>"
                        } else if (data == 4) {
                            tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='detailTech(\"" + full.supIndex + "\",\"" + data + "\")'>" + '详情' + "</a>"
                        } else if (data == 5) {
                            tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='detailTech(\"" + full.supIndex + "\",\"" + data + "\")'>" + '详情' + "</a>"
                        } else if (data == 6) {
                            tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='detailTech(\"" + full.supIndex + "\",\"" + data + "\")'>" + '详情' + "</a>"
                        }
                    }
                    return tet;
                }
            },
            {
                width: "6%", data: "progress", render: function (data, type, full, meta) {
                    var tet = "";
                    if (full.supType == 0) {
                        tet = "-"
                    } else {
                        if (data == 0) {
                            tet = "<a>" + '/' + "</a>"
                        } else if (data == 1) {
                            tet = "<a>" + '/' + "</a>"
                        } else if (data == 2) {
                            tet = "<a>" + '/' + "</a>"
                        } else if (data == 3) {
                            tet = "<a>" + '/' + "</a>"
                        } else if (data == 4) {
                            tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='checkTransactionNotice(\"" + full.supIndex + "\")'>" + '下发' + "</a>"
                        } else if (data == 5) {
                            tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='detailTech(\"" + full.supIndex + "\",\"" + data + "\")'>" + '详情' + "</a>"
                        } else if (data == 6) {
                            tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='detailTech(\"" + full.supIndex + "\",\"" + data + "\")'>" + '详情' + "</a>"
                        }
                    }
                    return tet;
                }
            },
            {
                width: "6%", data: "progress", render: function (data, type, full, meta) {
                    var tet = "";
                    if (full.supType == 0) {
                        tet = "-"
                    } else {
                        if (data == 0) {
                            tet = "<a>" + '/' + "</a>"
                        } else if (data == 1) {
                            tet = "<a>" + '/' + "</a>"
                        } else if (data == 2) {
                            tet = "<a>" + '/' + "</a>"
                        } else if (data == 3) {
                            tet = "<a>" + '/' + "</a>"
                        } else if (data == 4) {
                            tet = "<a>" + '/' + "</a>"
                        } else if (data == 5) {
                            tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='excuteNextStep(\"" + full.supIndex + "\",\"" + data + "\")'>" + '上传' + "</a>"
                        } else if (data == 6) {
                            tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='detailTech(\"" + full.supIndex + "\",\"" + data + "\")'>" + '详情' + "</a>"
                        }
                    }
                    return tet;
                }
            },
            {
                width: "5%", data: "progress", render: function (data, type, full, meta) {
                    var tet = "";
                    if (data == 0) {
                        tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='viewFlowChart(\"" + full.supIndex + "\",\"" + full.supType + "\",\"" + data + "\")'>" + '开始' + "</a>"
                    } else if (data == 1) {
                        tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='viewFlowChart(\"" + full.supIndex + "\",\"" + full.supType + "\",\"" + data + "\")'>" + '预/告警单下发' + "</a>"
                    } else if (data == 2) {
                        tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='viewFlowChart(\"" + full.supIndex + "\",\"" + full.supType + "\",\"" + data + "\")'>" + '预/告警反馈单上传' + "</a>"
                    } else if (data == 3) {
                        tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='viewFlowChart(\"" + full.supIndex + "\",\"" + full.supType + "\",\"" + data + "\")'>" + '现场测试' + "</a>"
                    } else if (data == 4) {
                        tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='viewFlowChart(\"" + full.supIndex + "\",\"" + full.supType + "\",\"" + data + "\")'>" + '通知单下发' + "</a>"
                    } else if (data == 5) {
                        tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='viewFlowChart(\"" + full.supIndex + "\",\"" + full.supType + "\",\"" + data + "\")'>" + '通知反馈单上传' + "</a>"
                    } else if (data == 6) {
                        tet = "<a style='cursor: pointer;color:#4169E1;text-decoration:underline;' onclick='viewFlowChart(\"" + full.supIndex + "\",\"" + full.supType + "\",\"" + data + "\")'>" + '完结' + "</a>"
                    }

                    return tet;
                }
            },
            {
                width: "9%", data: "progress", render: function (data, type, full, meta) {
                    var extarUButton = "", extarDButton = "";
                    if (data == 0) {
                        extarUButton = "<button type='button' class='btn  btn-primary  mr10' onclick='updateTech(\"" + full.supIndex + "\",\"" + data + "\")' ><i class='glyphicon glyphicon-asterisk'></i>修改</button>";
                        extarDButton = "<button type='button' class='btn  btn-danger  mr10' onclick='deleteTech(\"" + full.supIndex + "\",\"" + data + "\")' ><i class='glyphicon glyphicon-trash'></i>删除</button>";
                    } else if (data != 6) {
                        extarUButton = "<button type='button' class='btn  btn-primary  mr10' onclick='updateTech(\"" + full.supIndex + "\",\"" + data + "\")' ><i class='glyphicon glyphicon-asterisk'></i>修改</button>";
                    }
                    return extarUButton + extarDButton;
                }
            }
        ],
        destroy: true,
        data: data,
        lengthChange: false,
        searching: true,
        dom: 'rtip',
        scrollY: height,
        // scrollX: true,
        lengthMenu: [length],
        language: {
            url: '/pqs9300/json/chine.json'
        },
        fnDrawCallback: function (oTable) {
            $("#runtimeid_info").addClass("fl");
            $("#runtimeid_paginate").addClass("fr");
            $("#runtimeid_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 = $("#runtimeid").dataTable();
            $('#dataTable-btn').click(function (e) {
                if ($("#changePage").val() && $("#changePage").val() > 0) {
                    var redirectpage = $("#changePage").val() - 1;
                } else {
                    var redirectpage = 0;
                }
                table.fnPageChange(redirectpage);
            });
        }
    });
    // dataTable.on('order.dt search.dt',
    //     function () {
    //         dataTable.column(0, {
    //             search: 'applied',
    //             order: 'applied'
    //         }).nodes().each(function (cell, i) {
    //             cell.innerHTML = i + 1;
    //         });
    //     }).draw();
    modifyCss(dataTable);

}

$("#supType").change(function () {
    var options = $("#supType option:selected").val();
    $("#devflag").find("option").remove();
    if (options == "") {
        var obj = document.getElementById('devflag');
        obj.options.add(new Option("全部", ""));
        obj.options.add(new Option("开始", "0"));
        obj.options.add(new Option("预/告警单下发", "1"));
        obj.options.add(new Option("预/告警反馈单上传", "2"));
        obj.options.add(new Option("现场测试", "3"));
        obj.options.add(new Option("整改通知单下发", "4"));
        obj.options.add(new Option("整改通知反馈单", "5"));
        obj.options.add(new Option("完结", "6"));
    } else if (options == "0") {
        var obj = document.getElementById('devflag');
        obj.options.add(new Option("全部", ""));
        obj.options.add(new Option("开始", "0"));
        obj.options.add(new Option("预/告警单下发", "1"));
        obj.options.add(new Option("预/告警反馈单上传", "2"));
        obj.options.add(new Option("完结", "6"));
    } else if (options == "1") {
        var obj = document.getElementById('devflag');
        obj.options.add(new Option("全部", ""));
        obj.options.add(new Option("开始", "0"));
        obj.options.add(new Option("预/告警单下发", "1"));
        obj.options.add(new Option("预/告警反馈单上传", "2"));
        obj.options.add(new Option("现场测试", "3"));
        obj.options.add(new Option("整改通知单下发", "4"));
        obj.options.add(new Option("整改通知反馈单", "5"));
        obj.options.add(new Option("完结", "6"));
    }
})
//实现全选功能
$("#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;
    });
});

$("#startSwitch").click(function () {
    var indexs = [];
    var $checkboxs = $("[name=test]:checked");
    var length = $checkboxs.length;
    if (length === 0) {
        layer.msg('请选中开启的流程', {icon: 2, time: 2000});
    } else {
        for (var i = 0; i < length; i++) {
            indexs.push($checkboxs.eq(i).val())
        }
        getStartSwitch(indexs);
    }
})

function getStartSwitch(index) {
    var i;
    $.ajax({
        type: "post",
        url: "/pqs9300/technical/getStartSwitch",
        dataType: "json",
        data: {
            index: index
        },
        success: function (data) {
            if (data.code === 200) {
                layer.msg(data.message, {icon: 1, time: 2000})
            } else {
                layer.msg(data.message, {icon: 2, time: 2000})
            }
            load();
        }
    })
}

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

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

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

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

}

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

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


//标签点击，展示流程图
//传递参数 supIndex、type、progress
//xy 2020.07.01
function viewFlowChart(flowChartSupIndex, flowChartType, flowChartProgress) {
    layer.open({
        type: 2,
        title: '流程状态',
        maxmin: false,
        area: ['100%', '100%'],
        content: '/pqs9300/flowChart/getFlowChart?flowChartSupIndex=' + flowChartSupIndex + '&flowChartType=' + flowChartType + '&flowChartProgress=' + flowChartProgress,
        scrollbar: false
    });
}


$("#addtech").click(function () {
    layer.open({
        type: 2,
        title: '新增技术监督事务',
        shadeClose: true, //点击遮罩关闭层
        area: ['100%', '100%'],
        content: 'addtechnical',
        scrollbar: false
    });
});

//预/告警单下发
function checkTransaction(supIndex) {
    var i;
    $.ajax({
        type: "post",
        url: "/pqs9300/transaction/checkTransaction",
        dataType: "json",
        data: {
            supIndex: supIndex
        },
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_DOWNLOAD();
        },
        success: function (data) {
            if (data.code === 200) {
                window.location.href = '/pqs9300/transaction/downTransactionWord?supIndex=' + supIndex;
            } else {
                layer.msg(data.message, {icon: 2, time: 2000});
            }
            ityzl_CLOSE_LOAD_LAYER(i);
            load();
        }
    })
}

//整改单下发
function checkTransactionNotice(supIndex) {
    var i;
    $.ajax({
        type: "post",
        url: "/pqs9300/transaction/checkTransactionNotice",
        dataType: "json",
        data: {
            supIndex: supIndex
        },
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_DOWNLOAD();
        },
        success: function (data) {
            if (data.code === 200) {
                window.location.href = '/pqs9300/transaction/downTransactionWordNotice?supIndex=' + supIndex;
            } else {
                layer.msg(data.message, {icon: 2, time: 2000});
            }
            ityzl_CLOSE_LOAD_LAYER(i);
            load();
        }
    })
}

//反馈单上传
function excuteNextStep(supIndex, stepCount) {
    layer.open({
        type: 2,
        title: "技术监督详情",
        maxmin: false,
        area: ['100%', '100%'],
        content: '/pqs9300/transaction/excuteNextStep?supIndex=' + supIndex + "&excuteStep=1" + "&stepCount=" + stepCount,
        scrollbar: false
    })
}

//详情
function detailTech(supIndex, progress) {
    layer.open({
        type: 2,
        title: "技术监督详情",
        maxmin: false,
        area: ['100%', '100%'],
        content: 'technicaldetail?supIndex=' + supIndex,
        scrollbar: false
    })
}

//修改
function updateTech(supIndex, progress) {
    layer.open({
        type: 2,
        title: "修改技术监督",
        maxmin: false,
        area: ['100%', '100%'],
        content: 'modtechnical?supIndex=' + supIndex,
        scrollbar: false
    })
}

//删除
function deleteTech(supIndex, progress) {
    layer.confirm('是否确认删除？', function (index) {
        $.ajax({
            type: 'POST',
            url: '/pqs9300/technical/delTechnical',
            data: {
                supIndex: supIndex
            },
            dataType: 'json',
            async: false,
            success: function (data) {
                if (data.code == 200) {
                    layer.msg('删除成功！', {icon: 1, time: 1000});
                    load();
                } else {
                    layer.msg('删除失败！', {icon: 2, time: 1000});
                }
            }
        });
        layer.close(index);
    });
}
