//专业应用--单人请假单
var leaveTypeNumber = "";
shr.defineClass("shr.ats.AtsLeaveBillEditExt", shr.ats.AtsLeaveBillEdit, {

    initalizeDOM: function () {
        shr.ats.AtsLeaveBillEditExt.superClass.initalizeDOM.call(this);
		var that = this;
		if(this.operateState.toUpperCase() != 'VIEW'){
			//初始化隐藏字段
			this.initFileHide();
			this.showColumnByPolicy();
		}else{
			this.initFileHide();
			this.showViewColumn();
		}
		var thisTechingDuties = atsMlUtile.getFieldOriginalValue("entries_TeachingDuties");
        if(thisTechingDuties){
            that.getField("entries_TeachingTask").shrTextField('option', 'required', true);
            that.getField("entries_AlternativeTeacher").shrTextField('option', 'required', true);
        }
        $("#entries_TeachingDuties").on("change", function () {
            var teachingDuties = atsMlUtile.getFieldOriginalValue("entries_TeachingDuties");
            if(teachingDuties){
                that.getField("entries_TeachingTask").shrTextField('option', 'required', true);
                that.getField("entries_AlternativeTeacher").shrTextField('option', 'required', true);
            }else{
                that.getField("entries_TeachingTask").shrTextField('option', 'required', false);
                that.getField("entries_AlternativeTeacher").shrTextField('option', 'required', false);
            }
        });
    },
	/**
	 *	初始化隐藏字段
	 */
	initFileHide : function(){
		$("#dowFile").hide();
		//教师假（教师旅行假）	
		$("#entries_Teachingperiod").parents(".field-area").hide();
		$("#entries_Teachinginnextsemester").parents(".field-area").hide();
		$("#entries_Startdateonextsemester").parents(".field-area").hide();
		$("#entries_DateofTestB").parents(".field-area").hide();
		$("#entries_DateofTestA").parents(".field-area").hide();
		$("#entries_DateofreturningtoGTIIT").parents(".field-area").hide();
		$("#entries_DateofleavingGTIIT").parents(".field-area").hide();
		//出差假（商务旅行）
		$("#entries_EstimatedTotalCost").parents(".field-area").hide();
		$("#entries_CITY").parents(".field-area").hide();
		$("#entries_Purpose").parents(".field-area").hide();
		//行政人员出差假	
		$("#entries_EmergencyContact").parents(".field-area").hide();
		$("#entries_Destination").parents(".field-area").hide();
		$("#entries_TripReason").parents(".field-area").hide();
		//病假、年假、出差假（商务旅行）
		$("#entries_AlternativeTeacher").parents(".field-area").hide();
		$("#entries_TeachingTask").parents(".field-area").hide();
		$("#entries_TeachingDuties").parents(".field-area").hide();
		$("#prompt").hide();
	},
	
	
	/**
	 * 根据选择的假期显示字段
	 */
    showColumnByPolicy : function(){
		var self = this;
		$("#entries_policy_el").on("change", function () {
			var policyId = $("#entries_policy_el").val();
			var personId = $("#entries_person_el").val();
            var personType = "";
            shr.callHandler({
                handler : "com.kingdee.shr.ats.web.handler.AtsLeaveBillEditHandlerEx",
                action : 'customerCheckParam',
                async : false,
                type : "POST",
                param : { "policyId" : "","personId" : personId },
                success:function(data){
                    if(data.personType){
                        personType = data.personType;
                    }
                }
            });

			shr.callHandler({
				handler : "com.kingdee.shr.ats.web.handler.AtsLeaveBillEditHandlerEx",
				action : 'getLeaveData',
				async : false,
				type : "POST",
				param : {
						"policyId" : policyId
						},
				success:function(data){
					
					self.getField("entries_Teachingperiod").shrPromptBox('option', 'required', false);
					self.getField("entries_Teachinginnextsemester").shrPromptBox('option', 'required', false);
					self.getField("entries_Startdateonextsemester").shrDateTimePicker('option', 'required', false);
					self.getField("entries_DateofTestB").shrDateTimePicker('option', 'required', false);						
					self.getField("entries_DateofTestA").shrDateTimePicker('option', 'required', false);
					self.getField("entries_DateofreturningtoGTIIT").shrDateTimePicker('option', 'required', false);
					self.getField("entries_DateofleavingGTIIT").shrDateTimePicker('option', 'required', false);
					self.getField("entries_EstimatedTotalCost").shrTextField('option', 'required', false);		
					self.getField("entries_CITY").shrTextField('option', 'required', false);
					self.getField("entries_Purpose").shrTextField('option', 'required', false);
					self.getField("entries_EmergencyContact").shrTextField('option', 'required', false);
					self.getField("entries_Destination").shrTextField('option', 'required', false);
					self.getField("entries_TripReason").shrTextField('option', 'required', false);	
					
					//教师假（教师旅行假）	
					$("#entries_Teachingperiod").parents(".field-area").hide();
					$("#entries_Teachinginnextsemester").parents(".field-area").hide();
					$("#entries_Startdateonextsemester").parents(".field-area").hide();
					$("#entries_DateofTestB").parents(".field-area").hide();
					$("#entries_DateofTestA").parents(".field-area").hide();
					$("#entries_DateofreturningtoGTIIT").parents(".field-area").hide();
					$("#entries_DateofleavingGTIIT").parents(".field-area").hide();
					//出差假（商务旅行）
					$("#entries_EstimatedTotalCost").parents(".field-area").hide();
					$("#entries_CITY").parents(".field-area").hide();
					$("#entries_Purpose").parents(".field-area").hide();
					//行政人员出差假	
					$("#entries_EmergencyContact").parents(".field-area").hide();
					$("#entries_Destination").parents(".field-area").hide();
					$("#entries_TripReason").parents(".field-area").hide();
					//病假、年假、出差假（商务旅行）
					$("#entries_AlternativeTeacher").parents(".field-area").hide();
					$("#entries_TeachingTask").parents(".field-area").hide();
					$("#entries_TeachingDuties").parents(".field-area").hide();
					$("#prompt").hide();
					$("#dowFile").hide();
					if(data){
						leaveTypeNumber = data.number;
						if(data.number == 'JQLX000001Y' || data.number == 'JQLX000003Y'){
							//病假、年假
							if("GTIIT_FACULTY" == personType || "GTIIT_OTHER" == personType){
                                $("#prompt").show();
                                $("#entries_TeachingDuties").parents(".field-area").show();
                                $("#entries_TeachingTask").parents(".field-area").show();
                                $("#entries_AlternativeTeacher").parents(".field-area").show();
                            }	
						}else if(data.number == 'JQLX000017Y' ){
							//行政人员出差假	
							$("#entries_EmergencyContact").parents(".field-area").show();
							$("#entries_Destination").parents(".field-area").show();
							$("#entries_TripReason").parents(".field-area").show();						
							self.getField("entries_EmergencyContact").shrTextField('option', 'required', true);
							self.getField("entries_Destination").shrTextField('option', 'required', true);
							self.getField("entries_TripReason").shrTextField('option', 'required', true);
						}else if(data.number == 'JQLX000020Y' ){
							//出差假（商务旅行）	
							$("#prompt").show();
							$("#entries_TeachingDuties").parents(".field-area").show();
							$("#entries_TeachingTask").parents(".field-area").show();
							$("#entries_AlternativeTeacher").parents(".field-area").show();
							$("#entries_EstimatedTotalCost").parents(".field-area").show();
							$("#entries_CITY").parents(".field-area").show();
							$("#entries_Purpose").parents(".field-area").show();
							
							self.getField("entries_EstimatedTotalCost").shrTextField('option', 'required', true);
							self.getField("entries_CITY").shrTextField('option', 'required', true);
							self.getField("entries_Purpose").shrTextField('option', 'required', true);
						}else if(data.number == 'JQLX000010Y' ){
							//教师假（教师旅行假）	
							$("#dowFile").show();
							$("#entries_Teachingperiod").parents(".field-area").show();
							$("#entries_Teachinginnextsemester").parents(".field-area").show();
							$("#entries_Startdateonextsemester").parents(".field-area").show();
							$("#entries_DateofTestB").parents(".field-area").show();
							$("#entries_DateofTestA").parents(".field-area").show();
							$("#entries_DateofreturningtoGTIIT").parents(".field-area").show();
							$("#entries_DateofleavingGTIIT").parents(".field-area").show();		
							self.getField("entries_Teachingperiod").shrPromptBox('option', 'required', true);
							self.getField("entries_Teachinginnextsemester").shrPromptBox('option', 'required', true);
							self.getField("entries_Startdateonextsemester").shrDateTimePicker('option', 'required', true);
							self.getField("entries_DateofTestB").shrDateTimePicker('option', 'required', true);						
							self.getField("entries_DateofTestA").shrDateTimePicker('option', 'required', true);
							self.getField("entries_DateofreturningtoGTIIT").shrDateTimePicker('option', 'required', true);
							self.getField("entries_DateofleavingGTIIT").shrDateTimePicker('option', 'required', true);
						}
					}
				}
			});
		});
		
		$("#entries_realLeaveLength").on("change", function () {
			self.setReasonText();
		});
		
		$("#entries_person").on("change", function () {
			self.setReasonText();
		});
	},

	showViewColumn : function(){
        var policyId = $("#entries_policy").val();
        var personId = $("#entries_person").val();
        var personType = "";
        shr.callHandler({
            handler : "com.kingdee.shr.ats.web.handler.AtsLeaveBillEditHandlerEx",
            action : 'customerCheckParam',
            async : false,
            type : "POST",
            param : { "policyId" : "","personId" : personId },
            success:function(data){
                if(data.personType){
                    personType = data.personType;
                }
            }
        });
        shr.callHandler({
            handler : "com.kingdee.shr.ats.web.handler.AtsLeaveBillEditHandlerEx",
            action : 'getLeaveData',
            async : false,
            type : "POST",
            param : {"policyId" : policyId},
            success:function(data){
                if(data){
                    if(data.number == 'JQLX000001Y' || data.number == 'JQLX000003Y'){
                        //病假、年假
                        if("GTIIT_FACULTY" == personType || "GTIIT_OTHER" == personType){
                            $("#prompt").show();
                            $("#entries_TeachingDuties").parents(".field-area").show();
                            $("#entries_TeachingTask").parents(".field-area").show();
                            $("#entries_AlternativeTeacher").parents(".field-area").show();
                        }
                    }else if(data.number == 'JQLX000017Y' ){
                        //行政人员出差假   
                        $("#entries_EmergencyContact").parents(".field-area").show();
                        $("#entries_Destination").parents(".field-area").show();
                        $("#entries_TripReason").parents(".field-area").show();                       
                    }else if(data.number == 'JQLX000020Y' ){
                        //出差假（商务旅行） 
                        $("#prompt").show();
                        $("#entries_TeachingDuties").parents(".field-area").show();
                        $("#entries_TeachingTask").parents(".field-area").show();
                        $("#entries_AlternativeTeacher").parents(".field-area").show();
                        $("#entries_EstimatedTotalCost").parents(".field-area").show();
                        $("#entries_CITY").parents(".field-area").show();
                        $("#entries_Purpose").parents(".field-area").show();
                    }else if(data.number == 'JQLX000010Y' ){
                        //教师假（教师旅行假）    
                        $("#dowFile").show();
                        $("#entries_Teachingperiod").parents(".field-area").show();
                        $("#entries_Teachinginnextsemester").parents(".field-area").show();
                        $("#entries_Startdateonextsemester").parents(".field-area").show();
                        $("#entries_DateofTestB").parents(".field-area").show();
                        $("#entries_DateofTestA").parents(".field-area").show();
                        $("#entries_DateofreturningtoGTIIT").parents(".field-area").show();
                        $("#entries_DateofleavingGTIIT").parents(".field-area").show();       
                    }
                }
            }
        });
    },

	setReasonText:function(){
		var realLeaveLength = $("#entries_realLeaveLength").shrNumberField("getValue");
		var policyId = $("#entries_policy_el").val();
		var leaveUnit = 0;
		if(policyId){
			shr.callHandler({
	            handler : "com.kingdee.shr.ats.web.handler.AtsLeaveBillEditHandlerEx",
	            action : 'getLeaveUnit',
	            async : false,
	            type : "POST",
	            param : {"policyId" : policyId},
	            success:function(data){
	                if(data){
	                    leaveUnit = data.leaveUnit;
	                }
	            }
	        });
		}
		
		if(leaveUnit == 2){
			realLeaveLength = realLeaveLength/7.5;
		}
		if(realLeaveLength >= 7){
			var personId = $("#entries_person_el").val();
			shr.callHandler({
				handler : "com.kingdee.shr.ats.web.handler.AtsLeaveBillEditHandlerEx",
				action : 'customerCheckParam',
				async : false,
				type : "POST",
				param : { "personId" : personId },
				success:function(data){
					if(	"GTIIT_SAS" == data.personType || "GTIIT_PSS" == data.personType || "GTIIT_GAS" == data.personType){
						$("#entries_toreasons").shrTextarea('option', 'required', true);	
						$("#entries_toreasons").parent().parent().find(".field-label").text("Request for leave：During my applied leave,the emergency contact will be ：");
					}else{
						$("#entries_toreasons").parent().parent().find(".field-label").text("Request for leave：");
						$("#entries_toreasons").shrTextarea('option', 'required', false);
					}
					if(("GTIIT_SAS" == data.personType || "GTIIT_PSS" == data.personType || "GTIIT_GAS" == data.personType) && leaveTypeNumber == "JQLX000017Y"){
						$("#entries_toreasons").parent().parent().find(".field-label").text("Request for leave：");
						$("#entries_toreasons").shrTextarea('option', 'required', false);
					}
				}
			
			});
		}else{
			$("#entries_toreasons").parent().parent().find(".field-label").text("Request for leave："); 
			$("#entries_toreasons").shrTextarea('option', 'required', false);
		}
	},

	dowFileAction:function(){
		shr.callHandler({
			handler : "com.kingdee.customer.util.handler.ConfigurationHandler",
			action : 'getCustomerConfig',
			async : false,
			param : { "fnumber" : "1001" },
			success:function(data){
				if(data){
					// 创建一个虚拟的链接元素
					var downloadLink = document.createElement('a');
					downloadLink.download = data.name;
					downloadLink.href = data.ip+"/leaveTemplate/"+data.name;
					document.body.appendChild(downloadLink);
					downloadLink.click();
					document.body.removeChild(downloadLink);
				}else{
					shr.showInfo({message: "No teacher fake template is configured !"});
				}
			}
		});
	},
});
