<?php
require_once "../config/db.php";

$id = (int) ($_GET['id'] ?? 0);

$invoice = $conn->query("
SELECT invoices.*, customers.name, customers.phone
FROM invoices
JOIN customers ON invoices.customer_id = customers.id
WHERE invoices.id = $id
")->fetch_assoc();

if(!$invoice){
    die("Invoice not found");
}

$phone = preg_replace('/[^0-9]/', '', $invoice['phone']);

if(strlen($phone) == 10){
    $phone = "91" . $phone;
}

$message = "Hello {$invoice['name']},\n";
$message .= "Reminder: Your invoice #{$id} is overdue.\n";
$message .= "Amount: Rs. {$invoice['total']}\n";
$message .= "Due Date: {$invoice['due_date']}\n";

// ✅ ADD THIS LINE HERE
$message .= "Download PDF: https://billing.digitalvega.in/actions/invoice_pdf.php?id=$id";

// Redirect to WhatsApp
header("Location: https://wa.me/$phone?text=$message");

$message = urlencode($message);

header("Location: https://wa.me/$phone?text=$message");
exit;


