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

<div class="page-header">
  <div class="page-title"><i class="bi bi-person-circle me-2"></i>My Profile</div>
</div>

<div class="row">
  <div class="col-lg-7">
    <div class="card">
      <div class="card-header"><span class="card-title">Update Profile &amp; Settings</span></div>
      <div class="card-body">
        <form method="POST" enctype="multipart/form-data">
          <input type="hidden" name="csrf_token" value="<?= csrfToken() ?>">
          <div class="row g-3 mb-3">
            <div class="col-md-6">
              <label class="form-label">Full Name</label>
              <input type="text" name="officer_name" class="form-control" value="<?= e($profileUser['officer_name']) ?>" required>
            </div>
            <div class="col-md-6">
              <label class="form-label">Email <small class="text-muted">(read-only)</small></label>
              <input type="email" class="form-control bg-light" value="<?= e($profileUser['email']) ?>" readonly>
            </div>
            <div class="col-md-6">
              <label class="form-label">Phone Number</label>
              <input type="text" name="phone_number" class="form-control" value="<?= e($profileUser['phone_number'] ?? '') ?>">
            </div>
            <div class="col-md-6">
              <label class="form-label">Role <small class="text-muted">(read-only)</small></label>
              <input type="text" class="form-control bg-light" value="<?= ucfirst(str_replace('_',' ',$profileUser['role'])) ?>" readonly>
            </div>
          </div>

          <hr>
          <div class="row g-3 mb-3">
            <div class="col-md-6">
              <label class="form-label">New Password <small class="text-muted">(leave blank to keep)</small></label>
              <input type="password" name="password" class="form-control" placeholder="New password">
            </div>
            <div class="col-md-6">
              <label class="form-label">Confirm Password</label>
              <input type="password" name="password_confirm" class="form-control" placeholder="Confirm new password">
            </div>
          </div>

          <hr>
          <div class="mb-4">
            <label class="form-label">Signature Image <small class="text-muted">(PNG or JPG, used on certificates)</small></label>
            <?php if (!empty($profileUser['signature'])): ?>
              <div class="mb-2">
                <img src="<?= APP_URL ?>/public/uploads/signatures/<?= e($profileUser['signature']) ?>" alt="Current signature" style="max-height:70px;border:1px solid #ddd;padding:4px;border-radius:4px;">
                <div class="form-text">Current signature. Upload a new file to replace it.</div>
              </div>
            <?php endif; ?>
            <input type="file" name="signature" class="form-control" accept="image/png,image/jpeg">
          </div>

          <button type="submit" class="btn btn-primary">
            <i class="bi bi-check-lg me-1"></i>Save Changes
          </button>
        </form>
      </div>
    </div>
  </div>

  <div class="col-lg-5">
    <div class="card">
      <div class="card-header"><span class="card-title">Account Information</span></div>
      <div class="card-body">
        <table class="table table-sm">
          <tr><td class="text-muted">User ID</td><td><code><?= $profileUser['user_id'] ?></code></td></tr>
          <tr><td class="text-muted">Role</td><td><span class="badge bg-primary"><?= ucfirst(str_replace('_',' ',$profileUser['role'])) ?></span></td></tr>
          <tr><td class="text-muted">Status</td><td><span class="badge <?= $profileUser['is_active'] ? 'bg-success' : 'bg-danger' ?>"><?= $profileUser['is_active'] ? 'Active' : 'Inactive' ?></span></td></tr>
          <tr><td class="text-muted">Last Login</td><td><?= $profileUser['last_login'] ? date('d M Y H:i', strtotime($profileUser['last_login'])) : 'N/A' ?></td></tr>
          <tr><td class="text-muted">Member Since</td><td><?= date('d M Y', strtotime($profileUser['date_created'])) ?></td></tr>
        </table>
      </div>
    </div>

    <?php if ($_SESSION['role'] !== 'admin'): ?>
    <?php $myLabs = getUserLabs((int)$_SESSION['user_id']); ?>
    <div class="card mt-3">
      <div class="card-header"><span class="card-title"><i class="bi bi-building me-2"></i>My Assigned Laboratories</span></div>
      <div class="card-body p-0">
        <?php if (empty($myLabs)): ?>
          <div class="text-center text-muted py-3 small">No laboratories assigned yet.</div>
        <?php else: ?>
        <ul class="list-group list-group-flush">
          <?php foreach ($myLabs as $lab): ?>
            <li class="list-group-item d-flex align-items-center gap-2">
              <span class="badge bg-primary"><?= e($lab['lab_code']) ?></span>
              <?= e($lab['lab_name']) ?>
            </li>
          <?php endforeach; ?>
        </ul>
        <?php endif; ?>
      </div>
    </div>
    <?php endif; ?>
  </div>
</div>

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