NavBar.setActive("navbar-hr");

function refresh_hr() {
	oEmpTable.fnDraw();
}

$(document).ready(function() {

	dept_list();

	info_dept_list();

	info_post_list();

	info_mgr_list();

	// info_isr_list();

		paystafflist();

		document.getElementById("btn-download-emp").disabled = true;
	});

$("#btn-dispatch-clear").click(function() {
	clear_dispatch();
});
function clear_dispatch() {
	$("#search-empid").val("");
	$("#search-empname").val("");
	$("#search-empstate").val("");
	$("#search-deptname").val("");
	$("#search-icid").val("");
}

function dept_list() {
	$.post("dept!deptlist?tm=" + new Date().getTime(), {}, function(data) {
		var deptlist = data.deptlist;
		if (deptlist.length == 0) {
			return false;
		}
		document.getElementById("search-deptname").options.length = 0;
		var option = document.createElement("option");
		option.setAttribute("value", "");
		option.innerHTML = "";
		document.getElementById("search-deptname").appendChild(option);
		for ( var i = 0; i < deptlist.length; i++) {
			var temp = deptlist[i];
			var option = document.createElement("option");
			option.setAttribute("value", (temp[1]));
			option.innerHTML = temp[1];
			document.getElementById("search-deptname").appendChild(option);
		}
	}, "json");
}

function info_dept_list() {
	$.post("dept!deptlist?tm=" + new Date().getTime(), {}, function(data) {
		var deptlist = data.deptlist;
		if (deptlist.length == 0) {
			return false;
		}
		document.getElementById("txt-deptname").options.length = 0;
		var option = document.createElement("option");
		option.setAttribute("value", "");
		option.innerHTML = "";
		document.getElementById("txt-deptname").appendChild(option);
		for ( var i = 0; i < deptlist.length; i++) {
			var temp = deptlist[i];
			var option = document.createElement("option");
			option.setAttribute("value", (temp[1]));
			option.innerHTML = temp[1];
			document.getElementById("txt-deptname").appendChild(option);
		}
	}, "json");
}

function info_isr_list() {
	$.post("insurance!isrlist?tm=" + new Date().getTime(), {}, function(data) {
		var isrlist = data.isrlist;
		if (isrlist.length == 0) {
			return false;
		}
		document.getElementById("txt-isrtype").options.length = 0;
		var option = document.createElement("option");
		option.setAttribute("value", "");
		option.innerHTML = "";
		document.getElementById("txt-isrtype").appendChild(option);
		for ( var i = 0; i < isrlist.length; i++) {
			var temp = isrlist[i];
			var option = document.createElement("option");
			option.setAttribute("value", (temp[0]));
			option.innerHTML = temp[0];
			document.getElementById("txt-isrtype").appendChild(option);
		}
	}, "json");
}

function info_post_list() {
	$.post("post!postmap?tm=" + new Date().getTime(), {}, function(data) {
		var postlist = data.postlist;
		if (postlist.length == 0) {
			return false;
		}
		document.getElementById("txt-emppost").options.length = 0;
		var option = document.createElement("option");
		option.setAttribute("value", "");
		option.innerHTML = "";
		document.getElementById("txt-emppost").appendChild(option);
		for ( var i = 0; i < postlist.length; i++) {
			var temp = postlist[i];
			var option = document.createElement("option");
			option.setAttribute("value", (temp[2]));
			option.innerHTML = temp[2];
			document.getElementById("txt-emppost").appendChild(option);
		}
	}, "json");
}

function info_mgr_list() {
	$.post("emp!mgrlist?tm=" + new Date().getTime(), {}, function(data) {
		var mgrInfo = data.mgrInfo;
		if (mgrInfo != "success") {
			alert("获取驻厂管理员账户失败");
			return false;
		}
		var mgrList = data.mgrList;
		if (mgrList.length == 0) {
			return false;
		}
		document.getElementById("txt-manager").options.length = 0;
//		var option = document.createElement("option");
//		option.setAttribute("value", "");
//		option.innerHTML = "";
//		document.getElementById("txt-manager").appendChild(option);
		for ( var i = 0; i < mgrList.length; i++) {
			var temp = mgrList[i];
			var option = document.createElement("option");
			option.setAttribute("value", temp[0]);
			option.innerHTML = temp[0] + "-" + temp[1] + "-" + temp[2];
			document.getElementById("txt-manager").appendChild(option);
		}
	}, "json");
}

function paystafflist() {
	$.post("paystaff!jsonlist?tm=" + new Date().getTime(), {}, function(data) {
		var jsonlist = data.jsonlist;
		if (jsonlist.length == 0) {
			return false;
		}
		document.getElementById("txt-isrtype").options.length = 0;
		var option = document.createElement("option");
		option.setAttribute("value", "");
		option.innerHTML = "";
		document.getElementById("txt-isrtype").appendChild(option);
		for ( var i = 0; i < jsonlist.length; i++) {
			var temp = jsonlist[i];
			var option = document.createElement("option");
			option.setAttribute("value", (temp[1] + "（" + temp[0] + "）"));
			option.innerHTML = (temp[1] + "（" + temp[0] + "）");
			document.getElementById("txt-isrtype").appendChild(option);
		}
	}, "json");

}

$("#a-hr-contract").click(function() {
	$("#contract-modify-modal").modal("toggle");
});

$("#a-hr-attendance").click(function() {
	$("#attendance-modify-modal").modal("toggle");
});

$('#hr-sendstaff-update').click(function() {
	editemp();
});

$("#a-hr-holiday").click(function() {
	$("#holiday-modify-modal").modal("toggle");
});
$("#a-hr-dimission").click(function() {
	$("#dimission-modify-modal").modal("toggle");
});
$("#a-hr-dispatch").click(function() {
	$("#dispatch-modify-modal").modal("toggle");
});

