<%- include('partials/header') %>

<div class="checkout">
    <h2>Checkout</h2>

    <% if (!user) { %>
        <p>You need to <a href="/login">log in</a> to proceed with checkout.</p>
    <% } else { %>
        <div class="order-summary">
            <h3>Order Summary</h3>
            <% if (cartItems.length > 0) { %>
                <% cartItems.forEach(item => { %>
                    <div class="order-item">
                        <span><%= item.name %> x <%= item.quantity %></span>
                        <span>฿<%= (item.price * item.quantity).toLocaleString() %></span>
                    </div>
                <% }); %>
                <div class="total">
                    <strong>Total:</strong>
                    <strong>฿<%= total.toLocaleString() %></strong>
                </div>
            <% } else { %>
                <p>Your cart is empty.</p>
            <% } %>
        </div>
        
        <form action="/order/create" method="POST" class="checkout-form">
            <div class="form-group">
                <label for="address">Shipping Address:</label>
                <textarea name="address" required></textarea>
            </div>
            <button type="submit" style="margin: 0; padding: 0.5rem 1rem;">Place Order</button>
        </form>

        <h3>Your Orders</h3>
        <% if (orders.length > 0) { %>
            <table border="1">
                <thead>
                    <tr>
                        <th>Order ID</th>
                        <th>Total Amount</th>
                        <th>Status</th>
                        <th>Shipping Address</th>
                        <th>Created At</th>
                    </tr>
                </thead>
                <tbody>
                    <% orders.forEach(order => { %>
                        <tr>
                            <td><%= order.id %></td>
                            <td>฿<%= order.total_amount.toLocaleString() %></td>
                            <td><%= order.status %></td>
                            <td><%= order.shipping_address %></td>
                            <td><%= order.created_at %></td>
                        </tr>
                    <% }) %>
                </tbody>
            </table>
        <% } else { %>
            <p>No orders found.</p>
        <% } %>
    <% } %>
</div>

<%- include('partials/footer') %>