
$(document).ready(function() {

	initCache();
	initCity();
	initZj();//初始化最近访问
	
	//搜索
	$('.btn-search-right').click(function(){		
		var _this = $(this);
		var val = _this.prev().val();
		if (val==''||val=='underfined') {
			//myAlert('请输入搜索内容');
			toast1.alert('请输入搜索内容');
			return false;
		}
		if(!_this.attr('data-disabled')){
			_this.attr('data-disabled','true');
			var tpl = '';			
			$('.search-city-wrapper').removeClass('hide');
			for(var obj in  searchCityObj){
				for(var i=0; i<searchCityObj[obj].length; i++){	
					if(searchCityObj[obj][i].indexOf(val)>=0){
						tpl += '<li class="pad-l-25 pos-relate br-bottom1" ><a href="#">'+searchCityObj[obj][i]+'</a></li>';						
					}
				}
			}
			$('.search-city-list').html(tpl);
			$('.select-area-layout').css('overflow','hidden');
			$('ul.search-city-list li').click(function(){
				loadCounty($(this).text());
			});
			_this.removeAttr('data-disabled');
		}
	});
	
	$('.area-back').click(function() {
		$('.select-area-layout').addClass('hide');
		$('.select-area-layout').css('overflow','auto');
		$('.search-city-list').children('li').remove();
		$('.area-search-form input').val('');
		$('.search-city-wrapper').addClass('hide');
		$('.search-page').removeClass('hide');
	});

	$('.page').on('click', '.search-city-list li', function() {
		$('.search-city-wrapper').addClass('hide');
		var txt = $(this).text();
		$('.select-court-layout').removeClass('hide').addClass('pageIn');
		$('.current-city').text(txt);
	});
	var alertflag=0;
	var af=[];
	window.myAlert = function(ctx,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 += ctx;
		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');
			}
		});
	};	
	postData(baseUrl + '/B/android', JSON.stringify({actionCode:'B617',token:token}), b617Succ, postDataDefaultErrorCallback);
})

function b617Succ(data){
	if(data){
		if(data.success && data.orderId && data.orderId!=''){
			var url = baseUrl+'/web/cd/toPlatConfirm?orderId='+data.orderId;
			if(0==data.orderState ){		//'待确认'
				url = baseUrl+'/web/cd/toPlatConfirm?orderId='+data.orderId;
			}else if(2==data.orderState){	//'待付款'
				url = baseUrl+'/web/cd/toLawyerPay?orderId='+data.orderId;
			}else if(3==data.orderState ){	//'待查收'
				url = baseUrl+'/web/cd/toCdIng?orderId='+data.orderId;
			}else if(4==data.orderState){	//'查档中'
				url = baseUrl+'/web/cd/toCdIng?orderId='+data.orderId;
			}else if( 5==data.orderState ){	//'待配送'
				url = baseUrl+'/web/cd/toCdIng?orderId='+data.orderId;
			}else if( 6==data.orderState ){	//'已配送'
				url = baseUrl+'/web/cd/toCdIng?orderId='+data.orderId;
			}			
			$("#b617Div").removeClass("hide").attr("href", url);
			$("#time").text(data.time);
			$("#orderStateName").text(data.orderStateName);
			$("#typeName").text(getOrderTypeName(data.orderType));
		}
	}
}

function initZj(){
	var str = localStorage.getItem('areaZJ');
	var areaZJ = JSON.parse(str==''?'[]':str);
	if(!(areaZJ instanceof Array) || areaZJ==null || areaZJ==''){
		areaZJ = new Array();
		localStorage.setItem('areaZJ', JSON.stringify(areaZJ));
	}else{
		var html = "";
		for(var i=areaZJ.length-1; i>=0; i--){
			html += '<li class="br pull-left">'+areaZJ[i]+'</li>';
		}
		$("#areaZJ").html(html); 
	}
}

