<?php
$isEdit    = isset($equipment) && $equipment;
$pageTitle = $isEdit ? 'Edit Equipment' : 'Add Equipment';
include __DIR__ . '/../shared/header.php';
$eq = $equipment ?? [];
?>

<div class="page-header">
  <div class="page-title"><?= $isEdit ? '<i class="bi bi-pencil me-2"></i>Edit Equipment' : '<i class="bi bi-plus-circle me-2"></i>Add Equipment' ?></div>
  <a href="<?= APP_URL ?>/index.php?page=equipment" class="btn btn-outline-secondary"><i class="bi bi-arrow-left me-1"></i>Back</a>
</div>

<div class="row"><div class="col-lg-8">
  <div class="card"><div class="card-body">
    <form method="POST">
      <input type="hidden" name="csrf_token" value="<?= csrfToken() ?>">
      <div class="mb-3">
        <label class="form-label">Company <span class="text-danger">*</span></label>
        <select name="company_id" class="form-select" required <?= $isEdit ? 'disabled' : '' ?>>
          <option value="">Select company...</option>
          <?php foreach ($companies as $co): ?>
            <option value="<?= $co['company_id'] ?>"
              <?= (int)($eq['company_id'] ?? $cid ?? 0) === (int)$co['company_id'] ? 'selected' : '' ?>>
              <?= e($co['company_name']) ?>
            </option>
          <?php endforeach; ?>
        </select>
        <?php if ($isEdit): ?>
          <input type="hidden" name="company_id" value="<?= $eq['company_id'] ?>">
        <?php endif; ?>
      </div>
      <div class="mb-3">
        <label class="form-label">Equipment Name <span class="text-danger">*</span></label>
        <input type="text" name="equipment_name" class="form-control" value="<?= e($eq['equipment_name'] ?? '') ?>" required placeholder="e.g. Digital Thermometer">
      </div>
      <div class="row g-3 mb-3">
        <div class="col-md-6">
          <label class="form-label">Manufacturer</label>
          <input type="text" name="manufacturer" class="form-control" value="<?= e($eq['manufacturer'] ?? '') ?>" placeholder="e.g. Fluke">
        </div>
        <div class="col-md-6">
          <label class="form-label">Model</label>
          <input type="text" name="model" class="form-control" value="<?= e($eq['model'] ?? '') ?>" placeholder="e.g. 1524">
        </div>
        <div class="col-md-6">
          <label class="form-label">Serial Number</label>
          <input type="text" name="serial_number" class="form-control" value="<?= e($eq['serial_number'] ?? '') ?>" placeholder="Manufacturer serial number">
        </div>
        <div class="col-md-6">
          <label class="form-label">Range / Capacity</label>
          <input type="text" name="range_capacity" class="form-control" value="<?= e($eq['range_capacity'] ?? '') ?>" placeholder="e.g. -200°C to 660°C">
        </div>
      </div>
      <div class="d-flex gap-2">
        <button type="submit" class="btn btn-primary"><i class="bi bi-check-lg me-1"></i><?= $isEdit ? 'Update' : 'Add' ?> Equipment</button>
        <a href="<?= APP_URL ?>/index.php?page=equipment" class="btn btn-outline-secondary">Cancel</a>
      </div>
    </form>
  </div></div>
</div></div>

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