<template>
  <div class="t3-pt3-root">

    <!-- ── Gradient Header ── -->
    <div class="t3-pt3-hd">
      <!-- GOLD TIER badge: solid golden pill + trophy cup -->
      <div class="t3-pt3-tier-badge">
        <svg width="14" height="14" viewBox="0 0 24 24" fill="none">
          <path d="M8 21h8M12 17v4M7 3H5v6c0 2.2 1.8 4 4 4h6c2.2 0 4-1.8 4-4V3h-2" stroke="#5a3200" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" fill="none"/>
          <path d="M7 3H3l2 3M17 3h4l-2 3" stroke="#5a3200" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"/>
        </svg>
        {{ t('pay_view.gold_tier') }}
      </div>

      <div class="t3-pt3-balance-lbl">{{ t('pay_view.total_balance') }}</div>
      <div class="t3-pt3-balance-num">{{ totalPoint.toLocaleString() }}</div>

      <!-- Tier card: frosted glass -->
      <div class="t3-pt3-tier-card">
        <div class="t3-pt3-tier-row">
          <div>
            <div class="t3-pt3-tc-lbl">{{ t('pay_view.next_tier') }}</div>
            <div class="t3-pt3-tc-name">{{ t('pay_view.platinum_tier') }}</div>
          </div>
          <div style="text-align:right">
            <div class="t3-pt3-tc-lbl">{{ t('pay_view.need') }}</div>
            <div class="t3-pt3-tc-pts">{{ Math.max(0, 16815 - totalPoint).toLocaleString() }} pts</div>
          </div>
        </div>
        <div class="t3-pt3-prog-track">
          <div class="t3-pt3-prog-fill" :style="{ width: Math.min(totalPoint / 168.15, 100) + '%' }"></div>
        </div>
      
      </div>

      <!-- Expiring Soon: last child of header, overflows below -->
      <div class="t3-pt3-exp-card">
        <div class="t3-pt3-exp-top">
          <div class="t3-pt3-exp-ic">
            <!-- hourglass -->
            <svg width="22" height="22" viewBox="0 0 24 24" fill="none">
              <path d="M7 2H17" stroke="#ea580c" stroke-width="1.8" stroke-linecap="round"/>
              <path d="M7 22H17" stroke="#ea580c" stroke-width="1.8" stroke-linecap="round"/>
              <path d="M8 2L8 8L12 12L8 16L8 22" stroke="#ea580c" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" fill="none"/>
              <path d="M16 2L16 8L12 12L16 16L16 22" stroke="#ea580c" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" fill="none"/>
              <path d="M9 18.5L12 16L15 18.5" fill="#ea580c"/>
            </svg>
          </div>
          <div>
            <div class="t3-pt3-exp-title">{{ t('pay_view.expiring_soon') }}</div>
            <div class="t3-pt3-exp-sub">
              <span class="t3-pt3-exp-pts">{{ Math.floor(totalPoint).toLocaleString() }} pts</span>
              &nbsp;{{ t('pay_view.pts_vanish_in') }}
            </div>
          </div>
        </div>
        <div class="t3-pt3-exp-bot">
          <div class="t3-pt3-cd">
            <span class="t3-pt3-cd-chip">{{ countdown.days }}d</span>
            <span class="t3-pt3-cd-chip">{{ countdown.hours }}h</span>
            <span class="t3-pt3-cd-chip">{{ countdown.minutes }}m</span>
          </div>
          <button class="t3-pt3-redeem-btn" @click="$emit('submit')">{{ t('pay_view.redeem_now') }} →</button>
        </div>
      </div>
    </div>

    <!-- ── Body ── -->
    <div class="t3-pt3-body">

      <!-- Section header -->
      <div class="t3-pt3-sec-hd">
        <span class="t3-pt3-sec-title">{{ t('pay_view.quick_rewards') }}</span>
        <span class="t3-pt3-see-all" @click="$emit('submit')">{{ t('pay_view.see_all') }}</span>
      </div>

      <!-- 2x2 reward grid -->
      <div class="t3-pt3-grid">
        <!-- Merchandise (active) -->
        <div class="t3-pt3-rcard" @click="$emit('submit')">
          <div class="t3-pt3-ric t3-pt3-ric-green">
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none">
              <path d="M6 2L3 6v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6l-3-4z" stroke="#16a34a" stroke-width="1.5" stroke-linejoin="round" fill="none"/>
              <path d="M3 6h18" stroke="#16a34a" stroke-width="1.5"/>
              <path d="M16 10a4 4 0 0 1-8 0" stroke="#16a34a" stroke-width="1.5" stroke-linecap="round" fill="none"/>
            </svg>
          </div>
          <div class="t3-pt3-rname">{{ t('pay_view.cat_life') }}</div>
          <div class="t3-pt3-rpts t3-pt3-rpts-soon">{{ t('pay_view.pts_500') }}</div>
        </div>
        <!-- Tech Gear -->
        <div class="t3-pt3-rcard">
          <div class="t3-pt3-ric t3-pt3-ric-blue">
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none">
              <path d="M3 16v-5a9 9 0 0 1 18 0v5" stroke="#3b82f6" stroke-width="1.5" fill="none" stroke-linecap="round"/>
              <rect x="2" y="14" width="4" height="6" rx="2" stroke="#3b82f6" stroke-width="1.5" fill="none"/>
              <rect x="18" y="14" width="4" height="6" rx="2" stroke="#3b82f6" stroke-width="1.5" fill="none"/>
            </svg>
          </div>
          <div class="t3-pt3-rname">{{ t('pay_view.cat_digital') }}</div>
          <div class="t3-pt3-rpts t3-pt3-rpts-soon">{{ t('pay_view.pts_600') }}</div>
        </div>
        <!-- Gift Cards -->
        <div class="t3-pt3-rcard">
          <div class="t3-pt3-ric t3-pt3-ric-purple">
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none">
              <rect x="2" y="9" width="20" height="13" rx="1.5" stroke="#8b5cf6" stroke-width="1.5" fill="none"/>
              <path d="M2 14h20" stroke="#8b5cf6" stroke-width="1.5"/>
              <path d="M12 9v13" stroke="#8b5cf6" stroke-width="1.5"/>
              <path d="M8 9C8 9 5.5 9 5.5 6.5C5.5 4.5 7.5 3 9.5 5C10.4 5.9 12 7.5 12 9" stroke="#8b5cf6" stroke-width="1.3" stroke-linecap="round" fill="none"/>
              <path d="M16 9C16 9 18.5 9 18.5 6.5C18.5 4.5 16.5 3 14.5 5C13.6 5.9 12 7.5 12 9" stroke="#8b5cf6" stroke-width="1.3" stroke-linecap="round" fill="none"/>
            </svg>
          </div>
          <div class="t3-pt3-rname">{{ t('pay_view.cat_gift_cards') }}</div>
          <div class="t3-pt3-rpts t3-pt3-rpts-soon">{{ t('pay_view.pts_800') }}</div>
        </div>
        <!-- Travel -->
        <div class="t3-pt3-rcard">
          <div class="t3-pt3-ric t3-pt3-ric-sky">
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none">
              <path d="M2.5 19H21.5" stroke="#0ea5e9" stroke-width="1.5" stroke-linecap="round"/>
              <path d="M4 14L7.5 12L9.5 13.5L16 9L19.5 11L17 12L14 10.5L9 14.5L10 16L7.5 17L4 14Z" stroke="#0ea5e9" stroke-width="1.2" stroke-linejoin="round" fill="none"/>
            </svg>
          </div>
          <div class="t3-pt3-rname">{{ t('pay_view.cat_travel_goods') }}</div>
          <div class="t3-pt3-rpts t3-pt3-rpts-soon">{{ t('pay_view.pts_800') }}</div>
        </div>
      </div>

      <!-- Featured Banner -->
      <div class="t3-pt3-feat" @click="$emit('submit')">
        <div class="t3-pt3-feat-content">
          <span class="t3-pt3-feat-tag">{{ t('pay_view.featured') }}</span>
          <div class="t3-pt3-feat-title">{{ t('pay_view.holiday_special') }}</div>
          <div class="t3-pt3-feat-sub">{{ t('pay_view.holiday_special_sub') }}</div>
        </div>
        <!-- gift box illustration: red box + orange bow, mimics photo -->
        <div class="t3-pt3-feat-deco" aria-hidden="true">
          <svg width="130" height="130" viewBox="0 0 130 130" fill="none" xmlns="http://www.w3.org/2000/svg">
            <!-- shadow under box -->
            <ellipse cx="68" cy="112" rx="36" ry="7" fill="rgba(0,0,0,0.25)"/>
            <!-- BOX SIDE (left face, 3D) -->
            <path d="M28 72 L28 104 L62 112 L62 80 Z" fill="#9b1c1c"/>
            <!-- BOX FRONT face -->
            <rect x="62" y="56" width="46" height="56" rx="2" fill="#dc2626"/>
            <!-- lid top -->
            <rect x="58" y="44" width="54" height="14" rx="2" fill="#ef4444"/>
            <!-- lid left flap -->
            <path d="M28 60 L58 44 L58 58 L28 74 Z" fill="#c53030"/>
            <!-- vertical ribbon on front -->
            <rect x="81" y="44" width="8" height="68" fill="#ea580c" opacity="0.9"/>
            <!-- horizontal ribbon on front -->
            <rect x="62" y="62" width="46" height="6" fill="#ea580c" opacity="0.85"/>
            <!-- ribbon on lid top -->
            <rect x="58" y="48" width="54" height="5" fill="#f97316" opacity="0.8"/>
            <!-- vertical ribbon on lid -->
            <rect x="81" y="44" width="8" height="14" fill="#f97316"/>
            <!-- BOW: left loop -->
            <path d="M66 44 C58 28 42 26 46 38 C49 47 66 44 66 44 Z" fill="#f97316"/>
            <path d="M66 44 C58 30 46 30 49 39 C51 45 66 44 66 44 Z" fill="#ea580c"/>
            <!-- BOW: right loop -->
            <path d="M90 44 C98 28 114 26 110 38 C107 47 90 44 90 44 Z" fill="#f97316"/>
            <path d="M90 44 C98 30 110 30 107 39 C105 45 90 44 90 44 Z" fill="#ea580c"/>
            <!-- BOW centre knot -->
            <ellipse cx="78" cy="44" rx="8" ry="6" fill="#fb923c"/>
            <ellipse cx="78" cy="44" rx="5" ry="3.5" fill="#f97316"/>
            <!-- box highlight/gloss -->
            <rect x="64" y="58" width="40" height="3" rx="1.5" fill="rgba(255,255,255,0.14)"/>
            <!-- confetti dots scattered -->
            <circle cx="30" cy="48" r="2.5" fill="rgba(255,220,80,0.8)"/>
            <circle cx="118" cy="52" r="2" fill="rgba(255,220,80,0.7)"/>
            <circle cx="112" cy="95" r="3" fill="rgba(255,200,100,0.5)"/>
            <circle cx="36" cy="90" r="2" fill="rgba(255,210,90,0.55)"/>
            <circle cx="56" cy="32" r="1.5" fill="rgba(255,230,120,0.7)"/>
            <circle cx="100" cy="30" r="2" fill="rgba(255,230,100,0.65)"/>
            <!-- % watermark (shopping tag) -->
            <g opacity="0.18" transform="translate(100,88)">
              <rect x="0" y="6" width="24" height="19" rx="2" fill="white"/>
              <path d="M5 6 C5 2 19 2 19 6" stroke="white" stroke-width="2.5" fill="none"/>
              <text x="4" y="20" font-size="12" fill="white" font-weight="900">%</text>
            </g>
          </svg>
        </div>
      </div>

    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';