$("#a-hr-beformal").click(function() {
	$("#beformal-modify-modal").modal("toggle");
});

$("#btn-emp-select").click(function() {
	selectall_emp();
});

$("#btn-emp-cancel").click(function() {
	cancelall_emp();
});

function selectall_emp() {
	var sel = document.getElementsByName("check-emp-delete");
	for ( var i = 0; i < sel.length; i++) {
		sel[i].parentNode.setAttribute("class","checked");
		sel[i].checked = true;
	}
}

function cancelall_emp() {
	var sel = document.getElementsByName("check-emp-delete");
	for ( var i = 0; i < sel.length; i++) {
		sel[i].parentNode.setAttribute("class","");
		sel[i].checked = false;
	}
}

$("#btn-emp-delete").click(function() {
	var list = "";
	var deleted = document.getElementsByName("check-emp-delete");
	var count = 0;
	for ( var i = 0; i < deleted.length; i++) {
		if (deleted[i].checked == true) {
			list = list + ";" + deleted[i].value;
			count++;
		}
	}
	if (count == 0) {
		alert('您没有勾选任何记录');
		return false;
	}
	list = list.substring(1);
	$.post("emp!deleteList", {
		"deleteList" : list
	}, function(data) {
		var info = data.deleteInfo;
		if (info == "DeleteSuccess") {
			alert("删除成功");
			refresh_hr();
		} else {
			alert(info);
		}
	}, "json");
	return false;
});

$("#btn-download-emp").click(function() {
	window.location.href = "emp!exportsendstaff";
	return false;
});

function setEditTable(oTable, arry, i) {
	// oTable._fnClearTable();
	var aiNew = oTable.fnAddData( [ arry[0], arry[1], arry[2], arry[3],
			arry[4], '', '' ]);
	var nRow = oTable.fnGetNodes(aiNew[0]);
	var aData = oTable.fnGetData(nRow);
	var jqTds = $('>td', nRow);
	jqTds[0].innerHTML = aData[0];
	jqTds[1].innerHTML = aData[1];
	jqTds[2].innerHTML = aData[2];
	jqTds[3].innerHTML = aData[3];
	jqTds[4].innerHTML = aData[4];
	nRow.id = i;
	jqTds[5].innerHTML = '<a class="edit" href="">编辑</a>';
	jqTds[6].innerHTML = '<a class="delete" href="">删除</a>';
}

var edu_count = 0;
var work_count = 0;

function setsendstaff(emp) {
	Hr_Edu_Table.fnClearTable();
	Hr_Work_Table.fnClearTable();
	edu_map = new HashMap();
	work_map = new HashMap();
	if (emp.eduback != null && emp.eduback != "") {
		var eduback = emp.eduback.split(';');
		for ( var i = 0; i < eduback.length; i++) {
			var oneedu = eduback[i];
			edu_map.put('edu_idx_' + i, oneedu);
			var rows = oneedu.split(',');
			var eduArray = new Array();
			for ( var j = 0; j < rows.length; j++) {
				eduArray.push(rows[j]);
			}
			setEditTable(Hr_Edu_Table, eduArray, i);
		}
		edu_count = eduback.length;
	}
	if (emp.workback != null && emp.workback != "") {
		var workback = emp.workback.split(';');
		for ( var i = 0; i < workback.length; i++) {
			var onework = workback[i];
			work_map.put('work_idx_' + i, onework);
			var rows = onework.split(',');
			var workArray = new Array();
			for ( var j = 0; j < rows.length; j++) {
				workArray.push(rows[j]);
			}
			setEditTable(Hr_Work_Table, workArray, i);
		}
		work_count = workback.length;
	}

	var id = $("#txt-emp-id").val(emp.id);
	var empid = $("#txt-empid").val(emp.empid);
	var empname = $("#txt-empname").val(emp.empname);
	var usedname = $("#txt-usedname").val(emp.usedname);
	var hometown = $("#txt-hometown").val(emp.hometown);
	var hukoutype = $("#txt-hukoutype").val(emp.hukoutype);
	var ethnic = $("#txt-ethnic").val(emp.ethnic);
	var icid = $("#txt-icid").val(emp.icid);
	var birthday = $("#txt-birthday").val(emp.birthday);
	var gender = $("#txt-gender").val(emp.gender);
	var married = $("#txt-married").val(emp.married);
	var education = $("#txt-education").val(emp.education);
	var phonenum = $("#txt-phonenum").val(emp.phonenum);
	var qq = $("#txt-qq").val(emp.qq);
	var telephone = $("#txt-telephone").val(emp.telephone);
	var linkman = $("#txt-linkman").val(emp.linkman);
	var linkphone = $("#txt-linkphone").val(emp.linkphone);
	var address = $("#txt-address").val(emp.address);
	var empdesc = $("#txt-empdesc").val(emp.empdesc);

	var proin = $("#txt-proin").val(emp.proin);
	var manager = $("#txt-manager").val(emp.manager);
	var emptype = $("#txt-emptype").val(emp.emptype);
	var deptname = $("#txt-deptname").val(emp.deptname);
	var emppost = $("#txt-emppost").val(emp.emppost);
	var empstate = $("#txt-empstate").val(emp.empstate);
	var rsgdate = $("#txt-rsgdate").val(emp.rsgdate);
	var rsgdesc = $("#txt-rsgdesc").val(emp.rsgdesc);

	var consalary = $("#txt-consalary").val(emp.consalary);
	var bassalary = $("#txt-bassalary").val(emp.bassalary);
	var paytype = $("#txt-paytype").val(emp.paytype);
	var bankname = $("#txt-bankname").val(emp.bankname);
	var bankcard = $("#txt-bankcard").val(emp.bankcard);
	var taxtype = $("#txt-taxtype").val(emp.taxtype);
	var resprem = $("#txt-resprem").val(emp.resprem);
	var isrtype = $("#txt-isrtype").val(emp.isrtype);
	var isrcard = $("#txt-isrcard").val(emp.isrcard);
	var isrpcid = $("#txt-isrpcid").val(emp.isrpcid);

	var senddate = $("#txt-senddate").val(emp.senddate);
	var senddesc = $("#txt-senddesc").val(emp.senddesc);
	var suplycom = $("#txt-suplycom").val(emp.suplycom);
	var sendcom = $("#txt-sendcom").val(emp.sendcom);
	var servfee = $("#txt-servfee").val(emp.servfee);

	$.post("emp!mgrlist", {}, function(data) {
		var mgrInfo = data.mgrInfo;
		if (mgrInfo != "success") {
			alert("获取驻厂管理员账户失败");
			return false;
		}
		var mgrList = data.mgrList;
		if (mgrList.length == 0) {
			return false;
		}
		document.getElementById("txt-manager").options.length = 0;
		for ( var i = 0; i < mgrList.length; i++) {
			var temp = mgrList[i];
			var option = document.createElement("option");
			option.setAttribute("value", temp[0]);
			option.innerHTML = temp[1] + "-" + temp[1] + "-" + temp[2];
			document.getElementById("txt-manager").appendChild(option);
		}
		var manager = $("#txt-manager").val(emp.manager);
	}, "json");

}

