// pages/leader/leader.js
import { createPage, request, syncRoleStatuses, syncProfileFields, ensureRoleOnCenterPage, isCenterPageActive, parseSceneOptions } from '../../utils/base-page.js'

Page(createPage({
  data: {
    isZuzhang: false,

    // 邀请码
    inviteCode: '',

    // 用户信息
    uid: '',
    nicheng: '',
    avatarUrl: '',

    // 组长信息
    ketixian: '0.00',
    guanshiCount: 0,
    fenhongZonge: '0.00',
  },

  onLoad(options) {
    const parsed = parseSceneOptions(options)
    if (parsed.inviteCode) {
      this.setData({ inviteCode: parsed.inviteCode })
    }
    wx.redirectTo({ url: '/pages/fighter/fighter' })
    return
    // 以下保留供恢复参考
    this.checkLoginStatus()
  },

  onShow() {
    this.registerNotificationComponent()
    this.checkColdStartPopup('zuzhangduan')
    if (isCenterPageActive(this, 'isZuzhang', 'zuzhangstatus')) {
      if (!this.data.isZuzhang) this.setData({ isZuzhang: true })
      ensureRoleOnCenterPage(this, 'zuzhang')
      this.getZuzhangInfo()
    }
  },

  // 检查登录状态
  checkLoginStatus() {
    const token = wx.getStorageSync('token')
    if (!token) {
      wx.showModal({
        title: '提示',
        content: '您尚未登录，请先登录',
        confirmText: '去登录',
        success: (res) => {
          if (res.confirm) {
            wx.reLaunch({ url: '/pages/gerenzhongxin/gerenzhongxin' })
          }
        }
      })
      return false
    }
    this.checkRoleStatus()
    return true
  },

  // 角色状态检查后的额外数据加载（由 mixin 的 checkRoleStatus 调用）
  checkGlobalData() {
    this.loadUserInfo()
    this.getZuzhangInfo()
  },

  // 获取组长信息（刷新）
  async getZuzhangInfo() {
    this.loadData({
      url: '/yonghu/zuzhangxinxi',
      method: 'POST',
      errorMsg: '获取信息失败',
      onSuccess: (data) => {
        this.setData({
          ketixian: data.ketixian || '0.00',
          guanshiCount: data.guanshiCount || 0,
          fenhongZonge: data.fenhongZonge || '0.00',
        })
        wx.showToast({ title: '刷新成功', icon: 'success', duration: 1500 })
      }
    })
  },

  // 刷新按钮点击
  refreshZuzhangInfo() {
    this.throttledRefresh(this.getZuzhangInfo)
  },

  // 邀请码输入
  onInviteCodeInput(e) {
    this.setData({ inviteCode: e.detail.value.trim() })
  },

  // 注册组长
  onRegister() {
    const { inviteCode } = this.data
    this.registerWithInviteCode({
      inviteCode,
      apiUrl: '/yonghu/zuzhangzhuce',
      role: 'zuzhang',
      statusKey: 'zuzhangstatus',
      successMsg: '注册成功！',
      onSuccess: (userData) => {
        this.setData({
          isZuzhang: true,
          ketixian: userData.ketixian || '0.00',
          guanshiCount: userData.guanshiCount || 0,
          fenhongZonge: userData.fenhongZonge || '0.00',
        })
        this.loadUserInfo()
        ensureRoleOnCenterPage(this, 'zuzhang')
      }
    })
  },

  // 跳转到提现页面
  goToWithdraw() {
    const { ketixian } = this.data
    wx.navigateTo({
      url: `/pages/withdraw/withdraw?from=zuzhang&amount=${ketixian}`
    })
  },

  // 跳转到分红记录
  goToFenhongJilu() {
    wx.navigateTo({ url: '/pages/leader-bonus-log/leader-bonus-log' })
  },

  // 跳转到邀请管事
  goToYaoqingGuanshi() {
    wx.navigateTo({ url: '/pages/invite-manager/invite-manager' })
  }
}, { roleConfig: { role: 'dashou', statusKey: 'zuzhangstatus', dataFlag: 'isZuzhang', pageId: 'zuzhangduan', apiUrl: '/yonghu/zuzhangxinxi' } }))
