// pages/category/category.js

var http = require("../../utils/http.js");
var util = require('../../utils/util.js');
var config = require("../../utils/config.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    selIndex: 0,
    categoryList: [],
    categoryImg: '',
    subCategoryList: [],
    pageTopHeight: wx.getWindowInfo().statusBarHeight,
    hotList: [],
    topCurrentIndex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getRecommended()
    this.get_neighborShop()
    // this.get_categoryInfo()
  },
  /**
   * 获取距离最近店铺id
   * @param {*} e 
   */
  get_neighborShop: function () {
    var params = {
      url: "/shop/neighborShop",
      method: "GET",
      data: {
        lat: wx.getStorageSync('LATITUDE'),
        lon: wx.getStorageSync('LONGITUDE'),
      },
      callBack: (res) => {
        // this.get_categoryInfo(res)

        if(typeof res == 'number'){
          this.get_categoryInfo(res)
        }else{
          this.get_categoryInfo(res.shopId)
        }
      }
    };
    http.request(params);
  },
  get_categoryInfo: function (shopid) {
    var ths = this;
    //加载分类列表
    var params = {
      url: "/category/categoryInfo",
      method: "GET",
      data: {
        shopId:shopid
      },
      callBack: function (res) {
        let categoryName = ''
        res.forEach(e => {
          categoryName = e.categoryName.split('>')
          e.categoryName = categoryName[0]
        })
        ths.setData({
          categoryList: res,
        });
        if (!getApp().globalData.categoryId) {
          ths.getProdList(res[0].categoryId)
          ths.setData({
            categoryImg: res[0].pic,
          })
        }
      }
    };
    http.request(params);
  },
 /**
   * 选择定位后更新列表
   */
  go_update() {
    this.get_neighborShop()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (getApp().globalData.categoryId) {
      let categoryId = getApp().globalData.categoryId
      let index = getApp().globalData.index
      let pic = getApp().globalData.pic
      this.getProdList(categoryId);
      this.setData({
        categoryImg: pic,
        selIndex: index
      });
    }else{
    this.get_neighborShop()
    }
  },

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

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

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

  },

  /**
   * 分类点击事件，获取子分类
   */
  onMenuTab: function (e) {
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    // this.getProdList(id);
    this.getProdList(this.data.categoryList[index].categoryId);
    this.setData({
      categoryImg: this.data.categoryList[index].pic,
      selIndex: index
    });
    getApp().globalData.categoryId = ''
    getApp().globalData.index = ''
    getApp().globalData.pic = ''
  },
  /**
   * 推荐词,type:1关键词,2热门搜索词，3推荐搜索词
   */
  getRecommended: function () {
    var params = {
      url: "/keyword/list",
      method: "GET",
      data: {
        type: 2
      },
      callBack: (res) => {
        let reslut = res.filter(item => item.status !== 2) //过滤下线词
        let timeReslut = reslut.filter(e => util.dateToTimestamp(e.effectiveTime) < new Date().getTime()) //过滤未生效词
        this.setData({
          hotList: timeReslut
        })
      }
    };
    http.request(params);
  },

  /**
   * 推荐词搜索
   */
  topSwiperChange: function (e) {
    this.setData({
      topCurrentIndex: e.detail.current // 获取当前滚动到的swiper-item的索引并更新到data中
    })
  },
  topHotSearch: function () {
    const topname = this.data.hotList[this.data.topCurrentIndex].name
    wx.navigateTo({
      url: '/pages/search-prod-show/search-prod-show?prodName=' + topname,
    })
  },

  // 跳转搜索页
  toSearchPage: function () {
    wx.navigateTo({
      url: '/pages/search-page/search-page',
    })
  },
  getProdList(categoryId) {
    this.setData({
      parentId: categoryId
    })
    //加载子分类列表
    var params = {
      url: "/category/categoryInfo",
      method: "GET",
      data: {
        parentId: categoryId
      },
      callBack: (res) => {
        let str='https://zswl-shop.oss-cn-chengdu.aliyuncs.com'
        res.forEach(item=>{
          item.categories.forEach(e=>{
            if(e.pic!=null){
              if(!e.pic.includes(str)){
                e.pic='https://zswl-shop.oss-cn-chengdu.aliyuncs.com/'+e.pic
              }
            }
          })
        })
        this.setData({
          subCategoryList: res,
        });
      }
    };
    http.request(params);
  },
  /**
   * 跳转到定位页面
   */
  toLocationPage: function () {
    wx.navigateTo({
      url: '/pages/locationAdd/locationAdd',
    })
  },
  // 跳转子分类商品页面
  toCatePage: function (e) {
    console.log(e);
    const {
      type,
      parentid,
      categoryid,
      subcateindex
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/sub-category/sub-category?parentId=${parentid}&categoryId=${type=='all'?this.data.subCategoryList[subcateindex].categoryId:categoryid}`,
    })
  },
  toCategoryPage: function () {
    wx.navigateTo({
      url: `/pages/sub-category/sub-category?categoryId=${this.data.parentId}`,
    })
  }
})