var personType = "";
//员工自助--多人请假单（新增）
shr.defineClass("shr.ats.atsLeaveBillEditNewExt", shr.ats.atsLeaveBillEditNew, {

    initalizeDOM: function () {
        shr.ats.atsLeaveBillEditNewExt.superClass.initalizeDOM.call(this);
		var that = this;
		$("#dowFile").hide();

		shr.callHandler({
			handler : "com.kingdee.shr.ats.web.handler.AtsLeaveBillEditHandlerEx",
			action : 'customerCheckParam',
			async : false,
			type : "POST",
			param : { "policyId" : "","personId" : $("#personId").val() },
			success:function(data){
				personType = data.personType;
			}
		});
		
		var entries_cont = waf("#entries");
		entries_cont.jqGrid("option", {

			beforeSaveCell: function (rowid, cellname, value, iRow, iCol) { // F7触发
				if (cellname == "policy") {
					action = "save";
				}
			}
			, afterSaveCell: function (rowid, cellname, value, iRow, iCol) { // F7 触发
				if(value["adminOrgUnit.id"]){
					$("#entries").jqGrid('setCell',rowid,"adminOrgUnit.id",value["adminOrgUnit.id"]);
				 }
				 if(cellname == "realLeaveLength"){
					if(	"GTIIT_SAS" == personType || "GTIIT_PSS" == personType || "GTIIT_GAS" == personType){
						var rowData = $("#entries").jqGrid("getRowData");
						var flag = false;
						for(var i = 0; i < rowData.length; i++){
							var leaveLength =  rowData[i].realLeaveLength;
							if(leaveLength >= 7){
								flag = true;
							}
						}
						if(flag){
							that.getField("remark").shrTextarea('option', 'required', true);	
							$("#remark").parent().parent().parent().parent().find(".field-label").text("Request for leave：During my applied leave,the emergency contact will be ：");
						}else{
							$("#remark").parent().parent().parent().parent().find(".field-label").text("Request for leave：");
							that.getField("remark").shrTextarea('option', 'required', false);
						}
					}
				}
				_self.afterSaveCellTrigger(rowid, cellname, value, iRow, iCol);
			}

			, beforeEditCell: function (rowid, cellname, value, iRow, iCol) { // 切换成文本的时候触发
				if(cellname == "isElasticCalLen"){
					var $entries_isElasticCalLen="#entries tr[id='" + rowid + "'] td[aria-describedby='entries_isElasticCalLen'] input";
					$($entries_isElasticCalLen).unbind("change");
					$($entries_isElasticCalLen).change(function(){
						that.countLeaveLength(rowid);
					});
				}
				if (cellname == "policy") {
					lastRowRemark = $("#entries tr[id='" + rowid + "'] td[aria-describedby='entries_policy']").attr('title');
					idROW = rowid;
					policyKey = $("#entries").jqGrid('getCell', rowid, "policy");
					action = "EDIT";
				}
				if (cellname == "leaveLength") {
					//					setTimeout(function () {
					//	                   $('#' + rowid + '_leaveLength').attr('readonly', true);
					//	                 }, 1);
					//请假长度可否编辑
					var personId = $("#entries").jqGrid('getCell', rowid, "person").id;
					if (typeof personId === 'undefined') {
						return;
					}
					var hrOrgUnitId = $("#hrOrgUnit_el").val();
					if (hrOrgUnitId == null || hrOrgUnitId == "") {
						shr.showWarning({ message: jsBizMultLan.atsManager_atsLeaveBillBatchEdit_i18n_ZFY_1 });
						return;
					}
					that.remoteCall({
						type: "post",
						method: "isLeaveLengthEdit",
						param: { personId: personId, hrOrgUnitId: hrOrgUnitId },
						async: false,
						success: function (res) {
							var info = res;
							if (info.errorString) {
								shr.showWarning({ message: info.errorString });
							} else {
								if (info.editable == 'true') {
									$("#" + rowid).find("td").eq(iCol).removeClass("disabled");
								} else {
									$("#entries").jqGrid('setCell', rowid, "leaveLength", "", "disabled");
									$("#entries").jqGrid('setCell', rowid, "realLeaveLength", "", "disabled");
									setTimeout(function () {
										$("#entries").jqGrid('restoreCell', iRow, iCol);
									}, 1);
								}
							}
						}
					});
				}
			}
			, afterEditCell: function (rowid, cellname, value, iRow, iCol) {
				if(cellname=="person"){
					var hrOrgUnitId=$("#hrOrgUnit_el").val();
					$("#"+rowid+"_person").shrPromptBox().attr("data-params",hrOrgUnitId);
				}
				_self.saveEditCellValue(rowid, cellname, value, iRow, iCol);
			}
			, afterRestoreCell: function (rowid, value, iRow, iCol) {
				if (iCol == 3 && action != "save") { // 将说明 重新复制 
					$("#entries tr[id='" + rowid + "'] td[aria-describedby='entries_policy']").attr('title', lastRowRemark);
				}
			}
		});


    },
	
	verify: function () {
		var that = this;
		var billerrorString="";
		//如果是查看页面提交工作流，不需要再次校验了
		if (that.getOperateState().toUpperCase() == 'ViEW') {
			return;
		}
		var obj = $("#entries").jqGrid("getRowData");

		if (obj.length == 0) {
			shr.showWarning({ message: jsBizMultLan.atsManager_atsLeaveBillBatchEdit_i18n_ZFY_6 });
			return false;
		}
		if (!that.checkRowIsOver()) {
			return false;
		}

		if (!that.checkEntry()) {
			return false;
		}
		
		if (!that.checkBilltime(obj)) {
			shr.showWarning({message: jsBizMultLan.atsManager_atsLeaveBillBatchEdit_i18n_NX_3});
			return false;
		}
		
		
		
		/*if(!that.checkEnoughRemain()){
			shr.showWarning({message: jsBizMultLan.atsManager_atsLeaveBillBatchEdit_i18n_NX_4});
			return false;
		}*/

		var errorString = "";
		var errorFlag = 0;
		jQuery(obj).each(function (n) {
			//			info = that.validateLeaveBillCycle(this.person.id,this.beginTime,this.endTime,this.policy.id,this.leaveLength);
			var rowid = $("#entries tr:eq(" + (n + 1) + ")").attr('id')
			errorString = that.checkIsNullEveryRow(this);
			if (errorString == "") {
				errorString = that.checkEveryRow(this);
			}
			if (errorString == "" || errorString == undefined) { // 检测请假时长是不是大于计算时长
				errorString = that.checkCalLength(this);
			}
			if (errorString == undefined || errorString == null || errorString == "") {
			} else {
				billerrorString= billerrorString + shr.formatMsg(jsBizMultLan.atsManager_atsLeaveBillBatchEdit_i18n_ZFY_7, [n+1]) + errorString +"<br>";
				var showMes = ""
				showMes += errorString.replace(/<br>/g, "");

				//	   			shr.showWarning({message: this.person.name + " "+ this.policy.name +" " + errorString});
				//	   			return false;
				that.preShowError(rowid, this.person.name + " " + this.policy.name + " " + showMes);
				errorFlag = 1;
			}
		});

		//校验是否为同一个假期类型
		if(	"GTIIT_SAS" == personType || "GTIIT_PSS" == personType || "GTIIT_GAS" == personType){
			for(var i = 0 ; i < obj.length ; i++){
				var realLeaveLength = obj[i].realLeaveLength;
				if(realLeaveLength >= 7 && (obj[i].reason == null || obj[i].reason == "")){
					shr.showWarning({message: "If the duration of leave is more than 7 days, the reason for leave cannot be empty !"});
					return false;
				}
			}
		}
		if (errorFlag == 0) {
			return true;
		} else {
			shr.showWarning({message: billerrorString});
			return false;
		}
		return false;
	},

	countLeaveLength: function (rowid, action) {
		var that = this;
		if ($("#entries").jqGrid('getCell', rowid, "person") == undefined
			|| $("#entries").jqGrid('getCell', rowid, "person") == null) {
			return;
		} else {
			personId = $("#entries").jqGrid('getCell', rowid, "person").id;
		}
		if ($("#entries").jqGrid('getCell', rowid, "policy") == undefined
			|| $("#entries").jqGrid('getCell', rowid, "policy") == null) {
			return;
		} else {
			holidayTypeId = $("#entries").jqGrid('getCell', rowid, "policy").id;
		}
		var personId = $("#entries").jqGrid('getCell', rowid, "person").id;
		var beginTime = $("#entries").jqGrid('getCell', rowid, "beginTime");
		var endTime = $("#entries").jqGrid('getCell', rowid, "endTime");
		var hrOrgUnitId = $("#hrOrgUnit_el").val();
		var isElasticCalLen = $("#entries").jqGrid('getCell', rowid, "isElasticCalLen")=="1";

		if (!strDateTime(beginTime) || !strDateTime(endTime)) {
			return;
		}
		var holidayTypeId = $("#entries").jqGrid('getCell', rowid, "policy").id;
		if (personId == undefined || personId == null
			|| holidayTypeId == undefined || holidayTypeId == null
			|| beginTime == undefined || beginTime == null
			|| endTime == undefined || endTime == null) {
			return;
		}
		that.remoteCall({
			type: "post",
			async: false,
			method: "getRealLeaveLengthInfo",
			param: { personId: personId, 
			beginTime: beginTime,
			endTime: endTime, 
			holidayTypeId: holidayTypeId, 
			action: action, 
			hrOrgUnitId: hrOrgUnitId,
			isElasticCalLen:isElasticCalLen},
			success: function (res) {
				info = res;
				if (info.errorString) {
					that.preShowError(rowid, info.errorString);
					//					shr.showError({message: info.errorString});
				} else {
					var day = parseFloat(info.leaveBillDays + info.segRest);
					var leaveLengDay = parseFloat(info.leaveBillDays);
					if (action != "EDIT") { // 第一次进入的时候重现计算先请假时长
						$("#entries").jqGrid('setCell', rowid, "leaveLength", leaveLengDay);
						$("#entries").jqGrid('setCell', rowid, "realLeaveLength", leaveLengDay);
					}
					//  每次触发之前都会记住这个 计算的时长
					$("#entries").jqGrid('setCell', rowid, "caleaveLength", day);
				}
			}
		});


		if(	"GTIIT_SAS" == personType || "GTIIT_PSS" == personType || "GTIIT_GAS" == personType){
			var rowData = $("#entries").jqGrid("getRowData");
			var flag = false;
			for(var i = 0; i < rowData.length; i++){
				var leaveLength =  rowData[i].realLeaveLength;
				if(leaveLength >= 7){
					flag = true;
				}
			}
			if(flag){
				that.getField("remark").shrTextarea('option', 'required', true);	
				$("#remark").parent().parent().parent().parent().find(".field-label").text("Request for leave：During my applied leave,the emergency contact will be ：");
			}else{
				$("#remark").parent().parent().parent().parent().find(".field-label").text("Request for leave：");
				that.getField("remark").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 !"});
				}
			}
		});
	},
	
	
	
	
});
