<?php $pageTitle = e($company['company_name']); include __DIR__ . '/../shared/header.php'; ?>

<div class="page-header">
  <div>
    <div class="page-title"><i class="bi bi-building me-2"></i><?= e($company['company_name']) ?></div>
    <div class="page-subtitle"><?= e($company['address'] ?? '') ?></div>
  </div>
  <div class="d-flex gap-2">
    <a href="<?= APP_URL ?>/index.php?page=companies&action=edit&id=<?= $company['company_id'] ?>" class="btn btn-outline-secondary">
      <i class="bi bi-pencil me-1"></i>Edit
    </a>
    <a href="<?= APP_URL ?>/index.php?page=companies" class="btn btn-outline-secondary">
      <i class="bi bi-arrow-left me-1"></i>Back
    </a>
  </div>
</div>

<div class="row g-3 mb-4">
  <div class="col-md-3">
    <div class="card text-center p-3">
      <div class="fw-bold fs-4 text-primary"><?= count($contacts) ?></div>
      <div class="text-muted small">Contact Persons</div>
    </div>
  </div>
  <div class="col-md-3">
    <div class="card text-center p-3">
      <div class="fw-bold fs-4 text-success"><?= count($equipments) ?></div>
      <div class="text-muted small">Equipment Items</div>
    </div>
  </div>
  <div class="col-md-3">
    <div class="card text-center p-3">
      <div class="fw-bold fs-5"><?= e($company['phone'] ?? '—') ?></div>
      <div class="text-muted small">Phone</div>
    </div>
  </div>
  <div class="col-md-3">
    <div class="card text-center p-3">
      <div class="fw-bold fs-6 text-truncate"><?= e($company['email'] ?? '—') ?></div>
      <div class="text-muted small">Email</div>
    </div>
  </div>
</div>

<div class="row g-3">
  <!-- Contact Persons -->
  <div class="col-lg-5">
    <div class="card">
      <div class="card-header d-flex justify-content-between align-items-center">
        <span class="card-title"><i class="bi bi-person-lines-fill me-2"></i>Contact Persons</span>
        <a href="<?= APP_URL ?>/index.php?page=contacts&action=create&company_id=<?= $company['company_id'] ?>" class="btn btn-sm btn-outline-primary">
          <i class="bi bi-plus"></i> Add
        </a>
      </div>
      <div class="card-body p-0">
        <?php if (empty($contacts)): ?>
          <div class="text-center text-muted py-4">No contacts added yet.</div>
        <?php else: ?>
        <ul class="list-group list-group-flush">
          <?php foreach ($contacts as $ct): ?>
          <li class="list-group-item d-flex justify-content-between align-items-start px-3">
            <div>
              <div class="fw-semibold"><?= e($ct['contact_name']) ?></div>
              <div class="text-muted small"><?= e($ct['position'] ?? '') ?></div>
              <div class="text-muted small"><?= e($ct['phone_number'] ?? '') ?> <?= $ct['email'] ? '&bull; ' . e($ct['email']) : '' ?></div>
            </div>
            <a href="<?= APP_URL ?>/index.php?page=contacts&action=delete&id=<?= $ct['contact_id'] ?>&company_id=<?= $company['company_id'] ?>"
               class="btn btn-sm btn-outline-danger" data-confirm="Remove this contact?"><i class="bi bi-trash"></i></a>
          </li>
          <?php endforeach; ?>
        </ul>
        <?php endif; ?>
      </div>
    </div>
  </div>

  <!-- Equipment -->
  <div class="col-lg-7">
    <div class="card">
      <div class="card-header d-flex justify-content-between align-items-center">
        <span class="card-title"><i class="bi bi-cpu me-2"></i>Equipment</span>
        <a href="<?= APP_URL ?>/index.php?page=equipment&action=create&company_id=<?= $company['company_id'] ?>" class="btn btn-sm btn-outline-primary">
          <i class="bi bi-plus"></i> Add Equipment
        </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>Name</th><th>Model</th><th>Serial No.</th><th>Manufacturer</th><th>Actions</th></tr></thead>
            <tbody>
            <?php if (empty($equipments)): ?>
              <tr><td colspan="5" class="text-center text-muted py-4">No equipment yet.</td></tr>
            <?php else: foreach ($equipments as $eq): ?>
              <tr>
                <td class="fw-semibold"><?= e($eq['equipment_name']) ?></td>
                <td><?= e($eq['model'] ?? '—') ?></td>
                <td><code><?= e($eq['serial_number'] ?? '—') ?></code></td>
                <td><?= e($eq['manufacturer'] ?? '—') ?></td>
                <td class="text-nowrap">
                  <a href="<?= APP_URL ?>/index.php?page=equipment&action=edit&id=<?= $eq['equipment_id'] ?>" class="btn btn-sm btn-outline-secondary me-1"><i class="bi bi-pencil"></i></a>
                  <a href="<?= APP_URL ?>/index.php?page=equipment&action=delete&id=<?= $eq['equipment_id'] ?>" class="btn btn-sm btn-outline-danger" data-confirm="Delete this equipment?"><i class="bi bi-trash"></i></a>
                </td>
              </tr>
            <?php endforeach; endif; ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</div>

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