<?php use App\Helpers\Helper; ?>
<div class="d-flex justify-content-between align-items-center mb-4">
  <div>
    <h1 class="admin-page-title"><?= Helper::e($request['title']) ?></h1>
    <p class="text-muted small mb-0">Ref: <strong><?= Helper::e($request['request_number']) ?></strong></p>
  </div>
  <a href="<?= APP_URL ?>/admin/maintenance" class="btn btn-outline-secondary btn-sm"><i class="bi bi-arrow-left me-1"></i>Back</a>
</div>
<div class="row g-4">
  <div class="col-lg-8">
    <div class="form-card mb-4">
      <div class="form-section-title">Request Details</div>
      <div class="row g-3">
        <div class="col-md-6"><div class="small text-muted">Property / Unit</div><div class="fw-600"><?= Helper::e($request['property_name']) ?> / Unit <?= Helper::e($request['unit_number']) ?></div></div>
        <div class="col-md-6"><div class="small text-muted">Tenant</div><div class="fw-600"><?= Helper::e($request['tenant_name']??'—') ?></div><div class="small text-muted"><?= Helper::e($request['tenant_phone']??'') ?></div></div>
        <div class="col-md-4"><div class="small text-muted">Category</div><div class="fw-600"><?= Helper::e($request['category']??'General') ?></div></div>
        <div class="col-md-4"><div class="small text-muted">Priority</div><?= Helper::statusBadge($request['priority']) ?></div>
        <div class="col-md-4"><div class="small text-muted">Status</div><?= Helper::statusBadge($request['status']) ?></div>
        <div class="col-12"><div class="small text-muted mb-1">Description</div><div class="bg-light rounded-2 p-3 small"><?= nl2br(Helper::e($request['description']??'')) ?></div></div>
        <?php if ($request['notes']): ?><div class="col-12"><div class="small text-muted mb-1">Staff Notes</div><div class="bg-light rounded-2 p-3 small"><?= nl2br(Helper::e($request['notes'])) ?></div></div><?php endif; ?>
      </div>
    </div>
    <div class="form-card">
      <div class="form-section-title">Costs</div>
      <div class="row g-3">
        <div class="col-md-4"><div class="small text-muted">Estimated Cost</div><div class="fw-600"><?= Helper::money($request['estimated_cost']??0) ?></div></div>
        <div class="col-md-4"><div class="small text-muted">Materials Cost</div><div class="fw-600"><?= Helper::money($request['materials_cost']??0) ?></div></div>
        <div class="col-md-4"><div class="small text-muted">Actual Cost</div><div class="fw-600 text-primary"><?= Helper::money($request['actual_cost']??0) ?></div></div>
        <?php if ($request['started_at']): ?><div class="col-md-4"><div class="small text-muted">Started</div><div class="small"><?= Helper::datetime($request['started_at']) ?></div></div><?php endif; ?>
        <?php if ($request['completed_at']): ?><div class="col-md-4"><div class="small text-muted">Completed</div><div class="small"><?= Helper::datetime($request['completed_at']) ?></div></div><?php endif; ?>
      </div>
    </div>
  </div>
  <div class="col-lg-4">
    <div class="form-card">
      <div class="form-section-title">Update Request</div>
      <form method="POST" action="<?= APP_URL ?>/admin/maintenance/<?= $request['id'] ?>">
        <?= Helper::csrf() ?>
        <div class="mb-3">
          <label class="form-label">Status</label>
          <select name="status" class="form-select">
            <?php foreach (['open','in_progress','on_hold','completed','cancelled'] as $s): ?>
            <option value="<?= $s ?>" <?= $request['status']===$s?'selected':'' ?>><?= ucfirst(str_replace('_',' ',$s)) ?></option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="mb-3">
          <label class="form-label">Assign To</label>
          <select name="assigned_to" class="form-select">
            <option value="">— Unassigned —</option>
            <?php foreach ($staff as $s): ?><option value="<?= $s['id'] ?>" <?= ($request['assigned_to']??'')==$s['id']?'selected':'' ?>><?= Helper::e($s['name']) ?></option><?php endforeach; ?>
          </select>
        </div>
        <div class="mb-3"><label class="form-label">Technician Name</label><input type="text" name="technician_name" class="form-control" value="<?= Helper::e($request['technician_name']??$request['assigned_to_name']??'') ?>"></div>
        <div class="mb-3"><label class="form-label">Estimated Cost (TZS)</label><input type="number" name="estimated_cost" class="form-control" value="<?= $request['estimated_cost']??0 ?>" min="0"></div>
        <div class="mb-3"><label class="form-label">Actual Cost (TZS)</label><input type="number" name="actual_cost" class="form-control" value="<?= $request['actual_cost']??0 ?>" min="0"></div>
        <div class="mb-3"><label class="form-label">Materials Cost (TZS)</label><input type="number" name="materials_cost" class="form-control" value="<?= $request['materials_cost']??0 ?>" min="0"></div>
        <div class="mb-3"><label class="form-label">Notes</label><textarea name="notes" class="form-control" rows="3"><?= Helper::e($request['notes']??'') ?></textarea></div>
        <button type="submit" class="btn btn-primary w-100"><i class="bi bi-save me-2"></i>Update</button>
      </form>
    </div>
  </div>
</div>
