
//添加汇总项目
function addRcptBillDetailsTblTr(){
	var itemNameOption='<option value="">请选择</option>'+$("#hselBillItemName").html();
	var trStr='<tr id="" onclick="selTblTr(this,\'rcptBillDetailsTbl\')"><td><select name="billItemCode">'+itemNameOption+'</select></td>'
			+'<td><input type="text" name="billAmt" /></td><td><input type="text" name="ownAmt" /></td><td><input type="text" name="divAmt" /></td>'
			+'<td><input type="text" name="medIns" /></td><td><input type="text" name="thirdPay" /></td><td><input type="text" name="deductionsAmt"/></td></tr>';
	$("#rcptBillDetailsTbl").append(trStr);
}
//添加药品清单
function addRcptDrugsListTblTr(){
	var itemNameOption='<option value="">请选择</option>'+$("#hselBillItemName").html();
	var medicalInsType='<option value="">请选择</option><option value="甲类">甲类</option><option value="乙类">乙类</option><option value="丙类">丙类</option><option value="其他">其他</option>';
	var isReject='<option value="">请选择</option><option value="Y">是</option><option value="N">否 </option>';
	var trStr='<tr id="" onclick="selTblTr(this,\'rcptDrugsListTbl\')"><td><input type="text" name="drugsCode" /></td><td><input type="text" name="drugsName" onfocus="bundleTip(this)" onkeyup="medicalChoose(this)" onkeydown="medicalChoose(this)"/></td>'
				+'<td><select name="billItemCode">'+itemNameOption+'</select></td><td><input type="text" name="specifications" /></td><td><input type="text" name="dosageForm" /></td>'
				+'<td><input type="text" name="units" /></td><td><input type="text" name="unitPrice" /></td><td><input type="text" name="quantity" /></td>'
				+'<td><input type="text" name="actualPayment" /></td><td><select name="medicalInsType">'+medicalInsType+'</select></td><td><input type="text" name="ownRate" /></td>'
				+'<td><input type="text" name="ownCostAmt" /></td><td><select name="isReject">'+isReject+'</select></td><td><input type="text" name="excludeReason" /></td></tr>';
	$("#rcptDrugsListTbl").append(trStr);
}
//添加其他费用清单
function addRcptOtherCostTblTr(){
	var medicalInsType='<option value="">请选择</option><option value="甲类">甲类</option><option value="乙类">乙类</option><option value="丙类">丙类</option><option value="其他">其他</option>';
	var itemNameOption='<option value="">请选择</option>'+$("#hselBillItemName").html();
	var isReject='<option value="">请选择</option><option value="Y">是</option><option value="N">否 </option>';
	var trStr='<tr id=""  onclick="selTblTr(this,\'rcptOtherCostTbl\')"><td><select name="billItemCode">'+itemNameOption+'</select></td>'
				+'<td><input type="text" name="expenseName" autocomplete="off" onfocus="bundleOthTip(this)" onkeyup="otherCostInfoChoose(this)" onkeydown="otherCostInfoChoose(this)" /></td><td><select>'+medicalInsType+'</select></td>'
				+'<td><input type="text" name="drugsCode" /></td><td><input type="text" name="actualPayment" /></td><td><input type="text" name="ownCostRate" /></td>'
				+'<td><input type="text" name="ownCostSAmt" /></td><td><input type="text" name="unitPrice" /></td><td><select name="isReject">'+isReject+'</select></td><td><input type="text" name="excludeReason" /></td></tr>';
	$("#rcptOtherCostTbl").append(trStr);
}
//汇总项目
function getRcptBillDetailsJson(){
	str="";
	var rcptBillDetailsTbltrs=$("#rcptBillDetailsTbl tr");
	for(var i=1;i<rcptBillDetailsTbltrs.length;i++){
		var tr=$(rcptBillDetailsTbltrs[i]);
		var id=tr.attr("id");
		var billItemCode=tr.find("select[name='billItemCode']").val();//项目代码
		var productName=tr.find("select[name='billItemCode'] option:selected").text();//账单名称
		var billAmt=tr.find("input[name='billAmt']").val();//账单金额
		var ownAmt=tr.find("input[name='ownAmt']").val();//自费金额
		var divAmt=tr.find("input[name='divAmt']").val();//分类自负
		var medIns=tr.find("input[name='medIns']").val();//医保给付金额
		var thirdPay=tr.find("input[name='thirdPay']").val();//第三方给付 金额
		var deductionsAmt=tr.find("input[name='deductionsAmt']").val();//扣款调整金额
		id=id==undefined?"":id;
		productName=billItemCode==''?'':productName;
		str+='{"id":"'+id+'","billItemCode":"'+billItemCode+'","productName":"'+productName+'","billAmt":"'+billAmt
				+'","ownAmt":"'+ownAmt+'","divAmt":"'+divAmt+'","medIns":"'+medIns
				+'","thirdPay":"'+thirdPay+'","deductionsAmt":"'+deductionsAmt+'"},';
	}
	if(str!=''){
		return str.substring(0,str.length-1);
	}else{
		return "";
	}
}
//其他费用清单
function getRcptOtherCostInfos(){
	str="";
	var rcptOtherCostTbltr=$("#rcptOtherCostTbl tr");
	for(var i=1;i<rcptOtherCostTbltr.length;i++){
		var tr=$(rcptOtherCostTbltr[i]);
		var id=tr.attr("id");
		var billItemCode=tr.find("select[name='billItemCode']").val();
		var productName=tr.find("select[name='billItemCode'] option:selected").text();
		var expenseName=tr.find("input[name='expenseName']").val();
		var medicalInsType=tr.find("select[name='medicalInsType']").val();
		var drugsCode=tr.find("input[name='drugsCode']").val();
		var actualPayment=tr.find("input[name='actualPayment']").val();
		var ownCostRate=tr.find("input[name='ownCostRate']").val();
		var ownCostSAmt=tr.find("input[name='ownCostSAmt']").val();
		var unitPrice=tr.find("input[name='unitPrice']").val();
		var isReject=tr.find("select[name='isReject']").val();
		var excludeReason=tr.find("input[name='excludeReason']").val();
		id=id==undefined?"":id;
		str+='{"id":"'+id+'","billItemCode":"'+billItemCode+'","productName":"'+productName+'","expenseName":"'+expenseName
		+'","medicalInsType":"'+medicalInsType+'","drugsCode":"'+drugsCode+'","actualPayment":"'+actualPayment
		+'","ownCostRate":"'+ownCostRate+'","ownCostSAmt":"'+ownCostSAmt+'","unitPrice":"'+unitPrice
		+'","isReject":"'+isReject+'","excludeReason":"'+excludeReason+'",},';
	}
	if(str!=""){
		return str.substring(0,str.length-1);
	}else{
		return "";
	}
}
//药品清单
function getRcptDrugsListsJson(){
	str="";
	var rcptDrugsListTbltr=$("#rcptDrugsListTbl tr");
	for(var i=1;i<rcptDrugsListTbltr.length;i++){
		var tr=$(rcptDrugsListTbltr[i]);
		var id=tr.attr("id");
		var drugsCode=tr.find("input[name='drugsCode']").val();//药品编码
		var drugsName=tr.find("input[name='drugsName']").val();//药品名称
		var billItemCode=tr.find("select[name='billItemCode']").val();//对应账单项代码
		var specifications=tr.find("input[name='specifications']").val();//规格
		var dosageForm=tr.find("input[name='dosageForm']").val();//剂型
		var units=tr.find("input[name='units']").val();//单位
		var unitPrice=tr.find("input[name='unitPrice']").val();//单价
		var quantity=tr.find("input[name='quantity']").val();//数量
		var actualPayment=tr.find("input[name='actualPayment']").val();//发生金额
		var medicalInsType=tr.find("select[name='medicalInsType']").val();//医保类别
		var ownRate=tr.find("input[name='ownRate']").val();//自付比例
		var ownCostAmt=tr.find("input[name='ownCostAmt']").val();//自付金额
		var isReject=tr.find("select[name='isReject']").val();//是否剔除
		var excludeReason=tr.find("input[name='excludeReason']").val();//剔除原因
		id=id==undefined?"":id;
		str+='{"id":"'+id+'","drugsCode":"'+drugsCode+'","drugsName":"'+drugsName+'","billItemCode":"'+billItemCode
				+'","specifications":"'+specifications+'","dosageForm":"'+dosageForm+'","units":"'+units
				+'","unitPrice":"'+unitPrice+'","quantity":"'+quantity+'","actualPayment":"'+actualPayment
				+'","medicalInsType":"'+medicalInsType+'","ownRate":"'+ownRate+'","ownCostAmt":"'+ownCostAmt
				+'","isReject":"'+isReject+'","excludeReason":"'+excludeReason+'"}';
		if(i!=rcptDrugsListTbltr.length-1){
			str+=",";
		}
		
	}
	return str;
}
//收据 详细信息
function saveReceiptDetail(){
	var rcptDrugsLists=getRcptDrugsListsJson();
	var rcptOtherCostInfos=getRcptOtherCostInfos();
	var rcptBillDetails=getRcptBillDetailsJson();
	var claimNo=$("#hclaimNo").val();
	var eventNo=$("#heventNo").val();
	var receiptNo=$("#hreceiptNo").val();
	var jsonStr='{"claimNo":"'+claimNo+'","receiptNo":"'+receiptNo
				+'","eventNo":"'+eventNo+'","rcptDrugsLists":['+rcptDrugsLists+'],"rcptOtherCostInfos":['+rcptOtherCostInfos
				+'],"rcptBillDetails":['+rcptBillDetails+']}';
	$.ajax({
		type:"POST",
		url:"/claimserver/epiboly/saveReceiptDetail.do",
		contentType:"application/json; charset=UTF-8",
		data:jsonStr,
		success:function(data){
			alert('保存成功');
			location='/claimserver/epiboly/findReceiptDetail.do?claimNo='+claimNo+"&receiptNo="+receiptNo;
		},error:function(e){
			alert('保存失败');
		}
	});
	
}
function bundleTip(obj){
	var aa = $("#x888z");
	aa.removeAttr("id");
	var bb=$("#x889z");
	bb.removeAttr("id");
	var cc=$("#x890z");
	cc.removeAttr("id");
	var dd=$("#x891z");
	dd.removeAttr("id");
	var ee=$("#x892z");
	ee.removeAttr("id");
	var ff=$("#x893z");
	ff.removeAttr("id");
	//项目名称
	var gg=$("#x894z");
	gg.removeAttr("id");
	//项目代码
	var hh=$("#895z");
	hh.removeAttr("id");
	$(obj).parent().parent().find("input").eq(0).attr("id","x888z");
	$(obj).parent().parent().find("input").eq(2).attr("id","x889z");
	$(obj).parent().parent().find("input").eq(3).attr("id","x890z");
	$(obj).parent().parent().find("input").eq(4).attr("id","x891z");
	$(obj).parent().parent().find("input").eq(5).attr("id","x892z");
	$(obj).parent().parent().find("input").eq(6).attr("id","x893z");
	$(obj).parent().parent().find("span").eq(0).attr("id","x894z");//项目名称
//	$(obj).parent().parent().find()//项目代码
	
	$("#u-tip-div").remove();
	var ct = document.getElementById("current-tip");
	$(ct).removeAttr("id");
	
	$(obj).attr("id","current-tip");
	$(obj).parent.parent;
	var html = "<div class=\"sel_list\" id=\"u-tip-div\" style=\"display:block; margin:0 0 0 53px; width:164px; padding:0;\"><ul id=\"u-tip\" ></ul></div>";
	$(obj).parent("td:eq(0)").append(html);
	
	
}
function medicalChoose(obj){
	$("#medicalTbl").html("");
	var medicalName =$(obj).val();
	if(medicalName==null||medicalName==""){
		return;
		
	}
	   $.ajax({  
	        type: "POST",  
	        url: "/claimserver/dataenter/medicalSearch.do",  
	        data:{medicalName:medicalName},
	        success: function(data){
	        	var items = data.medicalList; 
	        	$("#u-tip").html("");
	        	if(items.length>0){
	        		for ( var i = 0; i < items.length; i++) {
	        			var h = items[i].objId;
	        			$("#u-tip").append("<li onclick=\"fillInVal(this)\" data-code="+h+" onmouseover=\"changbackclrFn(this)\" >"+items[i].hmMedChName+"</li>");
	        			
	        		}
	        	}
	        	
	        }  
	    }); 
}

