// pages/search-prod-show/search-prod-show.js

var http = require('../../utils/http.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sts: 0,
    showType: 2,
    searchProdList: [],
    prodName: "",
    pages: 0,
    current: 1,
    size:10,
    shopId:0
  },

  changeShowType: function() {
    var showType = this.data.showType;
    if (showType == 1) {
      showType = 2;
    } else {
      showType = 1;
    }
    this.setData({
      showType: showType
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      prodName: options.prodName,
      shopId: options.shopId||0
    });

  },

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

  },

  //输入商品获取数据
  getSearchContent: function(e) {
    this.setData({
      prodName: e.detail.value
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.toLoadData();
  },

  //请求商品接口
  toLoadData: function() {
    wx.showLoading()
    var ths = this;
    let data = {
      current: this.data.current,
      prodName: this.data.prodName,
      size: this.data.size,
      sort: this.data.sts,
      isAllProdType: true,
      lat: wx.getStorageSync('LATITUDE'),
      lon: wx.getStorageSync('LONGITUDE'),
      distance: wx.getStorageSync('DISTANCE') || 0
    }
    if(ths.data.shopId !=0){
      data.shopId = this.data.shopId
    }
    //热门搜索
    var params = {
      url: "/search/searchProdPage",
      method: "GET",
      data ,
      callBack: function(res) {
        wx.hideLoading()
        let img=''
        res.records.forEach(item=>{
          img=item.pic.split(',')
          item.pic=img[0]
        })
        let list = []
        if (res.current == 1) {
          list = res.records
        } else {
          list = ths.data.searchProdList
          list = list.concat(res.records)
        }
        ths.setData({
          searchProdList: list,
          pages: res.pages
        });
      },
    };
    http.request(params);
  },

  //当前搜索页二次搜索商品
  toSearchConfirm: function() {
    this.toLoadData();
  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.current < this.data.pages) {
      this.setData({
        current: this.data.current + 1
      })
      this.toLoadData()
    }

  },

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

  },

  /**
   * 状态点击事件
   */
  onStsTap: function(e) {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300,
    })
    var sts = e.currentTarget.dataset.sts;
    this.setData({
      sts: sts,
      current: 1,
      pages: 1
    });
    this.toLoadData();
  },

  toProdPage: function(e) {
    var prodid = e.currentTarget.dataset.prodid;
    wx.navigateTo({
      url: '/pages/prod/prod?prodid=' + prodid,
    })
  },
})