<template>
  <div class="page-container login-container">
    <div class="login-box">
      <h1 class="login-title">账单</h1>
      <div class="login-form">
        <van-field
          v-model="password"
          type="password"
          placeholder="请输入密码"
          clearable
          :border="true"
          @keyup.enter="handleLogin"
        >
          <template #left-icon>
            <van-icon name="lock" />
          </template>
        </van-field>
        
        <van-button
          type="primary"
          block
          round
          :loading="loading"
          class="login-button"
          @click="handleLogin"
        >
          登录
        </van-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { showToast } from 'vant'
import { useAuthStore } from '@/stores/auth'

const router = useRouter()
const authStore = useAuthStore()

const password = ref('')
const loading = ref(false)

const handleLogin = async () => {
  if (!password.value) {
    showToast('请输入密码')
    return
  }

  loading.value = true
  try {
    await authStore.login(password.value)
    showToast({ type: 'success', message: '登录成功' })
    router.push('/calendar')
  } catch (error) {
    showToast({ type: 'fail', message: error.message || '登录失败' })
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-box {
  width: 90%;
  max-width: 400px;
  border-radius: 20px;
  padding: 40px 30px;
}

.login-title {
  text-align: center;
  font-size: 28px;
  font-weight: bold;
  margin-bottom: 40px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.login-button {
  margin-top: 10px;
  height: 44px;
  font-size: 16px;
}

:deep(.van-field) {
  border-radius: 12px;
  padding: 12px 16px;
}

:deep(.van-field__control) {
  font-size: 15px;
}
</style>