function clearedit() {
	var id = $("#txt-emp-id").val();
	var empid = $("#txt-empid").val("");
	var empname = $("#txt-empname").val("");
	var usedname = $("#txt-usedname").val("");
	var hometown = $("#txt-hometown").val("");
	var hukoutype = $("#txt-hukoutype").val("");
	var ethnic = $("#txt-ethnic").val("");
	var icid = $("#txt-icid").val("");
	var birthday = $("#txt-birthday").val("");
	var gender = $("#txt-gender").val("");
	var married = $("#txt-married").val("");
	var education = $("#txt-education").val("");
	var phonenum = $("#txt-phonenum").val("");
	var qq = $("#txt-qq").val("");
	var telephone = $("#txt-telephone").val("");
	var linkman = $("#txt-linkman").val("");
	var linkphone = $("#txt-linkphone").val("");
	var address = $("#txt-address").val("");
	var empdesc = $("#txt-empdesc").val("");

	var proin = $("#txt-proin").val("");
	var manager = $("#txt-manager").val("");
	var emptype = $("#txt-emptype").val("");
	var deptname = $("#txt-deptname").val("");
	var emppost = $("#txt-emppost").val("");
	var empstate = $("#txt-empstate").val("");
	var rsgdate = $("#txt-rsgdate").val("");
	var rsgdesc = $("#txt-rsgdesc").val("");

	var consalary = $("#txt-consalary").val("");
	var bassalary = $("#txt-bassalary").val("");
	var paytype = $("#txt-paytype").val("");
	var bankname = $("#txt-bankname").val("");
	var bankcard = $("#txt-bankcard").val("");
	var taxtype = $("#txt-taxtype").val("");
	var resprem = $("#txt-resprem").val("");
	var isrtype = $("#txt-isrtype").val("");
	var isrcard = $("#txt-isrcard").val("");
	var isrpcid = $("#txt-isrpcid").val("");

	var senddate = $("#txt-senddate").val("");
	var senddesc = $("#txt-senddesc").val("");
	var suplycom = $("#txt-suplycom").val("");
	var sendcom = $("#txt-sendcom").val("");
	var servfee = $("#txt-servfee").val("");

}

