<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Receipt</title>
    <style>
        body {
            font-family: 'Courier New', Courier, monospace;
            font-size: 12px;
            line-height: 1.3;
            max-width: 380px;
            margin: 0 auto;
            padding: 10px;
        }
        .header, .footer {
            text-align: center;
            margin-bottom: 10px;
        }
        .store-name {
            font-size: 16px;
            font-weight: bold;
            margin-bottom: 5px;
        }
        .receipt-details {
            display: flex;
            justify-content: space-between;
            border-top: 1px dashed #000;
            border-bottom: 1px dashed #000;
            padding: 5px 0;
            margin: 10px 0;
        }
        .customer-info {
            margin-bottom: 10px;
        }
        .items-table {
            width: 100%;
            margin-bottom: 10px;
        }
        .items-table th {
            text-align: left;
            border-bottom: 1px solid #000;
        }
        .items-table td {
            padding: 3px 0;
        }
        .quantity, .price {
            text-align: right;
        }
        .totals {
            width: 100%;
            margin-bottom: 10px;
        }
        .totals td {
            padding: 2px 0;
        }
        .total-label {
            text-align: right;
            padding-right: 10px;
        }
        .total-value {
            text-align: right;
        }
        .grand-total {
            font-weight: bold;
            font-size: 14px;
            border-top: 1px solid #000;
            padding-top: 5px;
        }
        .payment-info {
            margin-bottom: 10px;
            padding: 5px 0;
            border-bottom: 1px dashed #000;
        }
        .return-policy {
            font-size: 10px;
            text-align: center;
            margin-top: 15px;
            font-style: italic;
        }
        .barcode {
            text-align: center;
            margin: 15px 0;
            font-family: 'Libre Barcode 39', cursive;
            font-size: 40px;
        }
        .thank-you {
            text-align: center;
            font-weight: bold;
            margin: 10px 0;
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="store-name">{{ store_name }}</div>
        <div>{{ store_address }}</div>
        <div>Tel: {{ store_phone }}</div>
        <div>{{ store_website }}</div>
    </div>

    <div class="receipt-details">
        <div>
            <div>Receipt #: {{ receipt_number }}</div>
            <div>Date: {{ transaction_date }}</div>
            <div>Time: {{ transaction_time }}</div>
        </div>
        <div>
            <div>Cashier: {{ cashier_name }}</div>
        </div>
    </div>

    <div class="customer-info">
        <div>Customer: {{ customer_name }}</div>
        <div>Cust ID: {{ customer_id }}</div>
    </div>

    <table class="items-table">
        <thead>
            <tr>
                <th>Item</th>
                <th>Qty</th>
                <th>Price</th>
                <th>Total</th>
            </tr>
        </thead>
        <tbody>
            {% for item in items %}
            <tr>
                <td>{{ item.product_name }}<br><small>SKU: {{ item["sku"] }}</small></td>
                <td class="quantity">{{ item["quantity"] }}</td>
                <td class="price">${{ "%.2f"|format(item["unit_price"]) }}</td>
                <td class="price">${{ "%.2f"|format(item["item_total"]) }}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>

    <table class="totals">
        <tr>
            <td colspan="2" class="total-label">Subtotal:</td>
            <td class="total-value">${{ "%.2f"|format(subtotal) }}</td>
        </tr>
        <tr>
            <td colspan="2" class="total-label">Tax ({{ "%.2f"|format(tax_rate) }}%):</td>
            <td class="total-value">${{ "%.2f"|format(tax_amount) }}</td>
        </tr>
        {% if discount_amount > 0 %}
        <tr>
            <td colspan="2" class="total-label">Discount:</td>
            <td class="total-value">-${{ "%.2f"|format(discount_amount) }}</td>
        </tr>
        {% endif %}
        <tr class="grand-total">
            <td colspan="2" class="total-label">TOTAL:</td>
            <td class="total-value">${{ "%.2f"|format(total) }}</td>
        </tr>
    </table>

    <div class="payment-info">
        <div>Payment Method: {{ payment_method }}</div>
        {% if payment_method != "Cash" and card_last_four %}
        <div>Card ending in: {{ card_last_four }}</div>
        {% endif %}
        <div>Amount Paid: ${{ "%.2f"|format(total) }}</div>
    </div>

    <div class="barcode">
        {{ receipt_number }}
    </div>

    <div class="thank-you">
        Thank you for shopping with us!
    </div>

    <div class="return-policy">
        {{ return_policy }}
    </div>

    <div class="footer">
        {{ store_name }} - {{ store_website }}
    </div>
</body>
</html>
