var pers = []
var table,form,laydate
layui.use(['form','layer','table','laydate'], function () {
    table = layui.table;
    form = layui.form;
	laydate = layui.laydate

	//初始化日期选择框
	laydate.render({
	    elem: '#insMonth',
	    type: 'month',
		value: (new Date()).toISOString().substr(0, 7), // 获取当前日期并转换为'yyyy-MM'格式
		btns: ['now', 'confirm']
	});
	init();
});

//查询
function queryClick(){
	let insMonth = $('#insMonth').val();
	let aiDitStatus = $('#aiDitStatus').val();
	table.reload('layui_table', {
		where: {
			yjId:$(window.parent.document).find("input[id='taskSourceId']").attr("stationId"),
			insMonth: insMonth,
			aiDitStatus: aiDitStatus,
		}
	})
}
//重置
function resetClick(){
	var today = new Date();
	var year = today.getFullYear();
	var month = today.getMonth() + 1;
	if (month < 10) {
	    month = '0' + month;
	}
	var currentMonth = year + '-' + month;
	$('#insMonth').val(currentMonth);
	$('#aiDitStatus').val('');
	form.render();
	table.reload('layui_table', {
		where: {
			yjId: "",
			insMonth: $('#insMonth').val(),
			aiDitStatus: "",
		}
	})
}

/**
 * 初始化数据
 */
function init(){
	var yjId = $(window.parent.document).find("input[id='taskSourceId']").attr("stationId"); 
      // 渲染表格
  table.render({
	id: 'layui_table',
    elem: '#table',
    url: PATH_URL + '/inspectionPlan/getLineProjectListPage',
    where: {
		insMonth:$('#insMonth').val(),
		yjId:yjId
    },
	page: true,
	method:'post',
    skin: 'line', // 表格样式
    cols: [[
        {
            title: '序号',
            field: 'zizeng',
            align: 'center',
            type: 'numbers',
			width: '5%'
        },
        {field:'fgsName', title: '分公司',align: 'center',width: '15%'},
        {field:'yjName', title: '运检站',align: 'center',width: '15%'},
        {field:'insMonth', title: '月份',align: 'center',width: '6%'},
        {field:'proPlanNum', title: '工程计划数量',align: 'center',width: '7%'},
		{field:'createUser', title: '提交人',align: 'center',width: '7%'},
		{field:'createTime', title: '提交时间',align: 'center',width: '15%'},
		{
			field: '',
			title: '审核状态',
			align: 'center',
			width: '10%',
			templet: function(d){
				var status = d.aiDitStatus;
				console.log(status)
				var text;
				var color;
				var html = "";
				if(status == '2'){
					text = '已通过';
					color = '#19BE6B';
				}
				if(status == '0' || status == '4'){
					text = '待审核';
					color = '#FF9900';
				}
				if(status == '3'){
					text = '未通过';
					color = '#F56C6C';
				}
				if(status == '1'){
					text = '已撤销';
					color = '#999999';
				}
				html += "<span style='display: flex;justify-content: center;align-items: center'>"
				html += `<span style='width: 5px;height: 5px;background: ${color};border-radius: 50%'></span>&nbsp;&nbsp;`
				html += `<span>${text}</span>`
				html += "</span>"
				return html;
			},
		},
        {
            field: '',
            title: '操作',
            align: 'center',
			width: '20%',
            templet: function(d){
				var id = id;
				var yjId= d.yjId;
				var insMonth= d.insMonth;
            	var status = d.aiDitStatus;
            	var text;
            	var html = "";
            	if(status == '2'){
            		//已通过
					html += `<button class="layui-btn layui-btn-sm" onclick="detailsView('${yjId}','${insMonth}')">详情</button>`
					html += `<button class="layui-btn layui-btn-sm layui-btn-primary" onclick="exportDataDetails('${yjId}','${insMonth}')">导出</button>`
            	}
            	if(status == '0' ){
            		//待审核
					html += `<button class="layui-btn layui-btn-sm" onclick="detailsView('${yjId}','${insMonth}')">详情</button>`
					html += `<button type="button" class="layui-btn layui-btn-sm layui-bg-orange" onclick="revokeView('${yjId}','${insMonth}')">撤销</button>`
					html += `<button class="layui-btn layui-btn-sm layui-btn-primary" onclick="exportDataDetails('${yjId}','${insMonth}')">导出</button>`
            	}
            	if(status == '3'){
            		//未通过
					html += `<button class="layui-btn layui-btn-sm" onclick="detailsView('${yjId}','${insMonth}')">详情</button>`
					html += `<button type="button" class="layui-btn layui-btn-sm layui-bg-blue" onclick="updateView('${yjId}','${insMonth}')">修改</button>`
					html += `<button type="button" class="layui-btn layui-btn-sm layui-bg-red" onclick="delData('${yjId}','${insMonth}')">删除</button>`
					html += `<button class="layui-btn layui-btn-sm layui-btn-primary" onclick="exportDataDetails('${yjId}','${insMonth}')">导出</button>`
            	}
            	if(status == '1'){
            		//已撤销
					html += `<button class="layui-btn layui-btn-sm" onclick="detailsView('${yjId}','${insMonth}')">详情</button>`
					html += `<button type="button" class="layui-btn layui-btn-sm layui-bg-blue" onclick="updateView('${yjId}','${insMonth}')">修改</button>`
					html += `<button type="button" class="layui-btn layui-btn-sm layui-bg-red" onclick="delData('${yjId}','${insMonth}')">删除</button>`
					html += `<button class="layui-btn layui-btn-sm layui-btn-primary" onclick="exportDataDetails('${yjId}','${insMonth}')">导出</button>`
            	}
				if(status == '4'){
					//待审核
					html += `<button class="layui-btn layui-btn-sm" onclick="detailsView('${yjId}','${insMonth}')">详情</button>`
					html += `<button class="layui-btn layui-btn-sm layui-btn-primary" onclick="exportDataDetails('${yjId}','${insMonth}')">导出</button>`
				}
            	
            	return html;
            },
        }
    ]],
  });
     
}