function initCache(){	
	var addr = sessionStorage.getItem('addr');
	if(addr && addr!='' && addr!=null){
		$("#addr").val(sessionStorage.getItem('addr'));
		$("#selectArea").text($("#addr").val());
	}
	var countyId = sessionStorage.getItem('countyId');
	if(countyId && countyId!='' && countyId!=null){
		$("#countyId").val(sessionStorage.getItem('countyId'));		
	}
	var cdType = sessionStorage.getItem('cdType');
	if(cdType && cdType!='' && cdType!=null){
		$("#selectType").text(sessionStorage.getItem('cdType'));
		$("#cdType").val(sessionStorage.getItem('cdType'));	
	}
}

/**
 * 初始化城市
 */
function initCity(){
	var areaSelectHtml = "";
	var areaHtml = "";
	for(var obj in  searchCityObj){
		areaSelectHtml+= "<li><a href='#area-select-"+obj+"'>"+obj.toUpperCase()+"</a></li>";
		areaHtml += "<p class='pad-tb-15 pad-l-25 fs-12 fc-grey bg-lgreen pos-relate br-top1 br-bottom1 bold' id='area-select-"+obj+"'>";
		areaHtml += obj.toUpperCase() + "</p>";
		areaHtml += "<ul class='fs-14 list-bottom area-city-list'>";
		for(var i=0; i<searchCityObj[obj].length; i++){
			areaHtml += "<li class='pad-l-25 pad-r-50 pos-relate br-bottom1'><p>"+searchCityObj[obj][i]+"</p></li>";
		}
		areaHtml += "</ul>";		
	}	
	$(".area-search-list").find("li").after(areaSelectHtml);	
	$(".area-recent-list").after(areaHtml);
}

//市区
function loadCounty(txt){
	var str = localStorage.getItem('areaZJ');
	var areaZJ = JSON.parse(str==''?'[]':str);	
	if(areaZJ instanceof Array){
		areaZJ.deleta(txt);
		areaZJ.push(txt);
		if(areaZJ.length>6){
			areaZJ.shift();
		}
	}else{
		areaZJ = new Array();
	}
	localStorage.setItem('areaZJ', JSON.stringify(areaZJ));
	
	var url = baseUrl + '/B/android';
	var parameters = {'actionCode':'B602','name':txt, leval:2};
	postData(url, JSON.stringify(parameters), b602Success, postDataDefaultErrorCallback);
}


/**
 * 加载县区成功，生成html页面
 * @param data
 */
function b602Success(data){
	var counttryHtml = "";
	if( $("#cdType").val()=='户籍信息'){		
		for(var i=0; i<data.areas.length; i++){
			counttryHtml+= '<li class="pos-relate">';
			counttryHtml+= '<p class="pad-lr-25 fs-16 ub ub-ac ub-pj br-b pos-relate">';
			counttryHtml+= '<span class="zone">'+data.areas[i].name+'</span>';
			counttryHtml+= '<span class="icon icon-down-blue2"></span>';
			counttryHtml+= '</p>';
			counttryHtml+= '<ul class="pad-l-40 list-bottom list-bottom-width100 court-list-town hide" county='+data.areas[i].name+'>';
			counttryHtml+= '</ul>';
			counttryHtml+= '</li>';
		}
		$(".court-list").html(counttryHtml);
		$('.court-list p').click(function(){
			courtListP(this);
		});
	}else {
		for(var i=0; i<data.areas.length; i++){
			counttryHtml+= '<li class="pos-relate" code='+data.areas[i].code+'><p class="pad-lr-25 fs-16 ub ub-ac ub-pj br-b pos-relate"><span class="zone">'+data.areas[i].name+'</span></p></li>';
		}
		$(".court-list").html(counttryHtml);
		$('.court-list li').click(function(){
			var _this = $(this);
			var txt = $(this).text();
			addr[0] = $('.current-city').text();
			addr[1] = _this.parent().prev().find('.zone').text();
			addr[2] = txt;
			var addr2 = addr.join('');
			$('.search-area input').val(addr2);
			$('.search-area .search-option').text(addr2);	
			$("#countyId").val(_this.attr("code"));
			setTimeout(function(){
				$('.select-court-layout,.select-area-layout').removeClass('pageIn').addClass('hide');
			},300);	
			
			//设置缓存 
			sessionStorage.setItem('addr', $("[name=addr]").val());
			sessionStorage.setItem('countyId', $("[name=countyId]").val());		
		});
	}
}