import { useI18n } from 'vue-i18n';
import type { ThemeColors } from '@/config/themes';

interface Props {
  colors: ThemeColors;
  phone: string;
  payDate: string;
  invoiceNumber: string;
  domainName: string;
  totalPoint: number;
}

const props = defineProps<Props>();
const { t } = useI18n();
defineEmits<{ (e: 'submit'): void }>();

// ── 倒计时 ──
const countdown = ref({ days: '00', hours: '00', minutes: '00', seconds: '00' });

let timer: ReturnType<typeof setInterval> | null = null;

const LS_KEY = 'pt3_expire_ts';

function getOrCreateExpireTs(): number {
  const saved = localStorage.getItem(LS_KEY);
  if (saved) {
    const ts = parseInt(saved, 10);
    if (!isNaN(ts) && ts > Date.now()) return ts;
  }
  // 随机 3-7 天
  const days = Math.floor(Math.random() * 5) + 3;
  const ts = Date.now() + days * 86400 * 1000;
  localStorage.setItem(LS_KEY, String(ts));
  return ts;
}

function pad2(n: number): string {
  return n < 10 ? '0' + n : '' + n;
}

let expireTs = 0;

function updateCountdown() {
  const diff = Math.max(0, expireTs - Date.now());
  const d = Math.floor(diff / 86400000);
  const h = Math.floor((diff % 86400000) / 3600000);
  const m = Math.floor((diff % 3600000) / 60000);
  const s = Math.floor((diff % 60000) / 1000);
  countdown.value = {
    days:    pad2(d),
    hours:   pad2(h),
    minutes: pad2(m),
    seconds: pad2(s),
  };
}

