<?php $pageTitle = 'Request: ' . e($request['request_number']); include __DIR__ . '/header.php'; ?>

<div class="page-header">
  <div>
    <div class="page-title"><i class="bi bi-clipboard-check me-2"></i><?= e($request['request_number']) ?></div>
    <div class="page-subtitle">
      <span class="badge badge-<?= $request['status'] ?> me-2"><?= ucfirst(str_replace('_',' ',$request['status'])) ?></span>
      <?php if ($request['priority'] === 'urgent'): ?>
        <span class="badge bg-danger"><i class="bi bi-exclamation-triangle me-1"></i>Urgent</span>
      <?php endif; ?>
    </div>
  </div>
  <div class="d-flex gap-2">
    <a href="<?= APP_URL ?>/index.php?page=requests" class="btn btn-outline-secondary">
      <i class="bi bi-arrow-left me-1"></i>Back
    </a>
    <?php if (in_array($_SESSION['role'], ['admin','lab_officer']) && $request['status'] !== 'completed' && $request['status'] !== 'cancelled'): ?>
    <a href="<?= APP_URL ?>/index.php?page=workform&action=edit&request_id=<?= $request['request_id'] ?>" class="btn btn-primary">
      <i class="bi bi-pencil-square me-1"></i>Open Work Form
    </a>
    <?php endif; ?>
    <?php if ($cert): ?>
    <a href="<?= APP_URL ?>/index.php?page=certificates&action=view&id=<?= $cert['cert_id'] ?>" class="btn btn-success">
      <i class="bi bi-award me-1"></i>View Certificate
    </a>
    <?php endif; ?>
  </div>
</div>

<div class="row g-3">
  <!-- Company Info -->
  <div class="col-md-6">
    <div class="card h-100">
      <div class="card-header"><span class="card-title"><i class="bi bi-briefcase me-2"></i>Company Information</span></div>
      <div class="card-body">
        <table class="table table-sm mb-0">
          <tr><td class="text-muted fw-semibold" style="width:40%">Company</td><td><?= e($request['company_name']) ?></td></tr>
          <tr><td class="text-muted fw-semibold">Address</td><td><?= e($request['company_address'] ?? '—') ?></td></tr>
          <tr><td class="text-muted fw-semibold">Phone</td><td><?= e($request['company_phone'] ?? '—') ?></td></tr>
          <tr><td class="text-muted fw-semibold">Email</td><td><?= e($request['company_email'] ?? '—') ?></td></tr>
          <?php if ($request['contact_name']): ?>
          <tr><td class="text-muted fw-semibold">Contact</td><td><?= e($request['contact_name']) ?> <?= $request['contact_phone'] ? '(' . e($request['contact_phone']) . ')' : '' ?></td></tr>
          <?php endif; ?>
        </table>
      </div>
    </div>
  </div>

  <!-- Equipment Info -->
  <div class="col-md-6">
    <div class="card h-100">
      <div class="card-header"><span class="card-title"><i class="bi bi-cpu me-2"></i>Equipment Details</span></div>
      <div class="card-body">
        <table class="table table-sm mb-0">
          <tr><td class="text-muted fw-semibold" style="width:40%">Equipment</td><td><?= e($request['equipment_name']) ?></td></tr>
          <tr><td class="text-muted fw-semibold">Manufacturer</td><td><?= e($request['manufacturer'] ?? '—') ?></td></tr>
          <tr><td class="text-muted fw-semibold">Model</td><td><?= e($request['model'] ?? '—') ?></td></tr>
          <tr><td class="text-muted fw-semibold">Serial No.</td><td><code><?= e($request['serial_number'] ?? '—') ?></code></td></tr>
          <tr><td class="text-muted fw-semibold">Range</td><td><?= e($request['range_capacity'] ?? '—') ?></td></tr>
        </table>
      </div>
    </div>
  </div>

  <!-- Request Info -->
  <div class="col-md-6">
    <div class="card h-100">
      <div class="card-header"><span class="card-title"><i class="bi bi-info-circle me-2"></i>Request Details</span></div>
      <div class="card-body">
        <table class="table table-sm mb-0">
          <tr><td class="text-muted fw-semibold" style="width:40%">Request No.</td><td><code><?= e($request['request_number']) ?></code></td></tr>
          <tr><td class="text-muted fw-semibold">Laboratory</td><td><?= e($request['lab_name']) ?></td></tr>
          <tr><td class="text-muted fw-semibold">Date Received</td><td><?= date('d M Y', strtotime($request['request_date'])) ?></td></tr>
          <tr><td class="text-muted fw-semibold">Created By</td><td><?= e($request['created_by_name']) ?></td></tr>
          <tr><td class="text-muted fw-semibold">Assigned To</td><td><?= e($request['assigned_to_name'] ?? 'Unassigned') ?></td></tr>
          <?php if ($request['notes']): ?>
          <tr><td class="text-muted fw-semibold">Notes</td><td><?= nl2br(e($request['notes'])) ?></td></tr>
          <?php endif; ?>
        </table>
      </div>
    </div>
  </div>

  <!-- Status Update -->
  <div class="col-md-6">
    <div class="card h-100">
      <div class="card-header"><span class="card-title"><i class="bi bi-arrow-repeat me-2"></i>Update Status</span></div>
      <div class="card-body">
        <?php if (in_array($_SESSION['role'], ['admin','lab_officer'])): ?>
        <form method="POST" action="<?= APP_URL ?>/index.php?page=requests&action=update_status&id=<?= $request['request_id'] ?>">
          <input type="hidden" name="csrf_token" value="<?= csrfToken() ?>">
          <div class="mb-3">
            <label class="form-label">Change Status</label>
            <select name="status" class="form-select">
              <option value="pending"     <?= $request['status']==='pending'     ?'selected':'' ?>>Pending</option>
              <option value="in_progress" <?= $request['status']==='in_progress' ?'selected':'' ?>>In Progress</option>
              <option value="completed"   <?= $request['status']==='completed'   ?'selected':'' ?>>Completed</option>
              <option value="cancelled"   <?= $request['status']==='cancelled'   ?'selected':'' ?>>Cancelled</option>
            </select>
          </div>
          <button type="submit" class="btn btn-outline-primary btn-sm">Update Status</button>
        </form>
        <?php else: ?>
          <p class="text-muted small mb-0">Status updates are performed by laboratory officers.</p>
        <?php endif; ?>

        <?php if ($workForm): ?>
        <hr>
        <div class="d-flex justify-content-between align-items-center">
          <div>
            <div class="fw-semibold small">Work Form</div>
            <span class="badge badge-<?= $workForm['form_status'] ?>"><?= ucfirst($workForm['form_status']) ?></span>
          </div>
          <a href="<?= APP_URL ?>/index.php?page=workform&action=edit&request_id=<?= $request['request_id'] ?>" class="btn btn-sm btn-outline-secondary">
            <i class="bi bi-pencil me-1"></i>Edit Form
          </a>
        </div>
        <?php endif; ?>
      </div>
    </div>
  </div>
</div>

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