// packageG/morePlugin/morePlugin.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    //小程序没有做的入口
    notShow: ['lifeService', 'uidCode', 'codePage', 'teamSell', 'eliteAwardHome', 'admarketHome', 'Bonus', 'extendHelper', 'publish_manage', 'hireCarManage', 'TransHome', 'ReplenishmentApply', 'DeliveryTerminalApply', 'signIndex', 'footprintMember', 'oilStation', 'gduIndex'],
    plugin: [],
    acindex: 0,
    topHeight: 55,
    currBox: "tab0",
    heightArray: [],
    toview: "",

    isClick: false,
  },

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

  getPlugins() {
    let urlStr = app.getNetAddresss("member.member.more-plugins");
    app._getNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          res.data.plugin.forEach((item, index) => {
            item.plugin.forEach((icon, index) => {
              if (this.data.notShow.indexOf(icon.url) >= 0) {
                item.plugin.splice(index, 1);
              }
            });
          });
          this.setData({
            plugin: res.data.plugin || []
          });
          setTimeout(() => {
            this.testTabs();
          }, 200);
        }
      }
    });
  },
  //获取模板id
  getTemplate_1() {
    let urlStr = app.getNetAddresss("member.member.getMiniTemplateCorrespond");
    let that = this;
    app._getNetWork({
      url: urlStr,
      data: {
        small_type: 4,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          let array = [];
          for (let i = 0; i < res.data.length; i++) {
            array.push(res.data[i].template_id);
          }
          that.setData({
            TemplateArray_1: array,
          });
          wx.requestSubscribeMessage({
            tmplIds: that.data.TemplateArray_1,
            success(res) {
              wx.navigateTo({
                url: "/packageA/member/SupplierCenter/SupplierCenter?uid=" +
                  that.data.supplier_id,
              });
            },
            fail() {
              wx.navigateTo({
                url: "/packageA/member/SupplierCenter/SupplierCenter?uid=" +
                  that.data.supplier_id,
              });
            },
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  //获取模板id
  getTemplate_2() {
    let urlStr = app.getNetAddresss("member.member.getMiniTemplateCorrespond");
    let that = this;
    app._getNetWork({
      url: urlStr,
      data: {
        small_type: 5,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          let array = [];
          for (let i = 0; i < res.data.length; i++) {
            array.push(res.data[i].template_id);
          }
          that.setData({
            TemplateArray_2: array,
          });
          wx.requestSubscribeMessage({
            tmplIds: that.data.TemplateArray_2,
            success(res) {
              wx.navigateTo({
                url: "/packageC/o2o/storeManage/storeManage",
              });
            },
            fail() {
              wx.navigateTo({
                url: "/packageC/o2o/storeManage/storeManage",
              });
            },
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getTemplate_3() {
    let urlStr = app.getNetAddresss("member.member.getMiniTemplateCorrespond");
    let that = this;
    app._getNetWork({
      url: urlStr,
      data: {
        small_type: 6,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          let array = [];
          for (let i = 0; i < res.data.length; i++) {
            array.push(res.data[i].template_id);
          }
          that.setData({
            TemplateArray_3: array,
          });
          wx.requestSubscribeMessage({
            tmplIds: that.data.TemplateArray_3,
            success(res) {
              wx.navigateTo({
                url: "/packageD/directSeeding/helperLiveList/helperLiveList",
              });
            },
            fail() {
              wx.navigateTo({
                url: "/packageD/directSeeding/helperLiveList/helperLiveList",
              });
            },
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  goextension: function () {
    //extension
    wx.reLaunch({
      url: "/packageG/pages/member/extension/extension",
    });
  },
  pluginGoto(e) {
    let item = e.currentTarget.dataset.item;
    let url = item.url;

    if (item.name == "courier") {
      wx.setStorage({
        key: "couriername",
        data: item.title,
      });
      wx.navigateTo({
        url: "/packageA/member/" + url + "/" + url,
      });
    } else if (item.name == "supplier" && item.url == "SupplierCenter") {
      this.getTemplate_1();
    } else if (item.url == "extension") {
      this.goextension();
    } else if (item.url == "storeManage") {
      //跳转到门店管理
      this.getTemplate_2();
    } else if (item.name == "supplier") {
      wx.navigateTo({
        url: "/packageA/member/" + url + "/" + url + "?uid=" + this.data.supplier_id,
      });
    } else if (item.url == "HotelManage") {
      wx.navigateTo({
        url: "/packageA/hotel/HotelManage/HotelManage?title=" +
          encodeURIComponent("酒店管理"),
      });
    } else if (item.url == "helperLiveList") {
      this.getTemplate_3();
    } else if (item.url == "cloudIndex") {
      wx.navigateTo({
        url: "/packageE/others/cloudwarehouse/cloud_index/cloud_index",
      });
    } else if (item.url == "collageIndex") {
      wx.navigateTo({
        url: "/packageE/member/collage/collageIndex/collageIndex",
      });
    }else if (item.name == "m-erweima") {
      // 海报
      this.openQrCode(this.data.dataIs);
    } else if (item.mini_url) {
      wx.navigateTo({
        url: item.mini_url,
      });
    }
  },
  openQrCode(e) {
    this.setData({
      isshow: true,
    });
  },
  taptest(e) {
    let ind = e.currentTarget.dataset.ind;
    this.setData({
      acindex: ind,
      toview: `ind${ind}`,
      currBox: "tab" + ind,
      isClick: true,
    });

    setTimeout(() => {
      this.setData({
        isClick: false,
      });
    }, 500);
  },
  bincroll(e) {
    // console.log(e.detail.scrollTop)
    // let scrollTop = e.detail.scrollTop;

    if (!this.data.isClick) {
      for (let i = 0; i < this.data.plugin.length; i++) {
        if (this.data.heightArray[1] >= e.detail.scrollTop) {
          this.setData({
            acindex: 0,
            currBox: "tab0",
          });
          break;
        } else if (
          this.data.heightArray[i] < e.detail.scrollTop &&
          e.detail.scrollTop < this.data.heightArray[i + 1]
        ) {
          this.setData({
            acindex: i,
            currBox: "tab" + i,
          });
          break;
        } else if (
          this.data.heightArray[this.data.plugin.length - 1] <=
          e.detail.scrollTop
        ) {
          this.setData({
            acindex: this.data.plugin.length - 1,
            currBox: "tab" + this.data.plugin.length - 1,
          });
          break;
        }
      }
    }

  },
  testTabs() {
    let that = this;
    let heightArray = [];
    for (let i = 0; i < this.data.plugin.length; i++) {
      wx.createSelectorQuery()
        .select(`#ind${i}`)
        .boundingClientRect(function (rect) {
          heightArray.push(rect.top - that.data.topHeight);
          if (i == that.data.plugin.length - 1) {
            that.setData({
              heightArray: heightArray,
            });
          }
        })
        .exec();
    }
  },

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

  },

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

  },

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

  },

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

  }
});