var contextPath="";
var visitPrefix="";
var currentPage=1;
$(document).ready(function(){
	contextPath = $("#contextPath").val();
	var userId = $("#userId").val();
	
	//默认加载
	query('0',userId);
	visitPrefix=$("#visitPrefix").val();
    refresher.init({
        id:"wrapper",
        pullDownAction:Refresh,
        pullUpAction:Load               
    });
   
// 下拉刷新                                                   
function Refresh() {
    setTimeout(function () {    // <-- Simulate network congestion, remove setTimeout from production!
    	currentPage=1;
    	query('1',userId);
        
        document.getElementById("wrapper").querySelector(".pullDownIcon").style.display="none";     
        document.getElementById("wrapper").querySelector(".pullDownLabel").innerHTML="<img class='refresh-img' src='"+contextPath+"/plugin/refresher/refresher_images/ok.png'/>刷新成功";
        setTimeout(function () {
        	wrapper.refresh();
            document.getElementById("wrapper").querySelector(".pullDownLabel").innerHTML="";                                
            },1000);
    }, 1000);
}
// 上拉加载
function Load() {
    setTimeout(function () {
        query('2',userId);
//        wrapper.refresh();
    },2000);    
}

//加载押金子项
$("#ulcontainer").on("click", ".orderpay_net", function(event){
	var _this = $(this);
	event.preventDefault();
	_this.siblings(".relist-block_net").slideToggle();
	//点击添加一个class
	_this.toggleClass("imgsel_nets");
	var id = _this.attr("deposit_id");
		//加载企业大厦数据 
		$.ajax({
			type:'POST',
			url:contextPath+'/wechat/signContract/findSignBasics',
			data:{'signContractId':id},
			dataType: 'json',//返回值类型 一般设置为json
			success: function (data)  //服务器成功响应处理函数
			{
				var subs = data.list;
				var html="";
				if(null != subs){
					for(var i=0;i<subs.length;i++){
						html+="<ul><li class='item-content'><div class='item-inner'>"
							+"<div class='item-title'>"+subs[i].name+":"+subs[i].number+"</div>"
							+"<div class='item-after'>￥"+subs[i].totalAmount+"元</div></div></li></ul>"
					}
				
				}
				$("#deposit").html(html);
			}
		});
	});
});

/**
 * @param actType   操作类型，默认0，下拉刷新1，上拉加载2
 * @param userId    用户ID
 */
function query(actType,userId){

	var userId = $("#userId").val();
	$.ajax({
		url :contextPath+"/wechat/signContract/contractList",
		type : "POST",
		data : {"userId":userId,"status":2,"pageNo":currentPage},
		dataType:"json",
		success : function(data) {
			var ulcontainer=$('#ulcontainer');
			if(data.page == null || data.page == ""){
				return;
			}
			if("1"==actType){
				ulcontainer.html("");
			}
			var items=data.page.pageList;
			if(items.length>0){
				currentPage=currentPage+1;
			}
			var strHTML="";
			if(items.length <= 0 && actType != 2){
				$('#wrapper').show();
				$('.nothing').remove();
				$('.pullUp').hide();
				strHTML ="<div class='nothing' style='height: 26rem; background: #fff;text-align:center;'>"
					+"<img src="+contextPath+"/images/nothingapply.png alt='' /><br>"
					+"<span>暂无数据</span></div>";
				$('.home-lost').after(strHTML);
//				ulcontainer.append(strHTML);
				return;
			}
			$('#wrapper').show();
			$('.nothing').remove();
			$('.pullUp').show();
			$.each(items,function(index,item){
				var id=item.id;
				var productSign = "";
				var virtualArchitectureId=item.virtualarchitectureId;
				var organizationId=item.organizationId;
				var serviceName=item.serviceName;
				var serviceIcon=item.serviceIcon;
				var effectiveTime=item.effectiveTime;
				var status=item.status;
				var price=item.price;
				var paymentMethod=item.paymentMethod;
				var startTime=item.startTime;
				var endTime=item.endTime;
				var isRenewalContract=item.isRenewalContract;
				var payment = "";
				if(paymentMethod == '0'){
					payment = "现付";
				}else{
					payment = "月结";
				}
				
				//查询意向商品信息
				$.ajax({
					url :contextPath+"/wechat/signContract/findProductSign",
					type : "POST",
					data : {"signContractId":id},
					dataType:"json",
					async: false,
					success : function(data) {
						var subs = data.list;
						for(var i=0;i<subs.length;i++){
							productSign+="<div class='sevicetime'><span>"+subs[i].name+"</span>"
							+"<div class='timeinputs'>￥"+subs[i].price+"元</div></div>";
						}
						strHTML	+="<div class='sevicecontent suspension_div'><h3 class='sevicecontit'>"
						   	+"<i><img src="+serviceIcon+"alt='' /></i>"
						   	+"<span>"+serviceName+"</span>";
							if(isRenewalContract == '0'){
								strHTML +="<a type='button' class='external' value='续约' onclick='contract("+id+","+organizationId+","+virtualArchitectureId+")'>续约</a>";
							}else{
								strHTML +="<input type='button' value='已续约'>"
							}
							strHTML +="<span class='sevicecontit_span'>"+effectiveTime+"</span></h3>"
						   	+"<div class='orderpay'><span>支付方式</span>"
						   	+"<span class='label-checkbox orderpayradio'>"+payment+"</span></div>"
						   	+"<div class='sevicetime'><span>服务期限</span>"
						   	+"<div class='timeinputs'>"+startTime+"至"+endTime+"</div></div>"
						   	+productSign
							+"<div class='orderpay orderpay_net' deposit_id='"+id+"'><span>押金子项</span>"
						   	+"<img src="+contextPath+"/images/common/xiangxia.png alt='' class='orderpay_left'>"
							+"<img src="+contextPath+"/images/common/xiangshang.png alt='' class='orderpay_right'></div>"
							+"<div class='list-block relist-block relist-block_net relist-block_ntot' id='deposit'></div>";
						if(status == '2'){
								strHTML+="<div class='suspension_view'><img src="+contextPath+"/images/common/shixiao.png alt=''></div>";
							}
							strHTML+="</div>";
					}
				});
					
			});
			ulcontainer.append(strHTML);
			wrapper.refresh();
			
		},
		error : function(data) {
			// alert("系统繁忙，请稍后再试.");
		}
	});

}
//续约
function contract(id,organizationId,virtualArchitectureId){
	window.location.href=contextPath+"/wechat/signContract/toOrderSeviceselectitem?renewalContractId="+id+"&organizationId="+organizationId+"&virtualArchitectureId="+virtualArchitectureId;
}
