<?php
// sales/receipt.php
session_start();
$pageTitle = 'Receipt';
require_once('./config/db.php');
require_once("./includes/auth.php");
include('./includes/header.php');

$id = isset($_GET['id']) ? (int)$_GET['id'] : ($_SESSION['last_sale_id'] ?? 0);
if (!$id) {
    echo '<div class="alert alert-danger">No receipt to show.</div>';
    include('./includes/footer.php');
    exit;
}

// Load master
$stmt = $pdo->prepare("SELECT * FROM sales_master WHERE SaleID = ?");
$stmt->execute([$id]);
$master = $stmt->fetch();
if (!$master) {
    echo '<div class="alert alert-danger">Sale not found.</div>';
    include('./includes/footer.php');
    exit;
}

// Load items
$items = $pdo->prepare("
    SELECT si.*, p.Name 
    FROM sale_items si 
    LEFT JOIN products p ON p.ProductID = si.ProductID 
    WHERE si.SaleID = ?
");
$items->execute([$id]);
$items = $items->fetchAll();
?>
<style>
    @media print {
        .no-print {
            display: none !important;
        }

        nav.navbar,
        .navbar,
        .navbar-modern,
        .no-print-global {
            display: none !important;
            visibility: hidden !important;
        }

        body {
            margin: 0 !important;
            padding: 20px !important;
        }
    }

    .receipt {
        max-width: 480px;
        margin: 0 auto;
        font-family: monospace;
    }

    .receipt .line {
        display: flex;
        justify-content: space-between;
    }
</style>

<div class="receipt border p-3 bg-white shadow-sm">

    <div class="text-center mb-3" style="padding-bottom:6px; border-bottom:1px dashed #000;">
        <h4 style="margin:0; font-weight:800; letter-spacing:1px;">Thone Thone Store</h4>
        <small style="font-size:13px; font-weight:600; color:#444;">RECEIPT</small>
    </div>

    <!-- Invoice + Customer Info -->
    <div class="mb-2">
        <div>Invoice: <strong><?= htmlspecialchars($master['InvoiceNo']) ?></strong></div>
        <div>Date: <strong><?= htmlspecialchars($master['Date']) ?></strong></div>

        <?php if (!empty($master['CustomerName']) || !empty($master['CustomerPhone'])): ?>
            <div style="margin-bottom:8px; font-weight:600; font-size:18px;">
                <div>Customer: <?= htmlspecialchars($master['CustomerName']) ?></div>
                <div>Phone: <?= htmlspecialchars($master['CustomerPhone']) ?></div>

            </div>

        <?php endif; ?>
    </div>

    <!-- Items -->
    <div class="mb-2">
        <?php foreach ($items as $it): ?>
            <div class="line">
                <div><?= htmlspecialchars($it['Name']) ?> x<?= (int)$it['Quantity'] ?></div>
                <div><?= number_format($it['TotalAmount'], 2) ?></div>
            </div>
        <?php endforeach; ?>
    </div>

    <hr>

    <!-- Total -->
    <div class="line fw-bold mb-3">
        <div>Total</div>
        <div><?= number_format($master['TotalAmount'], 2) ?></div>
    </div>

    <div class="text-center mb-2"
        style="margin-top:10px; padding-top:8px; border-top:2px solid #000; font-weight:700;">
        <small style="font-size:14px;">✨ Thank you for your purchase! ✨</small>
    </div>

    <div class="d-flex gap-2 no-print">
        <a href="sales.php" class="btn btn-outline-secondary flex-grow-1">New Sale</a>
        <button class="btn btn-primary" onclick="window.print()">Print</button>
    </div>
</div>

<script>
    document.addEventListener("DOMContentLoaded", function() {
        const customer = "<?= htmlspecialchars($master['CustomerName']) ?>";
        const date = "<?= htmlspecialchars($master['Date']) ?>";

        // Build filename
        let filename = "";
        if (customer) filename += customer.replace(/\s+/g, "_");
        if (date) filename += "-" + date.replace(/:/g, "-");

        // Set the document title
        if (filename) {
            document.title = filename;
        }
    });
</script>


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