{% extends "admin_base.html" %}
{% block title %}Registration {{ details.ref }} | Nexora Admin{% endblock %}
{% block content %}
<div class="admin-head">
  <p class="eyebrow">Ticket Details</p>
  <h1>{{ details.name }}</h1>
  <p>{{ details.ref }} · {{ details.game }} · {{ details.payment_status }}</p>
</div>

<section class="admin-panel registration-view-panel pro-ticket-view">
  <div class="view-toolbar">
    <a class="btn outline small" href="{{ url_for('admin_registrations') }}"><i class="fas fa-arrow-left"></i> Back</a>
    {% if can('edit_registrations') %}<a class="btn primary small"
      href="{{ url_for('edit_registration', reg_id=row.id) }}"><i class="fas fa-pen"></i> Edit</a>{% endif %}
    {% if can('download_pdf_tickets') %}<a class="btn outline small" target="_blank"
      href="{{ url_for('registration_ticket_html', reg_id=row.id) }}"><i class="fas fa-print"></i> Print View</a>
    <a class="btn outline small" href="{{ url_for('registration_ticket_pdf', reg_id=row.id) }}"><i
        class="fas fa-file-pdf"></i> Download PDF</a>{% endif %}
  </div>

  <div class="pro-ticket-hero">
    <div>
      <p class="eyebrow on-dark">Official Registration Ticket</p>
      <h2>{{ details.ticket_number }}</h2>
      <p>{{ details.game }} · {{ details.platform }} · {{ details.entry_type }}</p>
      <div class="pro-ticket-badges">
        <span class="badge {{ row.payment_status }}">{{ row.payment_status|replace('_', ' ')|title }}</span>
        {% if row.verified_at %}
        <span class="badge paid">QR Verified</span>
        {% else %}
        <span class="badge neutral">QR Not Scanned</span>
        {% endif %}
      </div>
    </div>
    {% if can('download_pdf_tickets') %}<div class="pro-ticket-side pro-ticket-qr">
      <img src="{{ url_for('registration_qr_png', reg_id=row.id) }}" alt="Ticket QR code">
      <span>Scan Ticket</span>
      <strong>{{ details.ticket_number }}</strong>
    </div>{% endif %}
  </div>

  <div class="pro-view-grid">
    <div class="pro-view-card">
      <h3><i class="fas fa-user"></i> Player Information</h3>
      <div class="pro-detail-list">
        <div><span>Full Name</span><strong>{{ row.full_name }}</strong></div>
        <div><span>Email</span><strong>{{ row.email }}</strong></div>
        <div><span>Contact Number</span><strong>{{ row.phone }}</strong></div>
        <div><span>CNIC</span><strong>{{ row.cnic }}</strong></div>
        <div><span>Institution</span><strong>{{ row.institution_type or 'University' }}</strong>
          <p>{{ row.university }}{% if row.roll_no %} · {{ row.roll_no }}{% endif %}</p>
        </div>
      </div>
    </div>

    <div class="pro-view-card">
      <h3><i class="fas fa-gamepad"></i> Game &amp; Team</h3>
      <div class="pro-detail-list">
        <div><span>Game Applied</span><strong>{{ details.game }}</strong>
          <p>{{ details.platform }}</p>
        </div>
        <div><span>Entry Type</span><strong>{{ details.entry_type }}</strong>
          <p>{{ details.member_count }} member{{ '' if details.member_count == 1 else 's' }}</p>
        </div>
        <div><span>Team Name</span><strong>{{ row.team_name or 'Individual entry' }}</strong></div>
        <div><span>Team ID</span><strong>{{ details.team_id }}</strong></div>
        <div><span>Leader Name</span><strong>{{ row.leader_name or row.full_name }}</strong></div>
      </div>
    </div>
  </div>

  <div class="pro-view-grid lower">
    <div class="pro-view-card">
      <h3><i class="fas fa-user"></i> Main Player</h3>
      <div class="pro-member-full-card">
        <div class="pmfc-qr">
          {% if can('download_pdf_tickets') %}
          <img src="{{ url_for('registration_qr_png', reg_id=row.id) }}" alt="{{ row.full_name }} QR">
          {% else %}
          <div class="pmfc-qr-placeholder"><i class="fas fa-qrcode"></i></div>
          {% endif %}
        </div>
        <div class="pmfc-details">
          <div class="pmfc-name">{{ row.leader_name or row.full_name }}</div>
          <div class="pmfc-meta">
            <span><i class="fas fa-envelope"></i> {{ row.email }}</span>
            <span><i class="fas fa-phone"></i> {{ row.phone }}</span>
          </div>
          <div class="pmfc-meta">
            <span><i class="fas fa-ticket"></i> {{ details.ticket_number }}</span>
            <span><i class="fas fa-fingerprint"></i> {{ details.ticket_id }}</span>
          </div>
          {% if row.team_name %}<div class="pmfc-meta"><span><i class="fas fa-users"></i> {{ row.team_name }} · {{
              details.entry_type }}</span></div>{% endif %}
        </div>
      </div>
    </div>

    {% if members %}
    <div class="pro-view-card">
      <h3><i class="fas fa-users"></i> Team Members ({{ members|length }})</h3>
      <div class="pmfc-grid">
        {% for member in members %}
        {% set mt = member_tickets[loop.index0] if loop.index0 < member_tickets|length else None %} <div
          class="pmfc-row">
          <div class="pmfc-qr">
            {% if mt and can('download_pdf_tickets') %}
            <img src="{{ url_for('member_ticket_qr_png', ticket_id=mt.id) }}" alt="{{ member.name }} QR">
            {% else %}
            <div class="pmfc-qr-placeholder"><i class="fas fa-qrcode"></i></div>
            {% endif %}
          </div>
          <div class="pmfc-details">
            <div class="pmfc-name">{{ member.name or 'Member ' ~ loop.index }}</div>
            <div class="pmfc-meta">
              <span><i class="fas fa-envelope"></i> {{ member.email or 'No email' }}</span>
              <span><i class="fas fa-phone"></i> {{ member.phone or 'No phone' }}</span>
            </div>
            <div class="pmfc-meta">
              <span><i class="fas fa-ticket"></i> {{ mt.ticket_serial if mt else 'Pending' }}</span>
              <span><i class="fas fa-fingerprint"></i> NX-{{ row.id }}-M{{ loop.index }}</span>
            </div>
          </div>
      </div>
      {% endfor %}
    </div>
  </div>
  {% endif %}
  </div>

  <div class="pro-view-grid lower">
    <div class="pro-view-card">
      <h3><i class="fas fa-wallet"></i> Payment &amp; Proof</h3>
      <div class="payment-summary-grid">
        <div><span>Total Payable</span><strong>{{ details.total_payable }}</strong></div>
        <div><span>Amount Paid</span><strong class="payment-paid">{{ details.amount_paid }}</strong></div>
        <div><span>Amount Remaining</span><strong class="payment-remaining">{{ details.remaining_amount }}</strong>
        </div>
      </div>
      <div class="pro-detail-list">
        <div><span>Payment Method</span><strong>{{ row.payment_method }}</strong></div>
        <div><span>Payment Status</span><strong><span class="badge {{ row.payment_status }}">{{ row.payment_status|title
              }}</span></strong></div>
        <div><span>Transaction / Reference ID</span><strong>{{ details.transaction_reference }}</strong></div>
        <div><span>QR Verification</span><strong>{% if row.verified_at %}Verified{% else %}Not scanned{% endif
            %}</strong>
          <p>{% if row.verified_at %}{{ row.verified_at|pretty_dt }}{% else %}Awaiting event-day scan{% endif %}</p>
        </div>
      </div>
      {% if row.proof_image and can('view_payment_details') %}
      <a class="proof-preview pro-proof" href="{{ url_for('registration_proof', reg_id=row.id) }}" target="_blank">
        <img src="{{ url_for('serve_proof_image', reg_id=row.id) }}" alt="Payment proof">
      </a>
      {% else %}
      <p class="muted-line">No payment proof uploaded.</p>
      {% endif %}
    </div>
  </div>

  {% if can('view_checkin_history') %}<div class="pro-view-grid lower">
    <div class="pro-view-card">
      <h3><i class="fas fa-ticket"></i> Ticket Information</h3>
      <div class="pro-detail-list">
        <div><span>Ticket Serial</span><strong>{{ details.ticket_number }}</strong></div>
        <div><span>Ticket ID</span><strong>{{ details.ticket_id }}</strong></div>
        <div><span>Ticket Issue Date</span><strong>{{ details.ticket_issued_at }}</strong></div>
        <div><span>Ticket Status</span><strong>{{ 'Checked In' if row.entry_status == 'approved' else ('Rejected' if
            row.entry_status == 'rejected' else ('Valid' if row.payment_status == 'paid' else 'Payment Pending'))
            }}</strong></div>
      </div>
    </div>
    <div class="pro-view-card">
      <h3><i class="fas fa-right-to-bracket"></i> Check-In History</h3>
      <div class="pro-detail-list">
        <div><span>Team Progress</span><strong>{{ checkin_summary.checked_in }} checked in · {{
            checkin_summary.remaining }} remaining</strong>
          <p>{{ checkin_summary.rejected }} rejected of {{ checkin_summary.total }} total</p>
        </div>
        <div><span>Leader Ticket</span><strong>{{ 'Checked In' if row.entry_status == 'approved' else ('Rejected' if
            row.entry_status == 'rejected' else 'Not Arrived') }}</strong>
          <p>{{ row.entry_marked_at|pretty_dt if row.entry_marked_at else 'Not arrived' }}</p>
        </div>
        {% for ticket in member_tickets %}
        <div><span>Member {{ ticket.member_number }} · {{ ticket.member_name }}</span><strong>{{ 'Checked In' if
            ticket.entry_status == 'approved' else ('Rejected' if ticket.entry_status == 'rejected' else 'Not Arrived')
            }}</strong>
          <p>{{ ticket.entry_marked_at|pretty_dt if ticket.entry_marked_at else 'Not arrived' }}{% if
            ticket.checkin_gate %} · {{ ticket.checkin_gate }}{% endif %}</p>
        </div>
        {% endfor %}
      </div>
    </div>
  </div>{% endif %}

  <div class="pro-view-grid lower">
    <div class="pro-view-card">
      <h3><i class="fas fa-rotate-left"></i> Refund History</h3>
      {% if row.payment_status == 'refunded' %}
      <p class="muted-line">Registration has been marked Refunded. Refund amount is not recorded.</p>
      {% else %}
      <p class="muted-line">No refund has been recorded for this registration.</p>
      {% endif %}
    </div>
    <div class="pro-view-card">
      <h3><i class="fas fa-envelope"></i> Email History</h3>
      <p class="muted-line">Ticket emails are sent on registration and whenever payment is confirmed. SMTP delivery logs
        are not stored in this database.</p>
    </div>
  </div>

  {% if can('view_audit_logs') %}<div class="pro-view-card">
    <h3><i class="fas fa-shield-halved"></i> Audit Log</h3>
    {% if audit_log %}
    <div class="pro-member-list">
      {% for item in audit_log %}
      <div><strong>{{ item.action|replace('_', ' ')|title }} · {{ item.ticket_serial }}</strong><span>{{
          item.created_at|pretty_dt }} · {{ item.staff_username or 'Admin' }}</span><span>{{ item.reason or
          item.gate_location or 'No additional note' }}</span></div>
      {% endfor %}
    </div>
    {% else %}
    <p class="muted-line">No ticket actions have been recorded yet.</p>
    {% endif %}
  </div>{% endif %}
</section>
{% endblock %}