function fillInVal(obj){
	hmGjypbm=$(obj).attr("data-code");
	$("#current-tip").val($(obj).html());
	$("#u-tip-div").remove();
	  $.ajax({
	        type: "POST", 
	        url: "/claimserver/dataenter/medicalSearchById.do",  
	        data:{objId:hmGjypbm},
	        success: function(data){
	        	$("#x888z").val(data.medical.hmGjypbm);//药品编码
	        	$("#x889z").val(data.medical.hmMedSpec);//规格
	        	$("#x890z").val(data.medical.hmMedForm);//剂型
	        	$("#x891z").val(data.medical.hmMedUnit);//单位
//	        	$("#x892z").val(data.medical.hmPrice);//价格
//	        	$("#x893z").val(data.medical.hmType);
	        	}
	        	
	    }); 
}
function changbackclrFn(flag){
	$(flag).css("background-color", "#e5e5e5").siblings().css("background-color", "#FFFFFF");
}
//删除行
function delTblTr(id){
	var tbl=$("#"+id);
	var index=tbl.find("input[name='deltrIndex']").val();
	tbl.find("tr:eq("+index+")").remove();
}
function selTblTr(obj,id){
	var index=$(obj).index();
	$("#"+id).find("input[name='deltrIndex']").val(index);
}
//费用名称查询
function otherCostInfoChoose(obj){
//	$("#medicalTbl").html("");
	var otherexpenseName =$(obj).val();
	if(otherexpenseName==null||otherexpenseName==""){
		return;
	}
	   $.ajax({
	        type: "POST",  
	        url: "/claimserver/dataenter/rcptOtherCostInfoSearchByName.do",  
	        data:{otherexpenseName:otherexpenseName},
			error :function(){
				alert("费用名称查询失败");
			},
	        success: function(data){
	        	var items = data.clmTreatment; 
	        	$("#u-tip").html("");
	        	if(items.length>0){
	        		for ( var i = 0; i < items.length; i++) {
	        			$("#u-tip").append("<li onclick=\"fillInOthVal(this,"+items[i].objId+")\" onmouseover=\"changbackclrFn(this)\" >"+items[i].dtChName+"</li>");
	        		}
	        	}
	        }  
	    }); 
}
function bundleOthTip(obj){
	var aa = $("#x888z");
	aa.removeAttr("id");
	var bb=$("#x889z");
	bb.removeAttr("id");
	var cc=$("#x890z");
	cc.removeAttr("id");
	var dd=$("#x891z");
	dd.removeAttr("id");
	var ee=$("#x892z");
	ee.removeAttr("id");
	var ff=$("#x893z");
	ff.removeAttr("id");
	//项目代码
	var gg=$("#x894z");
	gg.removeAttr("id");
//	$(obj).parent().parent().find("input").eq(0).attr("id","x888z");
//	$(obj).parent().parent().find("span").eq(0).attr("id","x889z");
	//医保编码
	$(obj).parent().parent().find("input").eq(1).attr("id","x890z");
	//项目代码
	$(obj).parent().parent().find("select").eq(0).attr("id","x894z");
	
	$("#u-tip-div").remove();
	var ct = document.getElementById("current-tip");
	$(ct).removeAttr("id");
	
	$(obj).attr("id","current-tip");
	$(obj).parent.parent;
	var html = "<div class=\"sel_list\" id=\"u-tip-div\" style=\"display:block; margin:0 0 0 53px; width:164px; padding:0;\"><ul id=\"u-tip\" ></ul></div>";
	$(obj).parent("td:eq(0)").append(html);
	
}
function fillInOthVal(obj,objId){
	hmGjypbm=objId;
	$("#current-tip").val($(obj).html());
	$("#u-tip-div").remove();
	  $.ajax({
	        type: "POST",  
	        url: "/claimserver/dataenter/rcptOtherCostInfoSearchById.do",  
	        data:{objId:objId},
	        success: function(data){
//	        	$("#x888z").val(data.clmTreatment.dtChName);
//	        	$("#x889z").html(data.clmTreatment.dtItemName);
	        	$("#x890z").val(data.clmTreatment.dtGbId);
	        	//项目代码
	        	$("#x894z").val(data.clmTreatment.dtItemCode);
	        	}
	        	
	    }); 
}