// pages/member/rankingListSecond/rankingListSecond.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: '',
    show1: false,
    inputs: '',
    goodList: [],
    chooseGood: [],
    chooseItem: [],
    lengths: 0,
    arrItms: [],
    // more
    isLoadMore: true,
    page: 1,
    total_page: 0
  },

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

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },

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

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

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

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

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  setGoodsData() {
    let chooseItem = '';
    wx.getStorage({
      key: 'chooseItem',
      success: (res) => {
        chooseItem = res.data;
      }
    });
    if (chooseItem) {
      this.setData({
        chooseItem});
    } else {
      this.setData({
        chooseItem: []
      });
    }
    this.setData({
      chooseGood: []
    });
    let arrs = this.data.chooseGood;
    for (let i = 0; i < this.data.chooseItem.length; i++) {
      arrs.push(this.data.chooseItem[i].id);
    }
    this.setData({
      chooseGood: arrs
    });
    console.log(this.data.chooseGood, '数组');
  },
  chooseGoods(e) {
    let item = e.target.dataset.item || e.currentTarget.dataset.item;
    let index = e.target.dataset.index || e.currentTarget.dataset.index;
    if (index == 0) {
      let arrs = this.data.goodList;
      let leg = this.data.lengths;
      arrs.forEach(items => {
        if (items.goods_id == item.goods_id) {
          items.bler = false;
          leg--;
        }
      });
      this.setData({
        chooseItem: []
      });
      this.setData({
        goodList: arrs,
        lengths: leg
      });
    } else {
      let arrs = this.data.goodList;
      let lengths = this.data.lengths;
      if (lengths >= 8) {
        this.setData({
          show1: true
        });
        return;
      }
      arrs.forEach(items => {
        if (items.goods_id == item.goods_id) {
          items.bler = true;
          lengths++;
        }
      });

      this.setData({
        goodList: arrs,
        lengths
      });
    }
    let ads = [];
    if (item.bler == false) {
      ads.push(item);
    }
    this.setData({
      chooseItem: ads
    });
  },
  // 取消弹框显示
  checkOk() {
    this.setData({
      show1: false
    });
  },
  toGood(e) {
    let id = e.target.dataset.id || e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2' + '?id=' + id
    });
  },
  sureChoose(event) {
    let arous = this.data.goodList;
    var times = 0;
    let arrs = [];
    arous.forEach(i => {
      if (i.bler) {
        times++;
        arrs.push(i);
        return;
      }
    });
    this.setData({
      chooseItem: arrs
    });
    if (times == 0) {
      wx.showToast({
        title: '请选择商品',
        icon: 'none'
      });
      return;
    }
    if (times >= 8) {
      this.setData({
        show1: true
      });
    } else {
      this.setData({
        show1: false
      });
      // 把选择的商品保存在本地
      let data = [];
      this.data.chooseItem.forEach(item => {
        data.push({
          title: item.title,
          price: item.price,
          thumb: item.thumb,
          id: item.goods_id
        });
      });
      wx.setStorage({
        key: 'chooseItem',
        data: data
      });
      wx.navigateTo({
        url: '/packageB/member/business_card/EditCard/EditCard' + '?card_id=' + this.options.card_id + '&isShow=' + true
      });
    }
  },
  enterSearch(e) {
    let inpValue = e.detail;
    this.setData({
      inputs:inpValue
    });
    this.getData();
  },
  // 获取数据
  getData() {
    this.setData({
      isLoadMore: false,
      page: 1
    });
    let urlStr = app.getNetAddresss('goods.goods.search-goods');
    urlStr += '&search[keyword]=' + this.data.inputs + '&page=' + this.data.page + '&search[category]=' + this.options.id;
    app._getNetWork({
      url: urlStr,
      success: (resdata) => {
        var response = resdata.data;
        if (response.result == 1) {
          response.data.data.forEach(item => {
            item.bler = false;
          });
          this.setData({
            isLoadMore: true,
            total_page: response.data.last_page,
            goodList: response.data.data
          });
          let goodList = this.data.goodList;
          console.log(goodList, '数组的对象');

          let len = this.data.lengths;
          wx.getStorage({
            key: 'chooseItem',
            success: res => {
              let data = res.data;
              for (let i = 0; i < data.length; i++) {
                goodList.forEach(items => {
                  if (items.id == data[i].id) {
                    items.bler = true;
                    len++;
                  }
                });
              }
              this.setData({
                goodList,
                lengths: len
              });
            }
          });

          if (!this.data.total_page) {
            this.setData({
              total_page: 0
            });
          }
        } else {
          wx.showToast({
            title: response.msg,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  // 获取更多数据
  getMoreData() {
    this.setData({
      isLoadMore: false
    });
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      let page = this.data.page + 1;
      this.setData({
        page});
      let urlStr = app.getNetAddresss('goods.goods.search-goods');
      urlStr += '&search[keyword]=' + this.data.inputs + '&page=' + this.data.page + '&search[category]=' + this.options.id;
      app._getNetWork({
        url: urlStr,
        success: (resdata) => {
          var response = resdata.data;

          this.setData({
            isLoadMore: true
          });
          if (response.result == 1) {
            var myData = response.data.data.concat(this.data.goodList);
            myData.forEach(item => {
              item.bler = false;
            });
            let goodLists = myData;

            wx.getStorage({
              key: 'chooseItem',
              success: res => {
                let data = res.data;
                for (let i = 0; i < data.length; i++) {
                  goodLists.forEach(items => {
                    if (items.id == data[i].id) {
                      items.bler = true;
                    }
                  });
                }
                this.setData({
                  goodList: goodLists
                });
              }
            });
          } else {
            wx.showToast({
              title: response.msg,
              icon: 'none'
            });
            let pages = this.data.page - 1;
            this.setData({
              page: pages,
              isLoadMore: false
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  }
});
