const getReq = require('../../config.js').getReq;
import recordSource from '../../utils/recordSource';
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    special_datas: {},
    summery: [],
    hasmore:false,
    curpage:1,
    brand_id : '',
    hot_id:'',
    keyword:'',
    isScroll: false,
    fromSource: '',
    firstLoad: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: options.title ? options.title : app.globalData.channel
    })
    wx.showLoading({
      title: '加载中',
    });
    const brand_id = options.brand_id ? options.brand_id : '';
    const hot_id = options.hot_id ? options.hot_id : '';
    const keyword = options.keyword ? options.keyword : '';
    const curpage = this.data.curpage;
    let fromSource = recordSource(app, `act=search&op=index&brand_id=${brand_id}&hot_id=${hot_id}&keyword=${keyword}&curpage=${curpage}&client_type=mini`);
    this.setData({
      hot_id,
      brand_id,
      keyword,
      fromSource: app.globalData.fromSource
    })
    let self = this;

    
    getReq(app,{
      act: 'search',
      op: 'index',
      brand_id,
      hot_id,
      keyword,
      curpage,
      from: fromSource
    }, function (res) {
      if (res.code == 200) {
        self.setData({
          special_datas: res.datas,
          summery: res.datas.summary,
          hasmore: res.datas.mobile_page.hasmore,
          firstLoad: false
        });
        wx.hideLoading();
      }
    })
  },
  onPageScroll(e) {
    clearTimeout(this.showTop);
    this.showTop = setTimeout(() => {
      let scrollTop = e.scrollTop;
      if (scrollTop >= 300) {
        this.setData({
          isScroll: true
        })
      }
      else {
        this.setData({
          isScroll: false
        })
      }
    }, 100);
  },
  backTop() {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(!this.data.firstLoad) {
      app.setFromSource(this.data.fromSource)
    }
  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      if(this.data.hasmore) {
        wx.showLoading({
          title: '加载中',
        });
        let self = this;
        getReq(app,{
          act: 'search',
          op: 'index',
          brand_id: this.data.brand_id,
          hot_id: this.data.hot_id,
          keyword: this.data.keyword,
          curpage: this.data.curpage+1
        }, function (res) {
          if (res.code == 200) {
            let newDatas = res.datas;
            let newSummery = res.datas.summary;
            let oldDatas = self.data.special_datas;
            let oldSummery = self.data.summery;
            
            let buildDatas = (function(){
              let buildDatas = {};
              for (let i in oldDatas) {
                if (oldDatas[i] instanceof Array) {
                  buildDatas[i] = oldDatas[i].concat(newDatas[i]);
                }
              }
              return buildDatas;
            })();
            let buildSummery = (function(){
              let buildDatas = [];
              buildDatas = oldSummery.concat(newSummery);
              return buildDatas;
            })();
            self.setData({
              curpage: self.data.curpage+1,
              special_datas: buildDatas,
              summery: buildSummery,
              hasmore: res.datas.mobile_page.hasmore
            });
            wx.hideLoading();
          }
        })
      }
  }
})