<template>
    <div class="signup-container">
      <div class="logo-container">
        <img src="@/assets/logo.png" alt="WorldCinema" class="logo-image">
        <h1 class="logo-text">WorldCinema</h1>
      </div>
  
      <form @submit.prevent="handleSubmit" class="signup-form">
        <input
          type="text"
          v-model="userData.firstName"
          placeholder="Имя"
          required
          class="input"
        >
  
        <input
          type="text"
          v-model="userData.lastName"
          placeholder="Фамилия"
          required
          class="input"
        >
  
        <input
          type="email"
          v-model="userData.email"
          placeholder="E-mail"
          required
          class="input"
        >
  
        <input
          type="password"
          v-model="userData.password"
          placeholder="Пароль"
          required
          class="input"
        >
  
        <input
          type="password"
          v-model="passwordConfirm"
          placeholder="Повторите пароль"
          required
          class="input"
        >
  
        <button
          type="submit"
          class="signup-button"
          :disabled="loading || !isPasswordsMatch"
        >
          Зарегистрироваться
        </button>
  
        <router-link to="/signin" class="login-link">
          У меня уже есть аккаунт
        </router-link>
  
        <p v-if="error" class="error-message">{{ error }}</p>
      </form>
    </div>
  </template>
  
  <script setup>
  import { ref, computed } from 'vue'
  import { useRouter } from 'vue-router'
  import { authAPI } from '@/services/api'
  
  const router = useRouter()
  const loading = ref(false)
  const error = ref('')
  const passwordConfirm = ref('')
  
  const userData = ref({
    firstName: '',
    lastName: '',
    email: '',
    password: ''
  })
  
  const isPasswordsMatch = computed(() => {
    return userData.value.password === passwordConfirm.value
  })
  
  const handleSubmit = async () => {
    if (!isPasswordsMatch.value) {
      error.value = 'Пароли не совпадают'
      return
    }
  
    try {
      loading.value = true
      error.value = ''
  
      await authAPI.register({
        firstName: userData.value.firstName,
        lastName: userData.value.lastName,
        email: userData.value.email,
        password: userData.value.password
      })
  
      const response = await authAPI.login(userData.value.email, userData.value.password)
  
      if (response && response.token) {
        router.push({ name: 'MainScreen' })
      }
    } catch (err) {
      console.error('Ошибка регистрации:', err)
      error.value = 'Ошибка при регистрации. Пожалуйста, проверьте введенные данные.'
    } finally {
      loading.value = false
    }
  }
  </script>
  
  <style scoped>
  .signup-container {
    min-height: 100vh;
    background-color: #000000;
    padding: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }
  
  .logo-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 48px;
  }
  
  .logo-image {
    width: 120px;
    height: 120px;
    margin-bottom: 16px;
  }
  
  .logo-text {
    color: #EF3A01;
    font-size: 24px;
    font-weight: bold;
  }
  
  .signup-form {
    width: 100%;
    max-width: 320px;
    display: flex;
    flex-direction: column;
    gap: 16px;
  }
  
  .input {
    width: 100%;
    padding: 12px 16px;
    background: transparent;
    border: 1px solid rgba(255, 255, 255, 0.3);
    border-radius: 4px;
    color: white;
    font-size: 16px;
  }
  
  .input::placeholder {
    color: rgba(255, 255, 255, 0.5);
  }
  
  .signup-button {
    width: 100%;
    padding: 12px;
    background: #2c74a4;
    border: none;
    border-radius: 4px;
    color: white;
    font-size: 16px;
    cursor: pointer;
    margin-top: 8px;
  }
  
  .signup-button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }
  
  .login-link {
    color: #2c74a4;
    text-align: center;
    text-decoration: none;
    font-size: 14px;
    margin-top: 8px;
  }
  
  .error-message {
    color: #FF3B30;
    text-align: center;
    font-size: 14px;
  }
  
  /* Статус бар */
  .status-bar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 44px;
    padding: 0 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: white;
  }
  
  .time {
    font-weight: 600;
  }
  
  .icons {
    display: flex;
    gap: 4px;
  }
  </style>
  
  