<?php
$adminTitle = 'Repair Requests';
require_once __DIR__ . '/includes/header.php';

// Update status
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    Database::execute("UPDATE repair_requests SET status=?, admin_notes=? WHERE id=?",
        [sanitize($_POST['status'] ?? ''), sanitize($_POST['admin_notes'] ?? ''), (int)$_POST['request_id']]);
    $_SESSION['flash']['success'] = 'Request updated.';
    header('Location: /admin/repair.php');
    exit;
}

$filter = trim($_GET['status'] ?? '');
$where  = $filter ? "WHERE status=?" : "";
$params = $filter ? [$filter] : [];
$total  = (int)(Database::fetchOne("SELECT COUNT(*) as c FROM repair_requests $where", $params)['c'] ?? 0);
$pag    = paginate($total, ADMIN_PER_PAGE, (int)($_GET['page'] ?? 1), '/admin/repair.php?' . http_build_query(array_filter(['status'=>$filter])));
$requests = Database::fetchAll(
    "SELECT * FROM repair_requests $where ORDER BY created_at DESC LIMIT ? OFFSET ?",
    array_merge($params, [$pag['per_page'], $pag['offset']])
);
?>
<div class="flex items-center justify-between mb-6">
  <h2 class="text-lg font-semibold">Repair Requests (<?= $total ?>)</h2>
  <div class="flex gap-2">
    <?php foreach ([''=>'All','received'=>'Received','assessed'=>'Assessed','quoted'=>'Quoted','in_progress'=>'In Progress','completed'=>'Completed'] as $v=>$l): ?>
    <a href="?status=<?= $v ?>" class="text-xs px-3 py-1.5 rounded <?= $filter===$v?'bg-orange-600 text-white':'bg-navy-700 text-gray-300' ?>"><?= $l ?></a>
    <?php endforeach; ?>
  </div>
</div>

<div class="space-y-4">
<?php foreach ($requests as $req): ?>
<div class="card-dark p-5">
  <div class="flex items-start justify-between gap-4 mb-3">
    <div>
      <div class="flex items-center gap-2 mb-1">
        <span class="font-mono text-xs text-orange-400"><?= e($req['request_number']) ?></span>
        <?= statusBadge($req['status']) ?>
        <?php if ($req['urgency'] !== 'normal'): ?><span class="badge <?= $req['urgency']==='emergency'?'badge-red':'badge-orange' ?>"><?= ucfirst($req['urgency']) ?></span><?php endif; ?>
      </div>
      <h3 class="font-semibold"><?= e($req['machine_type']) ?> <?= $req['make_model'] ? '— '.e($req['make_model']) : '' ?></h3>
      <div class="text-xs text-gray-400"><?= e($req['customer_name']) ?> | <?= e($req['customer_email']) ?> | <?= e($req['customer_phone'] ?? '') ?></div>
    </div>
    <div class="text-xs text-gray-500"><?= formatDate($req['created_at']) ?></div>
  </div>
  <p class="text-sm text-gray-300 mb-3"><?= e($req['issue_description']) ?></p>
  <?php if ($req['photos']): ?>
  <?php $photos = json_decode($req['photos'], true) ?? []; ?>
  <?php if (!empty($photos)): ?>
  <div class="flex gap-2 mb-3">
    <?php foreach ($photos as $photo): ?>
    <a href="/uploads/<?= e($photo) ?>" target="_blank"><img src="/uploads/<?= e($photo) ?>" class="w-16 h-16 object-cover rounded"></a>
    <?php endforeach; ?>
  </div>
  <?php endif; ?>
  <?php endif; ?>
  <form method="POST" class="flex gap-3 items-end">
    <input type="hidden" name="request_id" value="<?= $req['id'] ?>">
    <div><label class="text-xs text-gray-400 block mb-1">Status</label>
    <select name="status" class="text-sm py-1">
      <?php foreach (['received','assessed','quoted','in_progress','completed','cancelled'] as $s): ?>
      <option value="<?= $s ?>" <?= $req['status']===$s?'selected':'' ?>><?= ucwords(str_replace('_',' ',$s)) ?></option>
      <?php endforeach; ?>
    </select></div>
    <div class="flex-1"><label class="text-xs text-gray-400 block mb-1">Admin Notes</label><input type="text" name="admin_notes" value="<?= e($req['admin_notes'] ?? '') ?>" placeholder="Assessment notes..."></div>
    <button class="btn-primary text-sm py-1.5">Update</button>
  </form>
</div>
<?php endforeach; ?>
<?php if (empty($requests)): ?>
<div class="card-dark p-8 text-center text-gray-400">No repair requests found.</div>
<?php endif; ?>
</div>
<?= paginationHtml($pag) ?>

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