var yjId = $(window.parent.document).find("input[id='taskSourceId']").attr("stationId");
var pers = []
var table, form, laydate
var times = getCurrentDateTimeArr();
layui.use(['form', 'layer', 'table', 'laydate'], function() {
	table = layui.table;
	form = layui.form;
	laydate = layui.laydate;
	// 渲染时间选择器
	laydate.render({
		elem: '#wagesMonth',
		type: 'month',
		format: 'yyyy-MM',
		fullPanel: true,
		value: times[0] + "-" + times[1]
	});


	table.on('tool(layui_table)', function(obj) {
		switch (obj.event) {
			case 'details':
				console.log('详情', obj)
				detailsView()
				break;
			case 'edit':
				console.log('修改', obj)
				updateView()
				break;
			case 'del':
				console.log('删除', obj)
				delView(yjId, obj.data.wagesMonth)
				break;
		}
	});
	init();
});


function queryClick() {
	var yjId = $(window.parent.document).find("input[id='taskSourceId']").attr("stationId");
	var yjName = $("#yjName").val();
	var startWagesMonth = $("#wagesMonth").val().split('~')[0];
	var endWagesMonth = $("#wagesMonth").val().split('~')[1];

	table.reload('layui_table', {
		// //设定异步数据接口的额外参数
		page: {
			curr: 1
		},
		where: {
			yjName: yjName,
			startWagesMonth: startWagesMonth,
			endWagesMonth: endWagesMonth,
			yjId: yjId
		}
	})
}

function resetClick() {
	var yjId = $(window.parent.document).find("input[id='taskSourceId']").attr("stationId");
	$("#yjName").val('');
	$("#wagesMonth").val(times[0] + "-" + times[1]);

	// 对表格进行重载。id 即 table表格的id
	table.reload('layui_table', {
		// //设定异步数据接口的额外参数
		page: {
			curr: 1
		},
		where: {
			yjName: '',
			startWagesMonth: '',
			endWagesMonth: '',
			yjId: yjId
		}
	})
}

/**
 * 初始化数据
 */
function init() {
	var yjId = $(window.parent.document).find("input[id='taskSourceId']").attr("stationId");
	// 渲染表格
	table.render({
		id: 'layui_table',
		elem: '#table',
		url: PATH_URL + '/wages/getWagesEscalationList',
		where: {
			yjId: yjId
		},
		page: true, //开启分页
		method: 'post',
		skin: 'line', // 表格样式
		cols: [
			[{
					title: '序号',
					field: 'zizeng',
					align: 'center',
					type: 'numbers'
				},
				{
					field: 'fgsName',
					title: '分公司',
					align: 'center',
				},
				{
					field: 'yjName',
					title: '运检站',
					align: 'center',
				},
				{
					field: 'wagesMonth',
					title: '月份',
					align: 'center',
				},
				{
					field: 'peopleNum',
					title: '人数',
					align: 'center',
				},
				{
					field: 'xsNum',
					title: '巡视运检站（基*次）',
					align: 'center',
				},
				{
					field: 'txNum',
					title: '特巡天数（人*天）',
					align: 'center',
					width: 170
				},
				{
					field: 'phonePrice',
					title: '通讯费',
					align: 'center',
					width: 170
				},
				{
					field: 'reward',
					title: '奖惩',
					align: 'center',
				},
				{
					field: 'wages',
					title: '实发金额',
					align: 'center',
				},
				{
					title: '操作',
					toolbar: '#opeator-bar',
					align: 'center',
					width: 200
				}
			]
		],
		done: function(res, curr, count) {
			//checkPermission();
		}
	});

}

/**
 * 生成工资单页面
 * */
function add() {
	var height = '98%';
	var width = '99%';
	var index = layer.open({
		title: ['生成工资单页面', 'color:#3B70A1;background-color:#E8ECEB;font-size:20px'],
		type: 2,
		content: './producePayroll.html',
		area: [width, height],
		maxmin: false,
		success: function(layero, index) {
			var yjId = $(window.parent.document).find("input[id='taskSourceId']").attr("stationId");
			var yjName = $(window.parent.document).find("input[id='taskSourceId']").attr("stationName");
			var gsName = $(window.parent.document).find("input[id='taskSourceId']").attr("gsName");
			// //打开页面成功时执行
			var myIframe = window[layero.find('iframe')[0]['name']];
			//getTaskSource()为子页面的方法
			myIframe.setData(yjId, yjName, gsName);

		},
		end: function() {
			var sx = localStorage.getItem("sx")
			if (sx == "1") {
				queryClick();
			}
		}
	});
}



