<script setup lang="ts">
import { onLaunch, onShow } from '@dcloudio/uni-app'

onLaunch((options) => {
  // #ifdef MP-WEIXIN
  initPrivacy()
  handleLaunchParams(options?.query)
  // #endif
  // #ifdef H5
  handleH5UrlParams()
  // #endif
})

// #ifdef MP-WEIXIN
onShow((options) => {
  if (options?.query) {
    handleLaunchParams(options.query)
  }
})
// #endif

// #ifdef H5
function handleH5UrlParams() {
  const params = new URLSearchParams(window.location.search)
  const token = params.get('token')
  const buy = params.get('buy')
  if (token) {
    uni.setStorageSync('token', token)
  }
  if (buy === 'gravity') {
    // 延迟等 app 初始化完成再跳转
    setTimeout(() => {
      uni.navigateTo({ url: '/pages/member/member' })
    }, 300)
  }
}
// #endif

// #ifdef MP-WEIXIN
function handleLaunchParams(query?: Record<string, string>) {
  if (!query) return
  const token = query.token
  if (token) {
    uni.setStorageSync('token', token)
  }
  const shareCode = query.share || query.shareCode
  if (shareCode) {
    uni.setStorageSync('shareCode', shareCode)
  }
}

function initPrivacy() {
  if (wx.onNeedPrivacyAuthorization) {
    wx.onNeedPrivacyAuthorization((resolve) => {
      uni.showModal({
        title: '隐私政策授权',
        content: '请仔细阅读并同意《用户服务协议》和《隐私政策》后再使用本应用。您的个人信息将仅用于求职服务。',
        confirmText: '同意',
        cancelText: '拒绝',
        success: (res) => {
          if (res.confirm) {
            resolve({ event: 'agree' })
          } else {
            resolve({ event: 'disagree' })
          }
        },
      })
    })
  }
}
// #endif
</script>

<style>
/* ===== Design System - Design Tokens ===== */
page {
  --color-primary: #4F46E5;
  --color-primary-light: #818CF8;
  --color-primary-dark: #3730A3;
  --color-gradient-start: #4F46E5;
  --color-gradient-mid: #7C3AED;
  --color-gradient-end: #A855F7;
  --color-surface: #FFFFFF;
  --color-bg: #F3F4F6;
  --color-text: #111827;
  --color-text-secondary: #6B7280;
  --color-text-tertiary: #9CA3AF;
  --color-border: #E5E7EB;
  --color-success: #10B981;
  --color-warning: #F59E0B;
  --color-error: #EF4444;
  --radius-sm: 12rpx;
  --radius-md: 16rpx;
  --radius-lg: 20rpx;
  --radius-xl: 24rpx;
  --radius-round: 999rpx;
  --shadow-sm: 0 2rpx 8rpx rgba(0,0,0,0.04);
  --shadow-md: 0 4rpx 16rpx rgba(0,0,0,0.06);
  --shadow-lg: 0 8rpx 32rpx rgba(0,0,0,0.08);
  --shadow-purple: 0 6rpx 20rpx rgba(79,70,229,0.25);
  --font-title: 32rpx;
  --font-body: 28rpx;
  --font-caption: 24rpx;
  --font-small: 20rpx;
  --space-xs: 8rpx;
  --space-sm: 12rpx;
  --space-md: 16rpx;
  --space-lg: 24rpx;
  --space-xl: 32rpx;
  --space-2xl: 48rpx;
  /* Safe area */
  --safe-bottom: env(safe-area-inset-bottom, 0px);

  background-color: var(--color-bg);
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'PingFang SC', 'Noto Sans SC', sans-serif;
  font-size: 28rpx;
  color: var(--color-text);
  line-height: 1.6;
}

/* ===== Global Utility Classes ===== */

/* Gradient text */
.gradient-text {
  background: linear-gradient(135deg, var(--color-gradient-start), var(--color-gradient-end));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* Gradient button base */
.btn-gradient {
  background: linear-gradient(135deg, var(--color-gradient-start), var(--color-gradient-mid));
  color: #FFFFFF;
  border-radius: var(--radius-md);
  font-weight: 600;
  transition: all 0.25s ease;
  border: none;
}
.btn-gradient:active { opacity: 0.85; transform: scale(0.97); }
.btn-gradient[disabled] { opacity: 0.5; }

/* Outline button */
.btn-outline {
  background: transparent;
  color: var(--color-primary);
  border: 2rpx solid var(--color-primary);
  border-radius: var(--radius-md);
  font-weight: 500;
}

/* Card base */
.card {
  background: var(--color-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  transition: all 0.25s ease;
}
.card:active { transform: scale(0.98); box-shadow: var(--shadow-md); }

/* Section title */
.section-title {
  font-size: var(--font-title);
  font-weight: 700;
  color: var(--color-text);
}
.section-desc {
  font-size: var(--font-caption);
  color: var(--color-text-tertiary);
}

/* Smooth fade-in */
.fade-in {
  animation: fadeIn 0.4s ease forwards;
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(12rpx); }
  to { opacity: 1; transform: translateY(0); }
}
</style>
