// packageA/member/supplier/SupplierIndex/SupplierIndex.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    keyword: '',
    page: 1,
    is_open:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '供应商列表',
    });
    this.getClass();
    let language = wx.getStorageSync("langIndex");
    this.setData({
      language: language.en,
    });
  },
  goUrl(e){
    let link = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: link,
    });
  },
  goShop(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageA/member/supplier/SupplierShop/SupplierShop?id=' + id,
    });
  },
  tabrshowbtn(e) {
    let bol = e.detail;
    if (bol) {
      this.setData({
        kFootshow: false
      });
    }
  },
  tapinp(e) {
    let val = e.detail.value;
    this.setData({
      keyword: val,
      page: 1
    });
  },
  tapConfirm(){
    this.getClass();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.current_page >= this.data.last_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      });
      this.getClassMore();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      path:'packageA/member/supplier/SupplierIndex/SupplierIndex'
    };
  },
  onShareTimeline:function(){
    return{
      path:'packageA/member/supplier/SupplierIndex/SupplierIndex'
    };
  },
  getClass() {
    wx.showLoading({
      title: '加载中',
    });
    let urlStr = app.getNetAddresss("plugin.supplier.frontend.shop.index.getSupplierList");
    let json = {
      keyword: this.data.keyword,
      page: this.data.page
    };
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        wx.hideLoading({
          success: (res) => {},
        });
        var res = resdata.data;
        if (res.result == 1) {
          if(!res.data.is_open){
            wx.showToast({
              title:'插件未开启',
              icon:'none',
              duration:2000,
              success:function(res){
                setTimeout(() => {
                  wx.redirectTo({
                    url: '/packageG/index/index',
                  });
                }, 2000);
              }
            });
          }
          this.setData({
            is_open:res.data.is_open,
            info: res.data,
            listData: res.data.list.data,
            current_page: res.data.list.current_page,
            last_page: res.data.list.last_page
          });
        } else {
          wx.showToast({
            title: res.msg,
          });
        }
      }
    });
  },
  getClassMore() {
    wx.showLoading({
      title: '加载中',
    });
    let urlStr = app.getNetAddresss("plugin.supplier.frontend.shop.index.getSupplierList");
    let json = {
      keyword: this.data.keyword,
      page: this.data.page
    };
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        wx.hideLoading({
          success: (res) => {},
        });
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            listData: this.data.listData.concat(res.data.list.data),
            current_page: res.data.list.current_page,
            last_page: res.data.list.last_page
          });
        }else{
          wx.showToast({
            title: res.msg,
          });
        }
      }
    });
  }
});