shr.defineClass('shr.emp.EmpPreEntryEditExt',shr.emp.EmpPreEntryEdit , {
	
	
	
	initalizeDOM: function () {
		var _self = this;
		shr.emp.EmpPreEntryEditExt.superClass.initalizeDOM.call(this);
		if(this.operateState.toUpperCase() != 'VIEW'){
			_self.nameChange();
			_self.visaChange(); 
		}
		_self.ftorptF7Action();

	},
	 /**
     * 2、入职单需控“全职或兼职”字段为“兼职”时才允许填写；
     */
	 ftorptF7Action() {
		if($("#ftorpt").val()=="兼职"||$("#ftorpt").val()=="Part Time"){
			$('#jobEndTime').shrDateTimePicker('enable');//或者设为disable
			$("#jobEndTime").shrDateTimePicker('option', 'required', true);
		}
        $("#ftorpt").shrPromptBox("option", {
            onchange: function (e, value) {
                var current = value.current["BaseInfo.number"];
                if (current == "PART"||value.current.name=='Part Time') {
                    $('#jobEndTime').shrDateTimePicker('enable');//或者设为disable
					$("#jobEndTime").shrDateTimePicker('option', 'required', true);

                }else{
                    $('#jobEndTime').shrDateTimePicker('disable');//或者设为enable
                    $("#jobEndTime").shrDateTimePicker('setValue', "");

                }
            }
        })

    },
	visaChange:function(){
		var that = this;
		//监听文本域，数值控件的值变化
		$("#talent_birthday").on("change", function(){
			if(that.checkAge()){
				shr.showError({message:"Onboarding is not allowed for persons under 18 years old"});
				return;
			}
			that.checkVisa();
		});
	},
	//身份证变更事件处理
	processIDCardNoChangeEvent: function () {
		var self = this;

		$("#" + self.getRealId('talent.idCardNO')).blur(function () {
			var idCardNoValue = self.getFieldValue('talent.idCardNO');

			//根据身份证取出生日期和性别,并且OSF校验,调用人事OSF，如要修改请联系人事开发
			if (idCardNoValue) {
				self._idCardNo_IsChanged = true;
				self._idCardNo_IsPassed = false;
			} else {
				self._idCardNo_IsChanged = false;
				self._idCardNo_IsPassed = true;
				return;
			}
			var localLang = shr.getFieldLangByContext();
			var personName = self.getFieldValue("name")[localLang];
			shr.callService({
				serviceName: 'getPersonIdCardNOInfoService',
				param: {
					idCardNO: idCardNoValue,
					personId: "",
					hrOrgUnitId: self.getFieldValue("hrOrgUnit"),
					preEntryId: self.billId,
					personName: personName
				},
				async: false,
				success: function (data) {
					self._isIdCardNoTrue = true;
					self._idCardNo_IsPassed = true;
					if (data.birthday) {
						self.getField('talent.birthday').shrDateTimePicker('setValue', data.birthday);
						self.calculateAge(data.birthday);
						self.checkConstellationAndZodiac(data.birthday.split(" ")[0]);
						if(self.checkAge()){
							shr.showError({message:"Onboarding is not allowed for persons under 18 years old"});
							return;
						}else{
							self.checkVisa();
						}
					}
					if (data.gender_value) {
						var value = data.gender_value;
						self.getField('talent.gender').shrSelect('setValue', value);
					}
					_self.remoteCall({
		                method: "checkIdEntity",
		                param: {idCardNo:idCardNoValue}, 
		                async: false,
		                success: function(response) {
		                    if(response.resultMessage){
		                    	shr.showWarning({message:response.resultMessage});
		                    }
		                },
		                error : function(){}
		            });
				}
			});
		});
		$("#" + this.getRealId('talent.idCardNO')).keyup(function () {
			self._idCardNo_IsChanged = true;
			self._idCardNo_IsPassed = true;
		});
	},
	nameChange:function(){
		var that = this;
		//监听文本域，数值控件的值变化
		that.getField("surname").on("change", function(){
			var surname =  $("#surname").val();
			//全英校验
			//var regex = /^[A-Za-z]+$/;
			var regex = /^[A-Za-z\s]+$/;
			if( !regex.test(surname)){
				shr.showError({ message: "The first name must be in English" });
			}
			surname = surname.toUpperCase();
			that.getField("surname").shrTextField('setValue',surname);
			//用户名赋值
			that.updateUserName();
			//姓名赋值
			that.updateName();
			that.checkVisa();
		});
		that.getField("givenName").on("change", function(){
			//用户名赋值
			that.updateUserName();
			//姓名赋值
			that.updateName();
			that.checkVisa();
		});
		
		//本地名称
		that.getField("LocalName").on("change", function(){
			//姓名赋值
			that.updateName();
			that.checkVisa();
		});
		//试用期开始时间
		that.getField("preEnterDate").on("change", function(){
			//预入职日期
			var preEnterDate =  $("#preEnterDate").val();
			that.getField("trialStartDate").shrDateTimePicker('setValue',preEnterDate);
			that.updateTransferDate();
		});
		//预计转正日期
		that.getField("tryMonth").on("change", function(){
			that.updateTransferDate();
			
		});
		//职等过滤
		that.getField("position").on("change", function(){
			var positionId = $("#position_el").val();
			shr.callService({
				serviceName : 'getJobInfosByPositionService',
				param : {
					"positionId" : positionId
				},
				async:true,//职层职等可以异步更新
				success: function(data) {
					that.updateJobGrade(positionId,data);
				}
			});
			$("#jobGrade").shrPromptBox("setValue", null);			
		});
		
	},
	
	//职等过滤
	updateJobGrade:function(currentId,data){
		var self = this;
		if(data){
			var lowGradeObject = data.lowJobGrade;
			var highGradeObject = data.highJobGrade;
			var jobGradeModule = data.jobGradeModule;
			if(lowGradeObject && highGradeObject ){
				// 如果职位的最低职等=最高职等，F7只能选择一个
				if(lowGradeObject.id ==highGradeObject.id){
					 self.getField("jobGrade").shrPromptBox("setFilter","id ='"+lowGradeObject.id+"'");
				}else if((lowGradeObject.index || lowGradeObject.index == 0) && (highGradeObject.index || highGradeObject.index == 0)){
				   self.getField("jobGrade").shrPromptBox("setFilter", 
							" index <="+highGradeObject.index+" and index >="+lowGradeObject.index+" and jobGradeModule.id='"+jobGradeModule.id+"'");
				}
			} else{
				self.getField("jobGrade").shrPromptBox("setValueNoTrigger",null);
				self.getField("jobGrade").shrPromptBox("setFilter", "index<-9999");//-9999 没有最低和最高则不要选到
				
			}
		}
	},
	
	//用户名赋值
	updateUserName:function(){
		var that = this;
		//用户名赋值
		var surname =  $("#surname").val();
		var givenName = $("#givenName").val();
		var userName = givenName + "." + surname;
		if(userName.length > 20){
			if(surname != '' ){
				userName = givenName + "." + surname.slice(0,1).toUpperCase() ;
				if(userName.length > 20){
					var startLength = userName.length - 20;
					userName = userName.substring(startLength,userName.length);
				}	
			}
		}
		userName = userName.toLowerCase();
		that.getField("userName").shrTextField('setValue',userName);
		that.getField("workemail").shrTextField('setValue',userName+"@gtiit.edu.cn");
	},
	
	
	updateName: function(){
		var that = this;
		//姓
		var surname =  $("#surname").val();
		//名
		var givenName =  $("#givenName").val();
		//本地名称
		var localName =  $("#LocalName").val();
		
		var name = "";
		if(givenName != ''){
			name = givenName + " ";
		}
		
		if(surname != ''){
			name = name + surname + " ";
		}
		if(localName != ''){
			name = name + localName + " ";
		}
		//姓名
		that.getField("name").shrTextField('setValue',name);
	},
	
	
	//预计转正日期
	updateTransferDate:function(){
		var that = this;
		//预入职日期
		var preEnterDate =  $("#preEnterDate").val();
		//试用期（月）
		var tryMonth =  $("#tryMonth").val();
		let date = new Date(preEnterDate);  // 创建一个 Date 对象	
		if(tryMonth != '' && tryMonth > 0 && preEnterDate != null ){
			tryMonth = parseInt(tryMonth);
			date.setMonth(date.getMonth() + tryMonth);
			employeeType = {id:"00000000-0000-0000-0000-000000000002A29E85B3", name_l1:"On Probation",name:"试用员工",name_l2:"试用员工"};
		}else{
			date = null;
			employeeType = {id:"00000000-0000-0000-0000-000000000001A29E85B3", name_l1:"Permanent Employee",name:"正式员工",name_l2:"正式员工"};
		}
		that.getField("transferDate").shrDateTimePicker('setValue',date);
		$("#empType").shrPromptBox("setValue", employeeType);
	},
	
	
	createBillAction:function(){
		var self = this;
		self.remoteCall({
			method : 'createEmpBill',
			async : false,
			success:function(data){
				shr.showInfo({message: "Test"+data});
			}
		});
	},
	verify: function () {
		var self = this;
		var beginDate = self.getFieldValue("talent.idCardBeginDate");
		var endDate = self.getFieldValue("talent.idCardEndDate");
		var idCardBeginDate = new Date(beginDate);
		var idCardEndDate = new Date(endDate);
		var localLang = shr.getFieldLangByContext();
		var personName = self.getFieldValue("name")[localLang];
		if (idCardBeginDate.getTime() > idCardEndDate.getTime()) {
			shr.showError({
				message: jsBizMultLan.emp_empPreEntryEdit_i18n_0
			});
			return false;
		}

		var idCardNO = self.getFieldValue("talent.idCardNO");
		var passportNO = self.getFieldValue("talent.passportNO");
		if(idCardNO == "" && passportNO == ""){
			shr.showError({
				message: jsBizMultLan.emp_empPreEntryEdit_i18n_8
			});
			return false;
		}
		if (idCardNO) {
			var verifyFlag = false;
			shr.callService({
				serviceName: 'getPersonIdCardNOInfoService',
				param: {
					idCardNO: idCardNO,
					personId: "",
					hrOrgUnitId: self.getFieldValue("hrOrgUnit"),
					preEntryId: self.billId,
					personName: personName
				},
				async: false,
				success: function (data) {
					verifyFlag = true;
				}
			});
			if(verifyFlag == false)
				return false;
		}
		
		if (passportNO){
			var verifyFlag = false;
			shr.callService({
				serviceName: 'getPersonPassPortNOInfoService',
				param: {
					passportNO: passportNO,
					personId: "",
					preEntryId: self.billId,
					personName: personName
				},
				async: false,
				success: function (data) {
					verifyFlag = true;
				}
			});
			if(verifyFlag == false)
				return false;
		}
		if(self.checkAge()){
			shr.showError({message:"Onboarding is not allowed for persons under 18 years old"});
			return false;
		}

		return true;
	},
	checkVisa:function(){
		var name =  $("#name").val().trim();
		var birthday =  $("#talent_birthday").val();
		if(name && birthday){
			_self.remoteCall({
                method: "checkIdEntity",
                param: {name:name,birthday:birthday}, 
                async: false,
                success: function(response) {
                    if(response.resultMessage){
                    	shr.showWarning({message:response.resultMessage});
                    }
                },
                error : function(){}
            });
		}
	},
	checkAge:function(){
		var birthday =  $("#talent_birthday").val();
		if(birthday){
			var bomDate = new Date(birthday);
			var today = new Date();
		    // 计算年份差异
		    var yearDiff = today.getFullYear() - bomDate.getFullYear();
		    // 如果出生日期在今年之后，则减去年份差异
		    if (today < new Date(today.getFullYear(), bomDate.getMonth(), bomDate.getDate())) {
		        yearDiff--;
		    }
		    if (yearDiff >= 18) {
			    return false;
			} else {
			    return true;
			}
		}
	}
});
