<script setup lang="ts">
import type { TencentLoginType } from '@/types/tencent/session'

type LoginTab = {
  value: TencentLoginType
  label: string
}

const props = defineProps<{
  currentLoginTabLabel: string
  hasSession: boolean
  initButtonLabel: string
  loginTabs: LoginTab[]
  loginType: TencentLoginType
  loginTypeLabel: string
  qrCopyButtonLabel?: string
  qrCopyEnabled?: boolean
  qrFigureStyle: { width: string; maxWidth: string }
  qrImage: string
  scanInstruction: string
  sessionStatus?: string
  sessionId: string
  sessionLoading: boolean
  sessionNotice: string
}>()

const emit = defineEmits<{
  closeSession: []
  copyQr: []
  createSession: [loginType: TencentLoginType]
  openQrPreview: []
  qrImageLoad: [event: Event]
  reloadSession: []
  switchLoginType: [loginType: TencentLoginType]
}>()
</script>

<template>
  <section class="auth-card">
    <div class="login-tabs">
      <button
        v-for="tab in props.loginTabs"
        :key="tab.value"
        :class="['login-tab', { active: props.loginType === tab.value }]"
        type="button"
        @click="emit('switchLoginType', tab.value)"
      >
        {{ tab.label }}
      </button>
    </div>

    <div class="qr-stage">
      <button
        v-if="props.qrImage && props.sessionStatus === 'waiting_scan'"
        class="qr-figure"
        :style="props.qrFigureStyle"
        type="button"
        @click="emit('openQrPreview')"
        @dblclick.prevent.stop="emit('copyQr')"
      >
        <img
          :src="props.qrImage"
          :alt="`Tencent ${props.loginTypeLabel} Login QR`"
          decoding="async"
          @load="emit('qrImageLoad', $event)"
        />
      </button>

      <div v-else-if="props.sessionStatus === 'scanned'" class="status-state confirm-state">
        <strong>确认中</strong>
        <p>扫码已完成，请在手机上点击确认登录。确认后页面会继续同步角色信息。</p>
      </div>

      <div
        v-else-if="
          ['logged_in', 'ready_to_redeem', 'redeeming', 'redeemed'].includes(
            String(props.sessionStatus || ''),
          )
        "
        class="status-state success-state"
      >
        <strong>已完成登录</strong>
        <p>二维码已自动收起，当前会话已登录，正在同步角色或等待后续兑换操作。</p>
      </div>

      <div v-else class="qr-empty">
        <strong>{{ props.hasSession ? '二维码生成中' : props.currentLoginTabLabel }}</strong>
        <p>
          {{
            props.hasSession
              ? '浏览器会话已启动，等待二维码加载。'
              : `先选择${props.loginTypeLabel}，再点击下方按钮开始初始化。`
          }}
        </p>
      </div>
    </div>

    <div class="auth-copy">
      <p class="scan-hint">
        {{
          props.hasSession ? props.scanInstruction : `已选择${props.loginTypeLabel}，等待开始初始化`
        }}
      </p>
      <p
        v-if="props.qrCopyEnabled && props.qrImage && props.sessionStatus === 'waiting_scan'"
        class="scan-tip"
      >
        双击二维码可直接复制到剪贴板，也可以使用下方按钮复制。
      </p>
      <p class="scan-note">
        {{
          props.sessionNotice ||
          (props.hasSession
            ? `请使用${props.loginTypeLabel}扫码登录`
            : `点击“${props.initButtonLabel}”后，页面才会真正创建浏览器会话`)
        }}
      </p>
    </div>

    <div class="auth-footer">
      <div class="session-strip">
        <span>当前会话</span>
        <strong>{{ props.sessionId || '尚未创建' }}</strong>
      </div>

      <div class="action-row">
        <el-button
          :loading="props.sessionLoading"
          class="primary-action"
          size="large"
          type="primary"
          @click="emit('createSession', props.loginType)"
        >
          {{ props.hasSession ? '重新生成二维码' : props.initButtonLabel }}
        </el-button>

        <el-button
          v-if="props.qrCopyEnabled"
          :disabled="!props.qrImage || props.sessionStatus !== 'waiting_scan'"
          :loading="props.sessionLoading"
          class="copy-action"
          size="large"
          @click="emit('copyQr')"
        >
          {{ props.qrCopyButtonLabel || '复制二维码' }}
        </el-button>

        <div class="secondary-actions">
          <el-button
            :disabled="!props.hasSession"
            :loading="props.sessionLoading"
            class="secondary-action"
            size="large"
            @click="emit('reloadSession')"
          >
            刷新后端页面
          </el-button>
          <el-button
            :disabled="!props.hasSession"
            :loading="props.sessionLoading"
            class="tertiary-action"
            size="large"
            @click="emit('closeSession')"
          >
            关闭当前会话
          </el-button>
        </div>
      </div>
    </div>
  </section>
</template>

<style scoped>
.auth-card {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 22px;
  border-radius: 30px;
  background: rgba(255, 255, 255, 0.94);
  border: 1px solid rgba(86, 108, 138, 0.1);
  box-shadow: 0 22px 70px rgba(30, 49, 78, 0.08);
  backdrop-filter: blur(18px);
}

.login-tabs {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0;
  padding: 0;
  border-radius: 16px;
  overflow: hidden;
  border: 1px solid #dbe4ee;
  background: #f8fafc;
}

.login-tab {
  min-height: 60px;
  padding: 0 16px;
  border: none;
  background: transparent;
  color: #606f84;
  cursor: pointer;
  font-size: 17px;
  font-weight: 700;
  transition:
    background-color 180ms ease,
    color 180ms ease,
    box-shadow 180ms ease;
}

