// pages/boutique/outique.js
const app = getApp()
const link = app.globalData.url
Page({

  /**
   * 页面的初始数据
   */
  data: {
    offset:0,
    limit:20,
    channel_type:"4",
    pid:"9112489_209622235",
    totalCount:0,
    goodsList:[],
    cat_id:"20100"
  },

  _asyncData() {
    this.setData({
      scrollAbleTabs: [
        {
          tab: '精选',
          key: '4'
        },
        {
        tab: '今日热销',
        key: '1'
      }, {
        tab: '高佣商品',
        key: '6'
      }, {
        tab: '百货',
        key: '20100'
      }, {
        tab: '母婴',
        key: '20200'
      },{
        tab:'食品',
        key:'20300'
      },
      {
        tab:'女装',
        key:'20400'
      },
      {
        tab:'电器',
        key:'20500'
      },
      {
        tab:'鞋包',
        key:'20600'
      },{
        tab:'内衣',
        key:'20700'
      },
      {
        tab:'美妆',
        key:'20800'
      },
      {
        tab:'男装',
        key:'20900'
      },
      {
        tab:'水果',
        key:'21000'
      },{
        tab:'家纺',
        key:'21100'
      },{
        tab:'文具',
        key:'21200'
      }
      ,{
        tab:'运动',
        key:'21300'
      },{
        tab:'虚拟',
        key:'21400'
      },{
        tab:'汽车',
        key:'21500'
      },{
        tab:'家装',
        key:'21600'
      },{
        tab:'家具',
        key:'21700'
      },{
        tab:'医药',
        key:'21800'
      }
    ],
    });
  },

  //20100-百货,20200-母婴,20300-食品,20400-女装,20500-电器,20600-鞋包,20700-内衣,20800-美妆,20900-男装,21000-水果,21100-家纺,21200-文具,21300-运动,21400-虚拟,21500-汽车,21600-家装,21700-家具,21800-医药

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.offset = 0
    that.limit = 20
    that.channel_type = "4"
    that.pid = "9112489_209622235"
    that.cat_id = ""
    console.log(options)
   //var openId = options.openId
    
    wx.request( { 
      url: link + "/v1/set", 
      header: { 
       "Content-Type": "application/json"
      }, 
      method: "POST",
      data:  { version: "1.0.3" }, 
      complete: function( res ) { 
       console.log(res)
       if( res == null || res.data == null ) { 
        console.error( '网络请求失败' ); 
        return; 
       } 
       if (res.data.data.type == "true"){
          wx.redirectTo({
            url: '../search/search'
          })
       }else{
       
      //获取数据
      setTimeout(that._asyncData, 0);
      wx.request( { 
       url: link + "/v1/pdd/recommend/get", 
       header: { 
        "Content-Type": "application/json"
       }, 
       method: "POST",
       data:  { offset: that.offset.toString(), limit: that.limit.toString(),channel_type:that.channel_type,pid:that.pid,cat_id:that.cat_id}, 
       complete: function( res ) { 
        wx.hideLoading();
        if( res == null || res.data == null ) { 
          console.error( '网络请求失败' ); 
          return; 
         } 
        console.log(res)
        that.totalCount = res.data.data.goods_basic_detail_response.total
        that.data.goodsList = res.data.data.goods_basic_detail_response.list
        // for (var i = 0; i < res.data.data.goods_basic_detail_response.list.length; i++) {
        //   that.data.goodsList.push( res.data.data.goods_basic_detail_response.list[i]);
        // }
        
       that.setData({
        goodsList: that.data.goodsList,
      })
    
  
       } 
      }) 
       }


      } 
     }) 

     wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log(res)
        wx.request( { 
          url: link + "/v1/user/open.id/set", 
          header: { 
           "Content-Type": "application/json"
          }, 
          method: "POST",
          //
          data:  { code: res.code,openId: options.openId }, 
          complete: function( res ) { 
            console.log(res)
            console.log('openid='+ options.openId )
           if (res.data.code == 200){
            wx.setStorageSync('openId', res.data.data.openid)
           }
          
          } 
         }) 
      }
    })

  },
  search: function(options){
    wx.navigateTo({
      url: '../shouye/shouye'
    })
  },

  keyData:function(e){
    var that = this
   
    if (e.detail.activeKey == "4" || e.detail.activeKey == "1" || e.detail.activeKey == "6"){
      that.channel_type = e.detail.activeKey
      that.cat_id = ""
    } else{
      that.channel_type = "4"
      that.cat_id = e.detail.activeKey
    }
   
    console.log(e)

    that.offset = 0
    that.limit = 20
    wx.request( { 
    url: link + "/v1/pdd/recommend/get", 
    header: { 
      "Content-Type": "application/json"
    }, 
    method: "POST",
    data:  { offset: that.offset.toString(), limit: that.limit.toString(),channel_type:that.channel_type,pid:that.pid,cat_id:that.cat_id}, 
    complete: function( res ) { 
      wx.hideLoading();
      console.log(res)
      that.totalCount = res.data.data.goods_basic_detail_response.total
      that.data.goodsList = res.data.data.goods_basic_detail_response.list
      // for (var i = 0; i < res.data.data.goods_search_response.goods_list.length; i++) {
      //   that.data.goodsList.push( res.data.data.goods_search_response.goods_list[i]);
      // }
      
    that.setData({
      goodsList: that.data.goodsList,
    })

      console.log(res.data.data.goods_search_response)
      if( res == null || res.data == null ) { 
      console.error( '网络请求失败' ); 
      return; 
      } 
    } 
    })

  },
     

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
           //获取数据
   var  that = this; 
   if (that.totalCount <= that.offset + 20){  //提示页码到了
    that.setData({
      lType:"end"
    })
    return
   }
   
   that.offset +=20
  wx.request( { 
   url: link + "/v1/pdd/recommend/get", 
   header: { 
    "Content-Type": "application/json"
   }, 
   method: "POST",
   data:   { offset: that.offset.toString(), limit: that.limit.toString(),channel_type:that.channel_type,pid:that.pid,cat_id:that.cat_id}, 
   complete: function( res ) { 
    wx.hideLoading();
    console.log(res)
    that.totalCount = res.data.data.goods_basic_detail_response.total_count
    for (var i = 0; i < res.data.data.goods_basic_detail_response.list.length; i++) {
      that.data.goodsList.push( res.data.data.goods_basic_detail_response.list[i]);
    }

    console.log(res.data.data.goods_search_response)
    if( res == null || res.data == null ) { 
     console.error( '网络请求失败' ); 
     return; 
    } 
    
   that.setData({
    goodsList: that.data.goodsList,
  })

   
   } 
  }) 
  },

  goodsDetail:function(e){
    const goodsSign=e.currentTarget.dataset.sign;
    const searchId=e.currentTarget.dataset.searchid;
      
      wx.navigateTo({
       url: `/pages/details/details?goodsSign=${goodsSign}&searchId=${searchId}`,
      // url: `/pages/goods-detail/index`,
      })
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})