<template>
  <div class="payment-step">
    <v-card-text class="pa-6">
      <!-- Header Section -->
      <div class="header-section text-center mb-8">
        <h2 class="payment-title">Pindai Kode QR untuk Pembayaran</h2>
        <p class="payment-subtitle">
          Gunakan aplikasi mobile banking atau e-wallet Anda
        </p>
      </div>

      <!-- Main Content Section - QR & Details Side by Side -->
      <div class="main-content-section mb-8">
        <div class="content-grid">
          <!-- QR Code Section -->
          <div class="qr-section">
            <div class="qr-container">
              <div class="qris-logo-container mb-0">
                <v-img
                  src="https://iconlogovector.com/uploads/images/2024/03/lg-65ffda68a47ee-QRIS.webp"
                  height="60"
                  class="qris-logo"
                  contain
                />
              </div>
              <div class="qr-wrapper">
                <div class="qr-code-bg">
                  <img
                    ref="qrCodeImage"
                    alt="QR Code"
                    class="qr-image"
                    :class="{ 'qr-loaded': qrLoaded }"
                  />
                  <div class="qr-overlay" v-if="!qrLoaded">
                    <v-progress-circular
                      indeterminate
                      color="primary"
                      size="40"
                    />
                  </div>
                </div>
                <div class="qr-brand-label">
                  <span>QRIS</span>
                </div>
              </div>

              <!-- Scanning Animation -->
              <div class="scan-line" v-if="qrLoaded"></div>
            </div>

            <!-- QR Instructions -->
            <div class="qr-instructions text-center mt-4">
              <v-icon size="20" color="primary" class="mb-2"
                >mdi-qrcode-scan</v-icon
              >
              <p class="instruction-text">Arahkan kamera ke QR code</p>
            </div>
          </div>

          <!-- Payment Details Section -->
          <div class="details-section">
            <div class="payment-details-card">
              <div class="details-header">
                <v-icon color="primary" class="mr-2"
                  >mdi-receipt-text-outline</v-icon
                >
                <span class="details-title">Detail Pembayaran</span>
              </div>

              <div class="details-content">
                <div class="detail-row">
                  <div class="detail-label">
                    <v-icon size="18" class="mr-2" color="primary"
                      >mdi-account-outline</v-icon
                    >
                    Nama Pasien
                  </div>
                  <div class="detail-value patient-name">
                    {{ paymentStore.patientInfo.name }}
                  </div>
                </div>

                <div class="detail-row highlight-row">
                  <div class="detail-label">
                    <v-icon size="18" class="mr-2" color="success"
                      >mdi-cash</v-icon
                    >
                    Nominal
                  </div>
                  <div class="detail-value amount">
                    {{ formatCurrency(paymentStore.patientInfo.amount) }}
                  </div>
                </div>

                <div class="detail-row">
                  <div class="detail-label">
                    <v-icon size="18" class="mr-2" color="primary"
                      >mdi-file-document-outline</v-icon
                    >
                    Nomor Tagihan
                  </div>
                  <div class="detail-value">
                    {{ paymentStore.qrData.display_nobill }}
                  </div>
                </div>

                <div class="detail-row">
                  <div class="detail-label">
                    <v-icon size="18" class="mr-2" color="warning"
                      >mdi-clock-outline</v-icon
                    >
                    Berlaku Sampai
                  </div>
                  <div class="detail-value expired">Tidak terbatas</div>
                </div>
              </div>

              <!-- Status in Details Card -->
              <div class="payment-status mt-4">
                <div class="status-row">
                  <div class="status-indicator-inline">
                    <div class="status-dot pulsing"></div>
                    <span class="status-text">Menunggu pembayaran...</span>
                  </div>
                  <v-chip color="warning" variant="tonal" size="small">
                    <v-icon size="14" class="mr-1">mdi-clock-fast</v-icon>
                    Pending
                  </v-chip>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- Action Buttons -->
      <div class="action-buttons">
        <v-btn
          color="grey-lighten-1"
          variant="outlined"
          size="large"
          class="action-btn back-btn"
          @click="paymentStore.prevStep"
        >
          <v-icon class="mr-2">mdi-arrow-left</v-icon>
          Kembali
        </v-btn>

        <v-btn
          color="success"
          variant="flat"
          size="large"
          class="action-btn simulate-btn"
          @click="paymentStore.nextStep"
        >
          <v-icon class="mr-2">mdi-play-circle-outline</v-icon>
          Simulasi Pembayaran
        </v-btn>
      </div>

      <!-- Help Text -->
      <div class="help-text text-center mt-6">
        <v-icon size="16" class="mr-1">mdi-information-outline</v-icon>
        <span>Scan QR code menggunakan aplikasi pembayaran digital Anda</span>
      </div>
    </v-card-text>
  </div>
