<?php $pageTitle = 'Dashboard'; include __DIR__ . '/header.php'; ?>

<div class="page-header">
  <div>
    <div class="page-title">Dashboard</div>
    <div class="page-subtitle">Welcome back, <?= e($_SESSION['name'] ?? '') ?></div>
  </div>
  <div class="text-muted small"><i class="bi bi-calendar3 me-1"></i><?= date('l, d M Y') ?></div>
</div>

<!-- Stat Cards -->
<div class="row g-3 mb-4">
<?php
$cards = [];
if ($role === 'admin') {
    $cards = [
        ['label'=>'Total Users','value'=>$stats['total_users'],'icon'=>'bi-people','color'=>'#0055a5','bg'=>'#e8f0fe'],
        ['label'=>'Laboratories','value'=>$stats['total_labs'],'icon'=>'bi-building','color'=>'#28a745','bg'=>'#d4edda'],
        ['label'=>'Companies','value'=>$stats['total_companies'],'icon'=>'bi-briefcase','color'=>'#fd7e14','bg'=>'#fff3e0'],
        ['label'=>'Total Requests','value'=>$stats['total_requests'],'icon'=>'bi-clipboard-check','color'=>'#6f42c1','bg'=>'#ede7f6'],
        ['label'=>'Pending','value'=>$stats['pending_requests'],'icon'=>'bi-clock','color'=>'#ffc107','bg'=>'#fff8e1'],
        ['label'=>'Completed','value'=>$stats['completed_requests'],'icon'=>'bi-check-circle','color'=>'#20c997','bg'=>'#e0f7f4'],
        ['label'=>'Certificates','value'=>$stats['total_certs'],'icon'=>'bi-award','color'=>'#e83e8c','bg'=>'#fce4ec'],
    ];
} elseif ($role === 'front_desk') {
    $cards = [
        ['label'=>'My Requests','value'=>$stats['my_requests'],'icon'=>'bi-clipboard-check','color'=>'#0055a5','bg'=>'#e8f0fe'],
        ['label'=>'Companies','value'=>$stats['total_companies'],'icon'=>'bi-briefcase','color'=>'#28a745','bg'=>'#d4edda'],
        ['label'=>'Equipment','value'=>$stats['total_equipment'],'icon'=>'bi-cpu','color'=>'#fd7e14','bg'=>'#fff3e0'],
        ['label'=>'Pending Requests','value'=>$stats['pending_requests'],'icon'=>'bi-clock','color'=>'#ffc107','bg'=>'#fff8e1'],
    ];
} else {
    $cards = [
        ['label'=>'Lab Requests','value'=>$stats['lab_requests'],'icon'=>'bi-clipboard-check','color'=>'#0055a5','bg'=>'#e8f0fe'],
        ['label'=>'Pending','value'=>$stats['pending_requests'],'icon'=>'bi-clock','color'=>'#ffc107','bg'=>'#fff8e1'],
        ['label'=>'Completed','value'=>$stats['completed_requests'],'icon'=>'bi-check-circle','color'=>'#20c997','bg'=>'#e0f7f4'],
        ['label'=>'My Certificates','value'=>$stats['my_certs'],'icon'=>'bi-award','color'=>'#e83e8c','bg'=>'#fce4ec'],
    ];
}
foreach ($cards as $c): ?>
  <div class="col-6 col-md-3">
    <div class="stat-card">
      <div class="stat-icon" style="background:<?= $c['bg'] ?>; color:<?= $c['color'] ?>">
        <i class="bi <?= $c['icon'] ?>"></i>
      </div>
      <div>
        <div class="stat-value"><?= $c['value'] ?></div>
        <div class="stat-label"><?= $c['label'] ?></div>
      </div>
    </div>
  </div>
<?php endforeach; ?>
</div>

<!-- Recent Requests Table -->
<div class="row g-3">
  <div class="col-lg-<?= $role === 'admin' ? '8' : '12' ?>">
    <div class="card">
      <div class="card-header d-flex justify-content-between align-items-center">
        <span class="card-title"><i class="bi bi-clipboard-check me-2"></i>Recent Requests</span>
        <a href="<?= APP_URL ?>/index.php?page=requests" class="btn btn-sm btn-outline-primary">View All</a>
      </div>
      <div class="card-body p-0">
        <div class="table-responsive">
          <table class="table table-hover mb-0">
            <thead class="table-light">
              <tr><th>Req. #</th><th>Company</th><th>Equipment</th><th>Laboratory</th><th>Status</th><th></th></tr>
            </thead>
            <tbody>
            <?php if (empty($recentRequests)): ?>
              <tr><td colspan="6" class="text-center text-muted py-4">No requests yet.</td></tr>
            <?php else: foreach ($recentRequests as $r): ?>
              <tr>
                <td><code><?= e($r['request_number']) ?></code></td>
                <td><?= e($r['company_name']) ?></td>
                <td><?= e($r['equipment_name']) ?></td>
                <td><small><?= e($r['lab_name']) ?></small></td>
                <td><span class="badge badge-<?= $r['status'] ?>"><?= ucfirst(str_replace('_',' ',$r['status'])) ?></span></td>
                <td><a href="<?= APP_URL ?>/index.php?page=requests&action=view&id=<?= $r['request_id'] ?>" class="btn btn-xs btn-outline-secondary btn-sm py-0">View</a></td>
              </tr>
            <?php endforeach; endif; ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>

  <?php if ($role === 'admin' && !empty($recentLogs)): ?>
  <div class="col-lg-4">
    <div class="card">
      <div class="card-header d-flex justify-content-between align-items-center">
        <span class="card-title"><i class="bi bi-journal-text me-2"></i>Recent Activity</span>
        <a href="<?= APP_URL ?>/index.php?page=logs" class="btn btn-sm btn-outline-primary">View All</a>
      </div>
      <div class="card-body p-0">
        <ul class="list-group list-group-flush">
        <?php foreach ($recentLogs as $log): ?>
          <li class="list-group-item px-3 py-2">
            <div class="d-flex justify-content-between">
              <small class="fw-semibold"><?= e($log['officer_name'] ?? 'System') ?></small>
              <small class="text-muted"><?= date('H:i', strtotime($log['created_at'])) ?></small>
            </div>
            <div class="text-muted" style="font-size:12px;"><?= e($log['action']) ?> &mdash; <?= e($log['module']) ?></div>
          </li>
        <?php endforeach; ?>
        </ul>
      </div>
    </div>
  </div>
  <?php endif; ?>
</div>

<?php include __DIR__ . '/footer.php'; ?>
