<?php
require_once __DIR__ . '/../../app/core/bootstrap.php';
require_once __DIR__ . '/../../app/core/portal_auth.php';
auth_require_customer_portal();
$pdo = crm_pdo();
$page_title = 'Customer Portal';

$customerId = auth_customer_id();

if (auth_is_admin_like() && $customerId <= 0) {
    $customers = $pdo->query("SELECT id, name, email FROM customers WHERE active = 1 ORDER BY name ASC")->fetchAll();
    include __DIR__ . '/../includes/header.php';
    ?>
    <div class="page-header">
      <div class="page-header-title">
        <h1>Customer Portal</h1>
        <span>Admin access — choose customer</span>
      </div>
    </div>
    <div class="card">
      <div class="table-responsive">
        <table class="table">
          <thead><tr><th>Customer</th><th>Email</th><th></th></tr></thead>
          <tbody>
            <?php foreach ($customers as $c): ?>
              <tr>
                <td><?php echo h($c['name']); ?></td>
                <td><?php echo h($c['email']); ?></td>
                <td><a class="btn btn-sm btn-outline-light" href="/public/customers/my_warranties.php?customer_id=<?php echo (int)$c['id']; ?>">Open portal</a></td>
              </tr>
            <?php endforeach; ?>
          </tbody>
        </table>
      </div>
    </div>
    <?php
    include __DIR__ . '/../includes/footer.php';
    exit;
}

if ($customerId <= 0) {
    http_response_code(403);
    exit('Customer record not linked.');
}

$stmt = $pdo->prepare("
    SELECT pse.*, p.title AS project_title, p.code AS project_code
    FROM project_sold_equipment pse
    LEFT JOIN projects p ON p.id = pse.project_id
    WHERE pse.customer_id = :customer_id
    ORDER BY COALESCE(pse.warranty_end, '9999-12-31') ASC, pse.id DESC
");
$stmt->execute([':customer_id' => $customerId]);
$rows = $stmt->fetchAll();

include __DIR__ . '/../includes/header.php';
?>
<div class="page-header">
  <div class="page-header-title">
    <h1>Customer Portal</h1>
    <span><?php echo auth_is_admin_like() ? 'Admin warranty view' : 'My warranties'; ?></span>
  </div>
</div>

<div class="card">
  <?php if (!$rows): ?>
    <p class="small">No warranty records available.</p>
  <?php else: ?>
    <div class="table-responsive">
      <table class="table">
        <thead>
          <tr>
            <th>Project</th>
            <th>Equipment</th>
            <th>Serial</th>
            <th>Purchase</th>
            <th>Warranty start</th>
            <th>Warranty end</th>
            <th>Status</th>
          </tr>
        </thead>
        <tbody>
          <?php foreach ($rows as $r): ?>
            <?php $expired = !empty($r['warranty_end']) && strtotime((string)$r['warranty_end']) < strtotime(date('Y-m-d')); ?>
            <tr>
              <td><?php echo h(($r['project_code'] ?? '') . ' · ' . ($r['project_title'] ?? '')); ?></td>
              <td><?php echo h(trim(($r['item_name'] ?? '') . ' ' . ($r['brand'] ?? '') . ' ' . ($r['model'] ?? ''))); ?></td>
              <td><?php echo h($r['serial_number']); ?></td>
              <td><?php echo h($r['purchase_date']); ?></td>
              <td><?php echo h($r['warranty_start']); ?></td>
              <td><?php echo h($r['warranty_end']); ?></td>
              <td><?php echo $expired ? 'Expired' : 'Active'; ?></td>
            </tr>
            <tr><td colspan="7" class="small"><?php echo h(trim(('Supplier: ' . ($r['supplier_name'] ?? '')) . (!empty($r['notes']) ? ' · ' . $r['notes'] : ''))); ?></td></tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    </div>
  <?php endif; ?>
</div>

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