const Parse = getApp().Parse;
const company = getApp().globalData.company;
const getTabs = require("../../../utils/getTabs")
const login = require("../../../utils/login");
const dateF = require("../../../utils/date")
import Toast from '@vant/weapp/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    showModel: false,
    isSignIn:false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    login.loginNow()
    this.setData({
      active: options?.active || 0
    })
    let list = await getTabs.getDiyTabs()
    this.setData({
      tabbarList: list
    })
    let {
      q,
      invite,
      groupId,
      path
    } = options
    let str = decodeURIComponent(q);
    let obj = this.getParaName(str)
    console.log(obj);
    if(path){
      wx.navigateTo({
        url: path,
      });
    }
  },
  async onChange(event) {
    let currentIndex = event.detail;
    let userInfo = wx.getStorageSync("userLogin");
    if (currentIndex != 0 && userInfo == '') {
      console.log(userInfo);
      login.loginNow()
      return
    }
    this.setData({
      active: currentIndex
    })
    let componentPage = this.selectComponent(`#comp${currentIndex}`)
    switch (currentIndex) {
      case 0:
        break;
      case 2:
        break;
      case 3:
        // componentPage.getUser()
        break;
    }
  },
  toCart() {
    this.setData({
      active: 2
    })
  },
  //解析二维码
  getParaName(url) {
    if (!url || url.indexOf('?') == -1) {
      return
    }
    let paraname = url.split('?')[1]
    return this.setObject(paraname) //封装成对象
  },
  setObject(paraArr) {
    let obj = {}
    let arr1 = paraArr.split('&')
    arr1.forEach(item => {
      let str = item.split('=')
      let key = str[0]
      let val = str[1]
      obj[key] = val
    })
    return obj
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getRenew()
    let {
      active
    } = this.data
    let componentPage = this.selectComponent(`#comp${active}`)
    if (!componentPage) {
      return
    }
    switch (active) {
      case 0:
        // componentPage.refresh()
        break;
      case 1:
        // componentPage.setData({
        //   scate: []
        // })
        // componentPage.getcircle()
        break;
      case 2:
        componentPage.setData({
          allCheck: false,
          cartList: [],
          totalPrice: 0.00,
        })
        componentPage.getShopcart()
        break;
      case 3:
        componentPage.refresh()
        break;
    }
  },
  //朋友圈
  updateCom(){
    let componentPage = this.selectComponent(`#comp${this.data.active}`)
    if (!componentPage) {
      return
    }
    componentPage.getcircle2()
  },
  //首页
  updateCom2(){
    let componentPage = this.selectComponent(`#comp${this.data.active}`)
    if (!componentPage) {
      return
    }
    componentPage.updatetoday()
    componentPage.gettarget()
  },
  //我的页面
  updateCom3(){
    let componentPage = this.selectComponent(`#comp${this.data.active}`)
    if (!componentPage) {
      return
    }
    componentPage.getcircle()
    componentPage.getname()
  },
  /* 服务器是否需要续费 */
  async getRenew(){
    let query = new Parse.Query("Company")
    query.select('website','expireMap')
    query.equalTo('objectId',company)
    let c = await query.first()
    if(c?.id && c?.get('expireMap')){
      let url = getApp().globalData.rootPage || getApp().globalData.defaultTabBar.list[0].pagePath
      //website配置且renew（续费）存在
      if(c.get('expireMap')?.renew && c.get('website')){
        wx.showModal({
          title: '警告',
          content: '当前平台服务器承载已超出阈值，请与技术人员联系。',
          showCancel: false,
          cancelText: '取消',
          cancelColor: '#000000',
          confirmText: '确定',
          confirmColor: '#3CC51F',
          success: (result) => {
            if(result.confirm){
              wx.reLaunch({
                url: `/common-page/pages/loading/index?url=${url}`,
              });
            }
          },
          fail: ()=>{},
          complete: ()=>{}
        });
      }
    }
  },


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

  },

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

  },

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

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let uid = Parse.User.current().id
    return {
      title: '分享',
      // path: `index?invite=${uid}`,
      path: `index`,
      imageUrl: '',
    }
  }
})