var patt = /^(\d{4})-(0[1-9]|1[0-2])$/;
var time;
var hrNumber;
var billId;
shr.defineClass("shr.customer.StorefrontBudgetEdit", shr.framework.Edit, {

	initalizeDOM: function () {
		shr.customer.StorefrontBudgetEdit.superClass.initalizeDOM.call(this);
		this.storeF7Event();
	},
	//监控门店F7值
	storeF7Event: function () {
		//var self = this;
		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){
						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;
					}
					
					var storeId = $("#Store_el").val();
					getNumByStoreFrontNumber(storeId,time);
				}
			});
		}
	},
	//保存
	saveAction: function () {
		time = $("#Time").val();
		if (time && !patt.test(time)) {
			shr.showWarning({
				message: "时间格式有误,正确格式为 yyyy-MM",
			});
			return;
		}
		// var num;
		// var startPreparedMonth;
		// var openDate;
		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;
		}
		shr.customer.StorefrontBudgetEdit.superClass.saveAction.call(this);
	},
	//重写assembleModel的原因是,保存时无法获取到NumberOfPractical的值
	assembleModel: function () {
		var model = shr.customer.StorefrontBudgetEdit.superClass.assembleModel.call(this);
		model.NumberOfPractical = $("#NumberOfPractical").val();
		return model;
	},
	
});
//根据门店编码和时间获取实际人数
function getNumByStoreFrontNumber(storeId,time){
	var url = shr.getContextPath() + "/dynamic.do?handler=com.kingdee.eas.custom.newpreparation.web.customer.StorefrontBudgetEditHandler&method=getNumByStoreFrontNumber";
	shr.ajax({
		async: false,
		type: 'post',
		data: {
			storeId: storeId,
			time: time
		},
		url: url,
		success: function (res) {
			if(res.data){
				$("#NumberOfPractical").val(res.data);
			}else{
				$("#NumberOfPractical").val(0);
			}
		}
	});				
}
function verifyTime(billId,hrNumber,time){
	var url = shr.getContextPath() + "/dynamic.do?handler=com.kingdee.eas.custom.newpreparation.web.customer.StorefrontBudgetEditHandler&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; 		
}