
var b15Type = 1;
$(document).ready(function() {
	var b618 = 	{actionCode:'B618',orderId:orderId,step:4,token:token};
	postData(baseUrl + '/B/android', JSON.stringify(b618), b618Succ, postDataDefaultErrorCallback);
	$("#confirm").click(function(){
		b15Type = 2;
		var b615 = 	{actionCode:'B615',orderId:orderId,type:b15Type, token:token};
		postData(baseUrl + '/B/android', JSON.stringify(b615), b015Succ, postDataDefaultErrorCallback);
		
	});	
	$("#evaluate").click(function(){
		window.location.href=baseUrl + "/web/cd/toEvaluate?orderId=" + orderId;
	});
	
	$(".btn-viewDeliv").click(function(){		
		//var msg = "物流信息\n物流公司：" + $(this).attr("lgName") + "\n" + "物流单号：" + $(this).attr("lgNum");
		viewDeli(this);
	});
		
	$('.btn-pay-red').bind('click', b612);	
});

function b612(){
	$('.btn-pay-red').unbind('click', b612);	
	var b612 = 	{actionCode:'B612', orderId:orderId, payType:1, channel:"JS", openid:openid, token:token};
	postData(baseUrl + '/B/android', JSON.stringify(b612), b612Succ, postDataDefaultErrorCallback);
}


function b612Succ(data) {
	if(data){
		if(data.success){
			if (typeof WeixinJSBridge == "undefined") {
				if (document.addEventListener) {
					document.addEventListener('WeixinJSBridgeReady',onBridgeReadyB611, false);
				} else if (document.attachEvent) {
					document.attachEvent('WeixinJSBridgeReady', onBridgeReadyB611);
					document.attachEvent('onWeixinJSBridgeReady', onBridgeReadyB611);
				}
			} else {
				var payInfo = JSON.parse(data.payInfo);
				onBridgeReadyB611(payInfo);
			}
		}else {
			showMsg(data.message);
		}
	}else{
		showMsg("网络异常！");
	}	
	$('.btn-pay-red').bind('click', b612);
}

function onBridgeReadyB611(payInfo) {
	WeixinJSBridge.invoke('getBrandWCPayRequest', {
		"appId" : payInfo.appId, //公众号名称，由商户传入     
		"timeStamp" : payInfo.timeStamp, //时间戳，自1970年以来的秒数     
		"nonceStr" : payInfo.nonceStr, //随机串     
		"package" : "prepay_id=" + payInfo.prepay_id,
		"signType" : payInfo.signType,
		"paySign" : payInfo.paySign
	//微信签名 
	}, function(res) {
		var orderId = sessionStorage.getItem('orderId');
		var price = sessionStorage.getItem('orderId');
		if (res.err_msg == "get_brand_wcpay_request:ok") {					
			//支付成功
			window.location.reload();
		} else if (res.err_msg == "get_brand_wcpay_request:cancel") {//cancel 用户取消
			//window.location.href=baseUrl + "/web/cd/toLawyerPay?orderId="+orderId+ "&price=" + price;				
		} else if (res.err_msg == "get_brand_wcpay_request:fail") {//支付失败
			//window.location.href=baseUrl + "/web/cd/toLawyerPay?orderId="+orderId+ "&price=" + price;
		}
	});
}