</template>

<script setup>
import { usePaymentStore } from "~/stores/payment";
import QRCode from "qrcode";
import { ref, onMounted, watch } from "vue";

const paymentStore = usePaymentStore();
const qrCodeImage = ref(null);
const qrLoaded = ref(false);

const formatCurrency = (amount) => {
  return new Intl.NumberFormat("id-ID", {
    style: "currency",
    currency: "IDR",
    minimumFractionDigits: 0,
  }).format(amount);
};

const generateQRCode = async () => {
  const qrValue = paymentStore.qrData.qrvalue;
  if (qrValue) {
    try {
      qrLoaded.value = false;
      const dataUrl = await QRCode.toDataURL(qrValue, {
        width: 240,
        margin: 1,
        color: {
          dark: "#000000",
          light: "#FFFFFF",
        },
        errorCorrectionLevel: "M",
      });
      if (qrCodeImage.value) {
        qrCodeImage.value.src = dataUrl;
        qrCodeImage.value.onload = () => {
          qrLoaded.value = true;
        };
      }
    } catch (error) {
      console.error("Failed to generate QR Code:", error);
    }
  }
};

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

watch(
  () => paymentStore.qrData,
  () => {
    generateQRCode();
  }
);
</script>

<style scoped>
.payment-step {
  background: linear-gradient(135deg, #f8f9ff 0%, #e8f2ff 100%);
  min-height: 100vh;
  height: auto;
  padding: 1rem 0;
}

/* Header Section */
.header-section {
  padding-top: 8px;
}

.qris-logo-container {
  position: center;
  /* display: inline-block; */
}

.qris-logo {
  filter: drop-shadow(0 2px 8px rgba(0, 0, 0, 0.1));
  transition: transform 0.3s ease;
}

.qris-logo:hover {
  transform: scale(1.05);
}

.payment-title {
  font-size: 1.5rem;
  font-weight: 700;
  color: #1a1a1a;
  margin-top: 1rem;
  margin-bottom: 0.5rem;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.payment-subtitle {
  color: #666;
  font-size: 0.9rem;
  margin-bottom: 0;
}

/* Main Content Grid Layout */
.main-content-section {
  width: 100%;
}

.content-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 3rem;
  align-items: start;
  max-width: 1000px;
  margin: 0 auto;
}

/* QR Code Section */
.qr-section {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.details-section {
  display: flex;
  flex-direction: column;
}

.qr-container {
  position: relative;
  background: linear-gradient(145deg, #ffffff, #f0f4f8);
  border-radius: 24px;
  padding: 2rem;
  box-shadow:
    0 20px 40px rgba(0, 0, 0, 0.08),
    0 8px 16px rgba(0, 0, 0, 0.04),
    inset 0 1px 0 rgba(255, 255, 255, 0.8);
  border: 1px solid rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  width: 100%;
  max-width: 350px;
}

.qr-wrapper {
  position: relative;
  display: inline-block;
}

.qr-code-bg {
  background: #ffffff;
  border-radius: 16px;
  padding: 1rem;
  box-shadow:
    0 8px 32px rgba(0, 0, 0, 0.06),
    inset 0 1px 0 rgba(255, 255, 255, 0.9);
  position: relative;
  overflow: hidden;
}

.qr-image {
  width: 240px;
  height: 240px;
  border-radius: 8px;
  transition:
    opacity 0.3s ease,
    transform 0.3s ease;
  opacity: 0;
  transform: scale(0.9);
}

.qr-image.qr-loaded {
  opacity: 1;
  transform: scale(1);
}

.qr-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(248, 250, 252, 0.9);
  border-radius: 8px;
}

.qr-brand-label {
  position: absolute;
  bottom: -8px;
  right: -8px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 0.25rem 0.75rem;
  border-radius: 12px;
  font-size: 0.7rem;
  font-weight: 700;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}

/* Scanning Animation */
.scan-line {
  position: absolute;
  top: 2rem;
  left: 2rem;
  right: 2rem;
  height: 2px;
  background: linear-gradient(90deg, transparent, #667eea, transparent);
  animation: scanning 2s ease-in-out infinite;
}

@keyframes scanning {
  0% {
    top: 2rem;
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  100% {
    top: calc(100% - 2rem);
    opacity: 0;
  }
}

/* QR Instructions */
.qr-instructions {
  margin-top: 1rem;
}

.instruction-text {
  color: #666;
  font-size: 0.9rem;
  font-weight: 500;
  margin: 0;
}

/* Payment Details Card */
.payment-details-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  padding: 2rem;
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow:
    0 8px 32px rgba(0, 0, 0, 0.06),
    inset 0 1px 0 rgba(255, 255, 255, 0.8);
  height: fit-content;
  width: 100%;
}

.details-header {
  display: flex;
  align-items: center;
  margin-bottom: 1.5rem;
  padding-bottom: 1rem;
  border-bottom: 2px solid rgba(0, 0, 0, 0.06);
}

.details-title {
  font-weight: 700;
  color: #1a1a1a;
  font-size: 1.2rem;
}

.details-content {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.detail-row {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  padding: 0.5rem;
  border-radius: 12px;
  background: rgba(248, 250, 252, 0.6);
  border: 1px solid rgba(0, 0, 0, 0.04);
  transition: all 0.3s ease;
}

.detail-row:hover {
  background: rgba(248, 250, 252, 0.9);
  transform: translateY(-1px);
}

.highlight-row {
  background: rgba(46, 204, 113, 0.08) !important;
  border: 1px solid rgba(46, 204, 113, 0.2) !important;
}

.highlight-row:hover {
  background: rgba(46, 204, 113, 0.12) !important;
}

.detail-label {
  display: flex;
  align-items: center;
  color: #666;
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.detail-value {
  color: #1a1a1a;
  font-weight: 700;
  font-size: 1.1rem;
  margin-left: 1.5rem;
}

.detail-value.patient-name {
  color: #2c3e50;
  font-size: 1.2rem;
}

.detail-value.amount {
  color: #27ae60;
  font-size: 1.4rem;
  font-weight: 800;
}

.detail-value.expired {
  color: #e67e22;
  font-size: 1rem;
}

/* Payment Status */
.payment-status {
  padding: 1rem;
  background: rgba(255, 193, 7, 0.08);
  border-radius: 12px;
  border: 1px solid rgba(255, 193, 7, 0.2);
}

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

.status-indicator-inline {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

/* Status Indicator */
.status-indicator {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #f39c12;
}

.status-dot.pulsing {
  animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
  0%,
  100% {
    opacity: 1;
    transform: scale(1);
  }
  50% {
    opacity: 0.5;
    transform: scale(1.2);
  }
}

.status-text {
  color: #666;
  font-size: 0.9rem;
  font-weight: 500;
}

/* Action Buttons */
.action-buttons {
  display: flex;
  gap: 1rem;
  justify-content: center;
}

.action-btn {
  border-radius: 16px !important;
  font-weight: 600 !important;
  text-transform: none !important;
  padding: 0 2rem !important;
  min-width: 140px;
  transition: all 0.3s ease !important;
}

.back-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15) !important;
}

.simulate-btn {
  background: linear-gradient(135deg, #27ae60 0%, #2ecc71 100%) !important;
  box-shadow: 0 4px 15px rgba(46, 204, 113, 0.3) !important;
}

.simulate-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(46, 204, 113, 0.4) !important;
}

/* Help Text */
.help-text {
  color: #888;
  font-size: 0.8rem;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.25rem;
}

/* Responsive Design */
@media (max-width: 900px) {
  .content-grid {
    grid-template-columns: 1fr;
    gap: 2rem;
  }

  .qr-container {
    max-width: 320px;
  }

  .payment-details-card {
    width: 100%;
  }
}

@media (max-width: 480px) {
  .qr-container {
    padding: 1.5rem;
    max-width: 280px;
  }

  .qr-image {
    width: 200px;
    height: 200px;
  }

  .action-buttons {
    flex-direction: column;
  }

  .action-btn {
    min-width: 100%;
  }

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

  .payment-details-card {
    padding: 1.5rem;
  }
}

/* Tablet Landscape Optimization */
@media (orientation: landscape) and (min-width: 768px) and (max-height: 600px) {
  .header-section {
    padding-top: 0.5rem;
    margin-bottom: 2rem;
  }

  .payment-title {
    font-size: 1.3rem;
    margin-bottom: 0.25rem;
  }

  .payment-subtitle {
    font-size: 0.8rem;
  }

  .qr-container {
    padding: 1.5rem;
  }

  .qr-image {
    width: 200px;
    height: 200px;
  }

  .payment-details-card {
    padding: 1.5rem;
    min-height: 300px;
  }

  .content-grid {
    gap: 2rem;
  }
}

/* Large Tablet Landscape */
@media (orientation: landscape) and (min-width: 1024px) {
  .content-grid {
    max-width: 1200px;
    gap: 4rem;
  }

  .qr-container {
    max-width: 400px;
  }

  .qr-image {
    width: 280px;
    height: 280px;
  }

  .payment-details-card {
    width: 100%;
  }
}
</style>
