<script setup lang="ts">
import { ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import api from '../api';
import { ElMessage } from 'element-plus';
import { useAuthStore, type StaffUser } from '../stores/auth';
import RobotVerify from '../components/RobotVerify.vue';
import AdminLocaleSwitcher from '../components/AdminLocaleSwitcher.vue';
import { useAdminLocale } from '../composables/useAdminLocale';
import bgImage from '../assets/images/bg.png';

const router = useRouter();
const route = useRoute();
const auth = useAuthStore();
const { t } = useAdminLocale();

const form = ref({ username: '', password: '' });
const loading = ref(false);
const captchaRef = ref<InstanceType<typeof RobotVerify> | null>(null);
const isDev = import.meta.env.DEV;

async function quickLogin(username: string, password: string) {
  loading.value = true;
  try {
    const { data } = await api.post('/manage/auth/login', { username, password });
    const payload = data.data as { token: string; user: StaffUser };
    auth.setSession(payload.token, payload.user);
    router.push((route.query.redirect as string) || '/');
  } catch {
    ElMessage.error(t('login.err_quick'));
  } finally {
    loading.value = false;
  }
}

async function login() {
  if (!captchaRef.value?.validate()) {
    ElMessage.error(t('login.err_captcha'));
    captchaRef.value?.refresh();
    return;
  }
  loading.value = true;
  try {
    const { data } = await api.post('/manage/auth/login', form.value);
    const payload = data.data as { token: string; user: StaffUser };
    auth.setSession(payload.token, payload.user);
    router.push((route.query.redirect as string) || '/');
  } catch {
    ElMessage.error(t('login.err_failed'));
    captchaRef.value?.refresh();
  } finally {
    loading.value = false;
  }
}
</script>

<template>
  <div class="login-page" :style="{ backgroundImage: `url(${bgImage})` }">
    <div class="login-mask" />
    <div class="login-wrap">
      <form @submit.prevent="login" class="login-form" autocomplete="off">
        <div class="form-lang">
          <AdminLocaleSwitcher variant="gold" />
        </div>

        <div class="form-body">
          <aside class="form-brand">
            <img src="/logo.png" alt="TheBet365" class="logo-large" />
          </aside>

          <div class="form-fields">
            <h2 class="title">{{ t('login.title') }}</h2>

            <label>{{ t('login.username') }}</label>
            <input v-model="form.username" class="field" :placeholder="t('login.username_ph')" autocomplete="off" required />
            <label>{{ t('login.password') }}</label>
            <input v-model="form.password" class="field" type="password" :placeholder="t('login.password_ph')" autocomplete="off" required />

            <RobotVerify ref="captchaRef" />

            <button type="submit" class="btn-login" :disabled="loading">
              {{ loading ? '...' : t('login.submit') }}
            </button>

            <template v-if="isDev">
              <div class="quick-label">{{ t('login.quick_label') }}</div>
              <div class="quick-btns">
                <button type="button" class="quick-btn" :disabled="loading" @click="quickLogin('admin', 'Admin@123')">
                  <span class="quick-role">{{ t('login.quick_admin') }}</span>
                  <span class="quick-acc">admin</span>
                </button>
                <button type="button" class="quick-btn" :disabled="loading" @click="quickLogin('agent1', 'Agent@123')">
                  <span class="quick-role">{{ t('login.quick_agent') }}</span>
                  <span class="quick-acc">agent1</span>
                </button>
                <button type="button" class="quick-btn" :disabled="loading" @click="quickLogin('agent2', 'Agent@123')">
                  <span class="quick-role">{{ t('login.quick_agent2') }}</span>
                  <span class="quick-acc">agent2</span>
                </button>
              </div>
            </template>
          </div>
        </div>
      </form>
    </div>
  </div>
</template>

<style scoped>
.login-page {
  min-height: 100vh;
  background-color: #000;
  background-size: cover;
  background-position: center top;
  background-repeat: no-repeat;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}
.login-mask {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
}
.login-wrap {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 720px;
  padding: 0 20px;
}

.login-form {
  position: relative;
  padding: 14px 0 0;
  background: rgba(14, 14, 14, 0.92);
  border: 1px solid rgba(212, 175, 55, 0.28);
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.45);
  overflow: hidden;
}

