// DEMO 演示版 — 点击登录模拟成功，写入 mock 用户信息后跳转主页

Page({
  data: {
    agreed: false,
    loading: false,
    /** 系统状态栏高度（px），用于自定义导航栏顶部偏移 */
    statusBarHeight: 20,
  },

  onLoad() {
    const windowInfo = wx.getWindowInfo()
    this.setData({ statusBarHeight: windowInfo.statusBarHeight || 20 })
  },

  onAgreeChange() {
    this.setData({ agreed: !this.data.agreed })
  },

  async onLogin() {
    if (!this.data.agreed) {
      wx.showToast({ title: "请先同意用户协议", icon: "none" })
      return
    }
    if (this.data.loading) return

    this.setData({ loading: true })

    // DEMO：模拟登录成功，写入 mock 用户信息
    const mockToken = "demo_token_" + Date.now()
    const mockUser = {
      userId: "demo_user_001",
      status: "approved",
      nickname: "小燕",
      role: "coach",
      coachLevel: "senior",
      storeName: "朗朗桌球·旗舰店",
      avatar: "",
    }

    const app = getApp<IAppOption>()
    app.globalData.token = mockToken
    app.globalData.refreshToken = "demo_refresh_token"
    app.globalData.authUser = mockUser

    wx.setStorageSync("token", mockToken)
    wx.setStorageSync("refreshToken", "demo_refresh_token")
    wx.setStorageSync("userId", mockUser.userId)
    wx.setStorageSync("userStatus", mockUser.status)
    wx.setStorageSync("userRole", mockUser.role)
    wx.setStorageSync("storeName", mockUser.storeName)
    wx.setStorageSync("coachLevel", mockUser.coachLevel)
    wx.setStorageSync("avatar", mockUser.avatar)

    wx.showToast({ title: "登录成功", icon: "success" })

    // 500ms 后跳转主页
    setTimeout(() => {
      this.setData({ loading: false })
      wx.reLaunch({ url: "/pages/task-list/task-list" })
    }, 500)
  },
})
