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

const isLogin = ref(true);
const login = ref('');
const password = ref('');
const firstName = ref('');
const lastName = ref('');
const phone = ref('');
const email = ref('');
const message = ref('');

const handleSubmit = async () => {
  const url = isLogin.value ? '/api/login' : '/api/login/register';
  const payload = {
    login: login.value,
    password: password.value,
    ...(isLogin.value ? {} : { firstName: firstName.value }),
    ...(isLogin.value ? {} : { lastName: lastName.value }),
    ...(isLogin.value ? {} : { phone: phone.value }),
    ...(isLogin.value ? {} : { email: email.value })
  };

  try {
    const response = await fetch(`http://localhost:3000${url}`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(payload)
    });

    if (!response.ok) throw new Error(await response.text());

    if (isLogin.value) {
      const token = await response.json();
      localStorage.setItem('jwt_token', token);
      alert('Вход выполнен!');
      location.reload();
    } else {
      alert('Регистрация завершена! Теперь войдите.');
      isLogin.value = true;
    }
  } catch (e) {
    message.value = e.message;
  }
};
</script>

<template>
  <div class="tab-content"> 
    <h2>{{ isLogin ? 'Вход' : 'Регистрация' }}</h2>
    <form @submit.prevent="handleSubmit">
      <input v-model="login" placeholder="Логин" required />
      <input v-model="password" type="password" placeholder="Пароль" required />
      <input v-if="!isLogin" v-model="firstName" placeholder="Имя" />
      <input v-if="!isLogin" v-model="lastName" placeholder="Фамилия" />
      <input v-if="!isLogin" v-model="email" type="email" placeholder="e-mail" />
      <input v-if="!isLogin" v-model="phone" type="tel" placeholder="Телефон" />
      
      <button type="submit">{{ isLogin ? 'Войти' : 'Создать аккаунт' }}</button>
    </form>
    
    <p @click="isLogin = !isLogin" class="toggle-link">
      {{ isLogin ? 'Нет аккаунта? Регистрация' : 'Уже есть аккаунт? Войти' }}
    </p>
    <p v-if="message" class="error">{{ message }}</p>
  </div>
</template>

<style scoped>
h2 {
  font-size: 28px;
  font-weight: 800;
  margin-bottom: 30px;
  text-align: center;
}

form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

input {
  padding: 14px 16px;
  border: 1px solid #ddd;
  border-radius: 12px;
  font-size: 16px;
  font-family: inherit;
  transition: all 0.2s ease;
}

input:focus {
  outline: none;
  border-color: #2BD84C;
  box-shadow: 0 0 0 3px rgba(43, 216, 76, 0.1);
}

input::placeholder {
  color: #999;
}

button {
  padding: 16px;
  background: linear-gradient(135deg, #2BD84C, #1fa838);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s ease;
  margin-top: 10px;
  box-shadow: 0 4px 12px rgba(43, 216, 76, 0.2);
}

button:active {
  transform: scale(0.98);
}

button:hover {
  box-shadow: 0 6px 16px rgba(43, 216, 76, 0.3);
}

.toggle-link {
  color: #2BD84C;
  cursor: pointer;
  text-decoration: none;
  margin-top: 20px;
  text-align: center;
  font-weight: 600;
  font-size: 14px;
  transition: all 0.2s ease;
}

.toggle-link:hover {
  text-decoration: underline;
}

.error {
  color: #FF4B4B;
  text-align: center;
  font-weight: 600;
  padding: 12px;
  background: #FFF0F0;
  border-radius: 8px;
  font-size: 14px;
  margin-top: 10px;
}

/* Планшет и ПК */
@media (min-width: 768px) {
  h2 {
    font-size: 36px;
    margin-bottom: 40px;
  }

  form {
    gap: 16px;
    max-width: 500px;
    margin: 0 auto;
  }

  input {
    padding: 16px 18px;
    font-size: 16px;
    border-radius: 14px;
  }

  button {
    padding: 18px;
    font-size: 18px;
    border-radius: 14px;
    margin-top: 15px;
  }

  .toggle-link {
    font-size: 16px;
    margin-top: 24px;
  }
}

/* Большие ПК */
@media (min-width: 1024px) {
  h2 {
    font-size: 40px;
  }

  form {
    max-width: 600px;
  }

  input {
    padding: 18px 20px;
    font-size: 18px;
  }

  button {
    padding: 20px;
    font-size: 18px;
  }
}
</style>