<template>
  <div class="auth-container">
    <div class="page-container auth-form">
      <h1>Вход</h1>
      <form @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="email" class="form-label">Email</label>
          <input type="email" id="email" v-model="email" class="form-control" required />
        </div>
        <div class="form-group">
          <label for="password" class="form-label">Пароль</label>
          <input type="password" id="password" v-model="password" class="form-control" required />
        </div>
        <div v-if="error" class="error-message">{{ error }}</div>
        <button type="submit" :disabled="loading" class="btn btn-primary">
          {{ loading ? 'Входим...' : 'Войти' }}
        </button>
      </form>
      <div class="switch-link">
        <p>
          
          <NuxtLink to="/">Вернуться назад</NuxtLink>
        </p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const { login } = useAuth();
const email = ref('');
const password = ref('');
const error = ref(null);
const loading = ref(false);

const handleLogin = async () => {
  loading.value = true;
  error.value = null;
  try {
    await login(email.value, password.value);
    await navigateTo('/');
  } catch (err) {
    error.value = err.data?.message || 'An error occurred during login.';
  } finally {
    loading.value = false;
  }
};

definePageMeta({
  layout: 'login',
});
</script>

<style scoped>
.auth-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  width: 100%;
}
.auth-form {
  width: 100%;
  max-width: 420px;
}
.auth-form button {
  width: 100%;
}
.error-message {
  color: var(--danger-color);
  background-color: #fee2e2;
  padding: 1rem;
  border-radius: 0.375rem;
  margin-bottom: 1rem;
  text-align: center;
}
.switch-link {
  margin-top: 20px;
  text-align: center;
}
</style>