{% extends "base.html" %}
{% load static %}

{% block title %}Ticket — {{ ticket.client_nom }} | BilletFest{% endblock %}

{% block content %}
<link href="https://fonts.googleapis.com/css2?family=Bebas+Neue&family=DM+Sans:ital,wght@0,300;0,400;0,600;1,300&family=Space+Mono:wght@400;700&display=swap" rel="stylesheet">

<div class="tp-page">

  <!-- ── Notification succès ─────────────────────────────── -->
  <div class="tp-toast" id="successToast">
    <span class="tp-toast-icon">✦</span>
    Ticket généré avec succès !
    <button class="tp-toast-close" onclick="this.parentElement.style.opacity='0'">✕</button>
  </div>

  <!-- ── Titre de page ──────────────────────────────────── -->
  <header class="tp-header">
    <p class="tp-header-eyebrow">Aperçu du billet</p>
    <h1 class="tp-header-title">Votre ticket est prêt</h1>
  </header>

  <!-- ══════════════════════════════════════════════════════
       LE BILLET — zone capturée par html2pdf
       ══════════════════════════════════════════════════════ -->
  <div class="tp-stage">
    <div id="ticket-to-print" class="ticket">

      <!-- Bande gauche colorée + tallon -->
      <div class="ticket-spine">
        <span class="ticket-spine-text">BILLETFEST</span>
        <span class="ticket-spine-year">2025</span>
      </div>

      <!-- Corps principal du billet -->
      <div class="ticket-body">

        <!-- ── Zone haute : infos événement ── -->
        <div class="ticket-top">

          <!-- Arrière plan décoratif : cercles + lignes -->
          <div class="ticket-bg-deco" aria-hidden="true">
            <div class="deco-circle deco-c1"></div>
            <div class="deco-circle deco-c2"></div>
            <div class="deco-lines"></div>
          </div>

          <!-- Badge type de ticket -->
          <div class="ticket-badge">
            <span class="ticket-badge-star">★</span>
            {{ ticket.type_ticket.nom|upper }}
            <span class="ticket-badge-star">★</span>
          </div>

          <!-- Nom de l'événement -->
          <h2 class="ticket-event-name">{{ ticket.type_ticket.evenement.nom }}</h2>

          <!-- Métadonnées événement -->
          <div class="ticket-meta-row">
            <div class="ticket-meta-item">
              <span class="ticket-meta-icon">📅</span>
              <div>
                <span class="ticket-meta-label">Date</span>
                <span class="ticket-meta-value">{{ ticket.type_ticket.evenement.date|date:"l d F Y" }}</span>
              </div>
            </div>
            <div class="ticket-meta-sep"></div>
            <div class="ticket-meta-item">
              <span class="ticket-meta-icon">🕐</span>
              <div>
                <span class="ticket-meta-label">Heure</span>
                <span class="ticket-meta-value">{{ ticket.type_ticket.evenement.date|date:"H\hi" }}</span>
              </div>
            </div>
            <div class="ticket-meta-sep"></div>
            <div class="ticket-meta-item">
              <span class="ticket-meta-icon">💰</span>
              <div>
                <span class="ticket-meta-label">Tarif</span>
                <span class="ticket-meta-value">{{ ticket.type_ticket.prix|floatformat:0 }} FCFA</span>
              </div>
            </div>
          </div>
        </div>

        <!-- ── Ligne de découpe ── -->
        <div class="ticket-perforation">
          <div class="ticket-notch ticket-notch-left"></div>
          <div class="ticket-dash-line"></div>
          <div class="ticket-scissors">✂</div>
          <div class="ticket-dash-line"></div>
          <div class="ticket-notch ticket-notch-right"></div>
        </div>

        <!-- ── Zone basse : client + QR ── -->
        <div class="ticket-bottom">

          <!-- Infos client -->
          <div class="ticket-holder">
            <span class="ticket-holder-label">Détenteur du billet</span>
            <span class="ticket-holder-name">{{ ticket.client_nom }}</span>
            <div class="ticket-holder-pill">ENTRÉE VALIDE</div>
          </div>

          <!-- QR Code -->
          <div class="ticket-qr-block">
            <div class="ticket-qr-frame">
              <img
                src="{{ ticket.qr_code.url }}"
                alt="QR Code ticket"
                class="ticket-qr-img"
              />
            </div>
            <span class="ticket-qr-hint">Scanner à l'entrée</span>
          </div>

        </div>

        <!-- ── UUID en pied de billet ── -->
        <div class="ticket-uuid">
          <span class="ticket-uuid-label">ID :</span>
          <span class="ticket-uuid-code">{{ ticket.code_unique }}</span>
        </div>

      </div><!-- /ticket-body -->

      <!-- Coins décoratifs -->
      <div class="ticket-corner ticket-corner-tl"></div>
      <div class="ticket-corner ticket-corner-tr"></div>
      <div class="ticket-corner ticket-corner-bl"></div>
      <div class="ticket-corner ticket-corner-br"></div>

    </div><!-- /#ticket-to-print -->
  </div><!-- /.tp-stage -->

  <!-- ── Actions ────────────────────────────────────────── -->
  <div class="tp-actions">
    <button id="btnDownload" class="tp-btn tp-btn-primary" onclick="downloadTicket()">
      <span class="tp-btn-icon">📥</span>
      Télécharger le Ticket (PDF)
      <span class="tp-btn-loader" id="btnLoader" style="display:none">⏳</span>
    </button>

    <a href="{% url 'dashboard:evenement_detail' ticket.type_ticket.evenement.id %}"
       class="tp-btn tp-btn-ghost">
      <span class="tp-btn-icon">←</span>
      Nouvelle vente
    </a>
  </div>

