var cityList = [];
$(function () {
    var openId = $("#openId").val();
    var developId = $("#developId").val();
    var contextPath = $("#contextPath").val();
    var organId = $("#organId").val();
    if(!openId){
        openId = sessionStorage.getItem("openId");
    }
    if(!developId){
        developId = sessionStorage.getItem("developId")
    }
    getProjectList(openId, developId);
    //加载城市事件
    $('.container').show();
    //选择城市 start
    $('body').on('click', '.city-list p', function () {
        // alert($(this).html())
    });
    //点击索引查询城市
    $('body').on('click', '.letter a', function () {
        var s = $(this).html();
        $(window).scrollTop($('#' + s + '1').offset().top);
        $("#showLetter span").html(s);
        $("#showLetter").show().delay(500).hide(0);
    });
    //中间的标记显示
    $('body').on('onMouse', '.showLetter span', function () {
        $("#showLetter").show().delay(500).hide(0);
    });
    $("#searchName").bind("input propertychange", function (event) {
      setTimeout(function(){
        // console.log(event.target.value)
        if(event.target.value == ""){
          $(".current-position").show()
        }else{
          $(".current-position").hide()
        }
        getProjectList(openId, developId);
      },100)
    });
    $(".current").on("click",function(){
        var orgId = $(".current .projectName").data("id");
        var name = $(".current .projectName").data("name");
        if(orgId){
            localStorage.setItem('wx' + developId, orgId);
            localStorage.setItem(developId + "orgName", name);
            localStorage.setItem("orgId", orgId);
            sessionStorage.setItem('wx' + developId, orgId);
            sessionStorage.setItem("orgId", orgId);
            sessionStorage.setItem(developId + "orgName", name);
            window.location.href = $("#contextPath").val() + "/wechat/index/homePageView?openId=" + openId + "&developId=" + developId + "&organizationId=" + orgId;
        }
    })
})

function getProjectList(openId, developId) {
    $.ajax({
        url: $("#contextPath").val() + "/wechat/index/wechatPublicOrgList",
        type: "POST",
        dataType: "json",
        data: {
            developId: developId,
            orgName: $("#searchName").val(),
        },
        success: function (data) {
            if (data.code == "200") {
                console.log(data.data);
                cityList = data.data;
                $('.city').html('');
                $(".letter-list").html('');
                var html = '';
                var letter = '';
                $.each(cityList, function (i, item) {
                    html += '<div class="city-list"><span class="city-letter" id="' + item.key + '1">' + item.key + '</span>';
                    letter += '<li><a href="javascript:;">' + item.key + '</a></li>';
                    $.each(item.data, function (j, data) {
                        html += '<p data-id="' + data.id + '" class="item" data-name="' + data.name + '">' + data.name + '</p>';
                    })
                    html += '</div>';
                })
                $(".letter-list").append(letter);
                $('.city').append(html);
                $(".item").on("click", function () {
                    var orgId = $(this).data("id");
                    var name = $(this).data("name");
                    if (orgId) {
                        localStorage.setItem('wx' + developId, orgId);
                        localStorage.setItem(developId + "orgName", name);
                        localStorage.setItem("orgId", orgId);
                        sessionStorage.setItem('wx' + developId, orgId);
                        sessionStorage.setItem("orgId", orgId);
                        sessionStorage.setItem(developId + "orgName", name);
                        window.location.href = $("#contextPath").val() + "/wechat/index/homePageView?openId=" + openId + "&developId=" + developId + "&organizationId=" + orgId;
                    }

                })
            } else {
//                alert(data.msg)
            }
        },
        error: function (data) {
//            alert("系统繁忙..");
        }
    });
}