
var url="";
var myArray=new Array();
var selectValue=new Array();
/**
 * 签约协议JS
 */

$(function(){
	$('#otherInfoTab a:last').tab('show');//初始化显示哪个tab  
	   
    $('#otherInfoTab a').click(function(e) {  
        e.preventDefault();//阻止a链接的跳转行为  
        $(this).tab('show');//显示当前选中的链接及关联的content  
    })  
});


/**
 * 查询
 */
function searchServiceTypeInfo(){
	$("#conditionform").submit();
}

/**
 * 显示新增的动态model
 */
function showAddUpdateDeposit(id,serviceTypeInfoId){
	var contextPath=$("#contextPath").val();
	myArray=new Array();
	$("#serviceTypeInfoId").val(serviceTypeInfoId);
	url=contextPath+"/admin/signContact/getSignBasics";
	var title="押金信息明细";
	$.ajax({
    	url: url,
    	data: {signId:id},
        type: "POST",
        success: function(data){
        	$("#depositForm").empty();
        	var html="";
        		html="<div class='row' ><div class='col-sm-4'>押金信息名称</div><div class='col-sm-3'>数量</div><div class='col-sm-2'>金额</div><div class='col-sm-3'>操作</div></div>";
        		$("#depositForm").append(html);
        	var totalAmount=0;
        	if(data!=null && data!="" && data.length>0){
        		$.each(data,function(index,item){
        			html="";
        			totalAmount+=item.totalAmount;
        			html="<div class='row' id='row"+index+"'>";
        			html+="<input type='hidden' id='deposit"+index+"' value='"+item.id+"'>"
        			html+="<div class='col-sm-4'>" +
        						"<select name='basicsId'  id='basicsId"+index+"' onchange='basicsIdSelectValue("+index+")' class='form-control' style='width:150px;'>" +
        							"<option value=''>请选择押金项</option>" +
        						"</select>" +
        				 "</div>";
        			html+="<div class='col-sm-3' ><input class='form-control' style='margin-left:15px;text-align: center;' onkeyup='calculatedAmount("+index+")'  id='number"+index+"'  type='number' value='"+item.number+"' placeholder='请输入数量'/></div>";
        			html+="<div class='col-sm-2'><span id='totalAmount"+index+"' style='margin-top:20px;'>"+item.totalAmount+"</span></div>";
        			html+="<div class='col-sm-3'><a href='javascript:void(0);' title='保存' id='save"+index+"'  class='btn btn-default' style='display: none' onclick='saveUpdateDeposit("+index+")'><span>保存</span></a>" +
        					"<a href='javascript:void(0);'  title='编辑' class='btn btn-back' id='update"+index+"' onclick='updateDeposit("+index+")'><span>编辑</span></a>" +
        					" <a href='javascript:void(0);' title='删除' class='btn btn-danger' onclick='deleteDeposit("+index+")'><span>删除</span></a></div>";
        			html+="</div>";
        			$("#depositForm").append(html);
        			depositUpdateShowHide(index);
        			loadDepositItemInformationFormList(serviceTypeInfoId,index,item.basicsId);
        			myArray.push(index);
        		});
        		
        	}else{
        		html="";
        		html+="<div class='row' id='data1' style='text-align: center;font-size: 18px;'>暂无信息</div>";
        		$("#depositForm").append(html);
        	}
        	title+="<span style='padding-left:10px;' id='total'>总金额："+totalAmount+"</span>";
        	title+="<a style='float:right;margin-right:20px;' href='javascript:void(0);' title='押金管理' class='btn btn-success' onclick='addDeposit("+totalAmount+")'><span>新增</span></a>"
        	$("#myDepositModalLable").html(title);
        	$('#saveUpdateDepositModal').modal('show');
        },
        error: function(){
        	layer.msg("请检测网络", {icon: 5,end:function(){
        		
        	}});
        }
    });
	$("#signId").val(id);
}

/**
 * 新增押金项
 * @param datalength
 */
