var idNumber;
var loadingMsg;
var parentValue;


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


    $("#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
        }


        excelUpload();
    });
	
	if(parentValue.type == '1'){
		//从工程主页面打开，此时工程默认没有选中
		getProjectSelect("");
	}
	if(parentValue.type == '2'){
		//从工程详情页面打开，此时工程有值
		getProjectSelect(parentValue.proId);
	}

});
function setParam(data){
	parentValue = data;
}

function getProjectSelect(proId) {
	$("#projecct").empty();
	    $.ajax({
	        type: 'post',
	        url: PATH_URL + "/lineProject/getProList",
	        data: {
	        },
	        success: function (data) {
	            console.log("获取工程下拉列表OK");
	            if(data.code == 200){
	                let html = '<option value="">--请选择工程--</option>';
	                let resultObj = data.data;
	                let htm = setOption(proId,resultObj,html);
	                $("#projecct").html(htm);
	            }else{
	                $("#projecct").html('<option value="">--无数据--</option>');
	            }
				
				
				if(parentValue.type == '2'){
					$("#projecct").prop("disabled", true);
				}
				
	            layui.form.render();
	        },
	        error: function (err) {
	            console.log("获取工程下拉列表出错：", err);
	        }
	    });
}

/**
 * 下拉框set值
 */
function setOption(id,resultObj,html) {
    for (let i = 0; i < resultObj.length; i++) {
        if (id == resultObj[i].id) {
            html += '<option selected  value=\'' + resultObj[i].id + '\'>' + resultObj[i].name + '</option>';
        } else {
            html += '<option value=\'' + resultObj[i].id + '\'>' + resultObj[i].name + '</option>';
        }
    }
    return html;
}

function excelUpload() {
	if($("#projecct").val() == ""){
		layer.msg("请选择工程", { icon: 0 });
		return
	}
	
    loadingMsg = layer.msg('数据上传中,请稍候...', {icon: 16, scrollbar: false, time: 0, shade: [0.8, '#393D49']});    //④创建一个formData对象
    var fileData = new FormData();
    fileData.append('file', $('#file')[0].files[0]);
	fileData.append('proId', $("#projecct").val());
    $.ajax({
        url : PATH_URL + "/lineProject/importData",
        type : 'POST',
        data : fileData,
        processData : false,// ⑧告诉jQuery不要去处理发送的数据
        contentType : false, // ⑨告诉jQuery不要去设置Content-Type请求头
        success : function(result) {
            console.log(result)
            layer.close(loadingMsg);
            if (result.code == 200) {
				parent.document.getElementById('powerNum').innerText = result.data.powerNum;
				
                parent.layer.alert(result.msg,{icon:1});
				var index = parent.layer.getFrameIndex(window.name); //先得到当前 iframe层的索引
				parent.layer.close(index); //再执行关闭
            } else {
                layer.alert(result.msg,{icon:2});
            }
        },
        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 getProSelect(subId,proId) {
    $("#proId").empty();
    $.ajax({
        type: 'get',
        url: systemPath + '/select/getProBySubId',
        data: {
            subId: subId
        },
        async: false,
        success: function (data) {
            console.log("获取工程下拉列表OK");
            if(data.code == 200){
                let html = '<option value="">--请选择工程--</option>';
                let resultObj = data.data;
                let htm = setOption(proId,resultObj,html);
                $("#proId").html(htm);
            }else{
                $("#proId").html('<option value="">--无数据--</option>');
            }
            layui.form.render();
        },
        error: function (err) {
            console.log("获取工程下拉列表出错：", err);
        }
    });
}

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