function editemp() {
	// var vv = $("#form-editemp").valid();
	// console.log(vv);
	// if (!vv) {
	// return false;
	// }

	var id = $("#txt-emp-id").val();
	var empid = $("#txt-empid").val();
	var empname = $("#txt-empname").val();
	var usedname = $("#txt-usedname").val();
	var hometown = $("#txt-hometown").val();
	var hukoutype = $("#txt-hukoutype").val();
	var ethnic = $("#txt-ethnic").val();
	var icid = $("#txt-icid").val();
	var birthday = $("#txt-birthday").val();
	var gender = $("#txt-gender").val();
	var married = $("#txt-married").val();
	var education = $("#txt-education").val();
	var phonenum = $("#txt-phonenum").val();
	var qq = $("#txt-qq").val();
	var telephone = $("#txt-telephone").val();
	var linkman = $("#txt-linkman").val();
	var linkphone = $("#txt-linkphone").val();
	var address = $("#txt-address").val();
	var empdesc = $("#txt-empdesc").val();

	var proin = $("#txt-proin").val();
	var manager = $("#txt-manager").val();
	var emptype = $("#txt-emptype").val();
	var deptname = $("#txt-deptname").val();
	var emppost = $("#txt-emppost").val();
	var empstate = $("#txt-empstate").val();
	var rsgdate = $("#txt-rsgdate").val();
	var rsgdesc = $("#txt-rsgdesc").val();

	var consalary = $("#txt-consalary").val();
	var bassalary = $("#txt-bassalary").val();
	var paytype = $("#txt-paytype").val();
	var bankname = $("#txt-bankname").val();
	var bankcard = $("#txt-bankcard").val();
	var taxtype = $("#txt-taxtype").val();
	var resprem = $("#txt-resprem").val();
	var isrtype = $("#txt-isrtype").val();
	var isrcard = $("#txt-isrcard").val();
	var isrpcid = $("#txt-isrpcid").val();

	var senddate = $("#txt-senddate").val();
	var senddesc = $("#txt-senddesc").val();
	var suplycom = $("#txt-suplycom").val();
	var sendcom = $("#txt-sendcom").val();
	var servfee = $("#txt-servfee").val();

	var eduback = '';
	var keys = edu_map.keys();
	if (keys.length != 0) {
		for ( var i = 0; i < keys.length; i++) {
			eduback += ';' + edu_map.get(keys[i]);
		}
		eduback = eduback.substring(1, eduback.length);
	}

	var workback = '';
	keys = work_map.keys();
	if (keys.length != 0) {
		for ( var i = 0; i < keys.length; i++) {
			workback += ';' + work_map.get(keys[i]);
		}
		workback = workback.substring(1, workback.length);
	}

	if(deptname == '') {
		alert('请先在岗位资料中选择所属部门');
		return false;
	}
	
	$.post("emp!edit", {
		"newEmp.id" : id,
		"newEmp.empid" : empid,
		"newEmp.empname" : empname,
		"newEmp.usedname" : usedname,
		"newEmp.hometown" : hometown,
		"newEmp.ethnic" : ethnic,
		"newEmp.hukoutype" : hukoutype,
		"newEmp.icid" : icid,
		"newEmp.birthday" : birthday,
		"newEmp.gender" : gender,
		"newEmp.married" : married,
		"newEmp.education" : education,
		"newEmp.phonenum" : phonenum,
		"newEmp.qq" : qq,
		"newEmp.telephone" : telephone,
		"newEmp.linkman" : linkman,
		"newEmp.linkphone" : linkphone,
		"newEmp.address" : address,
		"newEmp.empdesc" : empdesc,
		"newEmp.eduback" : eduback,
		"newEmp.workback" : workback,

		"newEmp.proin" : proin,
		"newEmp.manager" : manager,
		"newEmp.emptype" : emptype,
		"newEmp.deptname" : deptname,
		"newEmp.emppost" : emppost,
		"newEmp.empstate" : empstate,
		"newEmp.rsgdate" : rsgdate,
		"newEmp.rsgdesc" : rsgdesc,

		"newEmp.consalary" : consalary,
		"newEmp.bassalary" : bassalary,
		"newEmp.paytype" : paytype,
		"newEmp.bankname" : bankname,
		"newEmp.bankcard" : bankcard,
		"newEmp.taxtype" : taxtype,
		"newEmp.resprem" : resprem,
		"newEmp.isrtype" : isrtype,
		"newEmp.isrcard" : isrcard,
		"newEmp.isrpcid" : isrpcid,

		"newEmp.senddate" : senddate,
		"newEmp.senddesc" : senddesc,
		"newEmp.suplycom" : suplycom,
		"newEmp.sendcom" : sendcom,
		"newEmp.servfee" : servfee
	}, function(data) {
		var info = data.editInfo;
		if (info == "EditSuccess") {
			alert("修改成功");
			$("#a-worker-search").click();
			refresh_hr();
		} else {
			alert(info);
		}
		// clearedit();
		}, "json");
	return false;
}

var issearchemp = 0;

$("#btn-emp-search").click(function() {
	issearchemp = 1;
	oEmpTable.fnDraw();
	return false;
});

$("#hr-info-modify").click(function() {
	editemp();
});

var allData = null;

