var empTypeInfo = {};
shr.defineClass("shr.affair.hrman.EmpEnrollBizBillHrManEditExt", shr.affair.hrman.EmpEnrollBizBillHrManEdit, {
	
	initalizeDOM : function () {
		var _self = this;
		shr.affair.hrman.EmpEnrollBizBillHrManEditExt.superClass.initalizeDOM.call(this);
		if(this.operateState.toUpperCase() != 'VIEW'){
			_self.nameChange(); 
		}
		if(this.operateState.toUpperCase() == 'ADDNEW'){
			var selectId = shr.getUrlRequestParam("selectId");
			if( !selectId){
				shr.remoteCall({
					method : 'getEmpNumber',
					async : false,
					success : function(res){
						//人员类型
						_self.getField("entrys_empNumber").shrTextField('setValue',res.empNumber);
					}
				});
			}
		}
		
	},
	
	nameChange:function(){
		var that = this;
		//监听文本域，数值控件的值变化
		that.getField("entrys_surname").on("change", function(){
			var surname =  $("#entrys_surname").val();
			surname = surname.toUpperCase();
			that.getField("entrys_surname").shrTextField('setValue',surname);
			//用户名赋值
			that.updateUserName();
			//姓名赋值
			that.updateName();
		});
		that.getField("entrys_givenName").on("change", function(){
			//用户名赋值
			that.updateUserName();
			//姓名赋值
			that.updateName();
			
		});
		//中间名
		that.getField("entrys_middleNames").on("change", function(){
			//姓名赋值
			that.updateName();
			
		});
		//本地名称
		that.getField("entrys_localname").on("change", function(){
			//姓名赋值
			that.updateName();
		});
		//试用期开始时间
		that.getField("entrys_bizDate").on("change", function(){
			//预入职日期
			var preEnterDate =  $("#entrys_bizDate").val();
			that.getField("entrys_trialStartDate").shrDateTimePicker('setValue',preEnterDate);
			that.updateTransferDate();
		});
		//预计转正日期
		that.getField("entrys_probation").on("change", function(){
			that.updateTransferDate();
			
		});
		
		
	},
	
	//用户名赋值
	updateUserName:function(){
		var that = this;
		//用户名赋值
		var surname =  $("#entrys_surname").val();
		var givenName = $("#entrys_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("entrys_userName").shrTextField('setValue',userName);
		that.getField("entrys_email").shrTextField('setValue',userName+"@gtiit.edu.cn");
	},
	
	
	updateName: function(){
		var that = this;
		//姓
		var surname =  $("#entrys_surname").val();
		//名
		var givenName =  $("#entrys_givenName").val();
		//本地名称
		var localName =  $("#entrys_localname").val();
		
		var name = "";
		if(givenName != ''){
			name = givenName + " ";
		}
		if(surname != ''){
			name = name + surname + " ";
		}
		if(localName != ''){
			name = name + localName + " ";
		}
		//姓名
		that.getField("entrys_empName").shrTextField('setValue',name);
	},
	
	
	//预计转正日期
	updateTransferDate:function(){
		var that = this;
		//预入职日期
		var preEnterDate =  $("#entrys_bizDate").val();
		//试用期（月）
		var tryMonth =  $("#entrys_probation").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:"正式员工"};
		}
		if(empTypeInfo != null ){
			employeeType =  empTypeInfo
		}
		that.getField("entrys_planFormalDate").shrDateTimePicker('setValue',date);
		$("#entrys_empType").shrPromptBox("setValue", employeeType);
	},
	
	
	/**
	 * 预入职下推初始化数据
	 */
	preEntryInit:function(){
		var _self = this;
		var selectId = shr.getUrlRequestParam("selectId");

		shr.remoteCall({
			url : shr.getContextPath() + "/dynamic.do?method=getPreEntryInitData&uipk="+jsBinder.uipk,
			type : "POST",
			param:{
				"billId" : selectId,
				"tempId" : _self.getTemporaryPersonId()//传入tempId以复制一个预入职头像都入职单
			},
			success : function(res){
				if(res.preEntryInfo != undefined){
					empTypeInfo = res.preEntryInfo.empType;
					var preEntryId = res.preEntryInfo.id; 
					var talentId = res.preEntryInfo.talent.id;
					if(res.preEntryInfo.sourceBillType.value == 2){//offer来的预入职单
						shr.callService( {
							serviceName : 'getPreEntryInfoOSF',
							param : {preEntryId : preEntryId},
							async : false,
							success : function(data) {
								_self.initParameterByRecurit("entrys_probation",data.probationPeriod == undefined ? null : data.probationPeriod,false,'numberField');//试用期
								_self.initParameterByRecurit("entrys_empType",data.employeeType == undefined ? null : data.employeeType,true);//用工关系状态
								_self.initParameterByRecurit("entrys_jobStartDate",data.jobStartDate == undefined ? null : data.jobStartDate.substring(0,10),false);//参加工作日期
							}
						});
					}
					if(res.preEntryInfo.hrOrgUnit != undefined){
						//基础资料在HR组织设置前先设置，可以接受HR组织可使用权限校验
						_self.initParameterByRecurit("entrys_variationReason",res.preEntryInfo.enrollSource,true);//招聘来源反写变动原因，，handler中用招聘来源按编码匹配查出变动原因放到enrollSource中
						var hrOrgUnitObj = {id:res.preEntryInfo.hrOrgUnit.id,name:res.preEntryInfo.hrOrgUnit.name}
						_self.initParameterByRecurit("hrOrgUnit",hrOrgUnitObj,true);//人事业务组织
					}
				
					if(res.preEntryInfo.talent != undefined){
						_self.initParameterByRecurit('entrys_IDCardNo',res.preEntryInfo.talent.idCardNO,false);//身份证号码
						_self.initParameterByRecurit("entrys_passportNo",res.preEntryInfo.talent.passportNO,false);//护照号码
					}
					_self.getField("entrys.preEntryID").val(selectId);//隐藏预入职ID字段发送至后端
					if (res.errorMsg){
						shr.showError({
            				message: data.errorMsg,
            				hideAfter: null
            			});
					}else if (res.personExist && res.personExist=="true") {
                    	$("#entrys_enrollAgain").val(1);
                        if(res.personOldInfo){
                			//编码,如果使用原编码，才设置原编码
                        	//osf取回来的 data.personOldInfo.useOldNumber 值为枚举值{0:使用原编码, 1：使用新编码}
                            if(!res.personOldInfo.useOldNumber && res.personOldInfo.empNumber){
                                $("#entrys_empNumber").val(res.personOldInfo.empNumber);
                        		$("#entrys_useOldNumber").val(1);
                        		_self.getField('entrys_empNumber').shrTextField('disable');
                            }
                            else if(res.personOldInfo.useOldNumber){
                            	$("#entrys_useOldNumber").val(0);
                            }
                            //参加工作日期
                            if (res.personOldInfo.jobStartDate) {
                                $("#entrys_jobStartDate").shrDateTimePicker('setValue', res.personOldInfo.jobStartDate);
                            }
                        }
                        _self.swapHrbizDefineInfo(_self._hrBizdefine_enrollAgain);
                        _self.getField("entrys.hrBizDefine").shrPromptBox("setFilter", _self._default_hrBizDefine_filter+" and bizDefineType = 16");//只能选择再入职变动操作
                        _self.showEnrollAgainMsg(res);
					}
					//职位放在加载再入职信息之后，避免再入职使用原编码时又按职位刷编码
					_self.initParameterByRecurit("entrys_position",res.preEntryInfo.position,true);//入职职位
					//根据入职日期填写入职时间
					var effdt=_self.getField('entrys.bizDate').shrDateTimePicker("getValue");
	 	 			_self.getField('entrys.bizTime').shrDateTimePicker('setValue', effdt);
					//本地名称
					_self.getField("entrys_localname").shrTextField('setValue',res.preEntryInfo.localname_l2);
					//姓
					_self.getField("entrys_surname").shrTextField('setValue',res.preEntryInfo.surname_l2);
					//中间名
					_self.getField("entrys_middleNames").shrTextField('setValue',res.preEntryInfo.middlenames_l2);
					//名
					_self.getField("entrys_givenName").shrTextField('setValue',res.preEntryInfo.givenname_l2);
					//员工编码
					_self.getField("entrys_empNumber").shrTextField('setValue',res.preEntryInfo.empNumber);
					//用户名
					_self.getField("entrys_userName").shrTextField('setValue',res.preEntryInfo.username_l2);
					//工作邮箱
					_self.getField("entrys_email").shrTextField('setValue',res.preEntryInfo.workemail);
					//私人邮箱
					_self.getField("entrys_pemail").shrTextField('setValue',res.preEntryInfo.email);
					//试用期
					_self.getField("entrys_probation").shrTextField('setValue',res.preEntryInfo.tryMonth);
					//教师年假额度
					_self.getField("entrys_faentitlement").shrTextField('setValue',res.preEntryInfo.faentitlement);
					//教师病假额度
					_self.getField("entrys_fsentitlement").shrTextField('setValue',res.preEntryInfo.fsentitlement);
					//教师病假最高额度
					_self.getField("entrys_fsmentitlement").shrTextField('setValue',res.preEntryInfo.fsmentitlement);
					//Admin Title
					_self.getField("entrys_admintitle").shrTextField('setValue',res.preEntryInfo.admintitle);
					//时薪
					_self.getField("entrys_hwage").shrTextField('setValue',res.preEntryInfo.hwage);
					//试用期开始日期
					_self.getField("entrys_trialStartDate").shrDateTimePicker('setValue',res.preEntryInfo.trialStartDate);
					//居民
					$('#entrys_resident').shrSelect('setValue',res.preEntryInfo.resident);
					//支付币种
					$('#entrys_paycurrency').shrSelect('setValue',res.preEntryInfo.paycurrency);
					shr.remoteCall({
						method : 'getEmpPreEntryData',
						async : false,
						param:{
							"billId" : selectId
						},
						success : function(response){
							//人员类型
							$("#entrys_persontype").shrPromptBox("setValue", {id:response.personTypeId , name : response.personTypeName });
							//员工类别
							$("#entrys_wcategory").shrPromptBox("setValue", {id:response.wcategoryId , name : response.wcategoryName });
							//全职或兼职
							$("#entrys_ftorpt").shrPromptBox("setValue", {id:response.ftorptId , name : response.ftorptName });
							//直接上级
							$("#entrys_lmanager").shrPromptBox("setValue", {id:response.lmanagerId , name : response.lmanagerName });
							//Academic Title
							$("#entrys_academictitle").shrPromptBox("setValue", {id:response.academictitleId , name : response.academictitleName });
							//job2
							$("#entrys_jobs").shrPromptBox("setValue", {id:response.jobsId , name : response.jobsName });
							//纳税方式
							$("#entrys_rmethod").shrPromptBox("setValue", {id:response.rmethodId , name : response.rmethodName });
							//职等
							$("#entrys_jobGrade").shrPromptBox("setValue", {id:response.jobId , name : response.jobName });
							//签发国家
							$("#entrys_countryRegion").shrPromptBox("setValue", {id:response.countryId , name : response.countryName });
							//签证类型
							$("#entrys_credentialsType").shrPromptBox("setValue", {id:response.credentTypeId , name : response.credentTypeName });
						}
					});
					
				}
			}
		});
		// 预入职转入职后端赋值，需要在前端主动触发一些change事件
		_self.autoChange();
	},

	setEnrollAgainInfo: function(data) {
		var that = this;
		//根据身份证号和护照号将查出来的再入职人员信息填充到页面
    	if (data && data.errorMsg){
    		shr.showError({
				message: data.errorMsg,
				hideAfter: null
			});
    	}else if ( data && data.personExist == "true") {
            that.fillEnrollPersonDataByNo(data);
            that.swapHrbizDefineInfo(that._hrBizdefine_enrollAgain);
            that.getField("entrys.hrBizDefine").shrPromptBox("setFilter", that._default_hrBizDefine_filter+" and bizDefineType = 16");//只能选择再入职变动操作
            that.showEnrollAgainMsg(data);

            var idEntity = "";
            if(data.personOldInfo.idCardNo){
				idEntity = data.personOldInfo.idCardNo;
            }else{
            	idEntity = data.personOldInfo.passportNo;
            }
            shr.remoteCall({
				method : 'getPersonName',
				async : false,
				param:{
					"idEntity" : idEntity
				},
				success : function(response){
					if(response.userName){
						//姓
						$("#entrys_surname").shrTextField("setValue", response.surName);
						//本地名称
						$("#entrys_localname").shrTextField("setValue", response.localName);
						//中间名
						$("#entrys_middleNames").shrTextField("setValue", response.middleName);
						//名
						$("#entrys_givenName").shrTextField("setValue", response.givenName);
						//用户名
						$("#entrys_userName").shrTextField("setValue", response.userName);
					}
				}
			});
        }else{
        	// 如果之前输入身份证带出过再入职人员才处理，避免正常情况下多次修改身份证也处理了以下逻辑
        	if($("#entrys_enrollAgain").val()=="1" || $("#entrys_enrollAgain").val()=="true"){
    		 	$("#entrys_enrollAgain").val(0);
        	 	// 父类中存储了第一次加载的变动操作，将变动操作还原
			 	that.swapHrbizDefineInfo(that._currentHRBizdefine.id);
			 	that.getField("entrys.hrBizDefine").shrPromptBox("setFilter", that._default_hrBizDefine_filter);//还原默认变动操作可选范围
        	}
        	// 如果之前输入身份证带出过再入职人员并且是原编码,才处理，避免正常情况下多次修改身份证也处理了以下逻辑
        	if($("#entrys_useOldNumber").val()=="1" || $("#entrys_useOldNumber").val()=="true"){
				$("#entrys_useOldNumber").val(0);
        		// 没有编码规则，清空编码
        		if(that._isAutoNumber == false){
            		that.getField('entrys_empNumber').shrTextField('enable');
            		that.getField('entrys_empNumber').val('');
        		}
        		// 有编码规则，需要看职位有没有选择
        		if(that._isAutoNumber == true){
        			// 如果职位没有选择，则清空编码
        			if($.isEmptyObject($("#entrys_position_el").val())){
        				that.getField('entrys_empNumber').val('');
        			}
        			// 如果职位不为空，重新按照编码规则刷新编码
					if(!$.isEmptyObject($("#entrys_position_el").val())){
						that.refreshNumber($("#entrys_position_el").val());
					}
        		}
        	}

   //      	//姓
			// $("#entrys_surname").shrTextField("setValue", "");
			// //本地名称
			// $("#entrys_localname").shrTextField("setValue", "");
			// //中间名
			// $("#entrys_middleNames").shrTextField("setValue", "");
			// //名
			// $("#entrys_givenName").shrTextField("setValue", "");
		}
	}
	
	
});