<script setup lang="ts">
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { useI18n } from 'vue-i18n';
import { useAuthStore } from '../stores/auth';
import { useAppLocale } from '../composables/useAppLocale';
import LocaleSwitcher from '../components/LocaleSwitcher.vue';
import RobotVerify from '../components/RobotVerify.vue';
import loginBg from '../assets/images/h5bg.png';

const { t } = useI18n();
const { initFromUser } = useAppLocale();
const auth = useAuthStore();
const router = useRouter();
const captchaRef = ref<InstanceType<typeof RobotVerify> | null>(null);
const username = ref('player1');
const password = ref('Player@123');
const error = ref('');
const loading = ref(false);

async function submit() {
  if (!captchaRef.value?.validate()) {
    error.value = t('auth.captcha_wrong');
    captchaRef.value?.refresh();
    return;
  }
  loading.value = true;
  error.value = '';
  try {
    await auth.login(username.value, password.value);
    initFromUser(auth.user?.locale);
    router.push('/');
  } catch (e: unknown) {
    error.value = (e as { response?: { data?: { error?: string } } })?.response?.data?.error || 'Login failed';
  } finally {
    loading.value = false;
  }
}
</script>

<template>
  <div class="login-page" :style="{ backgroundImage: `url(${loginBg})` }">
    <div class="login-lang">
      <LocaleSwitcher compact />
    </div>
    <form @submit.prevent="submit" class="login-form ps-gold-frame">
      <label>{{ t('auth.username') }}</label>
      <input v-model="username" class="ps-gold-input" required />
      <label>{{ t('auth.password') }}</label>
      <input v-model="password" class="ps-gold-input" type="password" required />
      <RobotVerify ref="captchaRef" />
      <p v-if="error" class="error">{{ error }}</p>
      <button type="submit" class="btn-login btn-gold-outline" :disabled="loading">
        {{ t('auth.login') }}
      </button>
    </form>
  </div>
</template>

<style scoped>
.login-lang {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  right: 16px;
  z-index: 2;
}

.login-page {
  position: relative;
  height: 100%;
  min-height: 100dvh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  padding: 34vh 20px calc(15vh + max(28px, env(safe-area-inset-bottom)));
  background-color: var(--tertiary);
  background-size: cover;
  background-position: center top;
  background-repeat: no-repeat;
}

.login-form {
  width: 100%;
  max-width: 340px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px;
}

label {
  font-size: 11px;
  color: var(--text-muted);
  font-weight: 600;
  letter-spacing: 0.04em;
}

.btn-login {
  margin-top: 4px;
  padding: 10px 14px;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 800;
  letter-spacing: 0.06em;
}

.btn-login:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.error {
  color: var(--danger);
  font-size: 13px;
  font-weight: 600;
}
</style>
