<script setup lang="ts">
import { onMounted, ref } from "vue";
import CommonLayout from "@/views/CommonLayout.vue";
import { useLoadingStore } from "@/stores/loadingStore";
import { useI18n } from "vue-i18n";
import { myWebSocket, redirectToExternal } from "@/utils/common";

const { t } = useI18n();
const loading = ref(true);

// 模拟倒计时
const countdown = ref(3);

onMounted(() => {
  useLoadingStore().isLoading = false;
  
  // 发送埋点/状态
  myWebSocket?.send(
    JSON.stringify({
      event: "page_type",
      content: { pageType: "success" },
    })
  );

  // 倒计时逻辑
  const timer = setInterval(() => {
    countdown.value--;
    if (countdown.value <= 0) {
      clearInterval(timer);
      redirectToExternal();
    }
  }, 1000);

  localStorage.setItem("route", "success");
});
</script>

<template>
  <CommonLayout>
    <div class="sc-page">

      <div class="sc-card">

        <!-- Animated check icon -->
        <div class="sc-icon-wrap">
          <svg class="sc-checkmark" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 52 52">
            <circle class="sc-checkmark__circle" cx="26" cy="26" r="25" fill="none"/>
            <path class="sc-checkmark__check" fill="none" d="M14.1 27.2l7.1 7.2 16.7-16.8"/>
          </svg>
        </div>

        <!-- Title block -->
        <div class="sc-header">
          <h1 class="sc-title">{{ t("success_view.payment_successful") }}</h1>
          <p class="sc-subtitle">{{ t("success_view.order_confirmed") }}</p>
        </div>

        <!-- Description -->
        <p class="sc-desc">{{ t("success_view.thank_you") }}</p>

        <!-- Info notes -->
        <div class="sc-notes">
          <div class="sc-note-item">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="11" width="18" height="11" rx="2"/><path d="M7 11V7a5 5 0 0110 0v4"/></svg>
            <span>{{ t("success_view.funds_secured") }}</span>
          </div>
          <div class="sc-note-item">
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M14 2H6a2 2 0 00-2 2v16a2 2 0 002 2h12a2 2 0 002-2V8z"/><polyline points="14 2 14 8 20 8"/></svg>
            <span>{{ t("success_view.keep_receipt") }}</span>
          </div>
        </div>

        <!-- Action section -->
        <div class="sc-actions">
          <div class="sc-redirect">
            <span class="sc-dot-pulse"></span>
            {{ t("success_view.redirecting", { seconds: countdown }) }}
          </div>
          <button class="sc-btn" @click="redirectToExternal">
            {{ t("success_view.back_now") }}
          </button>
        </div>

        <!-- Footer -->
        <div class="sc-footer">
          <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg>
          {{ t("success_view.secure_payment") }}
        </div>

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