var oEmpTable = $("#EmpList")
		.dataTable(
				{
					"sAjaxSource" : "emp!sendstaff",
					"aoColumns" : [ {
						"mData" : "id"
					}, {
						"mData" : "empid"
					}, {
						"mData" : "empname"
					}, {
						"mData" : "deptname"
					}, {
						"mData" : "emppost"
					}, {
						"mData" : "icid"
					}, {
						"mData" : "empstate"
					}, {
						"mData" : "emptype"
					}, {
						"mData" : "id"
					} ],
					"bProcessing" : true,
					"bServerSide" : true,
					"fnServerData" : retrieveData,
					"bFilter" : true,
					"bSort" : true,
					"bStateSave" : false,
					"sPaginationType" : "full_numbers",
					"oLanguage" : {
						"sLengthMenu" : "每页显示 _MENU_ 条记录",
						"sZeroRecords" : "抱歉， 没有记录",
						"sInfo" : "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
						"sInfoEmpty" : "没有数据",
						"sInfoFiltered" : "(从 _MAX_ 条数据中检索)",
						"oPaginate" : {
							"sFirst" : "首页",
							"sPrevious" : "前一页",
							"sNext" : "后一页",
							"sLast" : "尾页"
						},
						"sSearch" : "搜索：",
						"sProcessing" : "正在加载数据..."
					},
					"aoColumnDefs" : [
							{
								"aTargets" : [ 0 ],
								"fnCreatedCell" : function(nTd, sData, oData,
										iRow, iCol) {
									var check_del = $("<input name='check-emp-delete' type='checkbox' value='"
											+ oData.id + "'></input>");
									check_del.on('click', function() {
										return true;
									});
									$(nTd).empty();
									$(nTd).prepend(check_del);
								}
							},
							{
								"aTargets" : [ 8 ],
								"fnCreatedCell" : function(nTd, sData, oData,
										iRow, iCol) {
									var btn_edit = $('<a style="margin:0 2px 0 2px;cursor:pointer;">详情</a>');
									btn_edit.on('click', function() {
										setsendstaff(oData);
										$("#emp-modify-modal").modal("toggle");
										return false;
									});
									var btn_contract = $('<a id="btn-insurance" style="margin:0 2px 0 2px;cursor:pointer;">合同</a>');
									btn_contract.on("click", function(e) {
										var empid = oData.empid;
										$.post("contract!jsonlist", {
											"empid" : empid
										}, function(data) {
											var searchInfo = data.searchInfo;
											var listall = eval("(" + searchInfo
													+ ")");
											var conlist = listall.conlist;
											setconlist(conlist);
											return false;
										}, "json");
										$("#contract-records-modal").modal(
												"toggle");
										return false;
									});
									var btn_change = $('<a id="btn-change" style="margin:0 2px 0 2px;cursor:pointer;">异动</a>');
									btn_change
											.on(
													'click',
													function() {
														// setemp(oData);
														empid = oData.empid;
														empname = oData.empname;
														is_change_list = false;
														$
																.post(
																		"empchange!jsonlist",
																		{
																			"empid" : empid
																		},
																		function(
																				data) {
																			var searchInfo = data.searchInfo;
																			var changelist = eval("("
																					+ searchInfo
																					+ ")");
																			setchangelist(changelist);
																		},
																		"json");
														$("#change-info-modal")
																.modal("toggle");
														return false;
													});
									var btn_del = $('<a id="btn-del" style="margin:0 2px 0 2px;cursor:pointer;">删除</a>');
									btn_del
											.on(
													'click',
													function(e) {
														var c = confirm("确认删除么？");
														if (c == true) {
															$
																	.post(
																			"emp!delete?id="
																					+ oData.id,
																			{},
																			function(
																					data) {
																				var info = data.deleteInfo;
																				if (info == "DeleteSuccess") {
																					alert("删除成功");
																					refresh_hr();
																				} else {
																					alert(info);
																				}
																			},
																			"json");
														} else {
														}
														return false;
													});

									var btn_con = $('<a id="b-c-' + oData.id + '" style="display:none"></a>');
									btn_con.on('click', function(e) {
										$("#txt-contract-empid").val(
												oData.empid);
										$("#txt-contract-empname").val(
												oData.empname);
										return false;
									});

									var btn_bfm = $('<a id="b-m-' + oData.id + '" style="display:none"></a>');
									btn_bfm.on('click', function(e) {
										$("#txt-beformal-id").val(oData.id);
										$("#txt-beformal-empid").val(
												oData.empid);
										$("#txt-beformal-empname").val(
												oData.empname);
										$("#txt-beformal-proin").val(
												oData.proin);
										return false;
									});

									var btn_atd = $('<a id="e-a-' + oData.id + '" style="display:none"></a>');
									btn_atd.on('click', function(e) {
										$("#txt-hr-attendance-empid").val(
												oData.empid);
										$("#txt-hr-attendance-empname").val(
												oData.empname);
										return false;
									});

									var btn_lve = $('<a id="e-l-' + oData.id + '" style="display:none"></a>');
									btn_lve.on('click', function(e) {
										$("#txt-leave-empid").val(oData.empid);
										return false;
									});

									var btn_rsg = $('<a id="e-r-' + oData.id + '" style="display:none"></a>');
									btn_rsg.on('click',
											function(e) {
												$("#txt-resign-id").val(
														oData.id);
												$("#txt-resign-empid").val(
														oData.empid);
												$("#txt-resign-empname").val(
														oData.empname);
												$("#txt-resign-resigndate")
														.val(oData.rsgdate);
												$("#txt-resign-resigndesc")
														.val(oData.rsgdesc);
												return false;
											});

									var btn_dis = $('<a id="e-d-' + oData.id + '" style="display:none"></a>');
									btn_dis.on('click', function(e) {
										$("#txt-dispatch-empid").val(
												oData.empid);
										$("#txt-dispatch-empname").val(
												oData.empname);
										return false;
									});
									var btn_dispatch = $('<a id="btn-dispatch" style="margin:0 2px 0 2px;cursor:pointer;">派遣</a>');
									btn_dispatch.on('click', function() {
										empid = oData.empid;
										empname = oData.empname;
										$.post("dispatch!jsonlist", {
											"empid" : empid
										}, function(data) {
											var searchInfo = data.searchInfo;
											var listall = eval("(" + searchInfo
													+ ")");
											var dislist = listall.dislist;
											setdislist(dislist);
											return false;
										}, "json");
										$("#dispatch-modify-modal").modal(
												"toggle");
										return false;
									});
									$(nTd).empty();
									$(nTd).prepend(btn_contract);
									$(nTd).prepend(btn_dispatch);
									$(nTd).prepend(btn_change);
									$(nTd).prepend(btn_dis);
									$(nTd).prepend(btn_rsg);
									$(nTd).prepend(btn_lve);
									$(nTd).prepend(btn_atd);
									$(nTd).prepend(btn_bfm);
									$(nTd).prepend(btn_con);
									$(nTd).prepend(btn_del);
									$(nTd).prepend(btn_edit);
								}
							} ]
				});

function retrieveData(sSource, aoData, fnCallback) {
	aoData.push( {
		"name" : "searchEmpid",
		"value" : $("#search-empid").val()
	}, {
		"name" : "searchEmpname",
		"value" : $("#search-empname").val()
	}, {
		"name" : "searchEmptype",
		"value" : $("#search-emptype").val()
	}, {
		"name" : "searchDeptid",
		"value" : $("#search-deptname").val()
	}, {
		"name" : "searchEstate",
		"value" : $("#search-empstate").val()
	}, {
		"name" : "searchIcid",
		"value" : $("#search-icid").val()
	}, {
		"name" : "pvlg_emp_search",
		"value" : ""
	}, {
		"name" : "pvlg_attendance_upload",
		"value" : ""
	}, {
		"name" : "pvlg_attendance_download",
		"value" : ""
	});
	$
			.ajax( {
				type : "POST",
				url : sSource,
				dataType : "json",
				data : "emptype=1&issearchemp=" + issearchemp + "&jsonemp="
						+ JSON.stringify(aoData),
				success : function(data) {
					var info = data.pvlg_emp_search;
					if (info != "") {
						$("#pvlg_emp_search").text(info);
					}
					var pvlg_emp_upload = data.pvlg_emp_upload;
					var pvlg_emp_download = data.pvlg_emp_download;
					document.getElementById("btn-download-emp").disabled = (pvlg_emp_download == false);
					fnCallback(data);
					issearchemp = 0;
				}
			});
}

// /////////////////////////////////////////////////////////////////////////////////////////////////////

