<?php $this->start('body'); ?>
<?php $tokens = $this->data['tokens']; $meta = $this->data['apk_meta']; ?>

<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-mobile-alt me-2 text-muted"></i>App Downloads</h5>
    <div class="text-muted small">Generate a share link that lets one or more drivers install the Pacific Driver app.</div>
  </div>
  <div>
    <a href="<?= PROOT; ?>Download/apkAdmin" class="btn btn-sm btn-outline-secondary" target="_blank"><i class="fas fa-download me-1"></i>Get APK (admin)</a>
  </div>
</div>

<?= flashHtml(); ?>

<!-- APK snapshot card -->
<div class="card border-0 shadow-sm mb-3"><div class="card-body py-3 d-flex align-items-center flex-wrap gap-3">
  <div class="d-flex align-items-center gap-3 flex-grow-1">
    <div style="width:56px; height:56px; border-radius:14px; background:var(--pacific-navy,#2626F5); color:#fff; display:flex; align-items:center; justify-content:center; font-size:26px; font-weight:800;">P</div>
    <div>
      <div style="font-size:16px; font-weight:800;">Pacific Driver <span class="badge bg-light text-muted ms-1">v<?= htmlspecialchars($meta['version'] ?? '—'); ?></span></div>
      <div class="small text-muted">
        <?php if ($meta['size']): ?><?= number_format($meta['size'] / 1024 / 1024, 0); ?> MB &middot; <?php endif; ?>
        <?php if ($meta['built_on']): ?>built <?= htmlspecialchars($meta['built_on']); ?><?php else: ?><span class="text-warning">APK not yet built</span><?php endif; ?>
      </div>
    </div>
  </div>
</div></div>

<!-- Generate new link -->
<div class="card border-0 shadow-sm mb-3"><div class="card-body">
  <h6 class="text-uppercase text-muted small mb-3">Create a new install link</h6>
  <form method="POST" action="<?= PROOT; ?>Download/generate">
    <?= csrf_field(); ?>
    <div class="row g-3">
      <div class="col-md-7"><label class="form-label small">Label <span class="text-muted small">(optional &mdash; what/who is this link for)</span></label>
        <input type="text" name="label" class="form-control" placeholder="e.g. New drivers · Sept 2026"></div>
      <div class="col-md-2"><label class="form-label small">Expires in</label>
        <select name="days" class="form-select">
          <option value="1">1 day</option>
          <option value="3">3 days</option>
          <option value="7" selected>7 days</option>
          <option value="14">14 days</option>
          <option value="30">30 days</option>
          <option value="90">90 days</option>
        </select>
      </div>
      <div class="col-md-3 d-flex align-items-end">
        <button class="btn btn-primary w-100"><i class="fas fa-magic me-1"></i>Generate link</button>
      </div>
    </div>
  </form>
</div></div>

<!-- Existing links -->
<div class="card border-0 shadow-sm">
  <div class="table-responsive">
    <table class="table table-sm mb-0 align-middle">
      <thead class="table-light">
        <tr><th>Label</th><th>Created</th><th>Expires</th><th>State</th><th class="money">Downloads</th><th>Last used</th><th class="text-end"></th></tr>
      </thead>
      <tbody>
        <?php if (empty($tokens)): ?>
          <tr><td colspan="7" class="text-center text-muted py-5">No install links yet. Create one above.</td></tr>
        <?php else: foreach ($tokens as $t):
          $bg = ['active'=>'success','expired'=>'secondary','revoked'=>'dark'][$t['state']] ?? 'light';
        ?>
          <tr>
            <td>
              <a href="<?= PROOT; ?>Download/view/<?= (int)$t['c0d']; ?>" class="text-decoration-none">
                <div class="fw-semibold"><?= htmlspecialchars($t['label'] ?: '(no label)'); ?></div>
                <div class="font-monospace small text-muted"><?= substr($t['token'], 0, 12); ?>…</div>
              </a>
            </td>
            <td class="small"><?= humanDate($t['created_on']); ?><div class="text-muted small"><?= htmlspecialchars($t['created_by_name']); ?></div></td>
            <td class="small"><?= humanDate($t['expires_on']); ?></td>
            <td><span class="badge bg-<?= $bg; ?>"><?= htmlspecialchars($t['state']); ?></span></td>
            <td class="money font-monospace"><?= (int)$t['downloads_count']; ?></td>
            <td class="small text-muted"><?= $t['last_download_on'] ? humanDate($t['last_download_on']) : '—'; ?></td>
            <td class="text-end"><a href="<?= PROOT; ?>Download/view/<?= (int)$t['c0d']; ?>" class="btn btn-sm btn-outline-primary"><i class="fas fa-arrow-right"></i></a></td>
          </tr>
        <?php endforeach; endif; ?>
      </tbody>
    </table>
  </div>
</div>
<?php $this->end(); ?>
