<template>
  <div class="login-root">
    <!-- Left brand panel -->
    <div class="login-brand">
      <div class="login-brand__inner">
        <el-image :src="OSELogo" fit="contain" class="login-brand__logo" />
        <div class="login-brand__copy">
          <h1 class="login-brand__title">OSE Marketing Console</h1>
          <p class="login-brand__desc">視覺化 A/B 實驗平台 — 從變體建立到發布的完整工作流程</p>
        </div>
        <ul class="login-brand__features">
          <li>
            <span class="login-brand__feat-dot" />
            Visual Editor 無代碼編輯
          </li>
          <li>
            <span class="login-brand__feat-dot" />
            實驗分流 · 版本快照 · 即時回退
          </li>
          <li>
            <span class="login-brand__feat-dot" />
            頁面變更自動套用上線
          </li>
        </ul>
      </div>
      <p class="login-brand__footer">© OSE Technology — Marketing Platform</p>
    </div>

    <!-- Right form panel -->
    <div class="login-form-panel">
      <div class="login-form">
        <div class="login-form__heading">
          <h2 class="login-form__title">歡迎回來</h2>
          <p class="login-form__sub">請使用您的帳號登入後台</p>
        </div>

        <div class="login-form__fields">
          <div class="login-field">
            <label class="login-field__label">電子信箱</label>
            <el-input
              v-model="email"
              placeholder="example@company.com"
              type="email"
              size="large"
              class="login-field__input"
            />
          </div>
          <div class="login-field">
            <label class="login-field__label">密碼</label>
            <el-input
              v-model="password"
              type="password"
              size="large"
              show-password
              class="login-field__input"
              @keyup.enter="login_button"
            />
          </div>
        </div>

        <el-button
          type="primary"
          size="large"
          :loading="loading"
          class="login-form__submit"
          @click="login_button"
        >
          登入
        </el-button>

        <div class="login-divider">
          <span class="login-divider__line" />
          <span class="login-divider__text">或</span>
          <span class="login-divider__line" />
        </div>

        <a
          class="login-google"
          :href="`${api_url}/auth/login/google?redirect=${origin}/login`"
        >
          <svg-icon name="Google_Logo" size="20" />
          <span>以 Google 帳號登入</span>
        </a>
      </div>
    </div>
  </div>
</template>

<script setup>
import OSELogo from '@a/ose-logo.png'
import { ref } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import SvgIcon from '@/components/svg-icon.vue'
import { appConfig } from '@/config/env'
import { defaultAuthenticatedRoute } from '@/config/app-shell'

const router = useRouter()
const store = useStore()
const email = ref('')
const password = ref('')
const loading = ref(false)
const api_url = ref(appConfig.directusBaseUrl)
const origin = ref(window.location.origin)

const login_button = async () => {
  loading.value = true
  store.state.common.loading = true
  try {
    await store.dispatch('user/login', { email: email.value, password: password.value })
    await store.dispatch('user/getUserInfo')
    router.push(defaultAuthenticatedRoute)
  } catch (e) {
    // error handled by store
  } finally {
    loading.value = false
    store.state.common.loading = false
  }
}
</script>

<style scoped>
.login-root {
  display: flex;
  min-height: 100vh;
}

/* ─── Left brand panel ─── */
.login-brand {
  flex: 0 0 44%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 48px 52px;
  background: #0f172a;
  background-image:
    radial-gradient(circle at 20% 20%, rgba(37, 99, 235, 0.28) 0%, transparent 52%),
    radial-gradient(circle at 80% 80%, rgba(99, 102, 241, 0.18) 0%, transparent 48%);
}

.login-brand__inner {
  display: flex;
  flex-direction: column;
  gap: 36px;
}

.login-brand__logo {
  width: 120px;
  height: 56px;
  object-fit: contain;
  filter: brightness(0) invert(1) opacity(0.9);
}

.login-brand__copy {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.login-brand__title {
  margin: 0;
  color: #f1f5f9;
  font-size: 28px;
  font-weight: 800;
  letter-spacing: -0.01em;
  line-height: 1.2;
}

.login-brand__desc {
  margin: 0;
  color: #94a3b8;
  font-size: 15px;
  line-height: 1.7;
}

.login-brand__features {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.login-brand__features li {
  display: flex;
  align-items: center;
  gap: 10px;
  color: #cbd5e1;
  font-size: 14px;
}

.login-brand__feat-dot {
  display: block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #3b82f6;
  flex-shrink: 0;
}

.login-brand__footer {
  margin: 0;
  color: #475569;
  font-size: 12px;
}

/* ─── Right form panel ─── */
.login-form-panel {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f8fafc;
  padding: 40px 24px;
}

.login-form {
  width: 100%;
  max-width: 400px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.login-form__heading {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.login-form__title {
  margin: 0;
  color: #0f172a;
  font-size: 28px;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.login-form__sub {
  margin: 0;
  color: #64748b;
  font-size: 15px;
}

.login-form__fields {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.login-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.login-field__label {
  color: #374151;
  font-size: 13px;
  font-weight: 600;
}

.login-field__input :deep(.el-input__wrapper) {
  border-radius: 10px;
  box-shadow: 0 0 0 1px #e2e8f0;
  background: #ffffff;
  height: 44px;
}

.login-field__input :deep(.el-input__wrapper:hover),
.login-field__input :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 2px #3b82f6;
}

.login-form__submit {
  width: 100%;
  height: 46px;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 600;
  background: #2563eb;
  border-color: #2563eb;
}

.login-form__submit:hover {
  background: #1d4ed8;
  border-color: #1d4ed8;
}

/* ─── Divider ─── */
.login-divider {
  display: flex;
  align-items: center;
  gap: 12px;
}

.login-divider__line {
  flex: 1;
  height: 1px;
  background: #e2e8f0;
}

.login-divider__text {
  color: #94a3b8;
  font-size: 13px;
}

/* ─── Google button ─── */
.login-google {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  height: 46px;
  border-radius: 10px;
  border: 1px solid #e2e8f0;
  background: #ffffff;
  color: #374151;
  font-size: 14px;
  font-weight: 500;
  text-decoration: none;
  transition: background 0.12s, border-color 0.12s, box-shadow 0.12s;
}

.login-google:hover {
  background: #f8fafc;
  border-color: #cbd5e1;
  box-shadow: 0 2px 8px rgba(15, 23, 42, 0.06);
}

@media (max-width: 768px) {
  .login-brand {
    display: none;
  }
}
</style>
