<?php
require_once dirname(__DIR__) . '/config/config.php';
require_once dirname(__DIR__) . '/includes/functions.php';
startSecureSession();
requireCustomer();
$pageTitle = 'My Repair Requests';
$customer = getCurrentCustomer();
$custId = $customer['id'];

$repairs = Database::fetchAll("SELECT * FROM repair_requests WHERE customer_id=? ORDER BY created_at DESC", [$custId]);
include dirname(__DIR__) . '/includes/header.php';
?>

<div class="max-w-5xl mx-auto px-4 py-10">
  <div class="flex items-center justify-between mb-8">
    <div>
      <h1 class="text-2xl font-bold">My Repair Requests</h1>
      <p class="text-gray-400 text-sm mt-0.5">Track workshop and machining jobs</p>
    </div>
    <div class="flex gap-3">
      <a href="/repair-request.php" class="btn-primary text-sm px-4 py-2">+ New Request</a>
      <a href="/customer/dashboard.php" class="btn-outline text-sm px-4 py-2">← Dashboard</a>
    </div>
  </div>

  <?php if ($repairs): ?>
  <div class="space-y-4">
    <?php foreach ($repairs as $r): ?>
    <?php
    $statusColors = [
      'pending'     => 'bg-yellow-500/10 text-yellow-400 border-yellow-500/30',
      'reviewing'   => 'bg-blue-500/10 text-blue-400 border-blue-500/30',
      'quoted'      => 'bg-purple-500/10 text-purple-400 border-purple-500/30',
      'approved'    => 'bg-green-500/10 text-green-400 border-green-500/30',
      'in_progress' => 'bg-orange-500/10 text-orange-400 border-orange-500/30',
      'completed'   => 'bg-green-600/10 text-green-300 border-green-600/30',
      'cancelled'   => 'bg-red-500/10 text-red-400 border-red-500/30',
    ];
    $sc = $statusColors[$r['status']] ?? 'bg-gray-500/10 text-gray-400 border-gray-500/30';
    $statusLabels = [
      'pending' => 'Pending Review', 'reviewing' => 'Under Review', 'quoted' => 'Quote Ready',
      'approved' => 'Approved', 'in_progress' => 'In Progress', 'completed' => 'Completed', 'cancelled' => 'Cancelled'
    ];
    ?>
    <div class="card-dark overflow-hidden">
      <div class="p-5">
        <div class="flex flex-wrap items-start justify-between gap-3 mb-3">
          <div>
            <div class="text-xs text-gray-500 mb-1">Ref: RR-<?= str_pad($r['id'], 5, '0', STR_PAD_LEFT) ?> &nbsp;•&nbsp; Submitted <?= date('d M Y', strtotime($r['created_at'])) ?></div>
            <h3 class="font-bold text-white text-base"><?= e($r['machine_type'] ?? $r['service_type'] ?? 'Repair Request') ?></h3>
            <?php if ($r['service_type']): ?>
            <div class="text-xs text-orange-400 mt-0.5"><?= e($r['service_type']) ?></div>
            <?php endif; ?>
          </div>
          <span class="text-xs font-semibold px-3 py-1.5 rounded-full border <?= $sc ?>"><?= $statusLabels[$r['status']] ?? ucfirst($r['status']) ?></span>
        </div>

        <!-- Machine Details -->
        <div class="grid grid-cols-2 md:grid-cols-4 gap-3 mb-4">
          <?php if ($r['machine_make']): ?>
          <div><div class="text-xs text-gray-500">Make</div><div class="text-sm text-gray-300"><?= e($r['machine_make']) ?></div></div>
          <?php endif; ?>
          <?php if ($r['machine_model']): ?>
          <div><div class="text-xs text-gray-500">Model</div><div class="text-sm text-gray-300"><?= e($r['machine_model']) ?></div></div>
          <?php endif; ?>
          <?php if ($r['machine_serial']): ?>
          <div><div class="text-xs text-gray-500">Serial</div><div class="text-sm text-gray-300"><?= e($r['machine_serial']) ?></div></div>
          <?php endif; ?>
          <?php if ($r['urgency']): ?>
          <div><div class="text-xs text-gray-500">Urgency</div>
          <div class="text-sm <?= $r['urgency']==='urgent' ? 'text-red-400' : ($r['urgency']==='high' ? 'text-orange-400' : 'text-gray-300') ?>"><?= ucfirst(e($r['urgency'])) ?></div></div>
          <?php endif; ?>
        </div>

        <?php if ($r['problem_description']): ?>
        <div class="mb-4 bg-navy-700 rounded-lg p-3">
          <div class="text-xs text-gray-500 mb-1">Problem Description</div>
          <p class="text-sm text-gray-300"><?= nl2br(e($r['problem_description'])) ?></p>
        </div>
        <?php endif; ?>

        <?php if ($r['admin_notes']): ?>
        <div class="mb-3 bg-orange-600/5 border border-orange-600/20 rounded-lg p-3">
          <div class="text-xs text-orange-400 font-semibold mb-1">Update from Our Team</div>
          <p class="text-sm text-gray-300"><?= nl2br(e($r['admin_notes'])) ?></p>
        </div>
        <?php endif; ?>

        <!-- Progress tracker -->
        <?php
        $stages = ['pending', 'reviewing', 'quoted', 'in_progress', 'completed'];
        $currentIdx = array_search($r['status'], $stages);
        if ($currentIdx !== false):
        ?>
        <div class="mt-4">
          <div class="flex items-center gap-0">
            <?php foreach ($stages as $i => $stage): ?>
            <?php $done = $i <= $currentIdx; $current = $i === $currentIdx; ?>
            <div class="flex-1 flex flex-col items-center gap-1">
              <div class="w-6 h-6 rounded-full flex items-center justify-center text-xs font-bold <?= $done ? 'bg-orange-600 text-white' : 'bg-navy-600 text-gray-500' ?>">
                <?= $done ? '✓' : ($i + 1) ?>
              </div>
              <?php if ($i < count($stages) - 1): ?>
              <?php else: ?>
              <?php endif; ?>
              <div class="text-xs text-center <?= $current ? 'text-orange-400 font-semibold' : ($done ? 'text-gray-400' : 'text-gray-600') ?> leading-tight px-1">
                <?= ucfirst(str_replace('_', ' ', $stage)) ?>
              </div>
            </div>
            <?php if ($i < count($stages) - 1): ?>
            <div class="h-0.5 flex-1 mb-5 <?= $i < $currentIdx ? 'bg-orange-600' : 'bg-navy-600' ?>"></div>
            <?php endif; ?>
            <?php endforeach; ?>
          </div>
        </div>
        <?php endif; ?>
      </div>
    </div>
    <?php endforeach; ?>
  </div>
  <?php else: ?>
  <div class="card-dark p-12 text-center">
    <div class="text-5xl mb-4">🔧</div>
    <h3 class="text-xl font-bold mb-3">No Repair Requests Yet</h3>
    <p class="text-gray-400 mb-6">Submit a workshop or machining request and track its progress here.</p>
    <a href="/repair-request.php" class="btn-primary px-8 py-3">Submit Repair Request</a>
  </div>
  <?php endif; ?>
</div>

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