$(function(){

	var ordnum = getURLParameter('ordnum');

	fellFastOrder = function(){
		var _path=buildHTTP("/fastord/fetch.shtml?ordId="+ordnum);
		$.ajax({
			url: _path, 
			type: "get", 
			dataType: "json",
			contentType:"application/json",
			success: function(data) { 
				if(data.code=='01'){
					var fastord = data.content;
					$("#ordstatus").val(fastord.ordStatus);
					$("#ordId").val(fastord.ordId);

					$("#_ordnum").html("订单号："+fastord.ordId);
					$("#sum_pay").html("总价：<lable class='red-span'>"+fastord.sum+"&yen;</lable>");

					var vhtml = template("addrHtml",fastord);
					$("#_addr").html(vhtml);

					var phtml = template("prodHtml",fastord);
					$("#_prod").html(phtml);

				}else{
					$.toast("订单查询失败");
				}
			}
		});
	};

	fellFastOrder();

	var user = fetchUSER();

	$("#delBtn").click(function(){ // 确认删除当前订单
		var status = $("#ordstatus").val();
		if(status=='UnPay'){
			var params = {hostId:user.hostId,ordId:ordnum};
			$.confirm("确认删除当前订单？", function(){
				var _path=buildHTTP("/fastord/remove.shtml");
				$.ajax({
					url: _path, 
					type: "post", 
					dataType: "json",
					data:$.toJSON(params),   
					contentType:"application/json",
					success: function(data) { 
						if(data.code=='01'){
							$.confirm("删除成功，返回订单列表？", function(){
								window.location.href = HTTP_PREFIX+"/protal/fastord.html";
							});
						}else{
							$.toast(data.msg);
						}
					}
				});
			});
		}else{
			$.toast("交易中 暂不支持删除");
		}

	});

	/**调用微信JS API 支付
	"appId" : "wx2421b1c4370ec43b",     //公众号名称，由商户传入     
    "timeStamp":" 1395712654",         //时间戳，自1970年以来的秒数     
    "nonceStr" : "e61463f8efa94090b1f366cccfbbb444", //随机串     
    "package" : "prepay_id=u802345jgfjsdfgsdg888",     
    "signType" : "MD5",         //微信签名方式:     
    "paySign" : "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名 
	 **/
	function jsApiCall(json)
	{	 
		var params ={
				appId:json.appid,
				timeStamp:json.timestamp,
				nonceStr:json.noncestr,
				package:"prepay_id="+json.prepayid,
				signType:"MD5",
				paySign:json.sign
		}
		WeixinJSBridge.invoke(
				'getBrandWCPayRequest',
				params,
				function(res){
					WeixinJSBridge.log(res.err_msg);
				}
		);
	}

	function callpay(json)
	{
		if (typeof WeixinJSBridge == "undefined"){
			if( document.addEventListener ){
				document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
			}else if (document.attachEvent){
				document.attachEvent('WeixinJSBridgeReady', jsApiCall); 
				document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
			}
		}else{
			jsApiCall(json);
		}
	}


	$("#payBtn").click(function(){ // 立即支付
		var payType = $("input[name='payType']:checked ").val();
		console.log(payType);

		var ordId =$("#ordId").val();
		if(ordId==''){
			return;
		}
		$.confirm("确认支付当前订单？", function(){
			var _path=HTTP_PREFIX+"/pay/fastpay.shtml?ordId="+ordId+"&payType="+payType+"&scene=2";
			$.ajax({
				url: _path, 
				type: "get", 
				dataType: "json",
				contentType:"application/json",
				success: function(data) { 
					if(data.code=='01'){
						if(payType=='1'){ // 支付宝 alipaysubmit
							$("#pay_result").html(data.content);
						}else{ // 微信
							var wxjson = data.content;
							if(wxjson.build=='1'){
								callpay(wxjson);
							}
						}

					}else{
						$.toast(data.msg);
					}
				}
			});
		});
	});

	$.init();

});