<template>
  <div class="page login">
    <div class="login__hero">
      <div class="login__logo">校</div>
      <h1 class="login__brand">校跑侠</h1>
      <p class="login__slogan">校园跑腿 · 代拿代买 · 表白墙</p>
    </div>

    <div class="login__card">
      <div class="login__tabs">
        <button :class="{ 'is-active': mode === 'login' }" @click="mode = 'login'">登录</button>
        <button :class="{ 'is-active': mode === 'register' }" @click="mode = 'register'">注册</button>
      </div>

      <div class="login__form">
        <van-field
          v-if="mode === 'register'"
          v-model="form.name"
          label="昵称"
          placeholder="怎么称呼你"
          maxlength="12"
        />
        <van-field v-model="form.phone" label="手机号" type="tel" placeholder="请输入手机号" maxlength="11" />
        <van-field v-model="form.password" label="密码" type="password" placeholder="至少 6 位" maxlength="20" />
        <div v-if="mode === 'register'" class="login__campus">
          <span class="login__campus-label">校区</span>
          <div class="login__chips">
            <button
              v-for="c in campuses"
              :key="c"
              class="chip"
              :class="{ 'is-on': form.campus === c }"
              @click="form.campus = c"
            >
              {{ c }}
            </button>
          </div>
        </div>
      </div>

      <van-button block round type="primary" :loading="loading" @click="submit">
        {{ mode === 'login' ? '登 录' : '注册并登录' }}
      </van-button>

      <p class="login__switch">
        <template v-if="mode === 'login'">还没有账号？<a @click="mode = 'register'">去注册</a></template>
        <template v-else>已有账号？<a @click="mode = 'login'">去登录</a></template>
      </p>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { showToast } from 'vant'
import { login, register } from '../store/session.js'

const router = useRouter()
const route = useRoute()

const mode = ref('login')
const loading = ref(false)
const campuses = ['东校区', '西校区', '南校区']
const form = reactive({ name: '', phone: '', password: '', campus: '东校区' })

function validate() {
  if (!/^1\d{10}$/.test(form.phone.trim())) {
    showToast('请输入正确的手机号')
    return false
  }
  if (form.password.length < 6) {
    showToast('密码至少 6 位')
    return false
  }
  if (mode.value === 'register' && !form.name.trim()) {
    showToast('请填写昵称')
    return false
  }
  return true
}

async function submit() {
  if (!validate()) return
  loading.value = true
  try {
    if (mode.value === 'login') {
      await login(form.phone.trim(), form.password)
    } else {
      await register({
        name: form.name.trim(),
        phone: form.phone.trim(),
        password: form.password,
        campus: form.campus
      })
    }
    showToast(mode.value === 'login' ? '登录成功' : '注册成功')
    router.replace(route.query.redirect || '/home')
  } catch (e) {
    showToast(e.message || '操作失败')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login {
  min-height: 100vh;
  background: var(--brand-bg);
}
.login__hero {
  background: linear-gradient(135deg, #1677ff, #0958d9);
  padding: calc(env(safe-area-inset-top) + 48px) 20px 60px;
  text-align: center;
  color: #fff;
}
.login__logo {
  width: 60px;
  height: 60px;
  margin: 0 auto 14px;
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.2);
  border: 2px solid rgba(255, 255, 255, 0.6);
  font-size: 28px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login__brand {
  margin: 0;
  font-size: 26px;
  font-weight: 800;
}
.login__slogan {
  margin: 8px 0 0;
  font-size: 13px;
  opacity: 0.9;
}
.login__card {
  margin: -36px 18px 0;
  background: var(--brand-surface);
  border-radius: var(--brand-radius-lg);
  box-shadow: var(--brand-shadow-md);
  padding: 8px 16px 22px;
  position: relative;
}
.login__tabs {
  display: flex;
  gap: 24px;
  padding: 14px 4px 6px;
}
.login__tabs button {
  border: none;
  background: none;
  font-size: 17px;
  color: var(--brand-text-tertiary);
  font-weight: 600;
  padding: 6px 0;
  position: relative;
  cursor: pointer;
}
.login__tabs button.is-active {
  color: var(--brand-text);
  font-weight: 800;
}
.login__tabs button.is-active::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 3px;
  border-radius: 2px;
  background: var(--brand-primary);
}
.login__form {
  margin: 6px 0 18px;
}
.login__form :deep(.van-cell) {
  padding-left: 0;
  padding-right: 0;
}
.login__campus {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 0 4px;
}
.login__campus-label {
  color: var(--brand-text-secondary);
  font-size: 14px;
}
.login__chips {
  display: flex;
  gap: 10px;
}
.chip {
  border: 1px solid var(--brand-border);
  background: #fff;
  color: var(--brand-text-secondary);
  border-radius: 16px;
  padding: 6px 14px;
  font-size: 13px;
  cursor: pointer;
}
.chip.is-on {
  border-color: var(--brand-primary);
  color: var(--brand-primary);
  background: var(--brand-primary-lighter);
  font-weight: 600;
}
.login__switch {
  margin: 16px 0 0;
  text-align: center;
  font-size: 13px;
  color: var(--brand-text-tertiary);
}
.login__switch a {
  color: var(--brand-primary);
  font-weight: 600;
}
</style>