/**
 * 查看
 * */
function detailsView() {
	let height = '98%';
	let width = '99%';
	//测试
	var index = layer.open({
		title: ['详情', 'color:#3B70A1;background-color:#E8ECEB;font-size:20px'],
		type: 2,
		content: './producePayrollDetails.html',
		area: [width, height],
		maxmin: false,
		success: function(layero, index) {
			var yjId = $(window.parent.document).find("input[id='taskSourceId']").attr("stationId");
			var yjName = $(window.parent.document).find("input[id='taskSourceId']").attr("stationName");
			var gsName = $(window.parent.document).find("input[id='taskSourceId']").attr("gsName");

			var wagesMonth = $("#wagesMonth").val();
			// //打开页面成功时执行
			var myIframe = window[layero.find('iframe')[0]['name']];
			//getTaskSource()为子页面的方法
			myIframe.setData(yjId, yjName, gsName, wagesMonth);

		},
	});
}



/**
 * 修改
 * */
function updateView(id) {
	let height = '98%';
	let width = '99%';

	var index = layer.open({
		title: ['修改', 'color:#3B70A1;background-color:#E8ECEB;font-size:20px'],
		type: 2,
		content: './producePayrollUpdate.html',
		area: [width, height],
		maxmin: false,
		success: function(layero, index) {
			var yjId = $(window.parent.document).find("input[id='taskSourceId']").attr("stationId");
			var yjName = $(window.parent.document).find("input[id='taskSourceId']").attr("stationName");
			var gsName = $(window.parent.document).find("input[id='taskSourceId']").attr("gsName");

			var wagesMonth = $("#wagesMonth").val();
			// //打开页面成功时执行
			var myIframe = window[layero.find('iframe')[0]['name']];
			//getTaskSource()为子页面的方法
			myIframe.setData(yjId, yjName, gsName, wagesMonth);
		},
		end: function() {
			var sx = localStorage.getItem("sx")
			if (sx == "1") {
				queryClick();
			}

		}
	});
}



/**
 * 删除
 * */
function delView(yjId, wagesMonth) {
	let index = layer.confirm("是否确定删除数据？", function() {
		$.ajax({
			type: 'post',
			url: PATH_URL + '/wages/delWagesId',
			contentType: "application/json; charset=utf-8",
			data: JSON.stringify({
				'yjId': yjId,
				'wagesMonth': wagesMonth
			}),
			success: function(data) {
				layer.close(index);
				if (data.code == 200) {
					parent.layer.msg('删除成功', {
						icon: 1
					});
					queryClick();
				} else {
					layer.msg("删除失败", {
						icon: 2
					}, function() {
						// example.ajax.reload(); // 刷新页面
					});
				}
			}
		})
	})

}

function importData() {
	var height = '80%';
	var width = '50%';
	var index = layer.open({
		title: [`导入`, 'color:#3B70A1;background-color:#E8ECEB;font-size:20px'],
		type: 2,
		// content: 'child/orgInformation.html',
		content: 'staffImport.html',
		area: [width, height],
		maxmin: false,
		success: function(layero, index) {
			/*var myIframe = parent.window[layero.find('iframe')[0]['name']];
			var json = {'proIds': proId}
			myIframe.setParams(json)*/
		},
		yes: function(index, layero) {},
		end: function() {}
	});
}

function importDataThread(formData, idx) {
	$.ajax({
		url: oiPlanUrl + "/basePerson/importData",
		type: 'POST',
		async: false,
		data: formData,
		timeout: 20000,
		// 告诉jQuery不要去处理发送的数据
		processData: false,
		// 告诉jQuery不要去设置Content-Type请求头
		contentType: false,
		success: function(data) {
			layer.close(idx);
			if (data.code == 200) {
				layer.alert(data.data, {
					icon: 1
				});
				// example.ajax.reload(); // 刷新页面
			} else {
				layer.alert(data.msg, {
					icon: 2
				});
			}

		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			console.log(JSON.stringify(errorThrown));
			layer.close(idx);
		}
	});
	$("#basePersonFile").val("");
}

//导出
function exportData() {
	var yjName = $("#yjName").val();
	var wagesMonth = $("#wagesMonth").val();

	window.location.href = PATH_URL + `/wages/exportWagesEscalationData?token=` + token +
		"&yjName=" + yjName +
		"&wagesMonth=" + wagesMonth
}