<?php
require_once __DIR__ . '/../../app/core/bootstrap.php';
auth_require_login();
$pdo = crm_pdo();
$page_title = 'Warranty Reminders';

$stmt = $pdo->prepare("SELECT setting_value FROM settings WHERE setting_key = 'warranty_reminder_days' LIMIT 1");
$stmt->execute();
$days = (int)($stmt->fetchColumn() ?: 30);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $days = max(1, (int)($_POST['warranty_reminder_days'] ?? 30));
    $stmt = $pdo->prepare("
        INSERT INTO settings (setting_key, setting_value)
        VALUES ('warranty_reminder_days', :v)
        ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)
    ");
    $stmt->execute([':v' => (string)$days]);
}

$stmt = $pdo->prepare("
    SELECT pse.*, c.name AS customer_name, c.email AS customer_email, p.title AS project_title
    FROM project_sold_equipment pse
    LEFT JOIN customers c ON c.id = pse.customer_id
    LEFT JOIN projects p ON p.id = pse.project_id
    WHERE pse.warranty_end IS NOT NULL
      AND pse.warranty_end >= CURDATE()
      AND pse.warranty_end <= DATE_ADD(CURDATE(), INTERVAL :days DAY)
    ORDER BY pse.warranty_end ASC
");
$stmt->bindValue(':days', $days, PDO::PARAM_INT);
$stmt->execute();
$rows = $stmt->fetchAll();

include __DIR__ . '/../includes/header.php';
?>
<div class="page-header">
  <div class="page-header-title">
    <h1>Warranty Reminders</h1>
    <span>Upcoming warranty expirations</span>
  </div>
  <div class="page-header-actions">
    <a class="btn btn-secondary btn-sm" href="/public/settings/index.php">Back to settings</a>
  </div>
</div>

<div class="card" style="margin-bottom:14px">
  <form method="post">
    <div class="form-grid">
      <div class="form-group">
        <label>Reminder days before expiry</label>
        <input type="number" min="1" name="warranty_reminder_days" value="<?php echo (int)$days; ?>">
      </div>
    </div>
    <div class="form-actions">
      <button class="btn btn-primary" type="submit">Save</button>
    </div>
  </form>
</div>

<div class="card">
  <?php if (!$rows): ?>
    <p class="small">No warranties expiring in the selected period.</p>
  <?php else: ?>
    <div class="table-responsive">
      <table class="table">
        <thead>
          <tr>
            <th>Warranty end</th>
            <th>Customer</th>
            <th>Project</th>
            <th>Equipment</th>
            <th>Email</th>
          </tr>
        </thead>
        <tbody>
          <?php foreach ($rows as $r): ?>
            <tr>
              <td><?php echo h($r['warranty_end']); ?></td>
              <td><?php echo h($r['customer_name']); ?></td>
              <td><?php echo h($r['project_title']); ?></td>
              <td><?php echo h(trim(($r['item_name'] ?? '') . ' ' . ($r['brand'] ?? '') . ' ' . ($r['model'] ?? ''))); ?></td>
              <td><?php echo h($r['customer_email']); ?></td>
            </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    </div>
  <?php endif; ?>
</div>

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