<?php
/**
 * WashHouse — Confirm & Mark Bill Ready
 * -------------------------------------
 * When link is opened, asks for confirmation first.
 * Marks bill as READY only when confirmed.
 */

require_once __DIR__ . '/../includes/db.php';
date_default_timezone_set('Asia/Colombo');

$bill_no = $_GET['bill'] ?? '';
$confirm = $_GET['confirm'] ?? '';
$message = '';
$ready_done = false;

// Step 1: Check if valid bill exists
if ($bill_no) {
    $stmt = $pdo->prepare("SELECT bill_id, status FROM bills WHERE bill_number = ?");
    $stmt->execute([$bill_no]);
    $bill = $stmt->fetch(PDO::FETCH_ASSOC);

    if (!$bill) {
        $message = "❌ Invalid Bill Number.";
    } elseif ($bill['status'] === 'Ready') {
        $message = "ℹ️ Bill #{$bill_no} is already marked as READY.";
        $ready_done = true;
    } elseif ($confirm === 'yes') {
        // ✅ Mark as ready only after confirmation
        $update = $pdo->prepare("
            UPDATE bills 
            SET status = 'Ready', ready_date = NOW(), ready_sms_sent = 1 
            WHERE bill_id = ?
        ");
        $update->execute([$bill['bill_id']]);

        // Optional log entry
        $log = $pdo->prepare("
            INSERT INTO sms_log (customer_id, phone, message, response) 
            VALUES (NULL, NULL, ?, ?)
        ");
        $log->execute([
            "Bill #$bill_no marked as Ready by user",
            "IP: " . $_SERVER['REMOTE_ADDR'] . ", Time: " . date('Y-m-d H:i:s')
        ]);

        $message = "✅ Bill #{$bill_no} has been successfully marked as READY.";
        $ready_done = true;
    }
} else {
    $message = "❌ No Bill Number provided.";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Mark Ready | WashHouse</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
  font-family: 'Segoe UI', sans-serif;
  background-color: #f4f6f8;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  margin: 0;
}
.container {
  background: white;
  padding: 30px 25px;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  text-align: center;
  max-width: 400px;
}
h1 {
  color: #007bff;
  font-size: 22px;
  margin-bottom: 15px;
}
p {
  font-size: 16px;
  color: #333;
  margin-bottom: 25px;
}
a.button {
  display: inline-block;
  padding: 10px 16px;
  margin: 5px;
  border-radius: 6px;
  text-decoration: none;
  font-weight: 600;
  transition: background-color 0.2s ease;
}
a.confirm {
  background-color: #28a745;
  color: white;
}
a.confirm:hover { background-color: #218838; }
a.cancel {
  background-color: #dc3545;
  color: white;
}
a.cancel:hover { background-color: #c82333; }
a.dashboard {
  background-color: #007bff;
  color: white;
}
a.dashboard:hover { background-color: #0056b3; }
</style>
</head>
<body>
<div class="container">
  <h1>WashHouse 🧺</h1>

  <?php if ($message && $ready_done): ?>
      <p><?= htmlspecialchars($message) ?></p>
      <a href="/pages/dashboard.php" class="button dashboard">🔙 Back to Dashboard</a>

  <?php elseif ($bill_no && !$ready_done): ?>
      <p>Do you want to mark <strong>Bill #<?= htmlspecialchars($bill_no) ?></strong> as READY?</p>
      <a href="?bill=<?= urlencode($bill_no) ?>&confirm=yes" class="button confirm">✅ Confirm</a>
      <a href="/pages/dashboard.php" class="button cancel">❌ Cancel</a>

  <?php else: ?>
      <p><?= htmlspecialchars($message) ?></p>
      <a href="/pages/dashboard.php" class="button dashboard">🔙 Back to Dashboard</a>
  <?php endif; ?>

</div>
</body>
</html>
