
var token = '';
var path = '';
var qs = parseQueryString(window.location.href);
var cityList = [];
if (qs.token) {
    token = qs.token;
}
if(qs.path){
	path = qs.path;
}

//自动唤起键盘
$(".search input").trigger("click").focus()

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


function deleteSearch () {
	$(".search input").val("")
	$(".container ul").html("")
}

/**
 * [getLoginInfo 获取城市登录信息]
 */
function getLoginInfo() {
    var regionCode = $('.city-name').attr('data-code');
    $.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 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 != ""){
				$(".container ul").html(html)
			}else{
				html += `<li class="searchActive">无结果</li>`;
				$(".container ul").html(html)
			}
		}else{
			$(".search em").css("display","none")
			$(".container ul").html(html)
		}
	}) 
	$(".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(path == "choose_city02"){
			      		var choose_city02_data = {
							name: city,
							code: code
						}
					    window.webkit.messageHandlers.CityLocationMessageHandler.postMessage(choose_city02_data);
			      }else{
			      	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()
})
