/**
 * 转正申请扩展
 */
var customFields;
shr.defineClass("shr.affair.hrman.EmpHireBizBillHrManEditEx", shr.affair.hrman.EmpHireBizBillHrManEdit, {
	initalizeDOM: function () {
		this.renderPersonInfoTable();
		shr.affair.hrman.EmpHireBizBillHrManEditEx.superClass.initalizeDOM.call(this);
	},
	/**
	* 根据personId和uipk关联查询出关联字段并赋值
	*/
	initDataFromStructure: function (personID) {
		var self = this;
		var uipk = shr.getUrlParam('uipk');
		shr.callService({
			serviceName: "getDataFromStructure",
			param: {
				personID: personID,
				uiClass: uipk
			},
			async: true,
			success: function (data) {
				if (data && data.length > 0) {
					$.each(data, function (i, v) {
						//$('#' + v.name).shrTextField('setValue',v.value);
						if (v.name) {
							self.getField(v.name).shrTextField('setValue', v.value);
							// if ("personnelCa_name" == v.name) {
							// 	$("#personnelCa_nameEx").shrTextField("setValue", v.value)
							// }else if ("salarysche_name" == v.name) {
							// 	$("#salarysche_nameEx").shrTextField("setValue", v.value)
							// }else if ("manageca_name" == v.name) {
							$("#" + v.name + "Ex").shrTextField("setValue", v.value);
							//}
						}
					})
				}
			}
		})
	},
	setDepartmentValue: function (data) {
		this.setF7Value("entrys.position.adminOrgUnit.department", data);
		$('#entrys_position_adminOrgUnit_departmentEx').shrPromptBox("setValue", data);
	},
	/**
	 * 初始化职位，如果是目标职位，则可选，如果是原职位，则不可选 
	 */
	initTargetPositionF7: function (data, isNeedUptVal) {
		var self = this;
		if (data[0] === undefined) {
			return;
		}
		var needTargetPosition = true;
		if (data && data[0]) {
			var defineFieldState = data[0].defineFieldState;
			if (defineFieldState != null) {
				needTargetPosition = defineFieldState.needTargetPosition;
			}
		}
		if (needTargetPosition) {
			self.getField("entrys.position").shrPromptBox("enable");
			$("#entrys_positionEx").shrPromptBox("enable");
		} else {
			self.getField("entrys.position").shrPromptBox("disable");
			$("#entrys_positionEx").shrPromptBox("disable");
		}
	},

	/**
	* 初始化用工关系状态,根据OSF GetHrBizDefineByDefineIdService||GetHRBizDefineService 返回的参数做判断
	* 
	* 1只有一个默认带出可编辑，多个默认带出第一个且必填，无则灰
	* 2目标用工关系状态为原目标用工关系状态时，尽管配置了出口用工关系，目标用工关系也是原用工关系
	* @param isNeedUptVal 页面编辑时:false|页面新增时:true
	*/
	initOutEmpTypeF7: function (data, isNeedUptVal) {
		var self = this,
			needLaborState = true,
			laborStateInitValue = null;
		if (data && data[0]) {
			var defineFieldState = data[0].defineFieldState;
			if (defineFieldState != null) {
				needLaborState = defineFieldState.needLaborState;
			}
			laborStateInitValue = defineFieldState.laborStateInitValue;
		}
		var outEmpType = $(data[0].outEmpType);
		// 编辑浏览状态下不可以清空，新增状态或者变动操作修改后,先清空
		if (isNeedUptVal == true) {
			if (self.getFieldValue("entrys.empType")) {
				$('#entrys_empType').shrPromptBox("setValue", null);
			}
			if ($('#entrys_empTypeEx')) {
				$('#entrys_empTypeEx').shrPromptBox("setValue", null);
			}
			if (outEmpType.length == 1) {
				self.getField("entrys.empType").shrPromptBox("setValueNoTrigger",
					{
						id: outEmpType[0].id,
						name: outEmpType[0].name
					});
				$('#entrys_empTypeEx').shrPromptBox("setValueNoTrigger",
					{
						id: outEmpType[0].id,
						name: outEmpType[0].name
					});
				$("#entrys_empType").shrPromptBox('option', 'required', false);
				$("#entrys_empTypeEx").shrPromptBox('option', 'required', false);
			}
		}
		//目标用工关系状态为原目标用工关系状态时，尽管配置了出口用工关系，目标用工关系也是原用工关系，
		if (needLaborState) {//如果是目标用工关系状态
			$('#entrys_empType').shrPromptBox("option", "required", true);
			$('#entrys_empType').shrPromptBox("option", "readonly", false);
			$('#entrys_empType').shrPromptBox("enable");
			$('#entrys_empTypeEx').shrPromptBox("option", "required", true);
			$('#entrys_empTypeEx').shrPromptBox("option", "readonly", false);
			$('#entrys_empTypeEx').shrPromptBox("enable");
		} else if (needLaborState == false) {//如果不是目标用工关系状态，则设置原用工关系状态
			if (isNeedUptVal == true) {//编辑状态下不可以修改,新增状态或者变动操作修改后,才允许修改
				// 如果有原用工关系状态，则带出原用工关系状态否则清空
				if (self._currentPersonOrgInfo) {
					var oldEmpTypeData = self._currentPersonOrgInfo.empType;
					// 原用工关系状态
					if (oldEmpTypeData != null) {
						var entrys_oldEmpTypeValue = {
							id: oldEmpTypeData.id,
							name: oldEmpTypeData.name
						}
						$("#entrys_empType").shrPromptBox("setValue", entrys_oldEmpTypeValue);
						$("#entrys_empTypeEx").shrPromptBox("setValue", entrys_oldEmpTypeValue);
					}
				} else {
					$('#entrys_empType').shrPromptBox("setValue", null);
					$('#entrys_empTypeEx').shrPromptBox("setValue", null);
				}
			}
			$('#entrys_empType').shrPromptBox("option", "required", false);
			$('#entrys_empType').shrPromptBox("disable");
			$('#entrys_empTypeEx').shrPromptBox("option", "required", false);
			$('#entrys_empTypeEx').shrPromptBox("disable");
		}
		var fliter = self.buildOutEmpTypeFilter(data);
		$("#entrys_empType").shrPromptBox("setFilter", fliter);
		$('#entrys_empTypeEx').shrPromptBox("setFilter", fliter);
	},


	/**
	 *  子类处理 getPersonPosOrgInfo返回的值，给子类覆写 （转正和调动、离职、返聘退休等）
	 * @param {*} data 
	 */
	initShrAffairBizBillData: function (data) {
		shr.affair.hrman.EmpHireBizBillHrManEditEx.superClass.initShrAffairBizBillData.call(this, data);
		var self = this,
			personPosOrgData = data[0];
		var personData = personPosOrgData.person,
			oldPositionData = personPosOrgData.position,
			empLaborRelationData = personPosOrgData.empLaborRelation;
		var personPosOrgData = data[0];
		var personData = personPosOrgData.person,
			oldPositionData = personPosOrgData.position,
			oldJobData = personPosOrgData.oldJob,
			oldAdminOrgData = personPosOrgData.adminOrg,
			oldCompanyData = personPosOrgData.company,
			oldDepartment = personPosOrgData.department,
			oldEmpPostRankData = personPosOrgData.oldEmpPostRank,
			oldEmpTypeData = personPosOrgData.empType,
			hrbizdefine = personPosOrgData.hrbizdefine;
		//原员工状态
		if (oldEmpTypeData != null) {
			var entrys_oldEmpTypeValue = {
				id: oldEmpTypeData.id,
				name: oldEmpTypeData.name
			}
			$("#entrys_oldEmpTypeEx").shrPromptBox("setValue", entrys_oldEmpTypeValue);
		}
		if (empLaborRelationData) {
			//预计转正日期
			if (!empLaborRelationData.planFormalDate) {
				$("#entrys_planFormalDateEx").shrDateTimePicker('setValue', "");
			} else {
				$("#entrys_planFormalDateEx").shrDateTimePicker('setValue', empLaborRelationData.planFormalDate);
			}
			//转正日期
			if (!empLaborRelationData.formalDate) {
				$("#entrys_bizDateEx").shrDateTimePicker('setValue', "");
			} else {
				$("#entrys_bizDateEx").shrDateTimePicker('setValue', empLaborRelationData.formalDate);
				//self.changeOldDateHandler(null, self);
			}
		}

		if (oldPositionData != null) {
			var entrys_oldPositionValue = {
				id: oldPositionData.id,
				name: oldPositionData.name
			}
			//原职位
			$("#entrys_oldPositionEx").shrPromptBox("setValue", entrys_oldPositionValue);
			//var adminOrgUnitDetailData = self.getAdminOrgUnitDetailData(entrys_oldPositionValue.id);
			//self.setOldOrgUnitLayerValue(adminOrgUnitDetailData);
			//现职位
			$("#entrys_positionEx").shrPromptBox("setValue", entrys_oldPositionValue);
		}
		if (oldJobData != null) {
			var entrys_oldJobValue = {
				id: oldJobData.id,
				name: oldJobData.name
			}
			if ($('#entrys_oldJobEx') != null) {
				//原职务
				$("#entrys_oldJobEx").shrPromptBox("setValue", entrys_oldJobValue);
			}
			if ($('#entrys_jobEx') != null) {
				//转正后职务
				$("#entrys_jobEx").shrPromptBox("setValue", entrys_oldJobValue);
			}
		}
		//当前职级
		if (oldEmpPostRankData != null) {
			if ($('#entrys_oldJobGradeEx') != null) {
				$('#entrys_oldJobGradeEx').shrPromptBox("setValue", { id: oldEmpPostRankData.jobGradeId, name: oldEmpPostRankData.jobGradeName });
			}
		}
		var oldDepartment = $("#entrys_oldAdminOrg").shrPromptBox("getValue");
		//var department = $("#entrys_adminOrg").shrPromptBox("getValue");
		$("#entrys_oldPosition_adminOrgUnit_departmentEx").shrPromptBox("setValue", oldDepartment)
		$("#entrys_position_adminOrgUnit_departmentEx").shrPromptBox("setValue", oldDepartment)
	},
	/**
	 * 渲染人员组织表格p
	 */
	renderPersonInfoTable: function () {
		const $tbody = $("#myTable tbody");
		const $tr1 = $("<tr></tr>");
		$tr1.append("<td><div class='flex-c'>调整前</div></td>");
		$tbody.append($tr1);
		this.getF7Html("entrys_oldEmpType", $tr1);//原用工关系状态
		this.getDateHtml("entrys_planFormalDate", $tr1);//预计转正日期
		this.getTextFieldHtml("manageca_name", $tr1);//系统分类
		this.getF7Html("entrys_oldPosition_adminOrgUnit_department", $tr1);//现任部门
		this.getF7Html("entrys_oldPosition", $tr1);//现任职位
		this.getF7Html("entrys_oldJobGrade", $tr1);;//当前职级
		this.getTextFieldHtml("salarysche_name", $tr1);//计薪方式
		this.getTextFieldHtml("personnelCa_name", $tr1);//人员类别
		const $tr2 = $("<tr></tr>");
		$tr2.append("<td><div class='flex-c'>调整后</div></td>");
		$tbody.append($tr2);
		this.getF7Html("entrys_empType", $tr2);//目标用工关系状态
		this.getDateHtml("entrys_bizDate", $tr2);//实际转正日期
		this.getF7Html("entrys_manageCa", $tr2);//系统分类
		this.getF7Html("entrys_position_adminOrgUnit_department", $tr2);//转正后部门
		this.getF7Html("entrys_position", $tr2);//转正后职位
		this.getF7Html("entrys_jobGrade", $tr2);//转正后职级
		this.getF7Html("entrys_salaryScheme", $tr2);//计薪方式
		this.getF7Html("entrys_personnelCa", $tr2);//人员类别
		//实际转正日期
		$("#entrys_bizDateEx").change(function () {
			var changeDate = $("#entrys_bizDateEx").shrDateTimePicker("getValue");//发生变化的日期值
			$("#entrys_bizDate").shrDateTimePicker("setValue", changeDate);
			self.changeOldDateHandler(null, self);
			self.changeLeftCompanyDate(bizDate);
		});
	},

	/**
	 * 获取F7html
	 * @returns 
	 */
	getF7Html: function (element, $tr) {
		//判断元素是否存在
		if ($('#' + element) == null) {
			//元素不存在
		}
		var self = this;
		var elementIdName = element + "Ex";
		var elementName = elementIdName.replace("_", ".");
		if ("VIEW" == self.getOperateState()) {
			var html = '<td><div class="flex-c"><span id="' + elementIdName + '"></span></div></td>';
			$tr.append(html);
			//$tbody.append($tr);
			var name = $("#" + element).html();
			var id = $("#" + element).val();
			var options = {
				value: { id: id, name: name },
				originalValue: id,
				className: "field_input"
			};
			$("#" + elementIdName).shrFieldDisplay(options);
		} else {

			var value = $("#" + element).shrPromptBox("getValue");
			var options = $("#" + element).shrPromptBox("option")
			var validate = options.validate
			options.id = elementIdName;
			options.name = elementName;
			options.value = value;
			if (options.readonly != "readonly") {
				//不等于只读时，绑定值改变事件，对原有字段同步更新值
				options.onchange = function (e, value) {
					if (value.current) {
						$("#" + element).shrPromptBox("setValue", value.current)
					} else {
						$("#" + element).shrPromptBox("setValue", null)
					}
				}
				options.verifyBeforeOpenCallback = function (event) {
					var options = $("#" + element).shrPromptBox("option");
					$("#" + elementIdName).shrPromptBox("setFilter", options.subWidgetOptions.filter);
				}
			}
			var html = '<td><div class="flex-c"><input id="' + elementIdName + '" name="' + elementName + '" class="block-father input-height" type="text"  istotemplate="true"';
			if (validate.includes("required:true")) {
				html += ' required="true" ';
			}
			html += '></div></td>';
			$tr.append(html);
			$("#" + elementIdName).shrPromptBox(options);
		}
	},
	/**
	 * 获取日期html
	 * @returns Date
	 */
	getDateHtml: function (element, $tr) {
		//判断元素是否存在
		var self = this;
		var elementIdName = element + "Ex";
		var elementName = elementIdName.replace("_", ".");
		if ("VIEW" == self.getOperateState()) {
			var html = '<td><div class="flex-c"><span id="' + elementIdName + '"></span></div></td>';
			$tr.append(html);
			var value = $("#" + element).html();
			var options = {
				value: value,
				originalValue: value,
				alias: value,
				className: "field_input"
			};
			$("#" + elementIdName).shrFieldDisplay(options);
		} else {
			var value = $("#" + element).shrDateTimePicker("getValue");
			var options = $("#" + element).shrDateTimePicker("option");
			options.id = elementIdName;
			options.name = elementName;
			options.value = value;
			var validate = options.validate
			var html = '<td><div class="flex-c"><input id="' + elementIdName + '" name="' + elementName + '" class="block-father input-height" type="text" istotemplate="true"';
			if (validate.includes("required:true")) {
				html += ' required="true" ';
			}
			html += '></div></td>';
			$tr.append(html);
			$("#" + elementIdName).shrDateTimePicker(options);
			if (options.readonly != 'readonly') {
				//不等于只读时，绑定只改变事件，对原有字段同步更新值
				$("#" + elementIdName).change(function () {
					var changeDate = $("#" + elementIdName).shrDateTimePicker("getValue");//发生变化的日期值
					$("#" + element).shrDateTimePicker("setValue", changeDate);
				});
			}
		}
	},
	/**
	* 获取文本字段html
	* @returns 
	*/
	getTextFieldHtml: function (element, $tr) {
		//判断元素是否存在
		if ($('#' + element) == null) {
			//元素不存在
		}
		var self = this;
		var elementIdName = element + "Ex";
		var elementName = elementIdName.replace("_", ".");
		if ("VIEW" == self.getOperateState()) {
			var html = '<td><div class="flex-c"><span id="' + elementIdName + '"></span></div></td>';
			$tr.append(html);
			//$tbody.append($tr);
			var name = $("#" + element).html();
			var id = $("#" + element).val();
			var options = {
				value: { id: id, name: name },
				originalValue: id,
				className: "field_input"
			};
			$("#" + elementIdName).shrFieldDisplay(options);
		} else {

			var value = $("#" + element).shrTextField("getValue");
			var options = $("#" + element).shrTextField("option")
			var validate = options.validate
			options.id = elementIdName;
			options.name = elementName;
			options.value = value;
			if (options.readonly != "readonly") {
				//不等于只读时，绑定值改变事件，对原有字段同步更新值
				options.onchange = function (e, value) {
					if (value.current) {
						$("#" + element).shrTextField("setValue", value.current)
					} else {
						$("#" + element).shrTextField("setValue", null)
					}
				}
			}
			var html = '<td><div class="flex-c"><input id="' + elementIdName + '" name="' + elementName + '" class="block-father input-height" type="text"  istotemplate="true"></div></td>';
			$tr.append(html);
			$("#" + elementIdName).shrTextField(options);
		}
	},
});