<?php
session_start();
if (!isset($_SESSION['role_id']) || !in_array($_SESSION['role_id'], [1, 2, 4])) {
    header("Location: ../../index.php");
    exit();
}

include('../../includes/config.php');

if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    die("Invalid transaction ID.");
}

$transaction_id = intval($_GET['id']);

// Fetch main transaction details
$stmt = $conn->prepare("
    SELECT st.id, st.created_at, st.total_amount, st.vat_total, st.payment_method,
           u.first_name AS cashier_first, u.last_name AS cashier_last,
           s.store_name
    FROM sales_transactions st
    JOIN users u ON st.sold_by = u.id
    JOIN stores s ON st.store_id = s.id
    WHERE st.id = ?
");
$stmt->bind_param("i", $transaction_id);
$stmt->execute();
$result = $stmt->get_result();
$transaction = $result->fetch_assoc();
$stmt->close();

if (!$transaction) {
    die("Transaction not found.");
}

// Fetch transaction items
$stmt = $conn->prepare("
    SELECT p.product_name, si.quantity, si.unit_price
    FROM sales_items si
    JOIN products p ON si.product_id = p.id
    WHERE si.transaction_id = ?
");
$stmt->bind_param("i", $transaction_id);
$stmt->execute();
$items_result = $stmt->get_result();
$items = $items_result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Receipt - Transaction #<?= htmlspecialchars($transaction_id) ?></title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        body {
            font-family: 'Segoe UI', sans-serif;
            background: #f5f5f5;
            padding: 40px;
        }
        .receipt {
            max-width: 700px;
            margin: auto;
            background: white;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 8px 20px rgba(0,0,0,0.2);
        }
        h2 {
            text-align: center;
            color: #ffa500;
        }
        .meta, .summary {
            margin-top: 20px;
        }
        .meta div, .summary div {
            margin-bottom: 8px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 25px;
        }
        th, td {
            padding: 10px;
            border-bottom: 1px solid #ddd;
            text-align: left;
        }
        th {
            background: #ffa500;
            color: #222;
        }
        .total {
            font-weight: bold;
        }
        .btn {
            margin-top: 30px;
            text-align: center;
        }
        .btn button {
            background: #ffa500;
            border: none;
            padding: 10px 20px;
            font-weight: bold;
            border-radius: 6px;
            cursor: pointer;
        }
        .btn button:hover {
            background: #e59400;
        }
    </style>
</head>
<body>
<div class="receipt">
    <h2>🧾 Receipt - Invoice #<?= $transaction['id'] ?></h2>

    <div class="meta">
        <div><strong>Date:</strong> <?= date('Y-m-d H:i:s', strtotime($transaction['created_at'])) ?></div>
        <div><strong>Store:</strong> <?= htmlspecialchars($transaction['store_name']) ?></div>
        <div><strong>Cashier:</strong> <?= htmlspecialchars($transaction['cashier_first'] . ' ' . $transaction['cashier_last']) ?></div>
        <div><strong>Payment Method:</strong> <?= htmlspecialchars(ucfirst($transaction['payment_method'])) ?></div>
    </div>

    <table>
        <thead>
            <tr>
                <th>Product</th>
                <th>Qty</th>
                <th>Unit Price (MWK)</th>
                <th>Total (MWK)</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($items as $item): ?>
                <tr>
                    <td><?= htmlspecialchars($item['product_name']) ?></td>
                    <td><?= $item['quantity'] ?></td>
                    <td><?= number_format($item['unit_price'], 2) ?></td>
                    <td><?= number_format($item['quantity'] * $item['unit_price'], 2) ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <div class="summary">
        <div><strong>VAT:</strong> MWK <?= number_format($transaction['vat_total'], 2) ?></div>
        <div class="total"><strong>Total Paid:</strong> MWK <?= number_format($transaction['total_amount'], 2) ?></div>
    </div>

    <div class="btn">
        <button onclick="window.print()">🖨️ Print Receipt</button>
    </div>
</div>
</body>
</html>
