<?php
session_start();
include('../../includes/config.php');
include('../../includes/header.php');
include('../../includes/sidebar.php');

// Check role
if (!isset($_SESSION['role_id']) || !in_array($_SESSION['role_id'], [1, 3, 4])) {
    echo "<div style='color: red; text-align: center; margin-top: 50px;'>Unauthorized access</div>";
    exit;
}

$sale_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
if ($sale_id <= 0) {
    echo "<div style='color: red; text-align: center; margin-top: 50px;'>Invalid sale ID</div>";
    exit;
}

// Fetch Sale Transaction
$stmt = $conn->prepare("SELECT st.*, s.store_name, u.first_name, u.last_name 
                        FROM sales_transactions st
                        LEFT JOIN stores s ON st.store_id = s.id
                        LEFT JOIN users u ON st.sold_by = u.id
                        WHERE st.id = ?");
$stmt->bind_param('i', $sale_id);
$stmt->execute();
$result = $stmt->get_result();
$sale = $result->fetch_assoc();
$stmt->close();

if (!$sale) {
    echo "<div style='color: red; text-align: center; margin-top: 50px;'>Sale not found</div>";
    exit;
}

// Fetch Sale Items
$stmt = $conn->prepare("SELECT si.*, p.product_name 
                        FROM sales_items si
                        LEFT JOIN products p ON si.product_id = p.id
                        WHERE si.transaction_id = ?");
$stmt->bind_param('i', $sale_id);
$stmt->execute();
$items_result = $stmt->get_result();
$sale_items = $items_result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
?>

<style>
    body, html {
        margin: 0;
        padding: 0;
        min-height: 100vh;
        font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        background: url('../../images/profile.jpg') no-repeat center center fixed;
        background-size: cover;
        color: #fff;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .glass-card {
        background: rgba(39, 34, 34, 0.85);
        border-radius: 15px;
        padding: 30px 40px;
        color: #fff;
        width: 100%;
        max-width: 900px;
        box-shadow: 0 8px 32px rgba(0,0,0,0.25);
        backdrop-filter: blur(10px);
        border: 1px solid rgba(255, 255, 255, 0.15);
        margin-top: 50px;
        margin-bottom: 50px;
    }

    .glass-card h2 {
        text-align: center;
        margin-bottom: 25px;
        color: #fff;
        font-weight: 600;
    }

    .glass-card p {
        margin: 5px 0;
        font-size: 14px;
    }

    .glass-table {
        width: 100%;
        border-collapse: collapse;
        margin-top: 20px;
        border-radius: 10px;
        overflow: hidden;
        background: rgba(255,255,255,0.03);
    }

    .glass-table th, .glass-table td {
        padding: 10px;
        text-align: left;
        font-size: 13px;
    }

    .glass-table th {
        background: rgba(255,255,255,0.1);
        font-weight: 600;
        color: #fff;
    }

    .glass-table tr:nth-child(even) {
        background: rgba(255,255,255,0.05);
    }

    .summary-section p {
        font-size: 14px;
        margin: 6px 0;
    }

    .summary-section strong {
        color: #ffa500;
    }

    @media (max-width: 768px) {
        .glass-card {
            padding: 20px;
        }
    }
</style>

<div class="main-content" style="width: 100%; display: flex; justify-content: center; align-items: center; padding: 40px 20px;">
    <div class="glass-card">

        <h2>Sale Receipt Preview</h2>

        <p><strong>Receipt Number:</strong> <?php echo htmlspecialchars($sale['receipt_number']); ?></p>
        <p><strong>Store:</strong> <?php echo htmlspecialchars($sale['store_name']); ?></p>
        <p><strong>Cashier:</strong> <?php echo htmlspecialchars($sale['first_name'] . ' ' . $sale['last_name']); ?></p>
        <p><strong>Transaction Date:</strong> <?php echo date('d M Y H:i', strtotime($sale['created_at'])); ?></p>
        <p><strong>Customer Name:</strong> <?php echo htmlspecialchars($sale['customer_name'] ?: 'N/A'); ?></p>

        <h4 style="margin-top: 25px;">Sale Items</h4>

        <table class="glass-table">
            <thead>
                <tr>
                    <th>Product</th>
                    <th>Qty</th>
                    <th>Unit Price</th>
                    <th>Discount</th>
                    <th>VAT</th>
                    <th>Total</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($sale_items as $item): ?>
                    <tr>
                        <td><?php echo htmlspecialchars($item['product_name']); ?></td>
                        <td><?php echo (int)$item['quantity']; ?></td>
                        <td><?php echo number_format($item['unit_price'], 2); ?></td>
                        <td><?php echo number_format($item['discount'], 2); ?></td>
                        <td><?php echo number_format($item['vat_rate'], 2); ?>%</td>
                        <td><?php echo number_format($item['total_price'], 2); ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>

        <h4 style="margin-top: 25px;">Summary</h4>

        <div class="summary-section">
            <p><strong>Subtotal:</strong> <?php echo number_format($sale['total_amount'] - $sale['vat_total'] + $sale['discount_total'], 2); ?></p>
            <p><strong>Discount:</strong> <?php echo number_format($sale['discount_total'], 2); ?></p>
            <p><strong>VAT:</strong> <?php echo number_format($sale['vat_total'], 2); ?></p>
            <p><strong>Total Amount:</strong> <strong style="font-size: 16px;"><?php echo number_format($sale['total_amount'], 2); ?></strong></p>
            <p><strong>Amount Paid:</strong> <?php echo number_format($sale['amount_paid'], 2); ?></p>
            <p><strong>Payment Method:</strong> <?php echo htmlspecialchars(ucfirst($sale['payment_method'])); ?></p>
        </div>
    </div>
</div>

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