
var token = '';
var qs = parseQueryString(window.location.href);
var cityList = [];
if (qs.token) {
    token = qs.token;
}
console.log(token)
//自动唤起键盘
$("#search input").trigger("click").focus()

//返回上一页
$("nav #back").click(function () {
	window.history.go(-1)
})


function deleteSearch () {
	$("#search input").val("")
	$("#city_02 .container ul").html("");
	$("#search").addClass("search");
	$("#search").removeClass("search_02");
	$(".searchBox").css({
	    'padding-bottom': '0',
		'background': '#f3f3f3'
	})
	$("#city_02").css("display",'none')
	$("#city_01").css("display",'block')
}

/**
 * [getLoginInfo 获取城市登录信息]
 */
function getLoginInfo() {
    $.ajax({
        type: "GET",
        url: BASEURL + '/city_list?type=fund&sort=true&token=' + token + '&time=' + Math.round(Date.now() / 1000),
        contentType: 'application/json;charset=utf-8',
        success: function(data) {
        	cityList = data;
            return renderLoginInfo(cityList);
        },
        error: function(err) {
            return showMsg('获取城市信息失败:' + JSON.parse(err.responseText).error);
        }
    });
}
/**
 * [renderLoginInfo 渲染城市登录信息]
 * @param  {[type]} cityInfo [城市登录信息]
 */
function renderLoginInfo(cityInfo) {
	var bind_name = 'input';
	if (navigator.userAgent.indexOf("MSIE") != -1){
	    bind_name = 'propertychange';
	}
	$("#search input").bind(bind_name, function(){
		var searchName = $("#search input").val().trim();
		var html = '';
		if(searchName != ""){
			$("#search").removeClass("search");
			$("#search").addClass("search_02");
			$(".searchBox").css({
			    'padding-bottom': '0.2rem',
    			'background': '#ffffff'
			})
			$("#city_02").css("display",'block')
			$("#city_01").css("display",'none')
			$("#search em").css("display","block")
			for(key in cityInfo){
				if(key != "hot"){
					cityInfo[key].forEach(function (item,index) {
						if(item.region.indexOf(searchName) != -1){
							html +=  `<li data-code=${item.regionCode}>${item.region}</li>`
						}else if(item.pinyin.indexOf(searchName) != -1){
							html +=  `<li data-code=${item.regionCode}>${item.region}</li>`
						}
					})
				}
			}
			if(html != ""){
				$("#city_02 .container ul").html(html)
			}else{
				html += `<li class="searchActive">无结果</li>`;
				$("#city_02 .container ul").html(html)
			}
		}else{
			$("#search").addClass("search");
			$("#search").removeClass("search_02");
			$(".searchBox").css({
			    'padding-bottom': '0',
    			'background': '#f3f3f3'
			})
			$("#city_02").css("display",'none')
			$("#city_01").css("display",'block')
			$("#search em").css("display","none")
			$("#city_02 .container ul").html(html)
		}
	}) 
	$("#city_02 .container ul").on("click", function(e) {
	    var target = e.target;
	    if ($(target).is("li")) {
	    	if($(target).attr("class") != "searchActive"){
			      var city = $(target).text().trim();
			      var code = $(target).attr('data-code');
			      if (document.referrer.indexOf('&name=') === -1) {
			          window.location.href = './fund_h5_api.html?token=' + token + '&name=' + city + '&code=' + code;
			      } else {
			          window.location.href = document.referrer.replace(/&name=[^&]+&code=[^&]+/, '&name=' + city + '&code=' + code);
			      }
	    		
	    	}
	    }
	});
}
/**
 * [showMsg 提示各种错误信息,3s后消失]
 */
function showMsg(msg) {
    var msgBox = $('.alert-info');
    msgBox.children('p').text(msg);
    msgBox.show();

    setTimeout(function() {
        msgBox.hide();
    }, 3000);
}
$().ready(function () {
	getLoginInfo()
})
