var idNumber;
var loadingMsg;
var trainFileNames = [];
var trainFilePaths = [];
var examFileNames = [];
var examFilePaths = [];

layui.use(['layer', 'form', 'upload'], function () {
    var upload1 = layui.upload;
    var upload2 = layui.upload;
    var form = layui.form;
    var layer = layui.layer;

    getProjectSelect();


    $("#closeBt").click(function () {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前 iframe层的索引
        parent.layer.close(index); //再执行关闭
    });
    $("#searchBt").click(function () {

        if(!$('#file')[0].files[0]){
            layer.msg("请上传填好的月计划表", { icon: 0 })
            return
        }

        $("#hideUpload1").trigger("click");

        excelUpload();
    });

});

function importData() {
    var fileData = new FormData();
    fileData.append('file', $('#file')[0].files[0]);
    // 加载提示
    loadingMsg = top.layer.msg('数据上传中,请稍候...', {icon: 16, scrollbar: false, time: 0, shade: [0.8, '#393D49']});
    // form请求地址
    var formUrl = oiPlanUrl + "/monthlyPlanSubmission/importData";
    $.ajax({
        type: 'POST',
        url: formUrl, // 请求地址
        data: fileData,
        processData: false,
        contentType: false,
        success: function (data) {
            top.layer.close(loadingMsg); //再执行关闭
            if (data.code == 200) {
                top.layer.alert(data.msg);

                reloading();
            } else {
                top.layer.alert(data.msg, {icon: 2});
            }
        },
        error: function (XMLHttpRequest, textStatus, e) {
            layer.msg('数据请求发生异常,请稍后重试', {icon: 16,  scrollbar: false});
            top.layer.close(addLoadingMsg); //再执行关闭
        }
    });
}

function excelUpload() {
    loadingMsg = top.layer.msg('数据上传中,请稍候...', {icon: 16, scrollbar: false, time: 0, shade: [0.8, '#393D49']});    //④创建一个formData对象
    var fileData = new FormData();
    fileData.append('file', $('#file')[0].files[0]);
    $.ajax({
        url : oiPlanUrl + "/monthlyPlanSubmission/importData",
        type : 'POST',
        data : fileData,
        processData : false,// ⑧告诉jQuery不要去处理发送的数据
        contentType : false, // ⑨告诉jQuery不要去设置Content-Type请求头
        success : function(result) {
            console.log(result)
            top.layer.close(loadingMsg);
            if (result.data.success) {
                layer.msg(result.data.msg,{icon:1,time:1500},function () {
                    // reloadData();
                    reloading();
                });
            } else {
                showModal(result.data);
            }
        },
        error : function(XMLHttpRequest, textStatus, errorThrown,result) {
            top.layer.close(loadingMsg);
            if(XMLHttpRequest.status === 401){
                localStorage.removeItem("token");
                layer.msg('用户登录过期,请重新登录',{icon:5,time:2000},function(){
                    location.href = ctxPath + '/login.html';
                });
            }else{
                layer.msg(result.data.msg,{icon:5,time:2000});
            }
            // $.dialog({title: '提示', content: result.msg});
        }
    });
}

function showModal(result) {
    $('#myModal_edit').modal('hide');
    let tableStr ="<table class='table table-striped confirm-table' style='width: 100%; height: 50px;font-size: 18px'>"
        + "<thead>"
        + "<tr>"
        + "<th style='width: 15%;text-align:center;'>序号</th>"
        + "<th style='text-align:center;width:35%;'>错误信息</th>"
        + "</tr>"
        + "</thead>"
        + "<tbody>";
    let html = '';
    for (var i = 0;i < result.data.length;i++){
        html += "<tr>"
            +"<td style='text-align:center;width: 15%;'>" + (i+1) + "</td>"
            +"<td style='text-align:center;width: 35%;'>" + result.data[i].errorMsg + "</td>"
            +"</tr>";
    }

    tableStr += html +"</tbody></table>";
    $("#tableAjax").empty().append(tableStr);
    setTimeout(function() {
        $('#myModal_edit').modal('show');
    }, 1000);
}

//刷新页面
function reloading() {
    var index = parent.layer.getFrameIndex(window.name); //先得到当前 iframe层的索引
    parent.layer.close(index); //再执行关闭
    window.parent.location.reload();
}