shr.defineClass('shr.salary.salaryBagChangeEdit',shr.framework.Edit , {
	
	
	
	initalizeDOM: function () {
		var _self = this;
		shr.salary.salaryBagChangeEdit.superClass.initalizeDOM.call(this);
		$("#addRow_entrys").hide();
		$("#deleteRow_entrys").hide();
		if(this.operateState.toUpperCase() != 'VIEW'){
			this.wageSplitting();
			//页面初始化
			this.initBillData();
			_self.getField("applyDate").shrDateTimePicker('setValue',new Date());
		}
		
		if(this.operateState.toUpperCase() == 'ADDNEW'){
			$("#sourceBillId").parents(".field-basis1").hide();
		}
	},
	
	initBillData:function(){
		var self = this;
		var billId = $.getUrlParam("sourceBillId");
		if(billId != null && billId != "" ){
			self.remoteCall({
				method : 'getSoureBillData',
				async : false,
				param : {
						"billId" : billId
						},
				success:function(data){
					
					$("#hrOrgUnit").shrPromptBox("setValue",{id:'00000000-0000-0000-0000-000000000000CCE7AED4',name:'Guangdong Technion Israel Institute of Technology'});
					//员工
					$("#person").shrPromptBox("setValue", {id:data.personId,name:data.personName});
					//币种
					$('#paycurrency').shrSelect('setValue',data.paycurrency);
					//薪酬项目
					$("#cmpItem").shrPromptBox("setValue", {id:data.cmpItemId,name:data.cmpItemName});
					//生效时间
					$("#startDate").shrDateTimePicker("setValue", data.startDate);
					//失效时间
					$("#endDate").shrDateTimePicker("setValue", data.endDate);
					//金额
					$("#money").shrTextField("setValue", data.money);
					//原始单据ID
					$("#sourceBillId").shrTextField("setValue", billId);
					var monthList = data.entry;
					for(var i = 1; i <= monthList.length ; i++){
						var initialRow = i - 1;
						var rowdata = monthList[initialRow];
						rowdata.cmpItem = {"id" : rowdata.cmpItemId , "name" : rowdata.cmpItemName};
						//生成分录数据
						var row = self.getField("entrys").jqGrid("addRowData", initialRow, rowdata, "last");
					}
				}
			});
		}
	},
	
	wageSplitting:function(){
		var self = this;
		self.getField('endDate').on("change", function(){
			if(self.checkDate()){
				self.defaultGriData();
			}
			
		});
		self.getField('reissueDate').on("change", function(){
			if(self.checkDate()){
				self.defaultGriData();
			}
			
		});
	},
	
	defaultGriData : function(){
		var self = this;
		var money =  $("#money").val();
		var startDate =  $("#startDate").val();
		var endDate =  $("#endDate").val();
		var reissueDate =  $("#reissueDate").val();
		var billId =  $("#sourceBillId").val();
		if(money && startDate && endDate && reissueDate && billId){
			//清空表格数据
			self.getField("entrys").jqGrid("clearGridData");
			self.remoteCall({
				method : 'createEntryDetail',
				async : false,
				param : {
						"startDate" : startDate+"-01",
						"endDate" : endDate+"-01",
						"reissueDate" : reissueDate+"-01",
						"money" : money,
						"billId" : billId
						},
				success:function(data){
					for(var i = 1; i <= data.length ; i++){
						var initialRow = i - 1;
						var rowdata = data[initialRow];
						rowdata.cmpItem = {"id" : rowdata.cmpItemId , "name" : rowdata.cmpItemName};
						//生成分录数据
						var row = self.getField("entrys").jqGrid("addRowData", initialRow, rowdata, "last");
					}
				}
			});
			
			
		}
	},
	

	checkDate :function(){
		var startDate =  $("#startDate").val();
		var endDate =  $("#endDate").val();
		var reissueDate =  $("#reissueDate").val();
		if(startDate && endDate && endDate < startDate){
			shr.showInfo({message: "The expiration date cannot be later than the effective date !"});
			return false;
		}
		if(reissueDate && endDate && endDate < reissueDate){
			shr.showInfo({message: "The replacement date cannot be longer than the expiration date !"});
		}
		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 obj = $("#entrys").jqGrid("getRowData");
		var money =  $("#money").val();
		var entriesMonty = 0;
		for(var i = 0 ; i < obj.length ; i++){
			entriesMonty = entriesMonty + obj[i].moneys;
		}
		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);
			}
		});	
	},
});
