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

var workTypeValue = -1;
var dropdown;
var layer;
var parentData;
layui.use(['layer', 'form', 'upload','laydate','dropdown'], function () {
    var laydate = layui.laydate;
    var upload1 = layui.upload;
    var upload2 = layui.upload;
    var form = layui.form;
    layer = layui.layer;
	dropdown = layui.dropdown;


    //按钮样式
    btnStyle();
	initPower()
 

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

    // 验证成功后才会执行下面的操作
    form.on('submit(formDemo)', function (data) {
        if($("#proId").val()==''){
            addData(data); // 新增
        }else{
            updateData(data); // 修改
        }
    });
});

//初始化
function initPower(){
	$.ajax({
		type: 'POST',
		url: PATH_URL + '/homePageSelect/getLineProtectorSubComAndStandDropdown', // 请求地址
		contentType: "application/json; charset=utf-8",
		data: {}, //获取提交的表单字段
		success: function (data) {
			console.log("data:", data)
			dropdown.render({
				elem: '#taskSourceId',
				data: data.data,
				click: function(item){
				  console.log(item)
				  $("#taskSourceId").val(`${item.parentName + ' / ' + item.title}`);
				  $("#taskSourceId").attr("stationId", item.id);
				  $("#taskSourceId").attr("stationName", item.title);
				},
			});
		},
		error: function (XMLHttpRequest, textStatus, e) {
			layer.msg('数据请求发生异常,请稍后重试', {icon: 16, scrollbar: false});
			top.layer.close(addLoadingMsg); //再执行关闭
		}
	});
}
 


/**
 * 新增
 * @param formData
 */
function addData(formData) {
    if (workTypeValue == -1) {
        layer.msg("请选择回路类型", {icon: 0})
        return
    }else {
		formData.field.isTwo = workTypeValue;
		formData.field.yjId = $("#taskSourceId").attr("stationId");
		formData.field.yjName = $("#taskSourceId").attr("stationName");
        // 加载提示
        let loadingMsg = layer.msg('数据上传中,请稍候...', {icon: 16, scrollbar: false, time: 0, shade: [0.8, '#393D49']});
        // form请求地址
        let formUrl = PATH_URL + "/lineProject/insertPro";
        $.ajax({
            type: 'POST',
            url: formUrl, // 请求地址
			contentType: "application/json; charset=utf-8",
            data: JSON.stringify(formData.field),
            success: function (data) {
                layer.close(loadingMsg); // 关闭提示层
                if(data.code == 200){
                    parent.layer.msg("添加成功", { icon: 1 });
					var index = parent.layer.getFrameIndex(window.name); //先得到当前 iframe层的索引
					parent.layer.close(index); //再执行关闭
                    // reloading();
                }else{
                    layer.alert(data.msg);
                }
            },
            error: function (XMLHttpRequest, textStatus, e) {
                layer.close(loadingMsg);
                layer.msg('数据请求发生异常,请稍后重试', {icon: 16, scrollbar: false});
            }
        });
    }
}

/**
 * 修改
 * @param formData
 * @returns {boolean}
 */
function updateData(formData) {
	var isTwo;
	var isTwos = $('.btnOption1 > div');
	for (var i = 0; i < isTwos.length; i++) {
		if($(isTwos[i]).attr("value")){
			isTwo = $(isTwos[i]).attr("value");
		}
	}
	
	
	
		formData.field.isTwo = isTwo;
		formData.field.yjId = $("#taskSourceId").attr("stationId");
		formData.field.yjName = $("#taskSourceId").attr("stationName");
        // 加载提示
        let loadingMsg = layer.msg('数据请求中,请稍候...', {icon: 16, scrollbar: false, time: 0, shade: [0.8, '#393D49']});
        // form请求地址
        let formUrl = PATH_URL + "/lineProject/updatePro";
        $.ajax({
            type: 'POST',
            url: formUrl, // 请求地址
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify(formData.field),
            success: function (data) {
                layer.close(loadingMsg); // 关闭提示层
                if(data.code == 200){
					parent.layer.msg("修改成功", { icon: 1 });
					var index = parent.layer.getFrameIndex(window.name); //先得到当前 iframe层的索引
					parent.layer.close(index); //再执行关闭
                    //reloading();
                }else{
                    layer.alert(data.msg);
                }
            },
            error: function (XMLHttpRequest, textStatus, e) {
                layer.close(loadingMsg);
                layer.msg('数据请求发生异常,请稍后重试', {icon: 16, scrollbar: false});
            }
        });
}



/**
 * 按钮样式，以及取值
 */
function btnStyle() {
    $('.btnOption1 > div').click(function () {
        $(this).addClass('pass').siblings().removeClass('pass');
        var value = $(this).data('value');
        $(this).attr('value', value).siblings().attr('value', '');
        workTypeValue = value;
    });
}




/**
 * 隐藏提交按钮和关闭按钮
 */
function hideSubmit() {
    // 使用getElementById根据ID选择按钮
    var submitButton = document.getElementById('submitBt');
    var closeButton = document.getElementById('closeBt');
    // 如果按钮存在，则隐藏它
    if (submitButton) {
        submitButton.style.display = 'none';
    }
    if (closeButton) {
        closeButton.style.display = 'none';
    }
}

/**
 * 设置数据
 * @param data
 */
function setData(data) {
	parentData = data;
	
	$("#proId").val(data.data.proId);
    $("#taskSourceId").val(data.data.fgsName + " / " + data.data.yjName);
	$("#lineProName").val(data.data.proName);
	$("#volLevel").val(data.data.volLevel);
	
	$("#taskSourceId").attr("stationId", data.data.yjId);
	$("#taskSourceId").attr("stationName", data.data.yjName);
	
    // 首先移除所有选项的 pass 类
    $('.btnOption1 > div').removeClass('pass');
    // 根据 值 添加 pass 类和设置 value 属性
    $('.btnOption1 > div').each(function () {
        if ($(this).data('value') === data.data.isTwo) {
            $(this).addClass('pass').attr('value', data.data.isTwo);
        } else {
            $(this).attr('value', '');
        }
    });

    var checkStatus = {};
    setCheckboxStates(checkStatus);
    console.log("checkStatus:", checkStatus);

}

// 根据提供的状态对象来设置复选框的状态
function setCheckboxStates(states) {
    var checkboxes = document.querySelectorAll('.bmscTrain input[type="checkbox"]');
    checkboxes.forEach(function(checkbox) {
        if (states.hasOwnProperty(checkbox.name)) {
            checkbox.checked = states[checkbox.name]; // 直接赋值布尔值
        }
    });
}

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