function addDeposit(datalength){
	if(datalength==0){
		$("#data1").remove();
	}
	var index=0;
	if(myArray.length<1){
		index=1;
		$("#data1").remove();
	}else{
		index=myArray[myArray.length-1]+1;
	}
	var html="";
	html="<div class='row' id='row"+index+"'>";
	html+="<input type='hidden' id='deposit"+index+"' value=''>"
	html+="<div class='col-sm-4'>" +
				"<select name='basicsId'  id='basicsId"+index+"' onchange='basicsIdSelectValue("+index+")' class='form-control' style='width:150px;'>" +
					"<option value=''>请选择押金项</option>" +
				"</select>" +
		 "</div>";
	html+="<div class='col-sm-3' ><input class='form-control' style='margin-left:15px;text-align: center;' onkeyup='calculatedAmount("+index+")'  id='number"+index+"'   type='number' placeholder='请输入数量'/></div>";
	html+="<div class='col-sm-2'><span id='totalAmount"+index+"' style='margin-top:20px;'>0</span></div>";
	html+="<div class='col-sm-3'><a href='javascript:void(0);' title='保存' id='save"+index+"'  class='btn btn-default' onclick='saveUpdateDeposit("+index+")'><span>保存</span></a>" +
			"<a href='javascript:void(0);' style='display: none' title='编辑' class='btn btn-back' id='update"+index+"' onclick='updateDeposit("+index+")'><span>编辑</span></a>" +
			" <a href='javascript:void(0);' title='删除' class='btn btn-danger' onclick='deleteDeposit("+index+")'><span>删除</span></a></div>";
	html+="</div>";
	$("#depositForm").append(html);
	myArray.push(index);
	var serviceTypeInfoId=$("#serviceTypeInfoId").val();
	loadDepositItemInformationFormList(serviceTypeInfoId,index);
	
}


/**
 * 删除
 * @param index
 */
function deleteDeposit(index){
	var deposit=$("#deposit"+index+"").val();
	if(deposit!=""){
		layer.msg('您确定要删除该押金配置项？', {
			  time: 0 //不自动关闭
			  ,btn: ['确定', '取消']
			  ,yes: function(indexs){
			    layer.close(indexs);
			    myArray.splice(myArray.indexOf(index),1);
				deleteDepositAjax(index);
			  }
			});
	}else{
		 myArray.splice(myArray.indexOf(index),1);
		$("#row"+index+"").remove();
		if(myArray.length<=1){
			$("#depositForm").append("<div class='row' id='data1' style='text-align: center;font-size: 18px;'>暂无信息</div>");
		}
		calculatTotal();
	}
}

/**
 * 根据服务ID查询配置项的押金项信息
 * @param serviceTypeInfoId
 */
function loadDepositItemInformationFormList(serviceTypeInfoId,indexs,basicsId){
	var contextPath=$("#contextPath").val();
	$("#basicsId"+indexs).empty();
	url=contextPath+"/admin/signContact/getDepositItemInformationFormList";
	$.ajax({
    	url: url,
    	data: {serviceTypeInfoId:serviceTypeInfoId},
        type: "POST",
        success: function(data){
        	if(data!=null && data!="" && data.length>0){
        		$.each(data,function(index,item){
        			if(item.id==basicsId){
        				$("#basicsId"+indexs).append("<option selected='selected' value='"+item.id+","+item.price+"'>"+item.deposName+"("+item.price+")</option>");
        			}else{
        				$("#basicsId"+indexs).append("<option value='"+item.id+","+item.price+"'>"+item.deposName+"("+item.price+")</option>");
        			}	
        		})
        	}
        },
        error: function(){
        	layer.msg("请检测网络", {icon: 5,end:function(){
        		
        	}});
        }
    });
}

/**
 * 服务项改变的时候
 * @param index
 */
function basicsIdSelectValue(index){
	$("#number"+index).val(0);
	for(var i=0;i<myArray.length;i++){
		var basicsId=$("#basicsId"+myArray[i]).val();
		var number=$("#number"+myArray[i]).val();
		var price=0;
		if(basicsId!="" && number!=""){
			var priceArray=basicsId.split(",");
			price=priceArray[1];
			
		}
		var total=number*price;
		$("#totalAmount"+myArray[i]).html(total);
		
	}
	calculatTotal();
}