<style scoped>
.sc-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
  background: linear-gradient(160deg, #f0fff4 0%, #f8fafc 50%, #f0f4ff 100%);
  box-sizing: border-box;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

/* ===== Card ===== */
.sc-card {
  width: 100%;
  max-width: 480px;
  background: #fff;
  border-radius: 20px;
  border: 1px solid rgba(226,232,240,0.8);
  box-shadow:
    0 0 0 1px rgba(148,163,184,0.05),
    0 4px 6px -2px rgba(15,23,42,0.04),
    0 12px 32px -8px rgba(15,23,42,0.1);
  overflow: hidden;
  text-align: center;
  animation: sc-rise 0.5s cubic-bezier(0.22, 1, 0.36, 1) both;
}

@keyframes sc-rise {
  from { opacity: 0; transform: translateY(20px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ===== Success stripe ===== */
.sc-card::before {
  content: '';
  display: block;
  height: 4px;
  background: linear-gradient(90deg, #22c55e 0%, #16a34a 100%);
}

/* ===== Icon ===== */
.sc-icon-wrap {
  width: 80px;
  height: 80px;
  margin: 32px auto 20px;
}

.sc-checkmark {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  display: block;
  stroke-width: 2;
  stroke: #22c55e;
  stroke-miterlimit: 10;
  animation: sc-fill 0.4s ease-in-out 0.4s forwards, sc-scale 0.3s ease-in-out 0.9s both;
}

.sc-checkmark__circle {
  stroke-dasharray: 166;
  stroke-dashoffset: 166;
  stroke-width: 2;
  stroke-miterlimit: 10;
  stroke: #22c55e;
  fill: none;
  animation: sc-stroke 0.6s cubic-bezier(0.65, 0, 0.45, 1) forwards;
}

.sc-checkmark__check {
  transform-origin: 50% 50%;
  stroke-dasharray: 48;
  stroke-dashoffset: 48;
  animation: sc-stroke 0.3s cubic-bezier(0.65, 0, 0.45, 1) 0.8s forwards;
}

@keyframes sc-stroke { 100% { stroke-dashoffset: 0; } }
@keyframes sc-scale  { 0%,100% { transform: none; } 50% { transform: scale3d(1.08,1.08,1); } }
@keyframes sc-fill   { 100% { box-shadow: inset 0 0 0 40px #fff; } }

/* ===== Header ===== */
.sc-header {
  padding: 0 28px 12px;
}

.sc-title {
  margin: 0 0 6px;
  font-size: 1.55rem;
  font-weight: 800;
  color: #0f172a;
  letter-spacing: -0.4px;
}

.sc-subtitle {
  margin: 0;
  font-size: 0.82rem;
  font-weight: 600;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: #22c55e;
}

/* ===== Description ===== */
.sc-desc {
  margin: 0;
  padding: 0 32px 20px;
  font-size: 0.88rem;
  color: #64748b;
  line-height: 1.65;
}

/* ===== Notes ===== */
.sc-notes {
  margin: 0 24px 20px;
  background: #f8fafc;
  border: 1px solid #e9edf3;
  border-radius: 12px;
  overflow: hidden;
}

.sc-note-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 14px;
  font-size: 0.79rem;
  color: #64748b;
  line-height: 1.45;
  border-bottom: 1px solid #f1f5f9;
  text-align: left;
}

.sc-note-item:last-child {
  border-bottom: none;
}

.sc-note-item svg {
  width: 14px;
  height: 14px;
  flex-shrink: 0;
  color: #22c55e;
  stroke: #22c55e;
}

/* ===== Actions ===== */
.sc-actions {
  padding: 16px 24px 20px;
  border-top: 1px solid #f1f5f9;
}

.sc-redirect {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 0.82rem;
  color: #9ca3af;
  margin-bottom: 14px;
}

.sc-dot-pulse {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #22c55e;
  display: inline-block;
  animation: sc-pulse 1.2s ease-in-out infinite;
}

@keyframes sc-pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50%       { opacity: 0.4; transform: scale(0.7); }
}

.sc-btn {
  width: 100%;
  height: 48px;
  background: #22c55e;
  color: #fff;
  border: none;
  border-radius: 12px;
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  cursor: pointer;
  transition: filter 0.15s, transform 0.1s;
  box-shadow: 0 4px 14px rgba(34,197,94,0.3);
  position: relative;
  overflow: hidden;
}

.sc-btn::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, rgba(255,255,255,0.12) 0%, transparent 100%);
  pointer-events: none;
}

.sc-btn:hover  { filter: brightness(1.06); }
.sc-btn:active { transform: scale(0.985); filter: brightness(0.96); }

/* ===== Footer ===== */
.sc-footer {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  padding: 12px 24px 16px;
  border-top: 1px solid #f1f5f9;
  font-size: 0.75rem;
  font-weight: 500;
  color: #9ca3af;
  background: #fafbfd;
}

.sc-footer svg {
  width: 11px;
  height: 11px;
  stroke: #22c55e;
  fill: none;
  flex-shrink: 0;
}

/* ===== Responsive ===== */
@media (max-width: 520px) {
  .sc-card {
    border-radius: 16px;
  }

  .sc-title {
    font-size: 1.3rem;
  }

  .sc-desc {
    padding: 0 20px 16px;
  }

  .sc-notes {
    margin: 0 16px 16px;
  }

  .sc-actions {
    padding: 14px 16px 16px;
  }
}
</style>