function b618Succ(data){
	if(data){
		if(data.success){
			
			$("#lawyerAddress").text(data.lawyerAddress);
			$("#lawyerLogo").attr("src",data.lawyerLogo).removeClass("hide");
			$("#lawyerName").text(data.lawyerName);
			$("#lawyerTel").text(data.lawyerTel);
			$("#atel").attr("href","tel:" +data.lawyerTel);
			
			$(".search-top").attr("href", baseUrl+'/web/cd/toOrderDetail?orderId='+data.orderId+'&step=0');
			if(data.lgsInputDate=='未填写快递信息'){
				if(data.orderState==3){//待查收状态
					$("#openLg").parent().removeClass("hide");
					$("#stateDesc").text('请快递纸质委托查档材料');				
				}
			}else{
				$("#stateDesc").text(data.stateDesc);
			}
			$("#lgsInputDate").text(data.lgsInputDate);					
				
			if(data.workingDate!=''){
				$("#workingDate").text(data.workingDate);		
			}else{
				$("#workingDate").parent().remove();
			}
			if(data.sendbackDate!=''){
				$("#sendbackDate").text(data.sendbackDate);		
			}else{
				$("#sendbackDate").parent().remove();
			}
			if(data.finishDate!=''){
				$("#finishDate").text(data.finishDate);		
			}else{
				$("#finishDate").parent().remove();
			}
			if(data.receiveDate!=''){
				$("#receiveDate").text(data.receiveDate);
			}else{
				$("#receiveDate").parent().remove();
			}			
			$("#serviceRecord").removeClass("hide");						
			$("#stateName").html(data.stateName);
			//判断按钮显示
			if(data.orderState==6){
				if(data.commentFlag==0){//评论					
					$("#eval").parent().removeClass("hide");	
				}else if(data.commentFlag==-1){//确认					
					$("#confirm").parent().removeClass("hide");	
				}
			}
			
			var isViewImg = 0;
			//处理打印费
			if(data.orderState==5){
				if(data.orderType==2){
					if(data.attachIs && data.attachIs.length>0){
						$("#attchId").attr("src",data.attachIs[0].pathSL).parent().attr("data-image",data.attachIs[0].path);	
						$("#printView").removeClass("hide")
						isViewImg=1;
						$("#priceP").text( data.priceP + "元");
						if(data.flag==1){
							//显示支付按钮
							$(".btn-pay-red").css("display", "block");	
							$("#stateDesc").text('待支付档案打印费');
						}
					}
				}
			}
			
			//处理查档结果
			var html = "";
			for(var i=0; i<data.attachRs.length; i++){
				html += '<li class="mar-l-20 mar-b-25 pull-left view-img" data-image="'+data.attachRs[i].path+'">';
				html += '	<img src="'+data.attachRs[i].pathSL+'">';
				html += '</li>';
			}
			if(html!=""){
				$("#attachRs").html(html).parent().parent().removeClass('hide');				
			}
			if(html!="" || isViewImg==1){
				initViewImg($);//初始化图片弹出效果		
			}
			
			$(".btn-viewDeliv").attr("lgName",data.lgName);
			$(".btn-viewDeliv").attr("lgNum",data.lgNum);
		}
	}
}

function b015(){
	var lgName = $("input[name=lgName]").val();
	var lgNum = $("input[name=lgNum]").val();
	if(lgName==null || lgNum==""){
		showMsg("请填写快递公司");
		return;
	}
	if(lgNum==null || lgNum==""){
		showMsg("请填写快递单号");
		return;
	}
	b15Type = 1;
	var b615 = 	{actionCode:'B615',orderId:orderId,lgName:lgName, lgNum:lgNum, type:b15Type, token:token};
	postData(baseUrl + '/B/android', JSON.stringify(b615), b015Succ, postDataDefaultErrorCallback);
}

function b015Succ(data){
	if(data){
		if(data.success){
			if(b15Type==1){
				showMsg('已填写快递信息');				
			}
			window.location.reload();
		}else{
			showMsg(data.message);
		}
	}else{
		showMsg("网络异常");
	}
}

var alertflag=0;
var af=[];
window.viewDeli = function(obj,callback){
	var alertbg='<div class="alert-layout ub ub-ac ub-pc fs-14"></div>'
	var htmlStr = '<div class="alert-wrapper bg-white br-radius5 pad-tb-30 pad-lr-20" id="alertFram'+alertflag+'">';
	htmlStr += '<div class="alert-content text-center fs-16">';
	htmlStr += '<p class="mar-b-5 fs-16">查看物流信息</p>';
	htmlStr += '<p class="mar-b-5 fs-14">物流公司：'+$(obj).attr("lgName")+"</p>";
	htmlStr += '<p class="mar-b-5 fs-14">物流单号：'+$(obj).attr("lgNum")+"<p>";
	htmlStr += '</div><div class="btn bg-blue fc-white mar-lr-auto alert-confirm-btn">确定</div></div>';
	if($('.alert-layout').get(0)){
	    $('.alert-layout').append(htmlStr);
	}
	else{
		$('body').append(alertbg).css('overflowY','hidden');
		$('.alert-layout').append(htmlStr);
	}
	af.unshift(alertflag);     
	alertflag+=1;
	$('.alert-confirm-btn').one('click', function(event) {
		if(callback&&typeof(callback)=='function'){
		    callback();
		}
		$('#alertFram'+af[0]).remove();
		af.shift();
		if(af.length==0){
		    $('.alert-layout').remove();
		    $('body').css('overflowY','auto');
		}
	});
};	
