var patt = /^(\d{4})-(0[1-9]|1[0-2])$/;
var time;
var hrNumber;
var billId;
var nowTime;
shr.defineClass("shr.customer.PersonConfigScheduleEdit", shr.framework.Edit, {
    initalizeDOM : function() {
        shr.customer.PersonConfigScheduleEdit.superClass.initalizeDOM.call(this);
		this.storeF7Event();
		//this.setEditEnable();
		this.getBudgetPersonTotal();
		nowTime = getNowTime();
    },

	//监控项目F7值
    storeF7Event : function() {
        if(this.getOperateState() != "VIEW"){
			time = $("#Time").val();
			hrNumber = $("#HRNumber").val();
			billId = $("#id").val();
            $("#Store").shrPromptBox("option", {
				onchange : function (e, value) {
					if(value.current == null){
						return;
					}
					hrNumber = value.current.number;
					$("#HRNumber").val(hrNumber);
					if(time && time == nowTime){//还要加上是否为当月判断
						var storeId = $("#Store_el").val();
						getNumByStoreFrontNumber(storeId,time);
					}		
				}
			});
			//监控时间
			$("#Time").blur(function () {
				time = $("#Time").val();
				if(!time){
					return;
				}else if (!patt.test(time)) {
					shr.showWarning({
						message: "时间格式有误,正确格式为 yyyy-MM",
					});
					return;
				}
				if(hrNumber){
					var arr = verifyTime(billId,hrNumber,time);
					if(arr[0] == '1'){
						shr.showWarning({
							message: "时间必须在项目筹备期 "+arr[1]+" 至 "+arr[2]+" 内"
						});
						return;
					}else if(arr[0] == '3'){
						shr.showWarning({
							message: "当前门店在该月份已存在预算维护数据, 请勿重复提交!"
						});
						return;
					}
					if(time && time == nowTime){
						//还要加上是否为当月判断
						var storeId = $("#Store_el").val();
						getNumByStoreFrontNumber(storeId,time);
					}else if(time && time != nowTime){
						$("#ManagerAmnoutPra").val('');
						$("#PersonAmountPra").val('');
					}
				}
			});
		}
	},
	//监控一线员工人数和管理员人数 计算计划人数总和
	getBudgetPersonTotal: function () {
		if (this.getOperateState() != "VIEW") {
			var frontLinePerson = $("#FrontLinePerson").val();
			var administrator = $("#Administrator").val();
			//监控管理人员
			$("#Administrator").blur(function () {
				administrator = $("#Administrator").val();
				if (administrator && frontLinePerson) {
					var total = parseInt(administrator) + parseInt(frontLinePerson);
					$("#BudgetPersonTotal").val(total);
				}
			});
			//监控一线人员
			$("#FrontLinePerson").blur(function () {
				frontLinePerson = $("#FrontLinePerson").val();
				if (administrator && frontLinePerson) {
					var total = parseInt(administrator) + parseInt(frontLinePerson);
					$("#BudgetPersonTotal").val(total);
				}
			});
			//计划人数合计已经设置为只读,下面的方法作废
			// //计划人数合计失焦
			// $("#BudgetPersonTotal").blur(function () {
			// 	frontLinePerson = $("#FrontLinePerson").val();
			// 	administrator = $("#Administrator").val();
			// 	if (administrator && frontLinePerson) {
			// 		var total = parseInt(administrator) + parseInt(frontLinePerson);
			// 		$("#BudgetPersonTotal").val(total);
			// 	}
			// });
			// //计划人数合计获焦
			// $("#BudgetPersonTotal").focus(function () {
			// 	frontLinePerson = $("#FrontLinePerson").val();
			// 	administrator = $("#Administrator").val();
			// 	if (administrator && frontLinePerson) {
			// 		var total = parseInt(administrator) + parseInt(frontLinePerson);
			// 		$("#BudgetPersonTotal").val(total);
			// 	}
			// });
		}
	},
	//保存
	saveAction : function(){
		var time = $("#Time").val();
		if(time && !patt.test(time)){
			shr.showWarning({
                message: "时间格式有误,正确格式为 yyyy-MM",
            });
			return;
		}
		var arr;
		hrNumber = $("#HRNumber").val();
		billId = $("#id").val();

		if(time && hrNumber){
			arr = verifyTime(billId,hrNumber,time);
		}
		if(time && hrNumber && arr[0] == '1'){
			shr.showWarning({
				message: "时间必须在项目筹备期 "+arr[1]+" 至 "+arr[2]+" 内"
			});
			return;
		}else if(time && hrNumber && arr[0] == '3'){
			shr.showWarning({
				message: "当前门店在该月份已存在预算维护数据, 请勿重复提交!"
			});
			return;
		}
		//判断实际人数是否都有值或都为空
		var managerAmnoutPra = $("#ManagerAmnoutPra").val();
		var personAmountPra = $("#PersonAmountPra").val();
		if((managerAmnoutPra && !personAmountPra) || (!managerAmnoutPra && personAmountPra)){
			shr.showWarning({
				message: "管理人员实际人数 和 一线人员实际人数 要么都有值,要么都为空!"
			});
			return;
		}	
		shr.customer.PersonConfigScheduleEdit.superClass.saveAction.call(this);
	},
	//重写assembleModel的原因是,保存时无法获取到PracticalPreparedTime的值
	assembleModel: function () {
		var model = shr.customer.PersonConfigScheduleEdit.superClass.assembleModel.call(this);
		model.BudgetPersonTotal = $("#BudgetPersonTotal").val();
		return model;
	},


	//设置编辑按钮是否可用
	// setEditEnable : function(){
	// 	//判断当前月份是否比单据参数时间月份大
	// 	//var billId = $("#id").val();
	// 	var flag = 'false';
	// 	var time =  $("#Time").text();
	// 	var split = time.split('-');
	// 	var dateTime = new Date(split[0],split[1]-1,1);

	// 	var date = new Date();
	// 	var year = date.getFullYear();
	// 	var month = date.getMonth();
	// 	date.setTime(new Date(year,month,1));//将日期设为本月一号
	// 	if((dateTime - date) < 0){//历史数据
	// 		//历史数据需要判断操作人是否有更改权限
	// 		var url = shr.getContextPath() + "/dynamic.do?handler=com.kingdee.eas.custom.newpreparation.web.customer.PersonConfigScheduleEditHandler&method=setEditEnable";
	// 		shr.ajax({
	// 			async:false,
	// 			type:'post',
	// 			data :{},
	// 			url:url,
	// 			success:function(res){
	// 				flag=res.data;
	// 			}
	// 		}); 
	// 		if(flag == 'true'){
	// 			$("#edit").hide();
	// 		}
	// 	}
	// }	
});
//根据门店编码和时间获取实际人数
function getNumByStoreFrontNumber(storeId,time){
	var url = shr.getContextPath() + "/dynamic.do?handler=com.kingdee.eas.custom.newpreparation.web.customer.PersonConfigScheduleEditHandler&method=getNumByStoreFrontNumber";
	shr.ajax({
		async: false,
		type: 'post',
		data: {
			storeId: storeId
		},
		url: url,
		success: function (res) {
			if(res.data){
				$("#ManagerAmnoutPra").val(res.data.managerNum);
				$("#PersonAmountPra").val(res.data.personNum);

			}
		}
	});				
}
//检查时间是否合法
function verifyTime(billId,hrNumber,time){
	var url = shr.getContextPath() + "/dynamic.do?handler=com.kingdee.eas.custom.newpreparation.web.customer.PersonConfigScheduleEditHandler&method=verifyTime";
	var arr = new Array();
	shr.ajax({
		async:false,
		type:'post',
		data :{
			billId : billId,
			hrNumber : hrNumber,
			time : time
		},
		url:url,
		success:function(res){
			arr[0] =res.data.num;
			arr[1] =res.data.startPreparedMonth;
			arr[2] =res.data.openDate;
		}
	});
	return arr; 		
}
function getNowTime(){
	var date = new Date();
	var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : '0'+(date.getMonth() + 1);
	return date.getFullYear() +"-"+ month;
}