// packageI/searchPage/searchPage.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    discoverInfo: [],
    discoverPage: 1,
    discoverLastPage: 0,
    discoverTap: false,
    // 热门搜索
    hotInfo: [],
    hotPage: 1,
    hotLastPage: 0,
    hotTap: false,
    keyWord: '',
    searchHistory: []

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '搜索',
    });
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDiscover();
    this.gethot();
    try {
      let History = wx.getStorageSync('searchHistory');
      this.setData({
        searchHistory: JSON.parse(History)
      });
      if(this.data.searchHistory[0]){
        this.setData({
          keyWord : this.data.searchHistory[0].name
        });
      }
    } catch (error) {
      console.log(error);
    }
  },
  goSearch() {
    if (this.data.keyWord.length > 0) {
      wx.navigateTo({
        url: '/packageB/member/category/search_v2/search_v2?keyword=' + this.data.keyWord,
      });
    } else {
      wx.navigateBack({
        delta: 1,
        fail: (res) => {
          return;
        }
      });
    }
  },
  bindinp(e) {
    let key = e.detail.value;
    this.setData({
      keyWord: key
    });
  },
  tapKeyWord(e) {
    let key = e.currentTarget.dataset.name;
    this.setData({
      keyWord: key
    });
    wx.navigateTo({
      url: '/packageB/member/category/search_v2/search_v2?keyword=' + this.data.keyWord,
    });
  },
  deleHistory() {
    wx.setStorageSync('searchHistory', '');
    this.setData({
      searchHistory: []
    });
  },
  colorFin(a){
    a=a.substring(1);  
    a=a.toLowerCase();  
    let b=new Array();  
    for(let x=0;x<3;x++){  
      b[0]=a.substr(x*2,2);  
      b[3]="0123456789abcdef";  
      b[1]=b[0].substr(0,1);  
      b[2]=b[0].substr(1,1);  
      b[20+x]=b[3].indexOf(b[1])*16+b[3].indexOf(b[2]);  
    }  
    return b[20]+","+b[21]+","+b[22];
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

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

  },

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

  },

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

  },

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

  },
  tapUrl(e) {
    let item = e.currentTarget.dataset.item;
    console.log(item.mini_url);
    try {
      wx.navigateTo({
        url: item.mini_url,
        fail: ((res) => {
          wx.switchTab({
            url: item.mini_url
          });
        })
      });
    } catch (error) {
      wx.switchTab({
        url: item.mini_url
      });
    }
  },
  gethot() {
    wx.showLoading({
      title: '加载中',
    });
    let urlStr = app.getNetAddresss('plugin.web-design.frontend.hot.index');
    app._postNetWork({
      url: urlStr,
      data: {
        page: this.data.hotPage
      },
      success: (resdata) => {
        wx.hideLoading();
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            hotInfo: res.data.data,
            hotLastPage: res.data.last_page,
            hotTap: true
          });
        } else {
          this.setData({
            hotInfo:false,
            hotTap:false
          });
        }
      },
      fail: function (res) {
        wx.hideLoading();
      }
    });
  },
  getDiscover() {
    wx.showLoading({
      title: '加载中',
    });
    let urlStr = app.getNetAddresss('plugin.web-design.frontend.discover.index');
    app._postNetWork({
      url: urlStr,
      data: {
        page: this.data.discoverPage
      },
      success: (resdata) => {
        wx.hideLoading();
        let res = resdata.data;
        if (res.result == 1) {
          let inif =  res.data.data;
          for(let i =0;i<inif.length;i++){
            inif[i].color = this.colorFin(inif[i].color);
          }
          this.setData({
            discoverInfo: inif,
            discoverLastPage: res.data.last_page,
            discoverTap: true
          });
        } else {
          this.setData({
            discoverInfo:false,
            discoverTap:false
          });
        }
      },
      fail: function (res) {
        wx.hideLoading();
      }
    });
  },
  discoverMore() {
    if (!this.data.discoverTap) {
      return;
    }
    this.data.discoverTap = false;
    if (this.data.discoverPage >= this.data.discoverLastPage) {
      return;
    } else {
      this.setData({
        discoverPage: this.data.discoverPage + 1
      });
      wx.showLoading({
        title: '加载中',
      });
      let urlStr = app.getNetAddresss('plugin.web-design.frontend.discover.index');
      app._postNetWork({
        url: urlStr,
        data: {
          page: this.data.discoverPage
        },
        success: (resdata) => {
          wx.hideLoading();
          let res = resdata.data;
          if (res.result == 1) {
            let inif =  res.data.data;
            for(let i =0;i<inif.length;i++){
              inif[i].color = this.colorFin(inif[i].color);
            }
            this.setData({
              discoverInfo: this.data.concat(inif),
              discoverLastPage: res.data.last_page
            });
            this.data.discoverTap = true;
          } else {
            this.setData({
              discoverPage: this.data.discoverPage - 1
            });
            this.data.discoverTap = true;
            app.tips(res.msg);
          }
        },
        fail: function (res) {
          wx.hideLoading();
        }
      });
    }
  },
  hotMore() {
    if (!this.data.hotTap) {
      return;
    }
    this.data.hotTap = false;
    if (this.data.hotPage >= this.data.hotLastPage) {
      return;
    } else {
      this.setData({
        hotPage: this.data.hotPage + 1
      });
      wx.showLoading({
        title: '加载中',
      });
      let urlStr = app.getNetAddresss('plugin.web-design.frontend.hot.index');
      app._postNetWork({
        url: urlStr,
        data: {
          page: this.data.hotPage
        },
        success: (resdata) => {
          wx.hideLoading();
          let res = resdata.data;
          if (res.result == 1) {
            this.setData({
              hotInfo: this.data.hotInfo(res.data.data),
              hotLastPage: res.data.last_page
            });
            this.data.hotTap = true;
          } else {
            this.setData({
              hotPage: this.data.hotPage - 1
            });
            this.data.hotTap = true;
            app.tips(res.msg);
          }
        },
        fail: function (res) {
          wx.hideLoading();
        }
      });
    }
  }
});