</div><!-- /.tp-page -->


<!-- ════════════════════════════════════════════════════════
     STYLES
     ════════════════════════════════════════════════════════ -->
<style>
/* ── Variables ─────────────────────────────────────────── */
:root {
  --tk-bg:       #0d0d12;
  --tk-surface:  #16161f;
  --tk-gold:     #f5c842;
  --tk-gold2:    #e8994d;
  --tk-rose:     #ff4d6d;
  --tk-violet:   #7c3aed;
  --tk-cyan:     #22d3ee;
  --tk-text:     #f0ede8;
  --tk-muted:    #8a8799;
  --tk-border:   rgba(245,200,66,.18);
  --tk-radius:   18px;

  /* Couleurs figées pour le PDF (pas de var() dynamiques) */
  --pdf-dark:    #111118;
  --pdf-light:   #faf8f2;
}

/* ── Page wrapper ───────────────────────────────────────── */
.tp-page {
  font-family: 'DM Sans', sans-serif;
  min-height: 100vh;
  background: var(--tk-bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 40px 20px 64px;
  gap: 32px;
}

/* ── Toast ─────────────────────────────────────────────── */
.tp-toast {
  display: flex;
  align-items: center;
  gap: 10px;
  background: linear-gradient(135deg, #052e0f, #073918);
  border: 1px solid #15803d;
  color: #4ade80;
  font-weight: 600;
  font-size: 14px;
  padding: 12px 20px;
  border-radius: 12px;
  box-shadow: 0 4px 24px rgba(0,0,0,.4);
  animation: toastSlide .5s cubic-bezier(.34,1.3,.64,1) both;
  transition: opacity .3s;
  max-width: 480px;
  width: 100%;
}
.tp-toast-icon { font-size: 18px; }
.tp-toast-close {
  margin-left: auto;
  background: none; border: none; cursor: pointer;
  color: #4ade80; font-size: 14px; opacity: .7;
}
@keyframes toastSlide {
  from { transform: translateY(-16px); opacity: 0; }
  to   { transform: translateY(0);     opacity: 1; }
}

/* ── Header ────────────────────────────────────────────── */
.tp-header { text-align: center; }
.tp-header-eyebrow {
  font-size: 11px;
  letter-spacing: .22em;
  text-transform: uppercase;
  color: var(--tk-gold);
  margin-bottom: 6px;
}
.tp-header-title {
  font-family: 'Bebas Neue', sans-serif;
  font-size: clamp(2.2rem, 6vw, 3.2rem);
  color: var(--tk-text);
  letter-spacing: .04em;
}

/* ── Stage (ombre autour du billet) ─────────────────────── */
.tp-stage {
  display: flex;
  justify-content: center;
  width: 100%;
  /* Perspective pour que l'ombre flotte */
  filter: drop-shadow(0 30px 60px rgba(0,0,0,.7))
          drop-shadow(0 0 40px rgba(245,200,66,.12));
  animation: stageReveal .8s cubic-bezier(.34,1.2,.64,1) both;
  animation-delay: .15s;
}
@keyframes stageReveal {
  from { transform: translateY(24px) scale(.96); opacity: 0; }
  to   { transform: translateY(0)    scale(1);   opacity: 1; }
}

/* ══════════════════════════════════════════════════════════
   LE BILLET — Dimensions fixes pour correspondre au PDF
   ══════════════════════════════════════════════════════════ */
.ticket {
  position: relative;
  display: flex;
  flex-direction: row;
  width: 720px;
  min-height: 290px;
  border-radius: 20px;
  overflow: hidden;
  background: var(--pdf-light);
  font-family: 'DM Sans', sans-serif;
  /* On fixe la couleur explicitement pour html2pdf */
  color: #111118;
}

/* ── Spine (bande latérale gauche) ─────────────────────── */
.ticket-spine {
  width: 46px;
  background: linear-gradient(180deg, #f5c842 0%, #e8994d 60%, #ff4d6d 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 18px 0;
  flex-shrink: 0;
  position: relative;
  z-index: 2;
}
.ticket-spine-text {
  font-family: 'Bebas Neue', sans-serif;
  font-size: 13px;
  letter-spacing: .3em;
  color: #fff;
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  text-shadow: 0 1px 4px rgba(0,0,0,.25);
}
.ticket-spine-year {
  font-family: 'Space Mono', monospace;
  font-size: 11px;
  color: rgba(255,255,255,.7);
  writing-mode: vertical-rl;
  transform: rotate(180deg);
}

/* ── Corps ─────────────────────────────────────────────── */
.ticket-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
}

/* ── Zone haute ─────────────────────────────────────────── */
.ticket-top {
  position: relative;
  background: linear-gradient(135deg, #111118 0%, #1c1730 100%);
  padding: 24px 28px 20px;
  overflow: hidden;
  flex-shrink: 0;
}

/* Déco géométrique fond sombre */
.ticket-bg-deco { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.deco-circle {
  position: absolute;
  border-radius: 50%;
  border: 1px solid rgba(245,200,66,.12);
}
.deco-c1 { width: 340px; height: 340px; top: -120px; right: -60px; }
.deco-c2 { width: 200px; height: 200px; top: -60px; right: 40px; border-color: rgba(245,200,66,.07); }
.deco-lines {
  position: absolute;
  inset: 0;
  background-image: repeating-linear-gradient(
    -55deg,
    transparent,
    transparent 28px,
    rgba(255,255,255,.025) 28px,
    rgba(255,255,255,.025) 29px
  );
}

/* Badge type */
.ticket-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: linear-gradient(90deg, #f5c842, #e8994d);
  color: #111118;
  font-family: 'Bebas Neue', sans-serif;
  font-size: 13px;
  letter-spacing: .18em;
  padding: 4px 14px;
  border-radius: 999px;
  margin-bottom: 10px;
  position: relative; z-index: 1;
  box-shadow: 0 2px 12px rgba(245,200,66,.35);
}
.ticket-badge-star { font-size: 9px; }

/* Nom événement */
.ticket-event-name {
  font-family: 'Bebas Neue', sans-serif;
  font-size: clamp(1.8rem, 4vw, 2.8rem);
  color: #fff;
  line-height: 1.05;
  letter-spacing: .03em;
  position: relative; z-index: 1;
  margin-bottom: 16px;
  /* Dégradé texte */
  background: linear-gradient(90deg, #ffffff 0%, #f5c842 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* Métadonnées */
.ticket-meta-row {
  display: flex;
  align-items: center;
  gap: 0;
  position: relative; z-index: 1;
  flex-wrap: wrap;
}
.ticket-meta-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 16px 0 0;
}
.ticket-meta-item:first-child { padding-left: 0; }
.ticket-meta-icon { font-size: 16px; }
.ticket-meta-label {
  display: block;
  font-size: 9px;
  letter-spacing: .15em;
  text-transform: uppercase;
  color: rgba(255,255,255,.45);
  line-height: 1;
  margin-bottom: 2px;
}
.ticket-meta-value {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: #fff;
  line-height: 1;
}
.ticket-meta-sep {
  width: 1px;
  height: 28px;
  background: rgba(255,255,255,.15);
  margin-right: 16px;
}

/* ── Ligne de découpe ───────────────────────────────────── */
.ticket-perforation {
  display: flex;
  align-items: center;
  background: #faf8f2;
  position: relative;
  height: 24px;
}
.ticket-notch {
  width: 20px; height: 20px;
  background: var(--tk-bg);        /* même couleur que la page */
  border-radius: 50%;
  position: absolute;
  top: 50%; transform: translateY(-50%);
  flex-shrink: 0;
}
.ticket-notch-left  { left: -10px; }
.ticket-notch-right { right: -10px; }
.ticket-dash-line {
  flex: 1;
  height: 0;
  border-top: 2px dashed rgba(17,17,24,.18);
  margin: 0 6px;
}
.ticket-scissors {
  font-size: 16px;
  color: rgba(17,17,24,.3);
  transform: rotate(-90deg);
  flex-shrink: 0;
  margin: 0 4px;
  line-height: 1;
}

/* ── Zone basse ─────────────────────────────────────────── */
.ticket-bottom {
  background: #faf8f2;
  padding: 18px 28px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  flex: 1;
}

/* Infos détenteur */
.ticket-holder {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.ticket-holder-label {
  font-size: 9px;
  letter-spacing: .18em;
  text-transform: uppercase;
  color: #8a8799;
  font-weight: 400;
}
.ticket-holder-name {
  font-family: 'Bebas Neue', sans-serif;
  font-size: clamp(1.4rem, 3vw, 2rem);
  color: #111118;
  letter-spacing: .04em;
  line-height: 1;
}
.ticket-holder-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: #111118;
  color: #f5c842;
  font-size: 9px;
  font-weight: 700;
  letter-spacing: .18em;
  text-transform: uppercase;
  padding: 4px 10px;
  border-radius: 999px;
  margin-top: 6px;
  width: fit-content;
}
.ticket-holder-pill::before {
  content: '✓';
  width: 12px; height: 12px;
  background: #f5c842;
  color: #111118;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 8px;
}

/* QR code */
.ticket-qr-block {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}
.ticket-qr-frame {
  padding: 8px;
  background: #fff;
  border-radius: 12px;
  border: 2px solid rgba(17,17,24,.08);
  box-shadow: 0 2px 12px rgba(0,0,0,.08);
  line-height: 0;
}
.ticket-qr-img {
  width: 100px;
  height: 100px;
  display: block;
  object-fit: contain;
}
.ticket-qr-hint {
  font-size: 9px;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: #8a8799;
}

/* ── UUID ────────────────────────────────────────────────── */
.ticket-uuid {
  background: #faf8f2;
  padding: 8px 28px 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  border-top: 1px dashed rgba(17,17,24,.1);
}
.ticket-uuid-label {
  font-size: 9px;
  letter-spacing: .15em;
  text-transform: uppercase;
  color: #8a8799;
}
.ticket-uuid-code {
  font-family: 'Space Mono', monospace;
  font-size: 10px;
  color: #555;
  word-break: break-all;
}

/* ── Coins décoratifs ───────────────────────────────────── */
.ticket-corner {
  position: absolute;
  width: 20px; height: 20px;
  border-color: var(--tk-gold);
  border-style: solid;
  pointer-events: none;
  z-index: 3;
}
.ticket-corner-tl { top: 10px;  left: 10px;  border-width: 2px 0 0 2px; }
.ticket-corner-tr { top: 10px;  right: 10px; border-width: 2px 2px 0 0; }
.ticket-corner-bl { bottom: 10px; left: 10px;  border-width: 0 0 2px 2px; }
.ticket-corner-br { bottom: 10px; right: 10px; border-width: 0 2px 2px 0; }

/* ── Actions ─────────────────────────────────────────────── */
.tp-actions {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  justify-content: center;
}
.tp-btn {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  padding: 14px 28px;
  border-radius: 14px;
  font-family: 'DM Sans', sans-serif;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  border: none;
  text-decoration: none;
  transition: transform .18s ease, box-shadow .18s ease, opacity .18s;
}
.tp-btn:hover { transform: translateY(-2px); }
.tp-btn:active { transform: scale(.97); }

.tp-btn-primary {
  background: linear-gradient(135deg, #f5c842, #e8994d);
  color: #111118;
  box-shadow: 0 6px 24px rgba(245,200,66,.35);
}
.tp-btn-primary:hover { box-shadow: 0 12px 36px rgba(245,200,66,.5); }
.tp-btn-primary:disabled { opacity: .65; cursor: wait; }

.tp-btn-ghost {
  background: rgba(255,255,255,.06);
  color: var(--tk-text);
  border: 1px solid rgba(255,255,255,.12);
}
.tp-btn-ghost:hover { background: rgba(255,255,255,.1); }

.tp-btn-icon { font-size: 18px; line-height: 1; }

/* ── Responsive ─────────────────────────────────────────── */
@media (max-width: 760px) {
  .ticket { width: 100%; min-width: 0; flex-direction: column; }
  .ticket-spine {
    width: 100%; height: 36px;
    flex-direction: row;
    writing-mode: horizontal-tb;
    padding: 0 16px;
  }
  .ticket-spine-text,
  .ticket-spine-year { writing-mode: horizontal-tb; transform: none; }
  .tp-stage { filter: drop-shadow(0 16px 32px rgba(0,0,0,.6)); }
  .ticket-event-name { font-size: 1.6rem; }
  .ticket-qr-img { width: 80px; height: 80px; }
}
</style>


<!-- ════════════════════════════════════════════════════════
     JAVASCRIPT — html2pdf (version locale)
     ════════════════════════════════════════════════════════ -->
{% block extrascripts %}
<script src="{% static 'js/html2pdf.bundle.min.js' %}"></script>

<script>
function downloadTicket() {
  const btn     = document.getElementById('btnDownload');
  const loader  = document.getElementById('btnLoader');
  const element = document.getElementById('ticket-to-print');

  /* Nom de fichier */
  const clientName = "{{ ticket.client_nom|slugify }}";
  const eventName  = "{{ ticket.type_ticket.evenement.nom|slugify }}";
  const filename   = `Ticket_${clientName}_${eventName}.pdf`;

  btn.disabled  = true;
  loader.style.display = 'inline';

  /* ── Options html2pdf ─────────────────────────────────── */
  const opt = {
    margin:       0,
    filename:     filename,
    image:        { type: 'jpeg', quality: 1 },
    html2canvas: {
      scale:            3,          /* haute résolution */
      useCORS:          true,
      allowTaint:       true,
      logging:          false,
      backgroundColor:  '#ffffff',
      /* Taille fixe = exactement le billet affiché */
      width:  element.offsetWidth,
      height: element.offsetHeight,
      scrollX: 0,
      scrollY: 0,
    },
    jsPDF: {
      unit:        'px',
      format:      [element.offsetWidth, element.offsetHeight],
      orientation: 'landscape',
      hotfixes:    ['px_scaling'],
    },
    pagebreak: { mode: 'avoid-all' },   /* Jamais de coupure de page */
  };

  html2pdf()
    .set(opt)
    .from(element)
    .save()
    .then(() => {
      btn.disabled = false;
      loader.style.display = 'none';
    })
    .catch(err => {
      console.error('Erreur export PDF :', err);
      btn.disabled = false;
      loader.style.display = 'none';
      alert('Une erreur est survenue lors de l\'export. Veuillez réessayer.');
    });
}

/* Auto-masquage du toast après 4s */
setTimeout(() => {
  const t = document.getElementById('successToast');
  if (t) t.style.opacity = '0';
}, 4000);
</script>
{% endblock %}

{% endblock %}