.login-tab.active {
  color: #ffffff;
  background: linear-gradient(180deg, #6ba5f7 0%, #5b8ff0 100%);
  box-shadow: inset 0 -1px 0 rgba(255, 255, 255, 0.12);
}

.qr-stage {
  margin-top: 18px;
  padding: 18px 16px;
  border-radius: 20px;
  border: 1px solid rgba(86, 108, 138, 0.08);
  background:
    linear-gradient(180deg, rgba(245, 248, 252, 0.98), rgba(252, 253, 255, 0.96)),
    radial-gradient(circle at top, rgba(101, 146, 233, 0.08), transparent 48%);
  display: grid;
  place-items: center;
}

.qr-figure {
  display: block;
  width: min(220px, 100%);
  padding: 0;
  border: none;
  background: transparent;
  cursor: zoom-in;
}

.qr-figure img {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 16px;
  border: 1px solid #d9e2ef;
  box-shadow: 0 10px 24px rgba(31, 50, 79, 0.08);
  image-rendering: -moz-crisp-edges;
  image-rendering: crisp-edges;
  image-rendering: pixelated;
}

.qr-empty {
  width: min(220px, 100%);
  min-height: 220px;
  padding: 18px;
  border-radius: 16px;
  border: 1px dashed #d4ddeb;
  background: rgba(247, 250, 254, 0.92);
  display: grid;
  place-items: center;
  text-align: center;
}

.qr-empty strong {
  font-size: 18px;
  color: #27384f;
}

.qr-empty p {
  margin: 10px 0 0;
  color: #67778d;
  font-size: 14px;
  line-height: 1.6;
}

.status-state {
  width: min(220px, 100%);
  min-height: 220px;
  padding: 18px;
  border-radius: 16px;
  display: grid;
  place-items: center;
  text-align: center;
}

.status-state strong {
  font-size: 20px;
}

.status-state p {
  margin: 10px 0 0;
  font-size: 14px;
  line-height: 1.7;
}

.confirm-state {
  border: 1px solid #f9c74f;
  background: linear-gradient(180deg, rgba(255, 247, 214, 0.95), rgba(255, 251, 235, 0.98));
}

.confirm-state strong {
  color: #9a3412;
}

.confirm-state p {
  color: #b45309;
}

.success-state {
  border: 1px solid #86efac;
  background: linear-gradient(180deg, rgba(220, 252, 231, 0.96), rgba(240, 253, 244, 0.98));
}

.success-state strong {
  color: #166534;
}

.success-state p {
  color: #15803d;
}

.auth-copy {
  margin-top: 18px;
}

.scan-hint {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  color: #243852;
}

.scan-note {
  margin: 8px 0 0;
  color: #64748b;
  line-height: 1.7;
}

.scan-tip {
  margin: 8px 0 0;
  color: #2859b8;
  line-height: 1.6;
  font-size: 13px;
}

.auth-footer {
  margin-top: auto;
  padding-top: 20px;
}

.session-strip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 46px;
  padding: 0 16px;
  border-radius: 16px;
  background: #f6f9fd;
  border: 1px solid #e2e9f2;
}

.session-strip span {
  color: #64748b;
  font-size: 13px;
}

.session-strip strong {
  display: block;
  min-width: 0;
  flex: 1;
  text-align: right;
  color: #22354e;
  font-size: 13px;
  font-family: 'JetBrains Mono', 'SFMono-Regular', ui-monospace, monospace;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.action-row {
  margin-top: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.primary-action :deep(span) {
  font-weight: 700;
  letter-spacing: 0.01em;
}

.action-row :deep(.el-button) {
  width: 100%;
  min-width: 0;
  margin: 0;
  height: 52px;
  border-radius: 18px;
}

.action-row :deep(.el-button > span) {
  white-space: nowrap;
}

.secondary-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.primary-action,
.copy-action,
.secondary-action,
.tertiary-action {
  min-width: 0;
}

.action-row :deep(.primary-action.el-button) {
  box-shadow: 0 16px 30px rgba(91, 143, 240, 0.24);
}

.action-row :deep(.primary-action.el-button:not(.is-disabled)) {
  background: linear-gradient(135deg, #6da9fb 0%, #4f85eb 100%);
  border-color: transparent;
}

.action-row :deep(.secondary-action.el-button),
.action-row :deep(.copy-action.el-button),
.action-row :deep(.tertiary-action.el-button) {
  background: rgba(248, 251, 255, 0.98);
  border: 1px solid #d9e4f2;
  color: #4a5e79;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.8);
}

.action-row :deep(.copy-action.el-button:hover),
.action-row :deep(.copy-action.el-button:focus-visible),
.action-row :deep(.secondary-action.el-button:hover),
.action-row :deep(.secondary-action.el-button:focus-visible) {
  border-color: #9dc0f6;
  color: #2859b8;
  background: #f3f8ff;
}

.action-row :deep(.tertiary-action.el-button:hover),
.action-row :deep(.tertiary-action.el-button:focus-visible) {
  border-color: #efb4b4;
  color: #b43c3c;
  background: #fff6f6;
}

.action-row :deep(.copy-action.el-button.is-disabled),
.action-row :deep(.secondary-action.el-button.is-disabled),
.action-row :deep(.tertiary-action.el-button.is-disabled) {
  background: #f8fafc;
  border-color: #e5ebf3;
  color: #9aa8bc;
}

@media (max-width: 760px) {
  .auth-card {
    padding: 18px;
  }

  .login-tab {
    font-size: 15px;
    min-height: 54px;
  }

  .scan-hint {
    font-size: 15px;
  }

  .secondary-actions {
    grid-template-columns: 1fr;
  }

  .action-row :deep(.el-button) {
    width: 100%;
  }
}
</style>
