shr.defineClass('shr.salary.salaryBagEdit',shr.framework.Edit , {
	
	
	
	initalizeDOM: function () {
		var _self = this;
		shr.salary.salaryBagEdit.superClass.initalizeDOM.call(this);
		$("#addRow_entrys").hide();
		$("#deleteRow_entrys").hide();
		if(this.operateState.toUpperCase() != 'VIEW'){
			this.wageSplitting();
		}
		
		if(this.operateState.toUpperCase() == 'ADDNEW'){
			_self.getField("applyDate").shrDateTimePicker('setValue',new Date());
			$("#reissueDate").parents(".field-basis1").hide();
			$("#sourceBillId").parents(".field-basis1").hide();
			$("#entrys").setGridParam().hideCol("beforeAmount");
			$("#entrys").setGridParam().hideCol("reissueAmount");
			$("#entrys").setGridParam().hideCol("amountPayable");
		}
		
	},
	
	
	
	wageSplitting:function(){
		var self = this;
		self.getField('paycurrency').on("change", function(){
			self.defaultGriData();
		});
		self.getField('cmpItem').on("change", function(){
			self.defaultGriData();
		});
		self.getField('money').on("change", function(){
			self.defaultGriData();
		});
		self.getField('startDate').on("change", function(){
			if(self.checkDate()){
				self.defaultGriData();
			}
		});
		self.getField('endDate').on("change", function(){
			if(self.checkDate()){
				self.defaultGriData();
			}
			
		});
	},
	
	defaultGriData : function(){
		var self = this;
		var paycurrency =  $("#paycurrency").val();
		var cmpItemId = this.getFieldValue("cmpItem");
		var cmpItemName =  $("#cmpItem").val();
		var money =  $("#money").val();
		var startDate =  $("#startDate").val();
		var endDate =  $("#endDate").val();
		if(paycurrency && cmpItemId && money && startDate && endDate){
			//清空表格数据
			self.getField("entrys").jqGrid("clearGridData");
			//1、获取月份明细
			var monthList = self.getMonthsBetween(startDate,endDate);
			money = Number(money);
			var avgMonty = Math.round( money / monthList.length).toFixed(2);
			//var remainder = money % monthList.length;
			var rowdata = {};
			//jdGrid列表初始化
			for(var i = 1; i <= monthList.length ; i++){
				var initialRow = i - 1;
				rowdata.months = monthList[initialRow];
				if( i == monthList.length ){
						//rowdata.moneys = avgMonty + remainder;
						// 总金额减去前面几个月的金额
					rowdata.moneys=(money-avgMonty*(monthList.length-1)).toFixed(2);
				}else{
					rowdata.moneys = avgMonty;
				}
				rowdata.paycurrency = paycurrency;
				rowdata.cmpItem = {"id" : cmpItemId , "name" : cmpItemName};
				//生成分录数据
				var row = self.getField("entrys").jqGrid("addRowData", initialRow, rowdata, "last");
			}
			
			
		}
	},
	
	getMonthsBetween : function(date1, date2) {  
		var start = new Date(date1);  
		var end = new Date(date2);  
		var months = [];  
		var currentDate = start;  
		while (currentDate <= end) {  
			months.push(new Date(currentDate));  
			currentDate.setMonth(currentDate.getMonth() + 1);  
		}  
		return months;
	},

	checkDate :function(){
		var startDate =  $("#startDate").val();
		var endDate =  $("#endDate").val();
		if(startDate && endDate && endDate < startDate){
			shr.showInfo({message: "The expiration date cannot be later than the effective date"});
			return false;
		}
		return true;
	},
	
	/**
	 * 对保存、提交的数据进行确认
	 */
	verify: function() {
		var self = this;
		// shr.showInfo({message: message});
		var startDate =  $("#startDate").val();
		var endDate =  $("#endDate").val();	
		var sourceBillId =  $("#sourceBillId").val();
		var fid = $("#id").val();
		var cmpItemId = $("#cmpItem_el").val();
		var personId = $("#person_el").val();
		if(startDate && endDate && endDate < startDate){
			shr.showInfo({message: "The expiration date cannot be later than the effective date"});
			return false;
		}
		var resultFlag = true;
		self.remoteCall({
			method : 'checkDate',
			async : false,
			param : {
					"startDate" : startDate+"-01",
					"endDate" : endDate+"-01",
					"fid" : fid,
					"sourceBillId" : sourceBillId,
					"cmpitemId" : cmpItemId,
					"personId" : personId
					},
			success:function(data){
				resultFlag = data;
			}
		});
		if( !resultFlag){
			shr.showInfo({message: "Employee salary package period overlaps"});
			return false;
		}
		return true;
	},
		

	
	submitEffectAction : function (event) {
		var _self = this,
		workArea = _self.getWorkarea(),
		$form = $('form', workArea);
		if ($form.valid() && _self.verify()) {
			shr.showConfirm('您确认要提交生效吗？', function() {
				_self.prepareSubmitEffect(event, 'submitEffect');
			});
		}	
	},
	
	prepareSubmitEffect : function (event, action){
		var _self = this;
		var data = _self.assembleSaveData(action);
		var target;
		if (event && event.currentTarget) {
			target = event.currentTarget;
		}
		shr.doAction({
			target: target,
			url: _self.dynamicPage_url,
			type: 'post', 
			data: data,
			success : function(response) {
				_self.goNextPage(target);
			}
		});	
	},
});
