/**
 * 我要报销页面
 * 
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司 
 * @license   http://www.u-bo.com/license/ 
 * 
 * @author Strief http://blog.u-bo.com
 * @since 0.1
 */
jQuery(function($) {

	//checkMoney();

	$(document).on('change', '.select-link', function() {
		var curTr = $('.link_name');
		if (curTr.attr('style') == "display: none;") {
			curTr.show();
		} else {
			curTr.hide();
		}
	});

	/*$(document).on('change', ".reim_money,select[name='Reimbursement[category_id]']", function() {
		checkMoney();
	});*/

	//报销关联检测
	$('#reimbursement-link_name').focusout(function() {
		$('.redstar-money').remove();
		$(this).closest('td').attr('class','link_name');

		if ($(this).val() == '') {
			$(this).closest('td').attr('class','link_name has-error');
			$(this).after("<span class='redstar redstar-money' style='font-size:10px;'>关联不能为空</span>");
		}
	});

	/**
	 * 若金额出现错误或报销日期不符合规定则禁止表单提交
	 */
	$("#main-form").submit(function () {
		$('.redstar-money').remove();
		$('#reimbursement-link_name').closest('td').attr('class','link_name');

		if ($("input[name='Reimbursement[link_name]']").val() == '') {
			$('#reimbursement-link_name').closest('td').attr('class','link_name has-error');
			$('#reimbursement-link_name').after("<span class='redstar redstar-money' style='font-size:10px;'>关联不能为空</span>");
			return false;
		}

		/*if ($('.reim_money').val() > maxMoney && $("input[name='Reimbursement[appli_id]']").val() == '0') {
			$('.reim_appli').show();
			$("input[name='Reimbursement[appli_id]']").after("<span class='redstar redstar-money' style='font-size:10px;'>请关联申请单</span>");
			return false;
		} else if(checkTime()) {
			return true;
		} else {
			return false;
		}*/
	});
});

/**
 * 报销金额检测
 */
function checkMoney() {
	$('.redstar-money').remove();
	$.get(getCheckMoney, {'cat_id':$("select[name='Reimbursement[category_id]']").val()}, function(resp) {
		if (resp.data == 1) { //若当前报销类别需要检测金额
			//更改金额输入框的placeholder
			$('.reim_money').attr('placeholder','费用超过' + maxMoney +'元需要关联申请单');
			//若此时已有金额输入并且金额超出，则出现关联申请单提醒
			if ($('.reim_money').val() > maxMoney) {
				$('.reim_money').after("<span class='redstar redstar-money' style='font-size:10px;'>金额超出规定，请关联申请单</span>");
				$('.reim_appli').show();
			}
		} else {
			$('.reim_money').attr('placeholder','该类别下报销金额无限制');
			$('.select-application').val('');
			$("input[name='Reimbursement[appli_id]']").val('0');
			$('.reim_appli').hide();
		}
	});
}

/**
 * 报销日期检测
 */
function checkTime() {
	$('.check-reim-money').remove();
	var nowDate = new Date();
	nowTime = nowDate.getFullYear()*10000 + (nowDate.getMonth()+1)*100 + nowDate.getDate();
	var reim_time = $("input[name='Reimbursement[reim_time]']");
	var selectTime = reim_time.val();
	//var selectTimes = Date.parse(selectTime.substring(0, 10).split('-'));
	selectTime = selectTime.substring(0, 10).split('-');
	selectTimes = selectTime[0]*10000 + selectTime[1]*100 + selectTime[2]*1;

	if (selectTimes > nowTime) {
		reim_time.after("<span class='redstar check-reim-money' style='font-size:10px;'>只能选择截止到今天的时间</span>");
		return false;
	} else if (nowTime - selectTimes >200) {
		if (selectTime[0] == nowDate.getFullYear()) {
			reim_time.after("<span class='redstar check-reim-money' style='font-size:10px;'>两个月之前的不允许报销</span>");
			return false;	
		} else if (Math.floor((nowDate.getTime()-Date.parse(selectTime))/(24*3600*1000)) > 61) {
			reim_time.after("<span class='redstar check-reim-money' style='font-size:10px;'>两个月之前的不允许报销</span>");
			return false;
		} else {
			return true;
		}
	} else {
		return false;
	}
}