<script setup lang="ts">
import { reactive, shallowRef } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useAuthStore } from '@/stores/auth'

const router = useRouter()
const auth = useAuthStore()
const loading = shallowRef(false)
const form = reactive({
  email: '',
  password: '',
})

async function submit() {
  loading.value = true
  try {
    await auth.login(form.email, form.password)
    ElMessage.success('登录成功')
    await router.push('/quiz')
  } finally {
    loading.value = false
  }
}

</script>

<template>
  <main class="login-page">
    <section class="login-panel">
      <div class="login-copy">
        <p class="eyebrow">QuickQuiz</p>
        <h1>把题库变成每天能继续的练习。</h1>
        <p>导入、授权、刷题、复盘都在一个清爽的工作台里完成。</p>
      </div>
      <ElForm class="login-form" :model="form" label-position="top" @submit.prevent="submit">
        <ElFormItem label="邮箱">
          <ElInput v-model="form.email" autocomplete="username" />
        </ElFormItem>
        <ElFormItem label="密码">
          <ElInput v-model="form.password" type="password" autocomplete="current-password" show-password />
        </ElFormItem>
        <ElButton type="primary" :loading="loading" class="w-full" @click="submit">登录</ElButton>
        <div class="login-links">
          <RouterLink to="/register">邀请码注册</RouterLink>
        </div>
      </ElForm>
    </section>
  </main>
</template>

<style scoped>
.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-panel {
  width: min(920px, 100%);
  display: grid;
  grid-template-columns: 1.1fr 0.9fr;
  border: 1px solid var(--qq-line);
  border-radius: 8px;
  overflow: hidden;
  background: white;
  box-shadow: 0 20px 70px rgba(23, 33, 27, 0.10);
}

.login-copy {
  padding: 44px;
  background: linear-gradient(135deg, rgba(31, 111, 91, 0.12), rgba(65, 87, 199, 0.08));
}

.eyebrow {
  margin: 0 0 18px;
  color: var(--qq-moss);
  font-weight: 800;
}

.login-copy h1 {
  margin: 0 0 16px;
  font-size: 36px;
  line-height: 1.12;
  letter-spacing: 0;
}

.login-copy p {
  color: var(--qq-muted);
}

.login-form {
  padding: 44px;
}

.login-links {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin-top: 14px;
  font-size: 14px;
}

.login-links a,
.login-links button {
  border: 0;
  padding: 0;
  background: transparent;
  color: var(--qq-moss);
  text-decoration: none;
  cursor: pointer;
}

@media (max-width: 720px) {
  .login-panel {
    grid-template-columns: 1fr;
  }
  .login-copy,
  .login-form {
    padding: 28px;
  }
  .login-copy h1 {
    font-size: 28px;
  }
}
</style>