/**
 * 计算金额
 * @param index
 */
function calculatedAmount(index){
	var number=$("#number"+index).val();
	 if(/[^\d]/.test(number)){//替换非数字字符  
         var temp_amount=number.replace(/[^\d]/g,'');  
         $(this).val(temp_amount);  
     }  
	var basicsId=$("#basicsId"+index).val();
	var price=0;
	if(basicsId!="" && number!=""){
		var priceArray=basicsId.split(",");
		price=priceArray[1];
	}
	var total=Number(number)*Number(price);
	$("#totalAmount"+index).html(total);
	calculatTotal();
}

/**
 * 计算总金额
 */
function calculatTotal(){
	var total=0;
	$.each(myArray,function(index,data){
		if($("#totalAmount"+data).html()!=""){
			total+=Number($("#totalAmount"+data).html());
		}
	});
	$("#total").html("总金额："+total);
}

/**
 * 保存押金信息明细
 * @param index
 */
function saveUpdateDeposit(index,id){
	var contextPath=$("#contextPath").val();
	var basicsId=$("#basicsId"+index).val();
	var depositId=$("#deposit"+index+"").val();
	if($.trim(basicsId)==""){
		layer.msg('请选择押金项！');
		return;
	}
	var basicsIdArray=basicsId.split(",");
	var number=$("#number"+index).val();
	if($.trim(number)==""){
		layer.msg('请输入数量！');
		return;
	}
	if(number<=0){
		layer.msg('数量不能为0');
		return;
	}
	var serviceTypeInfoId=$("#serviceTypeInfoId").val();
	if($.trim(number)==""){
		layer.msg('服务ID不能为空！');
		return;
	}
	var totalAmount=$("#totalAmount"+index).html();
	if($.trim(totalAmount)==""){
		layer.msg('总金额不能为空！');
		return;
	}
	var signId=$("#signId").val();
	url=contextPath+"/admin/signContact/saveUpdateDeposit";
	$.ajax({
    	url: url,
    	data: {id:depositId,number:number,serviceTypeInfoId:serviceTypeInfoId,
    		totalAmount:totalAmount,basicsId:basicsIdArray[0],signId:signId},
        type: "POST",
        success: function(data){
        	if(data.flag){
        		layer.msg(data.desc);
        		depositShowHide(index);
        		$("#deposit"+index+"").val(data.id);
        	}else{
        		layer.msg(data.desc);
        	}
        },
        error: function(){
        	layer.msg("请检测网络", {icon: 5,end:function(){
        		
        	}});
        }
    });
	
}

/**
 * 删除押金信息明细
 * @param index
 */
function deleteDepositAjax(index){
	var contextPath=$("#contextPath").val();
	var depositId=$("#deposit"+index+"").val();
	url=contextPath+"/admin/signContact/deleteDeposit";
	if(depositId!="" && depositId!=null){
		$.ajax({
	    	url: url,
	    	data: {id:depositId},
	        type: "POST",
	        success: function(data){
	        	if(data.flag){
	        		var flag=false;
	        		$("#row"+index+"").remove();
	        		layer.msg(data.desc);
	        		for(var i=0;i<myArray.length;i++){
	        			if(myArray[i]!=""){
	        				flag=true;
	        			}
	        		}
	        		
	        		if(!flag){
	        			$("#depositForm").append("<div class='row' id='data1' style='text-align: center;font-size: 18px;'>暂无信息</div>");
	        		}
	        		calculatTotal();
	        	}else{
	        		layer.msg(data.desc);
	        	}
	        },
	        error: function(){
	        	layer.msg("请检测网络", {icon: 5,end:function(){
	        		
	        	}});
	        }
	    });
	}
}

function updateDeposit(index){
	$("#save"+index).show();
	$("#update"+index).hide();
	$("#basicsId"+index).attr("disabled",false);
	$("#number"+index).attr("disabled",false);
}

function depositShowHide(index){
	$("#save"+index).hide();
	$("#update"+index).show();
	$("#basicsId"+index).attr("disabled",true);
	$("#number"+index).attr("disabled",true);
}