//详情
function detailsView(yjId,insMonth) {
    let height = '98%';
    let width = '95%';
    $.ajax({
        type: 'POST',
        url: PATH_URL + '/inspectionPlan/getDetails',
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({"yjId": yjId,"insMonth": insMonth}),
        success: function (data) {
    		console.log("data:", data)
    		
    		
            var index = layer.open({
                title: ['月计划详情'],
                type: 2,
                content: './monthPlanDetails.html',
                area: [width, height],
                maxmin: false,
                success: function (layero, index) {
                    // console.log(data);
                    var myIframe = window[layero.find('iframe')[0]['name']];
                    // myIframe.hideSubmit();
                    var fnc = myIframe.setData(data.data,yjId,insMonth); //aaa()为子页面的方法
    
                },
            });
        }
    })
}
//撤销
function revokeView(yjId,insMonth){
	let aiDitStatus = $('#aiDitStatus').val();
	let index = layer.confirm("确定撤销吗？", {icon: 0}, function () {
		$.ajax({
			type: 'POST',
			url: PATH_URL + '/inspectionPlan/updateRevokeReason',
			contentType: "application/json; charset=utf-8",
			data: JSON.stringify({ "yjId": yjId,"insMonth": insMonth,"reason":"" }),
			success: function (data) {
				layer.close(index);
				if (data.code == 200) {
					layer.msg("撤销成功", {icon: 1}, function () {
						table.reload('layui_table', {
							where: {
								yjId:yjId,
								insMonth: insMonth,
								aiDitStatus:aiDitStatus
							}
						})
					});
				} else {
					layer.msg("撤销失败", {icon: 2}, function () {
						// example.ajax.reload(); // 刷新页面
					});
				}
			}
		})
	})
}
//删除
function delData(yjId,insMonth){
	let aiDitStatus = $('#aiDitStatus').val();
	let index = layer.confirm("确定删除吗？", {icon: 0}, function () {
		$.ajax({
			type: 'POST',
			url: PATH_URL + '/inspectionPlan/delPlanId',
			contentType: "application/json; charset=utf-8",
			data: JSON.stringify({ "yjId": yjId,"insMonth": insMonth }),
			success: function (data) {
				layer.close(index);
				if (data.code == 200) {
					layer.msg("删除成功", {icon: 1}, function () {
						table.reload('layui_table', {
							where: {
								yjId:yjId,
								insMonth: insMonth,
								aiDitStatus:aiDitStatus
							}
						})
					});
				} else {
					layer.msg("删除失败", {icon: 2}, function () {
						// example.ajax.reload(); // 刷新页面
					});
				}
			}
		})
	})
}

