G.app.order.paytype={
	config:{divide:0},_curShippingId:0,_tpl:{desc:'<ul class="list_info"><li><span class="tit">付款方式：</span>{DivideName}</li><li><span class="tit">支付方式：</span>{PayTypeName}{PayTypePic}</li></div>'},//{PayTypeDescChn}
	_mod:{getAll:function(c,d){var b=G.app.order.paytype,a=b._dao;a.get(c,function(){d(a._list[c])},function(e){G.ui.popup.showMsg("获取支付方式失败，请稍候重试")})},get:function(d,c,e){var b=G.app.order.paytype,a=b._dao;a.get(d,function(){e(a._list[d][c])},function(f){G.ui.popup.showMsg("获取支付方式失败，请稍候重试")})}},
	_dao:{
		_list:{},
		get:function(b,d,e){
			if(G.logic.login.ifLogin(this,arguments)===false){return}
			var a=G.app.order.paytype._dao,c=G.logic.login.getLoginUid();
			if(a._list[b]){return d()}
			G.util.post("/purchase/order",{act:"getpaytypeofshipping",shippingtype:b,products:G.app.order.status.get("product").productIds},function(f){
				if(f&&f.errno==0){a._list[b]={};
				var index=0;
				$.each(f.data,function(h,g){a._list[b][g.pay_type]=g;if(index==0){G.app.order.status.setSavedId("paytype",g.pay_type)};index++;});
				$.isFunction(d)&&d()}else{$.isFunction(e)&&e(f)}
			})
		}
	},
	_view:{
		_curId:0,_dom:false,_fromSpecial:false,
		getDom:function(){
			var b=G.app.order.paytype,a=b._view;
			if(!a._dom){
				a._dom=$('<div class="mod_order id_pay"><div class="inner">	<div class="hd">		<h3>支付方式</h3>		<div class="more"><a href="#" onclick="return false" id="paytype_btn">更改</a></div>	</div><div class="bd"></div>');
				a._dom.insertAfter(G.app.order.shippingtype.getDom());a.switchBtnStatus(2)
			}
			return a._dom
		},
		list:function(){
				var d=G.app.order.paytype,a=d._view,c=d._mod,b=G.app.order.status.get("shippingtype");
				c.getAll(b.ShippingId,function(i){
					var g={offline:[],online:{bank:[],platform:[],special:[]}},h=a.getDom();
					$.each(i,function(n,m){if(m.IsNet==1){if(m.pay_type==8){g.online.special.push(m)}if(m.IsNetBank==1){g.online.bank.push(m)}else{g.online.platform.push(m)}}else{g.offline.push(m)}});
					var l=function(m){m=parseInt(m,10);return isNaN(m)?0:m},e=function(n,m){return l(n.OrderNumber)-l(m.OrderNumber)};
					g.online.bank.sort(e);g.online.platform.sort(e);g.offline.sort(e);
					var f=['<table class="table_nobor"><colgroup><col width="5%" /><col width="15%" /><col width="80%" /></colgroup><tbody><tr><td></td><td>付款方式</td><td><input type="hidden" name="divideType" value="1" /><ul class="divideType"><li data-value="1" class="selected">全额支付<i class="icon-check"></i></li><li data-value="2" class="disabled" style="display:none;">分期支付<i class="icon-check"></i></li></ul></td></tr>'];
					f.push('<tr><td></td><td></td><td><input type="hidden" name="installmentType" value="0" /><ul class="installmentType">');
					f.push('<li data-value="3" class="disabled">3期<i class="icon-check"></i></li><li data-value="6" class="disabled">6期<i class="icon-check"></i></li><li data-value="9" class="disabled">9期<i class="icon-check"></i></li><li data-value="12" class="disabled">12期<i class="icon-check"></i></li>');
					f.push('</td></ul></tr>');
					f.push('</tbody></table><table class="table_nobor"><colgroup><col width="5%" /><col width="15%" /><col width="80%" /></colgroup><tbody>');
					$.each(g.offline,function(n,m){f.push('<tr pay_type="'+m.pay_type+'"><td><input hotname="I.ORDER.PAYTYPE.'+m.pay_type+'" type="radio" name="sel_pay" value="'+m.pay_type+'"/></td><td>'+m.PayTypeName+"</td><td><p>"+m.PayTypeDesc+"</p></td></tr>")});
					if(g.online.bank.length>0||g.online.platform.length>0){f.push('<tr pay_type="-1"><td><input type="hidden" name="sel_pay" value="-1"></td><td>支付方式</td><td>');
					var k=function(n,m){f.push('<li><label><input type="radio" name="sel_online_pay" value="'+m.pay_type+'"/><img src="'+m.logo+'" alt="'+m.PayTypeName+'"/></label></li>')};
					if(g.online.bank.length>0){f.push('<h3 class="tit_list_info">网上银行</h3><ul class="list_info" style="display:none">');$.each(g.online.bank,k);f.push("</ul>")}
					if(g.online.platform.length>0){f.push('<h3 class="tit_list_info">支付平台</h3><ul class="list_info" style="display:none">');$.each(g.online.platform,k);f.push("</ul>")}
					f.push("<p>通过网上银行或支付平台在线付款</p></td></tr>")}
					f.push('</tbody><tfoot><tr><td colspan="4"><div class="wrap_btn"><a class="btn_strong" href="#" onclick="return false" t="psave">确认支付方式</a></div></td></tr></tfoot></table>');
					$(".bd",h).empty().html(f.join(""));
					$(".bd ul.installmentType").hide();
					var price = parseFloat(G.app.order.price.get("goodsprice"),true);
					$(".bd ul.installmentType li",h).each(function(){$(this).click(function(){
						if($(this).hasClass('disabled')){return false;}
						var num = $(this).data("value");
						$(this).addClass("selected").siblings().removeClass("selected");$(this).parent("ul.installmentType").prev("input[name=installmentType]").val(num);
						if(price<=4000){var shou=0.3,dai=0.7;}else{var shou=0.5;dai=0.5;}
						var qprice=price/num;
						var interestAmount=d.decimalTwo(price*dai*0.008);
						var divideMoney = d.decimalTwo(price*dai*0.008)+d.decimalTwo(price*dai/num);
						//interestAmount=divideMoney*num-price*dai;
						G.app.order.status.set("DivideNum",num);
						G.app.order.status.set("interestAmount",interestAmount);
						G.app.order.price.set("interestAmount",interestAmount);
						G.app.order.price.set("downPayment",price*shou,true);
						G.app.order.price.set("divideMoney",d.decimalTwo(price*dai*0.008)+d.decimalTwo(price*dai/num),true);
						G.app.order.price.set("totalprice",price*shou+divideMoney*num+parseFloat(G.app.order.price.get('shippingprice')),true);
					});});
					$(".bd ul.divideType li",h).each(function(){$(this).click(function(){
						if($(this).hasClass('disabled')){return false;}
						$(this).addClass("selected").siblings().removeClass("selected");$(this).parent("ul.divideType").prev("input[name=divideType]").val($(this).data("value"));
						G.app.order.status.set("DivideId",$(this).data('value'));G.app.order.status.set("DivideName",$(this).text());
						if($(this).data('value')==1){
							$(".bd ul.installmentType li",h).removeClass('selected');$(".bd ul.installmentType",h).hide();
							G.app.order.status.set("DivideNum",1);
							G.app.order.status.set("interestAmount",0);
							G.app.order.price.set("interestAmount",0);
							G.app.order.price.set("downPayment",0);
							G.app.order.price.set("divideMoney",0);
							G.app.order.price.set("totalprice",price+parseFloat(G.app.order.price.get('shippingprice')),true);
						}else{
							$(".bd ul.installmentType",h).find("li").not(".disabled").eq(0).trigger('click');$(".bd ul.installmentType",h).show();
						}
					});});
					$(".bd input[name=sel_pay], .bd input[name=sel_online_pay]",h).each(function(){$(this).click({pay_type:$(this).val()},function(m){a.select($(this).attr("name"),m.data.pay_type,this)})});
					$(".bd a[t=psave]",h).click(a.save);
					if(i[a._curId]){
						var j=i[a._curId];
						if(a._fromSpecial){
							$(".bd input.special",h).click()
						}else{
							$(".bd input[name="+(j.IsNet?"sel_online_pay":"sel_pay")+"][value="+j.pay_type+"]",h).first().click()
						}
					}else{
						$(".bd input[name=sel_pay]:first").click().length==0&&$(".bd input[name=sel_online_pay]:first").click()
					}
					if(DivideId=G.app.order.status.get("DivideId")){$(".bd ul.divideType li").removeClass('selected');$(".bd ul.divideType").find('li[data-value='+DivideId+']').addClass('selected');}
					if(DivideNum=G.app.order.status.get("DivideNum")){$(".bd ul.installmentType li").removeClass('selected');$(".bd ul.installmentType").find('li[data-value='+DivideNum+']').addClass('selected');$(".bd ul.installmentType").show();}
					$.ajax({type:'POST',url:'/data/config',data:{act:'info',inajax:1,name:'credit'},dataType:'json',success:function(json){
							if(json.success){$(".bd ul.divideType li",h).eq(1).show();}
						}
					});
					if(goodsprice=G.app.order.price.get("goodsprice")){
						if(goodsprice>=600 && goodsprice<=7000){
							if(goodsprice<=800){
								$("ul.installmentType").find("li").eq(0).removeClass('disabled');
							}else if(goodsprice<=1500){
								$("ul.installmentType").find("li").eq(0).removeClass('disabled');
								$("ul.installmentType").find("li").eq(1).removeClass('disabled');							
							}else if(goodsprice<=2000){
								$("ul.installmentType").find("li").eq(0).removeClass('disabled');
								$("ul.installmentType").find("li").eq(1).removeClass('disabled');							
								$("ul.installmentType").find("li").eq(2).removeClass('disabled');							
							}else{
								$("ul.installmentType").find("li").eq(0).removeClass('disabled');							
								$("ul.installmentType").find("li").eq(1).removeClass('disabled');							
								$("ul.installmentType").find("li").eq(2).removeClass('disabled');							
								$("ul.installmentType").find("li").eq(3).removeClass('disabled');														
							}
						}
						if(goodsprice<600){
							$(".bd ul.divideType li[data-value=2]").attr('title','购物金额大于600元才可选择分期付款');
						}else if(goodsprice>7000){
							$(".bd ul.divideType li[data-value=2]").attr('title','授信额度不足于支付当前购物金额，无法使用分期付款');
						}else{
							$.ajax({type:'POST',url:'/data/user',data:{act:'info',inajax:1},dataType:'json',cache:false,success:function(json){ if(json.success){ if(json.data.creditBalance!=-1 && json.data.creditBalance<goodsprice){
								$(".bd ul.divideType li[data-value=2]").attr('title','购物金额'+goodsprice+'超过当前信用额度余额'+json.data.creditBalance).addClass('disabled');
							}else{
								$(".bd ul.divideType li[data-value=2]").removeClass('disabled');
							} }else{ alert('信用额度查询失败：'+json.message); } }});
						}
					}
					a.switchBtnStatus(2)
				});
		},
		select:function(b,f,g){
						var a=G.app.order.paytype._view,h=false,e=$(g);
						if(b=="sel_online_pay"){h=true}
						a._fromSpecial=false;
						var d=$(".bd tr[pay_type]",a.getDom()).removeClass("status_on").filter("[pay_type="+(h?-1:f)+"]");
						if(f==-1){
							$(".tit_list_info",d).show();$(".list_info",d).show();$("input[name=sel_online_pay]:first",d).click()
						}else{
								if(h){
									$(".bd tr[pay_type=-1] .tit_list_info",a.getDom()).show();
									$(".bd tr[pay_type=-1] .list_info",a.getDom()).show();
									$(".list_info .status_on",d).removeClass("status_on");
									if(e.hasClass("special")){a._fromSpecial=true;e.parents("div.mod_kuai").addClass("status_on")}else{e.parent("label").addClass("status_on")}
									var c=$("input[name=sel_pay]",d);
									if(c.length>0){c[0].checked=true}
								}else{
									$(".bd tr[pay_type=-1] .tit_list_info",a.getDom()).hide();$(".bd tr[pay_type=-1] .list_info",a.getDom()).hide();
									$(".bd tr[pay_type=-1] .list_info li label",a.getDom()).removeClass("status_on").find("input[name=sel_online_pay]").removeAttr("checked");
									var c=d.addClass("status_on").find("input[name=sel_pay]");if(c.length>0){c[0].checked=true}
								}
								a._curId=f
						}
		},
		save:function(){
			var f=G.app.order.paytype,a=f._view,c=f._mod,b=G.app.order.status.get("shippingtype"),e=a._curId,d=$(".bd",a.getDom());
			c.get(b.ShippingId,e,function(g){if(!g){return}G.app.order.status.set("paytype",g);a.desc();G.app.order.status.saveIds();G.app.order.event.allinpay.process()})
		},
		desc:function(){
				var c=G.app.order.paytype,i=c._view,g=c._mod,d=c._tpl,e=G.app.order.status.get("paytype");
				if(e!=null){
					var f=[];if(e.IsNet==1){f.push('<div class="wrap_top"><img title="'+e.PayTypeDesc+'" src="');f.push(e.logo);
					f.push('" alt="'+e.PayTypeName+'"></div>')}
					e.PayTypePic=f.join("");
					//e.PayTypeDescChn=G.app.order._getYTipHtml(e.PayTypeDesc);
					e.DivideId=G.app.order.status.get("DivideId");
					e.DivideName=G.app.order.status.get("DivideName");
					if(e.DivideId==1){G.app.order.status.set("DivideNum",0);}else{
						e.DivideName+=' 期数：'+G.app.order.status.get("DivideNum");
					}
					e.DivideNum=G.app.order.status.get("DivideNum");
					$(".bd",i.getDom()).empty().html(G.ui.template.fillWithTPL(false,e,false,d.desc));
					i.switchBtnStatus(1);G.app.order.price.calcu()
				}else{
					var h=G.app.order.status.get("address"),a=G.app.order.status.get("shippingtype");var b=$(".bd",i.getDom()).empty().html(G.app.order._getYTipHtml("请先选择并确认配送方式")+"<p>&nbsp;</p>");$("a[name=add]",b).click(i.list)}
		},
		switchBtnStatus:function(a){
			var e=G.app.order.paytype,b=e._view,c=parent._mod,d=$("#paytype_btn");
			d.show();
			if(a==1){if(G.app.order.status.get("paytype")){G.app.order.status.get("paytype").uediting=false}d.html("更改").unbind().click(b.list)}else{if(!G.app.order.status.get("paytype")){d.hide()}else{G.app.order.status.get("paytype").uediting=true;d.html("取消").unbind().click(b.desc)}}
		},
		init:function(){
			var e=G.app.order.paytype,b=e._view,d=e._mod,c=G.app.order.status.get("shippingtype"),a=G.app.order.status.get("address");
			if(!c||!a){return}
			G.app.order.status.set("DivideId",1);
			G.app.order.status.set("DivideName",'全额支付');
			G.app.order.status.set("DivideNum",0);
			G.app.order.status.set("interestAmount",0);
			G.app.order.price.set("interestAmount",0);
			G.app.order.price.set("downPayment",0);
			G.app.order.price.set("divideMoney",0);
			
			d.getAll(c.ShippingId,function(f){
				var g=G.app.order.status.getSavedId("paytype")||0;
				if(g!=0){G.app.order.status.delSavedId("paytype")}
				g=g||a.default_pay_type;
				//g=0;
				b.list()
				if((e._curShippingId==0||e._curShippingId==c.ShippingId)&&g>0&&f[g]){b._curId=g;b.save()}else{b.list()}
				//e._curShippingId=c.ShippingId;
			//	G.app.order.event.allinpay.process()
			})
		}
	},
	hide:function(){
		$(".bd",this._view.getDom()).empty().html(G.app.order._getYTipHtml("请先选择并确认配送方式")+"<p>&nbsp;</p>");
		$("#paytype_btn").hide();
		G.app.order.status.set("paytype",null)
	},
	decimalTwo:function(a){
		var b = parseInt(a * 1000);
		if(a*1000>b){b=b+1;}
		var c = b.toString();
		var d = c.length;
		var e;
		if(c[d - 1] >= 1){
			e = parseInt(b / 10) + 1;return (e / 100);
		}else{
			e = parseInt(b / 10);return (e / 100);
		}
	},
	init:function(){this._view.init()}
}