var pageNo="1";
$(function(){
	/**
	 * 查询
	 */
	$("#btn-search").click(function(){
		$("#conditionform").submit();
	});
	
})
/**
 * 翻页
 * @param pageNo 页码
 */
function goPage(pageNo){
	if(pageNo=="gopage"){
		$("#pageNo").val($("#gopage").val());
	}else{
		$("#pageNo").val(pageNo);
	}
	$("#conditionform").submit();
}

/**
 * 显示充值的模态窗口
 */
function saveUpdateDepositRecordModel(){
	pageNo="1";
	getDepositRecord();
	calculatedAmount();
	clearForm();
}

/**
 * 清空form表单
 */
function clearForm(){
	$("#form1")[0].reset();
}

/**
 * 提交充值或扣减的预存款余额
 */
function commitDepositRecord(){
	var exp = /^([1-9][\d]{0,7}|0)(\.[\d]{1,2})?$/;
	var virtualId=$("#virtualId").val();
	var contextPath=$("#contextPath").val();
	var amountFee=$("#amountFee").val();
	 if(!exp.test(amountFee)){
		 layer.msg('请输入正确的金额');
		 return;
	}
	var totals=$("#totals").val();
	var status=$("#status").val();
	if(""==status){
		 layer.msg('请选择状态');
		 return;
	}
	if(status==-1){
		var price=totals-amountFee;
		if(price<0){
			 layer.msg('扣减的金额不能大于预存款的余额');
			 return;
		}
	}
	var remark=$("#remark").val();
	var url=contextPath+"/admin/bill/saveUpdateDepositRecord";
	$.ajax({
    	url: url,
    	data: {virtualId:virtualId,amountFee:amountFee,status:status,remark:remark},
        type: "POST",
        success: function(data){
        	if(data.flag){
        		layer.alert(data.desc, {icon: 6,end:function(){
	        		
	        	}});
        	}else{
        		layer.msg(data.desc, {icon: 5,end:function(){
	        		
	        	}});
        	}
        	clearForm();
        	getDepositRecord();
        	calculatedAmount();
			
        },
        error: function(){
        	layer.msg("提交失败，请检测网络", {icon: 5,end:function(){
        		
        	}});
        }
    });	
}

/**
 * 根据企业ID获取
 */
function getDepositRecord(){
	var virtualId=$("#virtualId").val();
	var contextPath=$("#contextPath").val();
	var url=contextPath+"/admin/bill/getDepositRecordPage";
	$("#example3").empty();
	$("#page3").empty();
	$.ajax({
    	url: url,
    	data: {virtualId:virtualId,pageNo:pageNo,pageSize:3},
        type: "POST",
        success: function(data){
        	var html="<thead>" +
						"<tr>" +
							"<th>日期</th><th>状态</th><th>金额</th><th>备注</th>	<th>处理人</th>" +
						"</tr>" +
					 "</thead>";
        	if(data!=null && data.items.length>0){
        		var items=data.items;
        		html+="<tbody>"
        		$.each(items,function(index,item){
        			var amountFee="";
        			var trs="";
        			trs+="<tr>";
        			trs+="<td>"+item.createTime+"</td><td>"+$("#status"+item.status+"").val()+"</td>";
        			if(item.status==-1){
        				amountFee="-"+item.amountFee;
        			}else{
        				amountFee=item.amountFee
        			}
        			trs+="<td>"+amountFee+"</td><td>"+item.remark+"</td><td>"+item.creator+"</td>";
        			trs+="</tr>";
        			html+=trs;
        		});
        		html+="</tbody>"
        	}else{
        		html+="<tbody><tr><td colspan='9' style='font-size: 24px;'>暂无预存款明细记录</td></tr></tbody>"
        	}
        	$("#example3").html(html);
        	var page="";
        		page+="<div align='center' class='pages clearfix'>";
        		$.each(data.iterPages,function(index,item){
        			if(data.currentPage==item){
        				page+="<span class='current'>"+item+"</span>";
        			}else if(item!=null){
        				page+="<a href='javascript:void(0);' onclick='ajaxgoPage("+item+");'>"+item+"</a>";
        			}else{
        				page+="<em>...</em>";
        			}
        		});
        		var param="-1";
        		page+="<strong>共<span style='color:black;font-weight:bold;font-size:16px;'>"+data.totalCount+"</span>条</strong>";
        		page+="<strong>跳转到第</strong><input type='text' id='gopages' value='"+pageNo+"' style='width: 40px;' onkeyup='yzValue()' onkeypress='yzValue()' /><strong>页</strong>";
        		page+="<a class='btn-sm  btn-primary' href='javascript:void(0);' id='bindBtn' onclick='ajaxgoPage("+param+");'>确定</a>";
        		page+="</div>";
        		page+="</div>";
              	$("#page3").html(page);
              	$("#saveUpdateDepositRecordModel").modal('show');
			
        },
        error: function(){
        	layer.msg("提交失败，请检测网络", {icon: 5,end:function(){
        		
        	}});
        }
    });	
}

function ajaxgoPage(pageNos){
	if(pageNos=="-1"){
		pageNo=$("#gopages").val();
	}else{
		pageNo=pageNos;
	}
	getDepositRecord();
}

function yzValue(){
	$("#gopages").val($("#gopages").val().replace(/\D/g,''));
}

/**
 * 根据企业ID查询企业余额
 */
