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

<div class="page-header">
  <div>
    <div class="page-title"><i class="bi bi-briefcase me-2"></i>Companies</div>
    <div class="page-subtitle">Manage client companies and organisations</div>
  </div>
  <a href="<?= APP_URL ?>/index.php?page=companies&action=create" class="btn btn-primary">
    <i class="bi bi-plus-circle me-1"></i>Add Company
  </a>
</div>

<!-- Search -->
<div class="card mb-3">
  <div class="card-body py-2">
    <form method="GET" class="d-flex gap-2">
      <input type="hidden" name="page" value="companies">
      <input type="text" name="q" class="form-control form-control-sm" placeholder="Search company name, email, phone..." value="<?= e($_GET['q'] ?? '') ?>" style="max-width:360px;">
      <button type="submit" class="btn btn-sm btn-primary"><i class="bi bi-search me-1"></i>Search</button>
      <?php if (!empty($_GET['q'])): ?>
        <a href="<?= APP_URL ?>/index.php?page=companies" class="btn btn-sm btn-outline-secondary">Clear</a>
      <?php endif; ?>
    </form>
  </div>
</div>

<div class="card">
  <div class="card-body p-0">
    <div class="table-responsive">
      <table class="table table-hover mb-0">
        <thead class="table-light">
          <tr><th>Company Name</th><th>Phone</th><th>Email</th><th>Equipment</th><th>Contacts</th><th>Added</th><th>Actions</th></tr>
        </thead>
        <tbody>
        <?php foreach ($companies as $c): ?>
          <tr>
            <td class="fw-semibold"><?= e($c['company_name']) ?></td>
            <td><?= e($c['phone'] ?? '—') ?></td>
            <td><?= e($c['email'] ?? '—') ?></td>
            <td><span class="badge bg-light text-dark"><?= $c['equip_count'] ?> items</span></td>
            <td><span class="badge bg-light text-dark"><?= $c['contact_count'] ?> persons</span></td>
            <td><small><?= date('d M Y', strtotime($c['date_created'])) ?></small></td>
            <td class="text-nowrap">
              <a href="<?= APP_URL ?>/index.php?page=companies&action=view&id=<?= $c['company_id'] ?>" class="btn btn-sm btn-outline-primary me-1" title="View"><i class="bi bi-eye"></i></a>
              <a href="<?= APP_URL ?>/index.php?page=companies&action=edit&id=<?= $c['company_id'] ?>" class="btn btn-sm btn-outline-secondary me-1" title="Edit"><i class="bi bi-pencil"></i></a>
              <a href="<?= APP_URL ?>/index.php?page=companies&action=delete&id=<?= $c['company_id'] ?>" class="btn btn-sm btn-outline-danger" title="Delete" data-confirm="Delete this company and all its data?"><i class="bi bi-trash"></i></a>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($companies)): ?>
          <tr><td colspan="7" class="text-center text-muted py-4">No companies found.</td></tr>
        <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>

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