var util = require('../../../utils/util.js');
var mall = require('../../../api/mall.js'); 
const user = require('../../../utils/user.js');
// const area = require('../../../utils/area.js');

//获取应用实例
const app = getApp();

Page({
  data: {
    categoryList: [],
    currentCategory: {},
    currentSubCategoryList: {},
    scrollLeft: 0,
    scrollTop: 0,
    goodsList:[],
    goodsCount: 0,
    scrollHeight: 0,
    hasLogin: true, 
    city:'',//当前业务城市
    citys:[],
    cityIndex:0,
    addressInfo:{}, 
  },
  onLoad: function (options) { 
    
  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.getCatalog();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  }, 
  listenerCitySelected(e){
    this.setData({
      cityIndex:e.detail.value,
      city:this.data.citys[e.detail.value]
    }) 
    app.globalData.city=this.data.city;
  },
  getCatalog: function () {
 
    let that = this;
    wx.showLoading({
      title: '加载中...',
    });
    util.request(mall.CatalogList).then(function (res) {
      //1036763 写死值 如果地址不是苏州市 隐藏活动专享
      //20220223关闭所有活动合作专区
     //console.info(res.data.categoryList)
      var list=res.data.categoryList;
     // if(that.data.addressInfo.city&&that.data.addressInfo.city!="苏州市"){
        var categorys=[];
        res.data.categoryList.forEach(item=>{
          if(item.id!=1036763)
             categorys.push(item);
        });
        list=categorys;
     // } 
      that.setData({
        categoryList:list,
        currentCategory: res.data.currentCategory,
        currentSubCategoryList: res.data.currentSubCategory
      });
      that.getCategoryGoods();
      wx.hideLoading();
    }).catch((err)=>{
      wx.hideLoading();
      console.log(err);
    });
    util.request(mall.GoodsCount).then(function (res) {
      that.setData({
        goodsCount: res.data
      });
    }); 
  },
  getCategoryGoods(){//获取一级类目下所有商品
    let that=this;
    util.request(mall.GoodsL1Category,{
      id:that.data.currentCategory.name=='热销商品'?'-1':that.data.currentCategory.id,
      // shopId:that.data.addressInfo.shopId,
      // isHot:that.data.currentCategory.name=='热销商品'?true:false
    }).then(res=>{
      console.info(res.data)
      that.setData({
        goodsList:res.data
      }) 
    })

  },
  getCurrentCategory: function (id) {
    let that = this;
    util.request(mall.CatalogCurrent, {
        id:id,
      })
      .then(function (res) {
        that.setData({
          currentCategory: res.data.currentCategory,
          currentSubCategoryList: res.data.currentSubCategory,

        });
        that.getCategoryGoods();
      });
  },
  onReady: function () {
    // 页面渲染完成 
    if (app.globalData.hasLogin) {
      this.setData({
        hasLogin: true
      });
    }
  },
  onShow: function () {
    this.getCatalog();  
    // 页面显示
    //显示购物车角标
    //user.cartshow();
    //确定业务城市 
     var addressInfo=wx.getStorageSync('addressInfo'); 
     if(addressInfo){
       this.setData({
         addressInfo:addressInfo
       })
     }else{ 
        //手机性能慢，可能首页接口没初始化完毕
        let that=this;
        setTimeout(() => {
          var addressInfo=wx.getStorageSync('addressInfo'); 
          if(addressInfo){
            that.setData({
                addressInfo:addressInfo
              })
            }
        }, 700);
      
     }

  },

  goLogin: function (e) { 
    if (e.detail.userInfo == null) {
      app.globalData.hasLogin = false;
      util.showErrorToast('微信授权失败');
      return;
    }
    app.globalData.hasLogin = false;
    wx.setStorageSync('userInfo', null);

    user.loginByWeixin(e.detail.userInfo).then(res => {
      //跳转注册页
      wx.navigateTo({
        url: "/pages/auth/register/register"
      });

    }).catch((err) => {
      app.globalData.hasLogin = false;
    });
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  switchCate: function (event) {
    var that = this;
    var currentTarget = event.currentTarget;
    if (this.data.currentCategory.id == event.currentTarget.dataset.id) {
      return false;
    }

    this.getCurrentCategory(event.currentTarget.dataset.id);
  },
  
  goinviteFriends(e) {
    console.log(app.globalData.hasLogin);
    //跳转到邀请页面
    if (app.globalData.hasLogin) {
      wx.navigateTo({
        url: '/pages/activity/invitefriends/inviteFriends'
      })
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },
})