<?php
$isEdit     = isset($editUser);
$pageTitle  = $isEdit ? 'Edit User' : 'Create User';
include __DIR__ . '/../shared/header.php';
$u = $editUser ?? [];
?>

<div class="page-header">
  <div>
    <div class="page-title"><?= $isEdit ? '<i class="bi bi-pencil me-2"></i>Edit User' : '<i class="bi bi-person-plus me-2"></i>Create User' ?></div>
  </div>
  <a href="<?= APP_URL ?>/index.php?page=users" 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="row g-3">
            <div class="col-md-6">
              <label class="form-label">Full Name <span class="text-danger">*</span></label>
              <input type="text" name="officer_name" class="form-control" value="<?= e($u['officer_name'] ?? '') ?>" required>
            </div>
            <div class="col-md-6">
              <label class="form-label">Email Address <span class="text-danger">*</span></label>
              <input type="email" name="email" class="form-control" value="<?= e($u['email'] ?? '') ?>" required>
            </div>
            <div class="col-md-6">
              <label class="form-label">Phone Number</label>
              <input type="text" name="phone_number" class="form-control" value="<?= e($u['phone_number'] ?? '') ?>">
            </div>
            <div class="col-md-6">
              <label class="form-label">Role <span class="text-danger">*</span></label>
              <select name="role" class="form-select" id="roleSelect">
                <option value="front_desk" <?= ($u['role'] ?? '') === 'front_desk' ? 'selected' : '' ?>>Front Desk Officer</option>
                <option value="lab_officer" <?= ($u['role'] ?? '') === 'lab_officer' ? 'selected' : '' ?>>Laboratory Officer</option>
                <option value="admin" <?= ($u['role'] ?? '') === 'admin' ? 'selected' : '' ?>>Admin</option>
              </select>
            </div>
            <div class="col-md-6">
              <label class="form-label"><?= $isEdit ? 'New Password (leave blank to keep)' : 'Password' ?> <?= $isEdit ? '' : '<span class="text-danger">*</span>' ?></label>
              <input type="password" name="password" class="form-control" <?= $isEdit ? '' : 'required' ?> placeholder="Minimum 8 characters">
            </div>
            <?php if ($isEdit): ?>
            <div class="col-md-6">
              <label class="form-label">Status</label>
              <div class="form-check mt-2">
                <input class="form-check-input" type="checkbox" name="is_active" id="isActive" <?= ($u['is_active'] ?? 1) ? 'checked' : '' ?>>
                <label class="form-check-label" for="isActive">Account Active</label>
              </div>
            </div>
            <?php endif; ?>
          </div>

          <!-- Lab Assignments -->
          <div class="mt-4" id="labSection">
            <label class="form-label fw-semibold">Laboratory Assignments</label>
            <div class="text-muted small mb-2">Select laboratories this officer can access (for Lab Officers). Admin has full access.</div>
            <div class="row g-2">
            <?php foreach ($labs as $lab): ?>
              <div class="col-md-6">
                <div class="form-check">
                  <input class="form-check-input" type="checkbox" name="lab_ids[]"
                         value="<?= $lab['lab_id'] ?>" id="lab_<?= $lab['lab_id'] ?>"
                         <?= in_array($lab['lab_id'], $assignedLabs ?? []) ? 'checked' : '' ?>>
                  <label class="form-check-label" for="lab_<?= $lab['lab_id'] ?>">
                    <span class="badge bg-light text-dark me-1"><?= e($lab['lab_code']) ?></span><?= e($lab['lab_name']) ?>
                  </label>
                </div>
              </div>
            <?php endforeach; ?>
            </div>
          </div>

          <div class="mt-4 d-flex gap-2">
            <button type="submit" class="btn btn-primary">
              <i class="bi bi-check-lg me-1"></i><?= $isEdit ? 'Update User' : 'Create User' ?>
            </button>
            <a href="<?= APP_URL ?>/index.php?page=users" class="btn btn-outline-secondary">Cancel</a>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>

<script>
document.getElementById('roleSelect').addEventListener('change', function() {
  document.getElementById('labSection').style.display = this.value === 'admin' ? 'none' : 'block';
});
// Initial
if (document.getElementById('roleSelect').value === 'admin') {
  document.getElementById('labSection').style.display = 'none';
}
</script>

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