//修改
function updateView(yjId,insMonth) {
    let height = '98%';
    let width = '95%';
	let aiDitStatus = $('#aiDitStatus').val();
    $.ajax({
        type: 'POST',
        url: PATH_URL + '/inspectionPlan/getDetails',
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({"yjId": yjId,"insMonth": insMonth}),
        success: function (data) {
            var index = layer.open({
                title: ['月计划上报'],
                type: 2,
                content: './updateMonthPlan.html',
                area: [width, height],
                maxmin: false,
                success: function (layero, index) {
                    // console.log(data);
                    var myIframe = window[layero.find('iframe')[0]['name']];
                    // myIframe.hideSubmit();
                    var fnc = myIframe.setData(data.data,yjId,insMonth); //aaa()为子页面的方法
    
                },
				end:function(){
					table.reload('layui_table', {
						where: {
							yjId:yjId,
							insMonth: insMonth,
							aiDitStatus:aiDitStatus
						}
					})
				}
            });
        }
    })
}

//新增月计划addMonthPlan
function addMonthPlan(id) {
	let yjId = $(window.parent.document).find("input[id='taskSourceId']").attr("stationId");
	let insMonth = $('#insMonth').val();
	let aiDitStatus = $('#aiDitStatus').val();
	
	let height = '98%';
	let width = '99%';
	var index = layer.open({
	    title: ['月计划上报', 'color:#3B70A1;background-color:#E8ECEB;font-size:20px'],
	    type: 2,
	    content: './addMonthPlan.html',
	    area: [width, height],
	    maxmin: false,
	    success: function (layero, index) {
	        var myIframe = window[layero.find('iframe')[0]['name']];
	        myIframe.setYjId($(window.parent.document).find("input[id='taskSourceId']").attr("stationId"));
	        // var fnc = myIframe.setData(data.data); //aaa()为子页面的方法
	
	    },
		end:function(){
			table.reload('layui_table', {
				where: {
					yjId:yjId,
					insMonth: insMonth,
					aiDitStatus:aiDitStatus
				}
			})
		}
	});
}

//导入计划
function importData(){
	let yjId = $(window.parent.document).find("input[id='taskSourceId']").attr("stationId");
	let insMonth = $('#insMonth').val();
	let aiDitStatus = $('#aiDitStatus').val();
	
	let height = '98%';
	let width = '62%';
	var index = layer.open({
	    title: ['月计划上报', 'color:#3B70A1;background-color:#E8ECEB;font-size:20px'],
	    type: 2,
	    content: './monthPlanImport.html',
	    area: [width, height],
	    maxmin: false,
	    success: function (layero, index) {
			var myIframe = window[layero.find('iframe')[0]['name']];
			myIframe.setYjId($(window.parent.document).find("input[id='taskSourceId']").attr("stationId"));
	        // var fnc = myIframe.setData(data.data); //aaa()为子页面的方法
	
	    },
		end:function(){
			table.reload('layui_table', {
				where: {
					yjId:yjId,
					insMonth: insMonth,
					aiDitStatus:aiDitStatus
				}
			})
		}
	});
}

//导出
function exportData(){
	let insMonth = $('#insMonth').val();
	let aiDitStatus = $('#aiDitStatus').val();
	let yjId = $(window.parent.document).find("input[id='taskSourceId']").attr("stationId");
	
	window.location.href = PATH_URL + `/inspectionPlan/exportPlanData?token=` + token
	    + "&insMonth=" + insMonth
		+ "&aiDitStatus=" + aiDitStatus + "&yjId=" + yjId
		
	layer.msg('下载中,请稍等', {
		icon: 16,
		scrollbar: false
	});
}


//导出
function exportDataDetails(yjId,insMonth){
	
	
	window.location.href = PATH_URL + `/inspectionPlan/exportMonthPlanData?token=` + token
	    + "&insMonth=" + insMonth
		+ "&yjId=" + yjId
		
		
	layer.msg('下载中,请稍等', {
		icon: 16,
		scrollbar: false
	});
}