function loadTownSuccess(data){	
	var html = "";
	for(var i=0; i<data.areas.length; i++){
		html+= '<li class="pos-relate br-b" code='+data.areas[i].code+'>'+data.areas[i].name+'</li>';
	}
	$("ul[county='"+county+"']").html(html);	
	$('.court-list .court-list-town li').click(function(event) {
		courtListTownLi(this);
	});
}

function courtListP(obj){
	var _this = $(obj);
	var _thispar = _this.parent();
	if (!_thispar.hasClass('active')) {
		_thispar.addClass('active').siblings().removeClass('active');
		_thispar.addClass('br-b');
		_thispar.siblings().removeClass('br-b');
		_thispar.siblings().find('.court-list-town').slideUp().addClass('hide');
		_thispar.siblings().find('p .icon').removeClass('icon-top-blue2').addClass('icon-down-blue2');
		_this.find('.icon').removeClass('icon-down-blue2').addClass('icon-top-blue2');
		_this.next().slideDown(300).removeClass('hide');

	}else{
		_thispar.removeClass('active');
		_thispar.removeClass('br-b');
		_this.find('.icon').removeClass('icon-top-blue2').addClass('icon-down-blue2');
		_this.next().slideUp(300).addClass('hide');
	}
	county = _this.text();
	postData(baseUrl + '/B/android', JSON.stringify({'actionCode':'B602','name':county}), loadTownSuccess, postDataDefaultErrorCallback);
}

var addr = [];
var county = '';
function courtListTownLi(obj){
	var _this = $(obj);
	var txt = $(obj).text();	
	addr[0] = $('.current-city').text();
	addr[1] = _this.parent().prev().find('.zone').text();
	addr[2] = txt;
	var addr2 = addr.join('');
	$('.search-area input').val(addr2);
	$('.search-area .search-option').text(addr2);
	$("#countyId").val(_this.attr("code"));
	setTimeout(function(){
		$('.select-court-layout,.select-area-layout').removeClass('pageIn').addClass('hide');
	},300);

	//设置缓存 
	sessionStorage.setItem('addr', $("[name=addr]").val());
	sessionStorage.setItem('countyId', $("[name=countyId]").val());	
}

function nextStep(){
	var cdTypeName = $("#cdType").val();
	if(cdTypeName=="" || cdTypeName==null){
		showMsg('请选择查档类型!');
		return false;
	}
	if($("#addr").val()=="" || $("#addr").val()==null){
		showMsg('请选择查档地区!');
		return false;
	}
	var b601 = {
		'actionCode':'B601',
		codeId:$("#countyId").val(), 
		type:getCdType(cdTypeName), 
		operateType : 1, //微信
		token: token
	};	
	postData(baseUrl + '/B/android', JSON.stringify(b601), b601Succ, postDataDefaultErrorCallback);
}

function b601Succ(data){
	if(data){
		if(data.success && data.areaPriceId){
			$("#form1").submit();
			return;
		}else{
			showMsg('该区域尚未开通服务');			
		}
	}else{
		showMsg('网络异常');	
	}
}

function getCdType(type){
	if("工商基本信息".indexOf(type)>=0){
		type = 1;
	}else if("工商内档信息".indexOf(type)>=0){
		type = 2;
	}else if("户籍信息".indexOf(type)>=0){
		type = 3;
	}else if("婚姻信息".indexOf(type)>=0){
		type = 4;
	}else if("房产信息".indexOf(type)>=0){
		type = 5;
	}else if("车辆信息".indexOf(type)>=0){
		type = 6;
	}
	return type;
}

var toast1 = new toast({
    pos:'center'
});
