<?php $this->start('body');
    $t = $this->data['t']; $dls = $this->data['downloads']; $url = $this->data['install_url'];
    $state = ((int)$t['revoked'] === 1) ? 'revoked'
            : (strtotime($t['expires_on']) < time() ? 'expired' : 'active');
    $bg = ['active'=>'success','expired'=>'secondary','revoked'=>'dark'][$state];

    // Generate QR of the install URL — reuse our slice-26 encoder + PNG output
    if (!class_exists('QrCode')) require_once ROOT . '/app/lib/qrcode/QrCode.php';
    $qrPng = __DIR__ . '/../../../images/qr-cache/install-' . $t['c0d'] . '.png';
    @mkdir(dirname($qrPng), 0775, true);
    if (!is_file($qrPng)) QrCode::png($url, $qrPng, 6, 3);
    $qrRel = 'images/qr-cache/install-' . $t['c0d'] . '.png';
?>

<div class="d-flex align-items-center justify-content-between mb-3 flex-wrap gap-2">
  <div>
    <h5 class="mb-0 fw-semibold">
      <i class="fas fa-link me-2 text-muted"></i>
      <?= htmlspecialchars($t['label'] ?: 'Install link'); ?>
      <span class="badge bg-<?= $bg; ?> ms-2" style="font-size:.6em;vertical-align:middle;text-transform:uppercase;"><?= $state; ?></span>
    </h5>
    <div class="text-muted small">
      Created <?= humanDateTime($t['created_on']); ?> by <?= htmlspecialchars($t['created_by_name']); ?>
      &middot; expires <?= humanDateTime($t['expires_on']); ?>
    </div>
  </div>
  <a href="<?= PROOT; ?>Download" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

<?= flashHtml(); ?>

<div class="row g-3 mb-3">
  <div class="col-lg-7">
    <div class="card border-0 shadow-sm h-100"><div class="card-body">
      <h6 class="text-uppercase text-muted small mb-3">Share this link</h6>

      <label class="form-label small text-muted">Install URL</label>
      <div class="input-group mb-3">
        <input type="text" class="form-control font-monospace small" id="urlBox" readonly value="<?= htmlspecialchars($url); ?>">
        <button class="btn btn-primary" type="button" onclick="navigator.clipboard.writeText(document.getElementById('urlBox').value); this.innerHTML='<i class=\'fas fa-check me-1\'></i>Copied';">
          <i class="fas fa-copy me-1"></i>Copy
        </button>
      </div>

      <div class="d-flex flex-wrap gap-2">
        <a href="https://wa.me/?text=<?= rawurlencode("Install the Pacific Driver app: $url"); ?>" target="_blank" class="btn btn-outline-success btn-sm">
          <i class="fab fa-whatsapp me-1"></i>Share on WhatsApp
        </a>
        <a href="sms:?body=<?= rawurlencode("Install the Pacific Driver app: $url"); ?>" class="btn btn-outline-primary btn-sm">
          <i class="fas fa-sms me-1"></i>Share via SMS
        </a>
        <a href="mailto:?subject=<?= rawurlencode('Install Pacific Driver'); ?>&body=<?= rawurlencode("Install the Pacific Driver app from this link: $url"); ?>" class="btn btn-outline-secondary btn-sm">
          <i class="fas fa-envelope me-1"></i>Email
        </a>
      </div>

      <?php if ($state === 'active'): ?>
        <hr class="my-3">
        <form method="POST" action="<?= PROOT; ?>Download/revoke/<?= (int)$t['c0d']; ?>" onsubmit="return confirm('Revoke this link? Existing downloads keep working; new taps get blocked.');">
          <?= csrf_field(); ?>
          <button class="btn btn-outline-danger btn-sm"><i class="fas fa-ban me-1"></i>Revoke link</button>
        </form>
      <?php endif; ?>
    </div></div>
  </div>
  <div class="col-lg-5">
    <div class="card border-0 shadow-sm h-100"><div class="card-body text-center">
      <h6 class="text-uppercase text-muted small mb-3">QR code</h6>
      <img src="<?= PROOT; ?><?= htmlspecialchars($qrRel); ?>" alt="Install QR" style="max-width:220px; width:100%;">
      <div class="small text-muted mt-2">Have the driver point their phone camera at this.</div>
    </div></div>
  </div>
</div>

<!-- Download history -->
<div class="card border-0 shadow-sm">
  <div class="card-body p-0">
    <div class="p-3 pb-2 d-flex justify-content-between align-items-center">
      <h6 class="text-uppercase text-muted small mb-0">Drivers who installed &middot; <?= count($dls); ?></h6>
    </div>
    <?php if (empty($dls)): ?>
      <div class="text-center text-muted small py-4">Nobody's used this link yet.</div>
    <?php else: ?>
      <div class="table-responsive"><table class="table table-sm mb-0 align-middle">
        <thead class="table-light"><tr><th>Name</th><th>Phone</th><th>Employee #</th><th>When</th><th>IP</th></tr></thead>
        <tbody>
          <?php foreach ($dls as $d): ?>
            <tr>
              <td class="small fw-semibold"><?= htmlspecialchars($d['downloader_name']); ?></td>
              <td class="small font-monospace"><?= htmlspecialchars($d['downloader_phone']); ?></td>
              <td class="small font-monospace"><?= htmlspecialchars($d['downloader_employee_no'] ?? '—') ?: '—'; ?></td>
              <td class="small"><?= humanDateTime($d['downloaded_on']); ?></td>
              <td class="small text-muted font-monospace"><?= htmlspecialchars($d['ip_address'] ?? '—') ?: '—'; ?></td>
            </tr>
          <?php endforeach; ?>
        </tbody>
      </table></div>
    <?php endif; ?>
  </div>
</div>
<?php $this->end(); ?>
