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';
const sev = require('../../service/request')

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
    console.log(options)

    if (path) {
      wx.navigateTo({
        url: path,
      });
    }
  },


  async onChange(event) {
    let currentIndex = event.detail;
   
    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
    })
  },
  /** 更新user的invitePath*/
  async agentParent() {
    if (!Parse.User.current()?.id) return
   await sev.bindShop(Parse.User.current()?.id)
    // wx.request({
    //   url: 'https://server.fmode.cn/api/user/agent/parent',
    //   data: {
    //     uid: Parse.User.current()?.id
    //   },
    //   header: {
    //     'content-type': 'application/json'
    //   },
    //   method: 'POST',
    //   dataType: 'json',
    //   responseType: 'text',
    //   success: async (result) => {
    //     let uid = Parse.User.current()?.id
    //     console.log(result);
    //     let query = new Parse.Query('_User')
    //     let user = await query.get(uid)
    //     let top_uid = user?.get('invitePath')[0]
    //     console.log(top_uid)
    //     if (!top_uid) return
    //     let shop_query = new Parse.Query('ShopStore')
    //     shop_query.equalTo('user', top_uid)
    //     shop_query.notEqualTo('isDeleted', true)
    //     shop_query.equalTo('isVerified', true)
    //     shop_query.select('objectId')
    //     let shop = await shop_query.first()
    //     console.log(shop)
    //     if (!shop?.id) return
    //     user.set('store', {
    //       __type: 'Pointer',
    //       className: 'ShopStore',
    //       objectId: shop?.id
    //     })
    //     await user.save()
    //   },
    //   fail: () => {},
    //   complete: () => {}
    // });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.agentParent()
    this.getRenew()
    let {
      active
    } = this.data
    let componentPage = this.selectComponent(`#comp${active}`)
    if (!componentPage) {
      return
    }
    switch (active) {
      case 0:
        break;
      case 1:
        break;
      case 2:
        break;
      case 3:
        break;
    }
  },

  /* 服务器是否需要续费 */
  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: '',
    }
  }
})