$("#a-hr-contract").click(function() {
	var deleted = $("input[name='check-emp-delete']:checked");
	if (deleted.length != 0) {
		var id = deleted[0].value;
		var bc = $("#b-c-" + id);
		bc.click();
	}
});

$("#btn-contract-add").click(function() {
	add_contract();
});

function add_contract() {
	var empid = $("#txt-contract-empid").val();
	var empname = $("#txt-contract-empname").val();
	var proin = $("#txt-contract-proin").val();
	var empin = $("#txt-contract-empin").val();
	var empout = $("#txt-contract-empout").val();
	var condesc = $("#txt-contract-condesc").val();
	$.post("contract!add", {
		"contract.empid" : empid,
		"contract.empname" : empname,
		"contract.proin" : proin,
		"contract.empin" : empin,
		"contract.empout" : empout,
		"contract.condesc" : condesc
	}, function(data) {
		var info = data.addInfo;
		if (info == "AddSuccess") {
			alert("添加成功");
		} else {
			alert(info);
		}
	}, "json");
	return false;
}

// /////////////////////////////////////////////////////////////////////////////////////////////////////
var numRegex = /^[0-9]{1,}$/;

$("#a-hr-attendance").click(function() {
	var deleted = $("input[name='check-emp-delete']:checked");
	if (deleted.length != 0) {
		var id = deleted[0].value;
		var bc = $("#e-a-" + id);
		bc.click();
	}
});

$("#hr-attendance-add").click(function() {
	add_attendance();
});

function add_attendance() {
	var empid = $("#txt-hr-attendance-empid").val();
	var empname = $("#txt-hr-attendance-empname").val();
	var atddate = $("#txt-hr-attendance-atddate").val();
	var earlyminutes = $("#txt-hr-attendance-earlyminutes").val();
	var lateminutes = $("#txt-hr-attendance-lateminutes").val();
	var absentdays = $("#txt-hr-attendance-absentdays").val();
	if (!numRegex.test(earlyminutes) || !numRegex.test(lateminutes)
			|| !numRegex.test(absentdays)) {
		alert("您输入的内容格式错误");
		return false;
	}
	$.post("attendance!add", {
		"attendance.empid" : empid,
		"attendance.empname" : empname,
		"attendance.atddate" : atddate,
		"attendance.earlyminutes" : earlyminutes,
		"attendance.lateminutes" : lateminutes,
		"attendance.absentdays" : absentdays
	}, function(data) {
		var info = data.addInfo;
		if (info == "AddSuccess") {
			alert("添加成功");
		} else {
			alert(info);
		}
	}, "json");
	return false;
}

