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

  /**
   * 页面的初始数据
   */
  data: {
    isLoadMore: true,
    page: 1,
    total_page: 0,
    value_s:'',
    goods_list:[],
    category_id:'',
    catory:[],
    producer_id:'',
    tapStart: false,
    show: false,
    list: ['全部', '蛋白质', '排毒养颜', '茶饮', '全部', '蛋白质', '排毒养颜', '茶饮'],
    ind: 0,
    showCard: false

  },

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

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.searchMore();
  },

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

  },
  getCatory() {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.producer.frontend.goodsCategory.getList');
    app._getNetWork({
      url: urlStr,
      data: {
        producer_id: that.data.producer_id
      },
      success: (resdata) => {
        var response = resdata.data;

        if (response.result == 1) {
          that.setData({
            catory: response.data
          });
        } else {
          wx.showToast({
            title: response.msg,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getData() {
    let that=this;
    let urlStr = app.getNetAddresss('plugin.producer.frontend.producer.getProducerCenterById');
    app._getNetWork({
      url: urlStr,
      data: {
        producer_id: that.data.producer_id
      },
      success: (resdata) => {
        var response = resdata.data;
       
        if (response.result == 1) {
          that.setData({
            info: resdata.data.data.producer,
          });
        } else {
          wx.showToast({
            title: response.msg,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  open_one(){
   
    this.setData({
      showCard:true
    });
  },
  open_two(){
    this.setData({
      show: true
    });
  },
  onClickLeft() {
    this.setData({
      showCard:false
    });
  },
  open_three(){
    this.setData({
      show: false
    });
  },
  toGoods(e){
    let goods_id = e.currentTarget.dataset.goodid;
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?producer_id=' + this.data.producer_id + '&id=' + goods_id
    });
  },
  toproclamation(e) {
    wx.navigateTo({
      url: '/packageE/managementIndex/proclamation/proclamation?producer_id=' + this.data.producer_id
    });
  },
  changeValue(e){
    this.setData({
      value_s:e.detail.value
    });
  },
  onSearch(e) {
    this.setData({
      page:1
    });
    let that = this;
    let urlStr = app.getNetAddresss('plugin.producer.frontend.goods.searchGoods');
    app._postNetWork({
      url: urlStr,
      data: {
        page: that.data.page,
        producer_id: that.data.producer_id,
        search: {
          keyword: that.data.value_s,
          category: that.data.category_id
        }
      },
      success: (resdata) => {
        var response = resdata.data;
        if (response.result == 1) {
          this.setData({
            isLoadMore: true,
            total_page: response.data.last_page,
            goods_list: response.data.data
          });
          console.log(that.data.goods_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);
      }
    });
  },
  // 获取更多数据
  searchMore() {
    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.goods.searchGoods');
      app._postNetWork({
        url: urlStr,
        data: {
          page:that.data.page,  
          producer_id: that.data.producer_id,
          search: {
            keyword: that.data.value_s,
            category: that.data.category_id
          }
        },
        success: (resdata) => {
          var response = resdata.data;
          if (response.result == 1) {
            var myData = response.data.data.concat(this.data.goods_list);
            this.setData({
              goods_list: myData
            });
          } 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);
        }
      });
    }
  },
  changeIndex(e){
    let inde = e.currentTarget.dataset.index;
    let id = e.currentTarget.dataset.id;
    this.setData({
      ind:inde,
      category_id: id
    });
    this.setData({
      page:1,
      show:false
    });
    this.onSearch();
  }
});