cl<template>
  <div class="auth-page">
    <div class="auth-container">
      <div class="auth-card">
        <div class="card">
          <div class="card-header text-center">
            <h1 class="auth-title">Вход в систему</h1>
          </div>

          <div class="card-body">
            <client-only>
              <form class="auth-form" @submit="onSubmit">
                <div class="form-group">
                  <label for="email" class="form-label">Email</label>
                  <input 
                  id="email" 
                  v-model="email" 
                  type="email" 
                  class="form-input" 
                  :class="{ 'error': errors.email }"
                  placeholder="your@email.com" 
                  autocomplete="email">
                  <span 
                  v-if="errors.email" 
                  class="error-message">
                    {{ errors.email }}
                  </span>
                </div>

                <div class="form-group">
                  <label for="password" class="form-label">Пароль</label>
                  <input 
                  id="password" 
                  v-model="password" 
                  type="password" 
                  class="form-input"
                  :class="{ 'error': errors.password }" 
                  placeholder="Введите пароль" 
                  autocomplete="current-password">
                  <span v-if="errors.password" class="error-message">
                    {{ errors.password }}
                  </span>
                </div>

                <button 
                type="submit" 
                class="btn btn-primary auth-button"
                :disabled="isSubmitting || auth.loading.value">
                  <span v-if="isSubmitting || auth.loading.value">Вход...</span>
                  <span v-else>Войти</span>
                </button>
              </form>
            </client-only>
          </div>

          <div class="card-footer text-center">
            <p class="auth-footer-text">
              Нет аккаунта?
              <NuxtLink to="/auth/register" class="auth-link">
                Зарегистрируйтесь
              </NuxtLink>
            </p>
            <NuxtLink to="/" class="auth-link home-link">
              ← На главную
            </NuxtLink>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useField, useForm } from 'vee-validate'
import { loginSchema } from '~/schemas/auth'
import type { LoginForm } from '~/types/auth'

definePageMeta({
  middleware: 'auth',
  layout: 'auth'
})

const auth = useAuth()
const { handleSubmit, errors, isSubmitting } = useForm({
  validationSchema: loginSchema
})

const { value: email } = useField('email')
const { value: password } = useField('password')

const onSubmit = handleSubmit(async (values) => {
  try {
    await auth.login(values as LoginForm)
    await navigateTo('/profile')
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
  } catch (error: any) {
    console.error('Login error:', error)

    if (error.status === 401) {
      alert('Неверный email или пароль')
    } else if (error.status === 422) {
      alert('Неверные данные для входа')
    } else {
      alert('Произошла ошибка при входе. Попробуйте позже.')
    }
  }
})
</script>

<style scoped>
.auth-page {
  min-height: 100vh;
  background: var(--bg-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-md);
}

.auth-container {
  width: 100%;
  max-width: 400px;
}

.auth-card {
  width: 100%;
}

.card {
  background: var(--bg-primary);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  border: 1px solid var(--border-light);
  overflow: hidden;
}

.card-header {
  padding: var(--space-xl) var(--space-xl) 0;
}

.card-body {
  padding: var(--space-lg) var(--space-xl);
}

.card-footer {
  padding: 0 var(--space-xl) var(--space-xl);
  border-top: 1px solid var(--border-light);
  background: var(--bg-secondary);
}

.auth-title {
  font-family: var(--font-heading);
  font-size: var(--text-2xl);
  font-weight: var(--font-semibold);
  color: var(--text-primary);
  margin-bottom: var(--space-sm);
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: var(--space-md);
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
}

.form-label {
  font-weight: var(--font-medium);
  color: var(--text-secondary);
  font-size: var(--text-sm);
}

.form-input {
  width: 100%;
  padding: var(--space-sm) var(--space-md);
  border: 1px solid var(--border-medium);
  border-radius: var(--radius-md);
  font-size: var(--text-base);
  background: var(--bg-primary);
  color: var(--text-primary);
  transition: all 0.2s;
}

.form-input:focus {
  outline: none;
  border-color: var(--primary-500);
  box-shadow: 0 0 0 3px rgb(14 165 233 / 0.1);
}

.form-input.error {
  border-color: var(--error-500);
  box-shadow: 0 0 0 3px rgb(239 68 68 / 0.1);
}

.error-message {
  color: var(--error-500);
  font-size: var(--text-sm);
  margin-top: var(--space-xs);
  display: block;
}

.auth-button {
  width: 100%;
  margin-top: var(--space-sm);
  padding: var(--space-sm) var(--space-md);
  font-size: var(--text-base);
}

.auth-button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.auth-footer-text {
  color: var(--text-secondary);
  margin-bottom: var(--space-md);
  font-size: var(--text-sm);
}

.auth-link {
  color: var(--primary-600);
  font-weight: var(--font-medium);
  text-decoration: none;
  font-size: var(--text-sm);
  transition: color 0.2s;
}

.auth-link:hover {
  color: var(--primary-700);
  text-decoration: underline;
}

.home-link {
  display: inline-block;
  margin-top: var(--space-sm);
  font-size: var(--text-sm);
}

/* Адаптивность для мобильных устройств */
@media (max-width: 640px) {
  .auth-container {
    max-width: 100%;
  }

  .card-header {
    padding: var(--space-lg) var(--space-lg) 0;
  }

  .card-body {
    padding: var(--space-md) var(--space-lg);
  }

  .card-footer {
    padding: 0 var(--space-lg) var(--space-lg);
  }

  .auth-title {
    font-size: var(--text-xl);
  }
}

/* Для очень маленьких экранов */
@media (max-width: 380px) {
  .auth-page {
    padding: var(--space-sm);
  }

  .card-header {
    padding: var(--space-md) var(--space-md) 0;
  }

  .card-body {
    padding: var(--space-sm) var(--space-md);
  }

  .card-footer {
    padding: 0 var(--space-md) var(--space-md);
  }
}

/* Темная тема специфические стили */
:global([data-theme="dark"]) .auth-page {
  background: var(--bg-primary);
}

:global([data-theme="dark"]) .card {
  background: var(--bg-secondary);
  border-color: var(--border-light);
}

:global([data-theme="dark"]) .card-footer {
  background: var(--bg-tertiary);
}

:global([data-theme="dark"]) .form-input {
  background: var(--bg-primary);
  border-color: var(--border-medium);
  color: var(--text-primary);
}

:global([data-theme="dark"]) .form-input:focus {
  border-color: var(--primary-400);
  box-shadow: 0 0 0 3px rgb(56 189 248 / 0.1);
}
</style>