// /////////////////////////////////////////////////////////////////////////////////////////////////////
var dateRegex = /((^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(10|12|0?[13578])([-\/\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(11|0?[469])([-\/\._])(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(0?2)([-\/\._])(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([3579][26]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][13579][26])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][13579][26])([-\/\._])(0?2)([-\/\._])(29)$))/;

$("#a-hr-holiday").click(function() {
	var deleted = $("input[name='check-emp-delete']:checked");
	if (deleted.length != 0) {
		var id = deleted[0].value;
		var bc = $("#e-l-" + id);
		bc.click();
	}
});

$("#hr-holiday-add").click(function() {
	add_leave();
});

function add_leave() {
	var empid = $("#txt-leave-empid").val();
	var lvbegin = $("#txt-leave-lvbegin").val();
	var lvend = $("#txt-leave-lvend").val();
	var lvday = $("#txt-leave-lvday").val();
	var lvcancel = $("input[name='txt-leave-lvcancel']:checked").val() == 0 ? '是'
			: '否';
	var canceldate = $("#txt-leave-canceldate").val();

	if (!numRegex.test(lvday)) {
		alert("您的部分输入内容格式有误");
		return false;
	}

	$.post("leave!add", {
		"leave.empid" : empid,
		"leave.lvbegin" : lvbegin,
		"leave.lvend" : lvend,
		"leave.lvday" : lvday,
		"leave.lvcancel" : lvcancel,
		"leave.canceldate" : canceldate
	}, function(data) {
		var info = data.addInfo;
		if (info == "AddSuccess") {
			alert("添加成功");
		} else {
			alert(info);
		}
	}, "json");
	return false;
}

// /////////////////////////////////////////////////////////////////////////////////////////////////////
$("#a-hr-dimission").click(function() {
	var deleted = $("input[name='check-emp-delete']:checked");
	if (deleted.length != 0) {
		var id = deleted[0].value;
		var bc = $("#e-r-" + id);
		bc.click();
	}
});

$("#hr-dimission-add").click(function() {
	add_resign();
});

function add_resign() {
	var id = $("#txt-resign-id").val() == "" ? 0 : $("#txt-resign-id").val();
	var empid = $("#txt-resign-empid").val();
	var resigndate = $("#txt-resign-resigndate").val();
	var resigndesc = $("#txt-resign-resigndesc").val();

	$.post("emp!resign", {
		"emp.id" : id,
		"emp.empid" : empid,
		"emp.rsgdate" : resigndate,
		"emp.rsgdesc" : resigndesc
	}, function(data) {
		var info = data.resignInfo;
		if (info == "ResignSuccess") {
			oEmpTable.fnDraw();
			alert("离职成功");
		} else {
			alert(info);
		}
	}, "json");
	return false;
}

// /////////////////////////////////////////////////////////////////////////////////////////////////////
$("#a-hr-beformal").click(function() {
	var deleted = $("input[name='check-emp-delete']:checked");
	if (deleted.length != 0) {
		var id = deleted[0].value;
		var bc = $("#b-m-" + id);
		bc.click();
	}
});

$("#hr-beformal-add").click(function() {
	add_beformal();
});

function add_beformal() {
	var empid = $("#txt-beformal-empid").val();
	var empname = $("#txt-beformal-empname").val();
	var proin = $("#txt-beformal-proin").val();
	var empin = $("#txt-beformal-empin").val();
	var empout = $("#txt-beformal-empout").val();
	var condesc = $("#txt-beformal-condesc").val();
	$.post("contract!beformal", {
		"contract.empid" : empid,
		"contract.empname" : empname,
		"contract.proin" : proin,
		"contract.empin" : empin,
		"contract.empout" : empout,
		"contract.condesc" : condesc
	}, function(data) {
		var info = data.beformalInfo;
		if (info == "BeformalSuccess") {
			oEmpTable.fnDraw();
			alert("转正成功");
		} else {
			alert(info);
		}
	}, "json");
	return false;
}

// /////////////////////////////////////////////////////////////////////////////////////////////////////
$("#a-hr-dispatch").click(function() {
	var deleted = $("input[name='check-emp-delete']:checked");
	if (deleted.length != 0) {
		var id = deleted[0].value;
		var bc = $("#e-d-" + id);
		bc.click();
	}
});

$("#hr-dispatch-add").click(function() {
	add_dispatch();
});

function add_dispatch() {
	var disid = $("#txt-dispatch-disid").val();
	var empid = $("#txt-dispatch-empid").val();
	var empname = $("#txt-dispatch-empname").val();
	var discom = $("#txt-dispatch-discom").val();
	var dispost = $("#txt-dispatch-dispost").val();
	var disin = $("#txt-dispatch-disin").val();
	var disout = $("#txt-dispatch-disout").val();
	var disdesc = $("#txt-dispatch-disdesc").val();
	var suplycom = $("#txt-dispatch-suplycom").val();
	$.post("dispatch!add", {
		"dispatch.disid" : disid,
		"dispatch.empid" : empid,
		"dispatch.empname" : empname,
		"dispatch.discom" : discom,
		"dispatch.dispost" : dispost,
		"dispatch.disin" : disin,
		"dispatch.disout" : disout,
		"dispatch.disdesc" : disdesc,
		"dispatch.suplycom" : suplycom
	}, function(data) {
		var info = data.addInfo;
		if (info == "AddSuccess") {
			alert("添加成功");
			clear_dispatch();
			oDispatchTable.fnDraw();
		} else {
			alert(info);
		}
	}, "json");
	return false;
}

// ///////////////////////////////////////////////////////////////////////////////

$("#change-modal-close").click(function() {
	HR_Change_Sendcom_Table.fnClearTable();
	HR_Change_Emppost_Table.fnClearTable();
	HR_Change_Salary_Table.fnClearTable();
	$("#change_list_empids").html("");
	issaved = true;
});
// /////////////////////////////////////////////////////////////////////////////////////////////////////

// /////////////////////////////////////////////////////////////////////////////////////////////////////
function setchangelist(changelist) {
	HR_Change_Salary_Table.fnClearTable();
	HR_Change_Emppost_Table.fnClearTable();
	HR_Change_Sendcom_Table.fnClearTable();

	var salarylist = changelist.salary;
	var emppostlist = changelist.emppost;
	var sendcomlist = changelist.sendcom;

	if (salarylist != null && salarylist.length != 0) {
		for ( var i = 0; i < salarylist.length; i++) {
			var change = salarylist[i];
			setSalaryEditTable(HR_Change_Salary_Table, change);
		}
	}
	if (emppostlist != null && emppostlist.length != 0) {
		for ( var i = 0; i < emppostlist.length; i++) {
			var change = emppostlist[i];
			setEmppostEditTable(HR_Change_Emppost_Table, change);
		}
	}
	if (sendcomlist != null && sendcomlist.length != 0) {
		for ( var i = 0; i < sendcomlist.length; i++) {
			var change = sendcomlist[i];
			setSendcomEditTable(HR_Change_Sendcom_Table, change);
		}
	}

}

function setSalaryEditTable(oTable, change) {
	// oTable._fnClearTable();
	var aiNew = oTable.fnAddData( [ change.cursendcom, change.cursalary,
			change.newsalary, change.changedate, change.changedesc, '', '' ]);
	var nRow = oTable.fnGetNodes(aiNew[0]);
	var aData = oTable.fnGetData(nRow);
	var jqTds = $('>td', nRow);
	nRow.id = change.id;
	jqTds[0].innerHTML = aData[0];
	jqTds[1].innerHTML = aData[1];
	jqTds[2].innerHTML = aData[2];
	jqTds[3].innerHTML = aData[3];
	jqTds[4].innerHTML = aData[4];
	jqTds[5].innerHTML = '<a class="edit" href="">编辑</a>';
	jqTds[6].innerHTML = '<a class="delete" href="">删除</a>';
}

function setEmppostEditTable(oTable, change) {
	// oTable._fnClearTable();
	var aiNew = oTable.fnAddData( [ change.cursendcom, change.curemppost,
			change.newemppost, change.changedate, change.changedesc, '', '' ]);
	var nRow = oTable.fnGetNodes(aiNew[0]);
	var aData = oTable.fnGetData(nRow);
	var jqTds = $('>td', nRow);
	nRow.id = change.id;
	jqTds[0].innerHTML = aData[0];
	jqTds[1].innerHTML = aData[1];
	jqTds[2].innerHTML = aData[2];
	jqTds[3].innerHTML = aData[3];
	jqTds[4].innerHTML = aData[4];
	jqTds[5].innerHTML = '<a class="edit" href="">编辑</a>';
	jqTds[6].innerHTML = '<a class="delete" href="">删除</a>';
}

function setSendcomEditTable(oTable, change) {
	// oTable._fnClearTable();
	var aiNew = oTable.fnAddData( [ change.cursendcom, change.newsendcom,
			change.changedate, change.changedesc, '', '' ]);
	var nRow = oTable.fnGetNodes(aiNew[0]);
	var aData = oTable.fnGetData(nRow);
	var jqTds = $('>td', nRow);
	nRow.id = change.id;
	jqTds[0].innerHTML = aData[0];
	jqTds[1].innerHTML = aData[1];
	jqTds[2].innerHTML = aData[2];
	jqTds[3].innerHTML = aData[3];
	jqTds[4].innerHTML = '<a class="edit" href="">编辑</a>';
	jqTds[5].innerHTML = '<a class="delete" href="">删除</a>';
}

var is_change_list = false;

// /////////////////////////////////////////////////////////////////////////////////////////////////////

var isclientsearch = 0;
$("#btn-clientlist-search").click(function() {
	$("#cdemand-clientinfo-search").modal("toggle");
});

$("#btn-clientlist-editlist").click(function() {
	$("#cdemand-clientinfo-search").modal("toggle");
});

var oCDClientListTable = $("#CDClientList")
		.dataTable(
				{
					"sAjaxSource" : "client!clientlist",
					"aoColumns" : [ {
						"mData" : "id"
					}, {
						"mData" : "cid"
					}, {
						"mData" : "cname"
					}, {
						"mData" : "ssale"
					}, {
						"mData" : "ctype"
					}, {
						"mData" : "cphone"
					} ],
					"bProcessing" : true,
					"bServerSide" : true,
					"fnServerData" : retrieveClientListData,
					"bFilter" : true,
					"bSort" : true,
					"bStateSave" : false,
					"sPaginationType" : "full_numbers",
					"oLanguage" : {
						"sLengthMenu" : "每页显示 _MENU_ 条记录",
						"sZeroRecords" : "抱歉， 没有记录",
						"sInfo" : "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
						"sInfoEmpty" : "没有数据",
						"sInfoFiltered" : "(从 _MAX_ 条数据中检索)",
						"oPaginate" : {
							"sFirst" : "首页",
							"sPrevious" : "前一页",
							"sNext" : "后一页",
							"sLast" : "尾页"
						},
						"sSearch" : "搜索：",
						"sProcessing" : "正在加载数据..."
					},
					"aoColumnDefs" : [ {
						"aTargets" : [ 0 ],
						"fnCreatedCell" : function(nTd, sData, oData, iRow,
								iCol) {
							var check_del = $("<button name='check-client-delete' value='"
									+ oData.id + "'>选择</button>");
							check_del.on('click',
									function() {
										$("#cdemand-clientinfo-search").modal(
												"toggle");
										$("#txt-sendcom").val(oData.cname);
										return true;
									});
							$(nTd).empty();
							$(nTd).prepend(check_del);
						}
					} ]
				});

function retrieveClientListData(sSource, aoData, fnCallback) {
	aoData.push( {
		"name" : "pvlg_client_search",
		"value" : ""
	});
	$.ajax( {
		type : "POST",
		url : sSource,
		dataType : "json",
		data : "isclientsearch=" + isclientsearch + "&jsonclient="
				+ JSON.stringify(aoData),
		success : function(data) {
			fnCallback(data);
			var info = data.pvlg_client_search;
			if (info != "") {
				$("#pvlg_client_search").text(info);
			}
			isclientsearch = 0;
		}
	});
}

//------------------------------------------------------------------------

var issuplycomsearch = 0;
$("#btn-suplycomlist-search").click(function() {
	$("#cltcontract-suplycominfo-search").modal("toggle");
});

$("#btn-suplycomlist-editlist").click(function() {
	$("#cltcontract-suplycominfo-search").modal("toggle");
});

var oCDSuplycomTable = $("#CDSuplycomList")
		.dataTable(
				{
					"sAjaxSource" : "suplycom!suplycomlist",
					"aoColumns" : [ {
						"mData" : "id"
					}, {
						"mData" : "cid"
					}, {
						"mData" : "cname"
					}, {
						"mData" : "ssale"
					}, {
						"mData" : "ctype"
					}, {
						"mData" : "cphone"
					} ],
					"bProcessing" : true,
					"bServerSide" : true,
					"fnServerData" : retrieveSuplycomListData,
					"bFilter" : true,
					"bSort" : true,
					"bStateSave" : false,
					"sPaginationType" : "full_numbers",
					"oLanguage" : {
						"sLengthMenu" : "每页显示 _MENU_ 条记录",
						"sZeroRecords" : "抱歉， 没有记录",
						"sInfo" : "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
						"sInfoEmpty" : "没有数据",
						"sInfoFiltered" : "(从 _MAX_ 条数据中检索)",
						"oPaginate" : {
							"sFirst" : "首页",
							"sPrevious" : "前一页",
							"sNext" : "后一页",
							"sLast" : "尾页"
						},
						"sSearch" : "搜索：",
						"sProcessing" : "正在加载数据..."
					},
					"aoColumnDefs" : [ {
						"aTargets" : [ 0 ],
						"fnCreatedCell" : function(nTd, sData, oData, iRow,
								iCol) {
							var check_del = $("<button name='check-suplycom-delete' value='"
									+ oData.id + "'>选择</button>");
							check_del.on('click',
									function() {
										$("#cltcontract-suplycominfo-search").modal(
												"toggle");
										$("#txt-suplycom").val(oData.cname);
										return true;
									});
							$(nTd).empty();
							$(nTd).prepend(check_del);
						}
					} ]
				});

function retrieveSuplycomListData(sSource, aoData, fnCallback) {
	aoData.push( {
		"name" : "pvlg_suplycom_search",
		"value" : ""
	});
	$.ajax( {
		type : "POST",
		url : sSource,
		dataType : "json",
		data : "issuplycomsearch=" + issuplycomsearch + "&jsonsuplycom="
				+ JSON.stringify(aoData),
		success : function(data) {
			fnCallback(data);
			var info = data.pvlg_suplycom_search;
			if (info != "") {
				$("#pvlg_suplycom_search").text(info);
			}
			issuplycomsearch = 0;
		}
	});
}
