// packageE/managementIndex/managementLis/managementLis.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodshow:false,
    tapStart: false,
    show: false,
    isLoadMore: true,
    page: 1,
    total_page: 0,
    list:[],
    language:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en });
    this.getList();
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  toGoods(e) {
    let id = e.currentTarget.dataset.id;
    let goods_id = e.currentTarget.dataset.goodid;
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?producer_id=' + id + '&id=' + goods_id
    });
  },
  toIndex(e){
    let id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageE/managementIndex/managementIndex?producer_id=' + id 
    });
  },
  getList(e) {
    let that=this;
    let urlStr = app.getNetAddresss('plugin.producer.frontend.producer.getList');
    app._getNetWork({
      url: urlStr,
      data: {

      },
      success: (resdata) => {
        var response = resdata.data;
        if (response.result == 1) {
          this.setData({
            isLoadMore: true,
            total_page: response.data.data.last_page,
            list: response.data.data.data
          });
          that.data.list.forEach((item, index) => {
            if (item.goods_list.length>8){
              item.goodshow=true;
              item.goods_list = item.goods_list.slice(0, 8);
            }
          });
          that.setData({
            list:that.data.list
          });
           
          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('plugin.producer.frontend.producer.getList');
      app._getNetWork({
        url: urlStr,
        data: {
       
        },
        success: (resdata) => {
          var response = resdata.data;
          if (response.result == 1) {
            var myData = response.data.data.data.concat(this.data.list);
            this.setData({
              list: myData
            });
            that.data.list.forEach((item, index) => {
              if (item.goods_list.length > 8) {
                item.goodshow = true;
                item.goods_list = item.goods_list.slice(0, 8);
              }
            });
            that.setData({
              list: that.data.list
            });
          } 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);
        }
      });
    }
  },
});