.form-lang {
  display: flex;
  justify-content: flex-end;
  padding: 0 18px 10px;
}

.form-body {
  display: grid;
  grid-template-columns: minmax(200px, 260px) 1fr;
  align-items: stretch;
}

.form-brand {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 28px 20px;
  background: rgba(0, 0, 0, 0.25);
  border-right: 1px solid rgba(212, 175, 55, 0.15);
}

.logo-large {
  width: 100%;
  max-width: 220px;
  height: auto;
  object-fit: contain;
  filter: drop-shadow(0 4px 24px rgba(212, 175, 55, 0.25));
}

.form-fields {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 22px 24px 24px;
  min-width: 0;
}

.title {
  text-align: left;
  font-size: 15px;
  font-weight: 700;
  color: #aaa;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  margin-bottom: 4px;
}

@media (max-width: 640px) {
  .login-wrap {
    max-width: 400px;
  }

  .form-body {
    grid-template-columns: 1fr;
  }

  .form-brand {
    padding: 20px 24px 12px;
    border-right: none;
    border-bottom: 1px solid rgba(212, 175, 55, 0.15);
  }

  .logo-large {
    max-width: 180px;
  }

  .form-fields {
    padding: 18px 20px 22px;
  }

  .title {
    text-align: center;
  }
}
label {
  font-size: 11px;
  color: #666;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}
.field {
  width: 100%;
  height: 44px;
  padding: 0 14px;
  background: #0a0a0a;
  border: 1px solid #2a2a2a;
  border-radius: 8px;
  color: #fff;
  font-size: 14px;
  font-family: inherit;
  outline: none;
  box-sizing: border-box;
  transition: border-color 0.2s;
}
.field::placeholder { color: #3a3a3a; }
.field:focus {
  border-color: var(--green-mid);
  box-shadow: 0 0 0 3px rgba(47, 181, 106, 0.15);
}
.field:-webkit-autofill,
.field:-webkit-autofill:focus {
  -webkit-box-shadow: 0 0 0 1000px #0a0a0a inset;
  -webkit-text-fill-color: #fff;
  caret-color: #fff;
}
.btn-login {
  margin-top: 6px;
  height: 46px;
  background: var(--primary-grad);
  border: 1px solid var(--green-border);
  border-radius: 8px;
  color: #fff;
  font-size: 15px;
  font-weight: 800;
  font-family: inherit;
  letter-spacing: 0.15em;
  box-shadow: var(--primary-shadow);
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.25);
  transition: background 0.2s, box-shadow 0.2s;
}
.btn-login:hover:not(:disabled) {
  background: var(--primary-grad-hover);
  border-color: rgba(120, 230, 170, 0.45);
  color: #fff;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.16) inset, 0 4px 14px rgba(0, 0, 0, 0.45), 0 0 20px rgba(47, 181, 106, 0.3);
}
.btn-login:disabled { opacity: 0.4; cursor: not-allowed; }
.quick-label {
  font-size: 10px;
  color: #2a2a2a;
  text-align: center;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  margin-top: 6px;
}
.quick-btns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.quick-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 10px;
  background: rgba(255, 255, 255, 0.02);
  border: 1px solid #1e1e1e;
  border-radius: 7px;
  font-family: inherit;
  cursor: pointer;
  transition: all 0.15s;
}
.quick-btn:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.06);
  border-color: rgba(255, 255, 255, 0.18);
}
.quick-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.quick-role {
  font-size: 11px;
  font-weight: 700;
  color: #555;
  letter-spacing: 0.04em;
}
.quick-acc {
  font-size: 12px;
  color: var(--green-text);
  font-weight: 600;
  font-family: monospace;
}
</style>
