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',
		btns: ['now', 'confirm']
	});

	init();
});

//查询
function queryClick() {
	let fgsName = $('#fgsName').val();
	let yjName = $('#yjName').val();
	let insMonth = $('#insMonth').val();
	let aiDitStatus = $('#aiDitStatus').val();
	var subComId = $(window.parent.document).find("select[id='subComId']").val();
	table.reload('layui_table', {
		where: {
			fgsId: subComId,
			fgsName: fgsName,
			yjName: yjName,
			insMonth: insMonth,
			aiDitStatus: aiDitStatus,
		}
	})
}
//重置
function resetClick() {
	$('#fgsName').val('');
	$('#yjName').val('');
	$('#insMonth').val('');
	$('#aiDitStatus').val('0');
	var subComId = $(window.parent.document).find("select[id='subComId']").val();
	form.render();
	table.reload('layui_table', {
		where: {
			fgsId: subComId,
			fgsName: "",
			yjName: "",
			insMonth: "",
			aiDitStatus: "0",
		}
	})
}

/**
 * 初始化数据
 */
function init() {
	let aiDitStatus = $('#aiDitStatus').val();
	var subComId = $(window.parent.document).find("select[id='subComId']").val();
	// 渲染表格
	table.render({
		id: 'layui_table',
		elem: '#table',
		url: PATH_URL + '/inspectionPlan/getLineProjectListAuditPage',
		where: {
			fgsId: subComId,
			aiDitStatus: aiDitStatus
		},
		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 = d.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="auditView('${yjId}','${insMonth}','${id}')">审批</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 type="button" class="layui-btn layui-btn-sm layui-bg-orange" onclick="auditTotalView('${yjId}','${insMonth}','${id}')">审 批</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 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 auditView(yjId, insMonth,id) {
	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) {
			var index = layer.open({
				title: ['月计划审核'],
				type: 2,
				content: './monthPlanAudit.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,id); //aaa()为子页面的方法
	
				},
				end:function(){
					debugger;
					$('#fgsName').val('');
					$('#yjName').val('');
					$('#insMonth').val('');
					$('#aiDitStatus').val('0');
					var subComId = $(window.parent.document).find("select[id='subComId']").val();
					form.render();
					table.reload('layui_table', {
						where: {
							fgsId: subComId,
							fgsName: "",
							yjName: "",
							insMonth: "",
							aiDitStatus: "0",
						}
					})
				}
			});
		},
		
	})
}

//总公司审批
function auditTotalView(yjId, insMonth,id) {
	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) {
			var index = layer.open({
				title: ['月计划审核'],
				type: 2,
				content: './monthPlanAuditTotal.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,id); //aaa()为子页面的方法
	
				},
				end:function(){
					$('#fgsName').val('');
					$('#yjName').val('');
					$('#insMonth').val('');
					$('#aiDitStatus').val('0');
					var subComId = $(window.parent.document).find("select[id='subComId']").val();
					form.render();
					table.reload('layui_table', {
						where: {
							fgsId: subComId,
							fgsName: "",
							yjName: "",
							insMonth: "",
							aiDitStatus: "0",
						}
					})
				}
			});
		},
		
	})
}

//导出
function exportData() {

	let fgsName = $('#fgsName').val();
	let yjName = $('#yjName').val();
	let insMonth = $('#insMonth').val();
	let aiDitStatus = $('#aiDitStatus').val();
	var subComId = $(window.parent.document).find("select[id='subComId']").val();
	let fgsId = subComId

	window.location.href = PATH_URL + `/inspectionPlan/exportAuditPlanData?token=` + token +
		"&fgsName=" + fgsName +
		"&yjName=" + yjName +
		"&insMonth=" + insMonth +
		"&aiDitStatus=" + aiDitStatus + "&fgsId=" + fgsId

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