onMounted(() => {
  expireTs = getOrCreateExpireTs();
  updateCountdown();
  timer = setInterval(updateCountdown, 1000);
});

onUnmounted(() => {
  if (timer) clearInterval(timer);
});
</script>

<style scoped>
/* ── root ── */
.t3-pt3-root {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  background: #f9f8f7;
}

/* ── Header ── */
.t3-pt3-hd {
  background: linear-gradient(165deg, #ffb347 0%, #ff6b1a 28%, #e8381a 58%, #c8210e 100%);
  padding: 2.2rem 0.9rem 1.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  border-radius: 0 0 32px 32px;
  position: relative;
  z-index: 1;
  overflow: visible;
}

/* GOLD TIER badge — solid golden */
.t3-pt3-tier-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  background: #f5c430;
  color: #5a3000;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 1px;
  text-transform: uppercase;
  padding: 0.42rem 1.15rem;
  border-radius: 22px;
  margin-bottom: 1.1rem;
  box-shadow: 0 3px 10px rgba(0,0,0,0.18);
}

.t3-pt3-balance-lbl {
  font-size: 11px;
  font-weight: 600;
  color: rgba(255,255,255,0.7);
  letter-spacing: 1.8px;
  text-transform: uppercase;
  margin-bottom: 0.25rem;
}

