var beforeOpenDate; //存放初始的开业时间
var thirtyDay = 1000 * 60 * 60 * 24 * 30;
var method;
var currentPlanPreparedTime;
var beforeJielouDate;
var beforeBudgetTotal;
shr.defineClass("shr.customer.StorefrontInfoEdit", shr.framework.Edit, {
	initalizeDOM: function () {
		shr.customer.StorefrontInfoEdit.superClass.initalizeDOM.call(this);
		this.storeF7Event();
		this.getStartPreparedMonth();
		method = this.getQueryVariable("method");
		beforeOpenDate = $('#OpenDate').val(); //存放初始的开业时间
		beforeJielouDate = $('#JielouDate').val(); //存放初始的接楼时间
		beforeBudgetTotal = $('#budgetTotal').val(); //存放筹备期预算总额
	},
	//监控项目F7值
	storeF7Event: function () {
		if (this.getOperateState() != "VIEW") {
			$("#projectName").shrPromptBox("option", {
				onchange: function (e, value) {
					if (value.current == null) {
						return;
					}
					$("#departmentNumber").val(value.current.number);
					var departmentId = value.current.id;
					var url = shr.getContextPath() + "/dynamic.do?handler=com.kingdee.eas.custom.newpreparation.web.customer.StoreFrontInfoEditHandler&method=getOrgInfoById";
					shr.ajax({
						async: false,
						type: 'post',
						data: {
							departmentId: departmentId
						},
						url: url,
						success: function (res) {
							var city = {
								id: res.data.cityID,
								name: res.data.cityName
							};
							$("#city").shrPromptBox("setValue", city);
							var businessFormat = {
								id: res.data.hrIndustryID,
								name: res.data.industryName
							};
							$("#businessFormat").shrPromptBox("setValue", businessFormat);
						}
					});
				}
			});
		}
	},
	//监控开业时间和计划筹备期
	getStartPreparedMonth: function () {
		if (this.getOperateState() != "VIEW") {
			var planPreparedTime = $('#PlanPreparedTime').val(); //计划筹备期
			var openDate = $('#OpenDate').val(); //开业时间
			//监控文本框
			// $("#PlanPreparedTime").bind("input propertychange", function () {
			// 	planPreparedTime = $(this).val();
			// 	if (planPreparedTime && openDate) {
			// 		calculateDate(openDate, planPreparedTime); //计算实际筹备期 和 开始筹备月份
			// 	}
			// });
			// //监控文本框失焦事件
			// $("#PlanPreparedTime").focus(function(){
			// 	currentPlanPreparedTime = $("#PlanPreparedTime").val();
			//   });
			//监控文本框失焦事件
			$("#PlanPreparedTime").blur(function () {
				planPreparedTime = $("#PlanPreparedTime").val();
				if (!planPreparedTime) {
					// if(currentPlanPreparedTime){
					// 	planPreparedTime = currentPlanPreparedTime;
					// }else{
					return;

				}
				if (isNaN(planPreparedTime)) {
					$("#PlanPreparedTime").val('');
					return;
				}
				planPreparedTime = parseFloat(planPreparedTime).toFixed(1);
				$("#PlanPreparedTime").val(planPreparedTime);
				if (planPreparedTime && openDate) {
					calculateDate(openDate, planPreparedTime); //计算实际筹备期 和 开始筹备月份
				}
			});
			//监控日期控件
			$('#OpenDate').change(function () { //监听日期的改变				
				openDate = $('#OpenDate').val();
				if (planPreparedTime && openDate) {
					calculateDate(openDate, planPreparedTime); //计算实际筹备期 和 开始筹备月份
				}
			});
		}
	},
	//保存
	saveAction: function () {
		// var area = $("#area").val();
		// if (area && area < 0) {
		// 	shr.showWarning({
		// 		message: "面积不得小于 0 !",
		// 	});
		// 	return;
		// }
		// var expectedNumberOfPerson = $("#ExpectedNumberOfPerson").val();
		// if (expectedNumberOfPerson && expectedNumberOfPerson < 0) {
		// 	shr.showWarning({
		// 		message: "投资预计人工不得小于 0 !",
		// 	});
		// 	return;
		// }
		// var planPreparedTime = $("#PlanPreparedTime").val();
		// if (planPreparedTime && planPreparedTime < 0) {
		// 	shr.showWarning({
		// 		message: "计划筹备期不得小于 0 !",
		// 	});
		// 	return;
		// }

		if (this.getOperateState() != "ADDNEW") {
			var openDate = $('#OpenDate').val(); //开业时间
			var jielouDate = $('#JielouDate').val(); //接楼时间
			var budgetTotal = $('#budgetTotal').val(); //筹备期预算总额
			var storeId = $("#projectName_el").val();
			var open;
			var jielou;
			var budget;
			var flag = false;
			if (openDate && openDate !== beforeOpenDate) {
				flag = true;
				open = beforeOpenDate;
			}
			if (jielouDate && jielouDate !== beforeJielouDate) {
				flag = true;
				jielou = beforeJielouDate;
			}
			if (budgetTotal && budgetTotal !== beforeBudgetTotal) {
				flag = true;
				budget = beforeBudgetTotal;
			}
			if(flag){
				insertStoreAdjustHistory(storeId, open, jielou, budget);
			}
		}


		shr.customer.StorefrontInfoEdit.superClass.saveAction.call(this);
	},
	// /**
	//  * 设置按钮可见性
	//  */
	// setButtonVisible: function () {
	// 	var tenthAdjustBusinessTime = $("#TenthAdjustBusinessTime").val(); //开业时间(第十次调整)
	// 	if (tenthAdjustBusinessTime == '') {
	// 		$("#edit").show();
	// 	} else {
	// 		$("#edit").hide();
	// 	}
	// },
	//重写assembleModel的原因是,保存时无法获取到PracticalPreparedTime的值
	assembleModel: function () {
		var model = shr.customer.StorefrontInfoEdit.superClass.assembleModel.call(this);
		model.PracticalPreparedTime = $("#PracticalPreparedTime").val();
		return model;
	},
	//从父url中获取参数
	getQueryVariable: function (variable) {
		var query = parent.window.location.search.substring(1);
		var vars = query.split("&");
		for (var i = 0; i < vars.length; i++) {
			var pair = vars[i].split("=");
			if (pair[0] == variable) {
				return pair[1];
			}
		}
		return (false);
	},

});
//计算实际筹备期 和 开始筹备月份
function calculateDate(openDate, planPreparedTime) {
	//8.2数值文本框设置保留一位小数无效
	// if (planPreparedTime % 0.1 !== 0) {
	// 	$("#PlanPreparedTime").val(parseFloat(planPreparedTime).toFixed(1));
	// }
	var date = new Date(openDate); //开业时间
	date.setDate(date.getDate() - planPreparedTime * 30);
	//创建时更新开始筹备月份
	if (method == 'addNew') {
		var year = date.getFullYear();
		var month = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1);
		$("#StartPreparedMonth").val(year + "-" + month); //更新开始筹备月份
	}
	//计算实际筹备期
	var openBusinessTime = $("#OpenBusinessTime").val();
	date = new Date(openDate);
	var openBusinessDate;
	if (!openBusinessTime) { //开业时间(首次)为空,需要判断第一次调整时是否同时更改开业时间和计划筹备期
		if (beforeOpenDate && openDate !== beforeOpenDate) {
			var openBusinessDate = new Date(beforeOpenDate);

		} else { //新增或者没有修改开业时间
			$("#PracticalPreparedTime").val(parseFloat(planPreparedTime).toFixed(1));
			return;
		}
	} else {
		var openBusinessDate = new Date(openBusinessTime);
		// var monthAmount = ((date - openBusinessDate) / thirtyDay).toFixed(1); //计算开业时间和首次开业时间之间的月份数,四舍五入
		// var practicalPreparedMonth = (parseFloat(monthAmount) + parseFloat(planPreparedTime));
		// var months = Number.isInteger(practicalPreparedMonth) ? practicalPreparedMonth + ".0" : practicalPreparedMonth
		// $("#PracticalPreparedTime").val(months);
	}
	var monthAmount = ((date - openBusinessDate) / thirtyDay).toFixed(1);
	var practicalPreparedMonth = (parseFloat(monthAmount) + parseFloat(planPreparedTime)).toFixed(1);
	//var months = Number.isInteger(practicalPreparedMonth) ? practicalPreparedMonth + ".0" : practicalPreparedMonth;
	$("#PracticalPreparedTime").val(practicalPreparedMonth);
};
//写入调整记录表
function insertStoreAdjustHistory(billId,open,jielou,budget) {
	var url = shr.getContextPath() + "/dynamic.do?handler=com.kingdee.eas.custom.newpreparation.web.customer.StoreFrontInfoEditHandler&method=insertStoreAdjustHistory";
	shr.ajax({
		async: false,
		type: 'post',
		data: {
			billId:billId,
			openDate:open,
			jielouDate:jielou,
			budgetTotal:budget
		},
		url: url,
		success: function (res) {
			flag = res.data;
		}
	});
}