function calculatedAmount(){
	var virtualId=$("#virtualId").val();
	var contextPath=$("#contextPath").val();
	var url=contextPath+"/admin/bill/getVirtualArchitecture";
	$.ajax({
    	url: url,
    	data: {virtualId:virtualId},
        type: "POST",
        success: function(data){
        	$("#totals").val(data.reserveBalance);
        	$("#total").html("预存款余额："+data.reserveBalance);
        },
        error: function(){
        	layer.msg("提交失败，请检测网络", {icon: 5,end:function(){
        		
        	}});
        }
    });	
}

/**
 * 跳转账单编辑页面
 */
function saveUpdateGeneralBills(id){
	var virtualId=$("#virtualId").val();
	var contextPath=$("#contextPath").val();
	var url=contextPath+"/admin/bill/generalBillsEidtView?virtualId="+virtualId+"&generalBillsId="+id+"&funcid="+$("#funcid").val();
	location.href=url;
}


/*
 * add by yubing
 * */
var mapObj = new Map(); //存储账单
var payHejiObj;			//缴费合计
var yueObj;				//预存款余额

/**
 * 显示支付状态管理窗口
 */
function showUpdatePayStatus(){
	var virtualId=$("#virtualId").val();
	var contextPath=$("#contextPath").val();
	var url=contextPath+"/admin/bill/queryPayBills";
	$.ajax({
    	url: url,
    	data: {virtualId:virtualId},
        type: "POST",
        dataType:"json",
        success: function(data){
        	mapObj.clear();//清空map
        	
        	$("#qianfeeMonth").html("");
        	if("noData"==data.dealFlag){
        		layer.msg("暂未未缴费账单", {icon: 5,end:function(){
            		
            	}});
        	}
        	else{
        		$("#saveUpdatePayStatus").modal('show');
        		var optionHtmlStr="";
        		var payHejiHtmlStr="";
        		if(data.bills.length>1){
        			optionHtmlStr+="<option value='-1' fee='"+data.totalFee+"'>全部</option>";
        			mapObj.set("-1", data.totalFee);
        			payHejiHtmlStr="未缴合计："+data.totalFee;
        			payHejiObj=data.totalFee;
        		}
        		$.each(data.bills,function(index,obj){
        			optionHtmlStr+="<option value='"+obj.id+"' fee='"+obj.totalFee+"'>"+obj.outMonth+"</option>";
        			mapObj.set(obj.id+"", obj.totalFee+"");
        			if(payHejiHtmlStr==""&&index==0){
        				payHejiHtmlStr="未缴合计："+obj.totalFee;
        				payHejiObj=obj.totalFee;
        			}
        		});
        		$("#qianfeeMonth").html(optionHtmlStr);
        		$("#pay_yue").html("预存款余额："+data.reserveBalance);
        		$("#pay_heji").html(payHejiHtmlStr);
        		yueObj=data.reserveBalance;
        	}
        },
        error: function(){
        	layer.msg("加载数据失败", {icon: 5,end:function(){
        		
        	}});
        }
    });	
}

function reloadPayHeji(){
	var qianfeeMonth=$("#qianfeeMonth").val();
	var payHejiHtmlStr="未缴合计："+mapObj.get(qianfeeMonth);
	$("#pay_heji").html(payHejiHtmlStr);
	payHejiObj=mapObj.get(qianfeeMonth);
}

/**
 * 支付状态更新
 */
function updatePayStatus(){
	var contextPath=$("#contextPath").val();
	var exp = /^([1-9][\d]{0,7}|0)(\.[\d]{1,2})?$/;
	
	var virtualId=$("#virtualId").val();
	var qianfeeMonth=$("#qianfeeMonth").val();
	var paidAmount=$("#paidAmount").val();	//实收金额
	var dedAmount=$("#dedAmount").val();    //抵扣金额
	if(!exp.test(paidAmount)||$.trim(paidAmount)==""){
		 layer.msg('请输入正确的实收金额');
		 return;
	}
	if(!exp.test(paidAmount)||$.trim(dedAmount)==""){
		 layer.msg('请输入正确的抵扣金额');
		 return;
	}
	else if(numSub(dedAmount,yueObj)>0){
		layer.msg('抵扣金额不能大于预存款余额');
		return;
	}
	var t=numSub(payHejiObj,numAdd(paidAmount,dedAmount));
	if(t!=0){
		 layer.msg('实收金额加抵+抵扣金额 必须等于欠费金额');
		 return;
	}
	
	var url=contextPath+"/admin/bill/billPayStatusUpdate";
	$.ajax({
    	url: url,
    	data: {virtualId:virtualId,billId:qianfeeMonth,payFee:paidAmount,dedFee:dedAmount},
        type: "POST",
        success: function(data){
			if("fail"==data.dealFlag){
				 layer.msg(data.resultDesc);
			}
			else{
				 layer.alert("处理成功！", {icon: 6,end:function(){
					 window.location.reload();//刷新当前页面.
				 }});
			}
        },
        error: function(){
        	layer.msg("提交失败，请检测网络", {icon: 5,end:function(){
        		
        	}});
        }
    });	
}

/**
 * 跳转打印账单
 */
function generalBillsPutView(id){
	var contextPath=$("#contextPath").val();
	var virtualId=$("#virtualId").val();
	var url=contextPath+"/admin/bill/generalBillsPutView?virtualId="+virtualId+"&generalBillsId="+id+"&funcid="+$("#funcid").val();
	location.href=url;
}