/**
 * 首页
 * 
 */
$(function () {
  var ERROR_OK = 200;
  var openId = $("#openId").val();
  var developId = $("#developId").val();
  var contextPath = $("#contextPath").val();
  var organId = $("#organId").val();
  var organizationId = $("#organizationId").val();
  if (!openId) {
    openId = sessionStorage.getItem("openId");
  }

    if(!!organizationId){
    console.log(organizationId + "," + developId + "," + openId);
        window.location.href = $("#contextPath").val() + "/wechat/index/homePageView?openId=" + openId + "&developId=" + developId + "&organizationId=" + organizationId;
    }

  if (!developId) {
    developId = sessionStorage.getItem("developId")
  }
  var Index = {
    init: function () {
      this.render();
      this.bindEvent();
    },
    render: function () {
      this.getPosition(developId);
    },
    bindEvent: function () {},
    getPosition: function (developId) {
      var _this = this;
      $.ajax({
        url: $("#contextPath").val() + "/wechat/visitor/fenxiang",
        type: "POST",
        async: true,
        cache: false,
        dataType: "json",
        data: {
          url: location.href.split('#')[0],
          developId: developId
        },
        success: function (data) {
          // console.log(data);
          wx.config({
            debug: false, // 开启调试模式
            appId: data.appid, // 必填，公众号的唯一标识
            timestamp: data.timestamp, // 必填，生成签名的时间戳
            nonceStr: data.nonceStr, // 必填，生成签名的随机串
            signature: data.signature, // 必填，签名，见附录1
            jsApiList: ['getLocation', 'hideOptionMenu']
            // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
          });
          //隐藏右上角菜单
          wx.hideOptionMenu();
          wx.ready(function () {
            wx.checkJsApi({
              jsApiList: [
                'getLocation',
                'hideOptionMenu'
              ],
              success: function (res) {
                //alert(res.errMsg);  
              }
            });
            wx.getLocation({
              success: function (res) {
                var latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
                var longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
                // var latitude = 22.54110; // 纬度，浮点数，范围为90 ~ -90
                // var longitude = 114.08266; // 经度，浮点数，范围为180 ~ -180。
                var speed = res.speed; // 速度，以米/每秒计
                var accuracy = res.accuracy; // 位置精度
                // alert(latitude,"纬度");
                // alert(longitude,"经度")
                //定位校区
                _this.getCurrentProject(latitude, longitude, developId);
              },
              cancel: function (res) {
                $(".projectName").html('');
                $(".current i").html("定位失败");
              }
            });
          });

        }

      });

    },
    getCurrentProject: function (latitude, longitude, developId) {
      $.ajax({
        url: $("#contextPath").val() + "/wechat/index/initOrgSelect",
        type: "POST",
        dataType: "json",
        data: {
          latitude: latitude,
          longitude: longitude,
          developId: developId
        },
        success: function (data) {
          if (data.code == "200") {
            console.log(data.data)
            var html = '';
            $.each(data.data, function (i, item) {
                html += '<div class="projectName" data-id="'+item.id+'" data-name="'+item.name+'">'+item.name+'</div>';
            })
            $(".current-position").append(html);
            $(".current i").html("当前定位附近校区");
            $(".projectName").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);
                  sessionStorage.setItem("orgName", name);
                  window.location.href = $("#contextPath").val() + "/wechat/index/homePageView?openId=" + openId + "&developId=" + developId + "&organizationId=" + orgId;
              }

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