function depositUpdateShowHide(index){
	$("#basicsId"+index).attr("disabled",true);
	$("#number"+index).attr("disabled",true);
}

function showAddUpdateInvoiceBill(){
	var virtualId=$("#virtualId").val();
	var contextPath=$("#contextPath").val();
	$("#tabForm1")[0].reset();
	$("#tabForm2")[0].reset();
	url=contextPath+"/admin/signContact/getSignServiceInvoiceObj";
	$("#type").val(1);
		$.ajax({
	    	url: url,
	    	data: {virtualId:virtualId,type:1},
	        type: "POST",
	        success: function(data){
	        	if(data==null || data==""){
	        		$("#save").show();
	        		$("#update").hide();
	        	}else{
	        		$("#update").show();
	        		$("#save").hide();
	            	$("#raises").val(data.raise);
	            	$("#identificationNumbers").val(data.identificationNumber);
	            	$("#raises").attr("disabled",true);
	            	$("#identificationNumbers").attr("disabled",true);
	        	}
	        },
	        error: function(){
	        	layer.msg("请检测网络", {icon: 5,end:function(){
	        		
	        	}});
	       }
	 });
	$("#generalTicket").attr("style","background-color:#00c0ef;color: white;");
	$("#specialTicket").attr("style","background-color: #666666;color: white;");
	$("#tabForm1").show();
	$("#tabForm2").hide();
	$('#saveUpdateInvoiceBillModal').modal('show');
}

function eidtInvoiceBillTab(){
	var type=$("#type").val();
	if(type==1){
		$("#raises").attr("disabled",false);
		$("#identificationNumbers").attr("disabled",false);
	}else{
		$("#raise").attr("disabled",false);
		$("#identificationNumber").attr("disabled",false);
		$("#address").attr("disabled",false);
		$("#mobile").attr("disabled",false);
		$("#bank").attr("disabled",false);
		$("#bankCard").attr("disabled",false);
	}
	$("#save").show();
	$("#update").hide();
}

/**
 * 切换tab
 * @param id
 */
function addUpdateInvoiceBillTab(status){
	$("#type").val(status);
	var virtualId=$("#virtualId").val();
	var contextPath=$("#contextPath").val();
	url=contextPath+"/admin/signContact/getSignServiceInvoiceObj";
	$.ajax({
    	url: url,
    	data: {virtualId:virtualId,type:status},
        type: "POST",
        success: function(data){
        	if(data==null || data==""){
        		$("#save").show();
        		$("#update").hide();
        	}else{
        		$("#update").show();
        		$("#save").hide();
        		if(status==1){
            		$("#raises").val(data.raise);
            		$("#identificationNumbers").val(data.identificationNumber);
            		$("#raises").attr("disabled",true);
            		$("#identificationNumbers").attr("disabled",true);
            	}else{
            		$("#raise").val(data.raise);
            		$("#identificationNumber").val(data.identificationNumber);
            		$("#address").val(data.address);
            		$("#mobile").val(data.mobile);
            		$("#bank").val(data.bank);
            		$("#bankCard").val(data.bankCard);
            		$("#raise").attr("disabled",true);
            		$("#identificationNumber").attr("disabled",true);
        			$("#address").attr("disabled",true);
        			$("#mobile").attr("disabled",true);
        			$("#bank").attr("disabled",true);
        			$("#bankCard").attr("disabled",true);
            	}
        	}
        	if(status==1){
        		$("#generalTicket").attr("style","background-color:#00c0ef;color: white;");
        		$("#specialTicket").attr("style","background-color: #666666;color: white;");
        		$("#tabForm1").show();
        		$("#tabForm2").hide();
        	}else{
        		$("#specialTicket").attr("style","background-color:#00c0ef;color: white;");
        		$("#generalTicket").attr("style","background-color: #666666;color: white;");
        		$("#tabForm2").show();
        		$("#tabForm1").hide();
        	}
        },
        error: function(){
        	layer.msg("请检测网络", {icon: 5,end:function(){
        		
        	}});
       }
 });
}

/**
 * 保存或编辑发票信息
 */
