// app.ts
import { API } from './config/api';

interface IAppInstance {
  globalData: Record<string, any>;
  showEnvironmentTip: () => void;
}

App<IAppOption & IAppInstance>({
  globalData: {},
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 显示当前环境提示
    this.showEnvironmentTip();

    // 登录
    wx.login({
      success: res => {
        console.log(res.code)
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      },
    })
  },

  // 显示环境提示
  showEnvironmentTip() {
    try {
      const accountInfo = wx.getAccountInfoSync();
      const envVersion = accountInfo.miniProgram.envVersion;
      
      let envName = '';
      let envColor = '#07c160';
      
      switch (envVersion) {
        case 'develop':
          envName = '开发环境';
          envColor = '#52c41a';
          break;
        case 'trial':
          envName = '体验环境';
          envColor = '#faad14';
          break;
        case 'release':
          envName = '生产环境';
          envColor = '#1677ff';
          break;
        default:
          envName = '未知环境';
      }
      
      // 输出到控制台
      console.log(`[App] 当前环境: ${envName}`);
      console.log(`[App] 后端地址: ${API.baseURL}`);
      
      // 显示弹窗提示 (仅开发和体验环境)
      if (envVersion === 'develop' || envVersion === 'trial') {
        wx.showModal({
          title: '环境提示',
          content: `当前运行在${envName}\n后端地址: ${API.baseURL}`,
          showCancel: false,
          confirmText: '知道了',
          confirmColor: envColor
        });
      }
    } catch (error) {
      console.error('[App] 获取环境信息失败:', error);
    }
  }
})