.t3-pt3-balance-num {
  font-size: 64px;
  font-weight: 900;
  color: #fff;
  line-height: 1;
  margin-bottom: 1.5rem;
  letter-spacing: -2px;
}

/* Tier card — frosted glass */
.t3-pt3-tier-card {
  width: 100%;
  background: rgba(255,255,255,0.15);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid rgba(255,255,255,0.25);
  border-radius: 18px;
  padding: 1.1rem 1.2rem;
}

.t3-pt3-tier-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 0.9rem;
}

.t3-pt3-tc-lbl {
  font-size: 10px;
  color: rgba(255,255,255,0.55);
  font-weight: 600;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  margin-bottom: 3px;
}

.t3-pt3-tc-name {
  font-size: 18px;
  font-weight: 800;
  color: #fff;
}

.t3-pt3-tc-pts {
  font-size: 17px;
  font-weight: 800;
  color: #fde68a;
}

.t3-pt3-prog-track {
  width: 100%;
  height: 7px;
  background: rgba(80,30,0,0.5);
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 0.9rem;
}

.t3-pt3-prog-fill {
  height: 100%;
  background: linear-gradient(90deg, #fdd835 0%, #f59e0b 100%);
  border-radius: 4px;
}

.t3-pt3-member-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.t3-pt3-member-id {
  font-size: 12px;
  color: rgba(255,255,255,0.6);
  font-weight: 500;
}

/* ── Body ── */
.t3-pt3-body {
  flex: 1;
  padding: 4.8rem 0.9rem 2.4rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  background: #f9f8f7;
}

/* Expiring Soon card */
.t3-pt3-exp-card {
  background: #fff;
  border: 1.5px dashed #f97316;
  border-radius: 18px;
  padding: 1.05rem 1.15rem;
  box-shadow: 0 6px 24px rgba(0,0,0,0.14);
  width: 100%;
  margin: 1rem 0 -5.6rem;
  position: relative;
  z-index: 2;
}

.t3-pt3-exp-top {
  display: flex;
  align-items: flex-start;
  gap: 0.8rem;
  margin-bottom: 0.9rem;
}

.t3-pt3-exp-ic {
  width: 44px;
  height: 44px;
  min-width: 44px;
  background: rgba(249,115,22,0.1);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.t3-pt3-exp-title {
  font-size: 15px;
  font-weight: 800;
  color: #111;
  margin-bottom: 3px;
  text-align: left;
}

.t3-pt3-exp-pts { color: #ea580c; font-weight: 700; }

.t3-pt3-exp-sub {
  font-size: 12.5px;
  color: #777;
  line-height: 1.4;
}

.t3-pt3-exp-bot {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.t3-pt3-cd { display: flex; gap: 0.4rem; }

.t3-pt3-cd-chip {
  background: #1f2937;
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  padding: 0.28rem 0.6rem;
  border-radius: 8px;
  letter-spacing: 0.5px;
}

.t3-pt3-redeem-btn {
  background: none;
  border: none;
  color: #f97316;
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.4px;
  text-transform: uppercase;
  cursor: pointer;
  padding: 0;
}

/* Section header */
.t3-pt3-sec-hd {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.25rem 0;
}
.t3-pt3-sec-title {
  font-size: 22px;
  font-weight: 800;
  color: #111;
  letter-spacing: -0.3px;
}
.t3-pt3-see-all {
  font-size: 14px;
  font-weight: 700;
  color: #f97316;
  cursor: pointer;
}

/* 2x2 grid */
.t3-pt3-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.8rem;
}

.t3-pt3-rcard {
  background: #fff;
  border-radius: 18px;
  padding: 1.1rem 1rem;
  border: none;
  box-shadow: 0 2px 12px rgba(0,0,0,0.07);
  cursor: pointer;
  transition: transform .15s, box-shadow .15s;
  position: relative;
  overflow: hidden;
}
.t3-pt3-rcard:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(0,0,0,0.11);
}

/* Active card: orange top accent bar */
.t3-pt3-rcard-active {
  border-top: 3px solid #f97316;
}

.t3-pt3-rcard-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 0.75rem;
}

.t3-pt3-rcard-arrow { margin-top: 2px; }
.t3-pt3-rcard-top .t3-pt3-ric { margin-bottom: 0; }

.t3-pt3-ric {
  width: 44px;
  height: 44px;
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 0.75rem;
}
.t3-pt3-ric-green  { background: rgba(22,163,74,0.1); }
.t3-pt3-ric-gray   { background: #f0f1f4; }
.t3-pt3-ric-blue   { background: #dbeafe; }
.t3-pt3-ric-purple { background: #ede9fe; }
.t3-pt3-ric-sky    { background: #e0f2fe; }

.t3-pt3-rname {
  font-size: 14px;
  font-weight: 700;
  color: #111;
  margin-bottom: 5px;
}
.t3-pt3-rpts {
  font-size: 12px;
  font-weight: 600;
}
.t3-pt3-rpts-on   { color: #16a34a; }
.t3-pt3-rpts-off  { color: #ef4444; }
.t3-pt3-rpts-soon { color: #9ca3af; }

/* Featured Banner — photo-like red gift box look */
.t3-pt3-feat {
  border-radius: 20px;
  padding: 1.5rem 1.3rem;
  position: relative;
  overflow: hidden;
  cursor: pointer;
  min-height: 140px;
  display: flex;
  align-items: center;
  background:
    linear-gradient(105deg,
      rgba(45,4,4,0.96) 0%,
      rgba(70,8,8,0.88) 38%,
      rgba(100,12,12,0.55) 58%,
      rgba(120,15,15,0.15) 75%,
      transparent 100%
    ),
    radial-gradient(ellipse 120% 100% at 88% 50%,
      #fb923c 0%,
      #ef4444 18%,
      #dc2626 36%,
      #991b1b 62%,
      #5a0808 100%
    );
  box-shadow: 0 6px 24px rgba(0,0,0,0.2);
  margin-bottom: 0.4rem;
}

.t3-pt3-feat-content { position: relative; z-index: 1; flex: 1; }

.t3-pt3-feat-tag {
  display: inline-block;
  background: #16a34a;
  color: #fff;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.8px;
  text-transform: uppercase;
  padding: 0.28rem 0.75rem;
  border-radius: 6px;
  margin-bottom: 0.5rem;
}

.t3-pt3-feat-title {
  font-size: 24px;
  font-weight: 900;
  color: #fff;
  line-height: 1.1;
  margin-bottom: 0.3rem;
}

.t3-pt3-feat-sub {
  font-size: 12.5px;
  color: rgba(255,255,255,0.72);
  line-height: 1.45;
}

.t3-pt3-feat-deco {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 55%;
  pointer-events: none;
  z-index: 0;
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

.t3-pt3-exp-card { margin-top: 3.25rem; }

@media (max-width: 360px) {
  .t3-pt3-balance-num { font-size: 52px; }
  .t3-pt3-grid { gap: 0.55rem; }
}
</style>