function saveUpdateInvoiceBillTab(){
	var type=$("#type").val();//发票类型
	var virtualId=$("#virtualId").val();
	var contextPath=$("#contextPath").val();
	url=contextPath+"/admin/signContact/saveUpdateInvoiceBillTab";
	var raise="";
	var identificationNumber="";
	var address="";
	var mobile="";
	var bank="";
	var bankCard="";
	if(type==1){
		raise=$("#raises").val();
		if($.trim(raise)==""){
			layer.msg('请输入发票抬头！');
			return;
		}
		identificationNumber=$("#identificationNumbers").val();
		if($.trim(identificationNumber)==""){
			layer.msg('请输入纳税人识别号！');
			return;
		}
	}else{
		raise=$("#raise").val();
		if($.trim(raise)==""){
			layer.msg('请输入发票抬头！');
			return;
		}
		identificationNumber=$("#identificationNumber").val();
		if($.trim(identificationNumber)==""){
			layer.msg('请输入纳税人识别号！');
			return;
		}
		address=$("#address").val();
		if($.trim(address)==""){
			layer.msg('请输入地址！');
			return;
		}
		mobile=$("#mobile").val();
		if($.trim(mobile)==""){
			layer.msg('请输入手机号码！');
			return;
		}
		bank=$("#bank").val();
		if($.trim(bank)==""){
			layer.msg('请输入开户银行！');
			return;
		}
		bankCard=$("#bankCard").val();
		if($.trim(bankCard)==""){
			layer.msg('请输入开会账号！');
			return;
		}
	}
	$.ajax({
    	url: url,
    	data: {type:type,virtualId:virtualId,raise:raise,identificationNumber:identificationNumber,
    		address:address,mobile:mobile,bank:bank,bankCard:bankCard},
        type: "POST",
        success: function(data){
        	if(data.flag){
        		layer.msg(data.desc);
        		$("#save").hide();
        		$("#update").show();
        		if(type==1){
        			$("#raises").attr("disabled",true);
        			$("#identificationNumbers").attr("disabled",true);
        			$("#generalTicket").attr("style","background-color:#00c0ef;color: white;");
            		$("#specialTicket").attr("style","background-color: #666666;color: white;");
            		$("#tabForm1").show();
            		$("#tabForm2").hide();
        		}else{
        			$("#raise").attr("disabled",true);
        			$("#identificationNumber").attr("disabled",true);
        			$("#address").attr("disabled",true);
        			$("#mobile").attr("disabled",true);
        			$("#bank").attr("disabled",true);
        			$("#bankCard").attr("disabled",true);
        			$("#specialTicket").attr("style","background-color:#00c0ef;color: white;");
            		$("#generalTicket").attr("style","background-color: #666666;color: white;");
            		$("#tabForm2").show();
            		$("#tabForm1").hide();
        		}
        	}
        },
        error: function(){
        	layer.msg("请检测网络", {icon: 5,end:function(){
        		
        	}});
       }
 });
}

/**
 * 显示意向购买商品明细model
 */
function showServicePackDetails(id){
	var contextPath=$("#contextPath").val();
	url=contextPath+"/admin/signContact/getIntentionalGoods";
	$("#tbody").empty();
	$.ajax({
    	url: url,
    	data: {id:id},
        type: "POST",
        success: function(data){
        	if(data!=null && data!="" && data.length>0){
        		$.each(data,function(index,item){
        			$("#tbody").append("<tr><td>"+item.name+"</td><td>"+item.price+"</td></tr>");
        		})
        	}else{
        		$("#tbody").html("<tr><td colspan='2'>暂无意向商品</td></tr>");
        	}
        	$('#servicePackDetails').modal('show');
        },
        error: function(){
        	layer.msg("请检测网络", {icon: 5,end:function(){
        		
        	}});
       }
 });
}
/**
 * 翻页
 * @param pageNo 页码
 */
function goPage(pageNo){
	if(pageNo=="gopage"){
		$("#pageNo").val($("#gopage").val());
	}else{
		$("#pageNo").val(pageNo);
	}
	$("#conditionform").submit();
}