// newsShop/pages/patternMenu/patternMenu.js
let app = getApp();
let host = app.globalData.servsers; // 请求的url
Page({

  /**
   * 页面的初始数据
   */
  data: {
    host:host,
    isSelect: "1", //默认全部
    dataStateArray: [
    {
      state: '1',
      label: "营销礼赠",
    },
    {
      state: '2',
      label: "商务送礼",
    },
    {
      state: '3',
      label: "员工/福利",
    }
    ],
    filterArray: [
      // { name: '1', value: '0~499元' },
      // { name: '2', value: '500~999元' },
      // { name: '3', value: '1000~1999元' },
      // { name: '4', value: '2000元以上' },
    ],
    requestState:true, //检测请求状态是否结束了
    filterStatic: false,
    isNew: true,
    isMin: true,
    goodsList:[],
    page:1, //默认是第一页
    limit:20, //一页20条数据
    maxPrice:null,//最大价格
    minPrice:null,//最低价格
    priceSort: null, //价格排序,默认降序
    timeSort: null, //价时间排序,默认降序
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getGiftNavTab(this)
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  tab(e){ // 头部tab切换
    let that = this;
    that.setData({
      isSelect: e.currentTarget.dataset.id,
      goodsList:[],
      page:1,
    })
    that.getGiftList(that)
  },
  timeSortTap(){ // 时间筛选
    let that = this;
    that.setData({
      timeSort: that.data.timeSort === null ? 'asc' : that.data.timeSort === 'asc' ? 'desc' : 'asc',
      goodsList: [],
      page: 1,
    })
    that.getGiftList(that)
  },
  priceSortTap() { // 价格筛选
    let that = this;
    that.setData({
      priceSort: that.data.priceSort === null ? 'asc' : that.data.priceSort === 'asc' ? 'desc' : 'asc',
      goodsList: [],
      page: 1,
    })
    that.getGiftList(that)
  },
  priceMaxMinTap(){ // 价格区间筛选
    let that = this;
    that.setData({
      filterStatic: true
    })
  },
  close(){
    let that = this;
    that.setData({
      filterStatic: false
    })
  },
  blurPriceMin(e){
    // event.detail = {value: value}
    let that = this;
    that.setData({
      minPrice: e.detail.value,
      page: 1,
      goodsList: [],
    })
    that.getGiftList(that)
  },
  blurPriceMax(e){
    let that = this;
    that.setData({
      maxPrice:e.detail.value,
      page:1,
      goodsList:[],
    })
    that.getGiftList(that)
  },
  getGiftNavTab(that){
    wx.request({
      url: host + '/classifyapi/classifyTwoAll',
      method: 'get',
      data: {
        classifyHigher:'3060'
      },
      success(res){
        console.log(res,"res")
        that.setData({
          dataStateArray:res.data.rows,
          isSelect:res.data.rows[0].id
        })
        that.getGiftList(that)
      }
    })
  },
  getGiftList(that){
    let data = {
      channelId: 5, // 渠道id,现在为5,一件选礼为5
      classId: that.data.isSelect,
      limit: that.data.limit, // 一页20条数据
      offset: that.data.page, //当前页数
    }
    if (that.data.priceSort) {
      data.priceSort = that.data.priceSort
    }
    if (that.data.timeSort) {
      data.timeSort = that.data.timeSort
    }
    if (that.data.maxPrice){
      data.maxPrice = that.data.maxPrice
    }
    if (that.data.minPrice) {
      data.minPrice = that.data.minPrice
    }
    wx.request({
      url: host +'/commodityapi/queryCommodityByClass',
      method:'get',
      data:data,
      success(res){
        let dataArray = that.data.goodsList;
        res.data.rows.forEach(el=>{
          el.logo = el.commodity_logo.split(",")
        })
        dataArray.push(...res.data.rows)
        that.setData({
          goodsList: dataArray
        })
      }
    })
  }
})