<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Checkout - EasyFood</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
          crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"
            crossorigin="anonymous"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .checkout-container {
            background-color: #fff;
            border-radius: 12px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            max-width: 1000px;
            margin: 2rem auto;
            padding: 2rem;
        }

        .form-section, .order-summary {
            background-color: #f8f9fa;
            border-radius: 12px;
            padding: 1.5rem;
            margin-bottom: 1.5rem;
        }

        .step-indicator {
            display: none;
        }

        .address-card, .payment-option {
            border: 2px solid #e9ecef;
            border-radius: 10px;
            padding: 1rem;
            margin: 0.5rem 0;
            transition: all 0.3s ease;
            cursor: pointer;
        }

        .address-card.selected, .payment-option.selected {
            border-color: #ffc107;
            background: rgba(255, 193, 7, 0.1);
        }

        .order-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0.75rem 0;
            border-bottom: 1px solid #dee2e6;
        }

        .order-item:last-of-type {
            border-bottom: none;
        }

        .total-row {
            padding: 1rem 0;
        }
    </style>
</head>
<body class="bg-light">

<nav class="navbar bg-body-tertiary shadow-sm">
    <div class="container-fluid">
        <a class="navbar-brand d-flex align-items-center" href="/home">
            <img src="/images/logo.JPG" alt="Logo" height="90" class="d-inline-block align-text-top">
        </a>
    </div>
</nav>

<div class="container-fluid mt-4">
    <div class="checkout-container">
        <div class="text-center mb-4">
            <h1 class="fw-bold"><i class="fas fa-shopping-cart me-3"></i>Checkout</h1>
            <p class="text-muted">Complete your order
                <span th:if="${restaurant != null}">from <strong th:text="${restaurant.getName()}"></strong></span>
            </p>        </div>

        <div class="row">
            <div class="col-md-8">
                <form th:action="@{/order/place}" method="post" id="checkoutForm">

                    <div class="form-section">
                        <h4 class="mb-3"><i class="fas fa-map-marker-alt me-2"></i>Delivery Address</h4>

                        <div th:if="${not #lists.isEmpty(addresses)}">
                            <p class="text-muted">Choose from saved addresses:</p>
                            <div th:each="address : ${addresses}">
                                <div class="address-card" onclick="selectAddress(this, [[${address.id}]])">
                                    <div class="form-check">
                                        <input class="form-check-input" type="radio" name="addressId"
                                               th:value="${address.id}" th:id="'address-' + ${address.id}">
                                        <label class="form-check-label" th:for="'address-' + ${address.id}">
                                            <strong th:text="${address.street}"></strong><br>
                                            <small class="text-muted"
                                                   th:text="${address.city} + ', ' + ${address.postalCode}"></small>
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <hr class="my-3">
                        </div>

                        <div class="address-card" onclick="toggleNewAddressForm(event)">
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="addressOption" value="new"
                                       id="newAddress">
                                <label class="form-check-label" for="newAddress">
                                    <i class="fas fa-plus-circle me-2"></i><strong>Add New Address</strong>
                                </label>
                            </div>
                        </div>

                        <div class="new-address-form" id="newAddressForm" style="display: none;">
                            <h6 class="mt-3"><i class="fas fa-home me-2"></i>New Address Details</h6>
                            <div class="row">
                                <div class="col-md-12 mb-3">
                                    <label for="newStreet" class="form-label">Street Address</label>
                                    <input type="text" class="form-control" id="newStreet" name="newStreet"
                                           placeholder="Enter street address">
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="newCity" class="form-label">City</label>
                                    <input type="text" class="form-control" id="newCity" name="newCity"
                                           placeholder="Enter city">
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="newPostalCode" class="form-label">Postal Code</label>
                                    <input type="text" class="form-control" id="newPostalCode" name="newPostalCode"
                                           placeholder="Enter postal code">
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="form-section">
                        <h4 class="mb-3"><i class="fas fa-credit-card me-2"></i>Payment Method</h4>
                        <div class="payment-option" onclick="selectPayment(this, 'card')">
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="paymentMethod" value="card"
                                       id="paymentCard">
                                <label class="form-check-label" for="paymentCard">
                                    <i class="fas fa-credit-card icon-payment text-primary"></i>
                                    <strong>Credit/Debit Card</strong>
                                </label>
                            </div>
                        </div>

                        <div class="payment-option" onclick="selectPayment(this, 'cash')">
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="paymentMethod" value="cash"
                                       id="paymentCash">
                                <label class="form-check-label" for="paymentCash">
                                    <i class="fas fa-money-bill-wave icon-payment text-success"></i>
                                    <strong>Cash on Delivery</strong>
                                </label>
                            </div>
                        </div>

                        <div class="payment-option" onclick="selectPayment(this, 'paypal')">
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="paymentMethod" value="paypal"
                                       id="paymentPaypal">
                                <label class="form-check-label" for="paymentPaypal">
                                    <i class="fab fa-paypal icon-payment text-info"></i>
                                    <strong>PayPal</strong>
                                </label>
                            </div>
                        </div>
                    </div>

                    <div class="form-section">
                        <h4 class="mb-3"><i class="fas fa-sticky-note me-2"></i>Order Notes</h4>
                        <textarea class="form-control" name="comment" rows="3"
                                  placeholder="Any special instructions for your order? (Optional)"></textarea>
                    </div>
                </form>
            </div>

            <div class="col-md-4">
                <div class="order-summary sticky-top" style="top: 2rem;">
                    <h4 class="mb-3"><i class="fas fa-receipt me-2"></i>Order Summary</h4>

                    <div th:each="item : ${cartItems}" class="order-item">
                        <div>
                            <strong th:text="${item.item.name}"></strong>
                            <small class="text-muted">x<span th:text="${item.quantity}"></span></small>
                        </div>
                        <span th:text="'$' + ${itemTotals[item.getItem().getId()]}"></span>
                    </div>

                    <div class="d-flex justify-content-between total-row">
                        <strong>Total:</strong>
                        <strong class="text-warning" th:text="'$' + ${totalAmount}"></strong>
                    </div>

                    <button type="submit" form="checkoutForm" class="btn btn-warning w-100 mt-3">
                        <i class="fas fa-check-circle me-2"></i>Place Order
                    </button>

                    <div class="text-center mt-3">
                        <a href="/cart" class="btn btn-outline-secondary">
                            <i class="fas fa-arrow-left me-2"></i>Back to Cart
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div th:if="${successMessage}" class="alert alert-success alert-dismissible fade show position-fixed"
         style="top: 20px; right: 20px; z-index: 1050;">
        <span th:text="${successMessage}"></span>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>

    <div th:if="${errorMessage}" class="alert alert-danger alert-dismissible fade show position-fixed"
         style="top: 20px; right: 20px; z-index: 1050;">
        <span th:text="${errorMessage}"></span>
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        function selectAddress(element) {
            document.querySelectorAll('.address-card').forEach(card => card.classList.remove('selected'));
            element.classList.add('selected');
            element.querySelector('input[type="radio"]').checked = true;
            document.getElementById('newAddressForm').style.display = 'none';
        }

        function toggleNewAddressForm(event) {
            document.querySelectorAll('.address-card').forEach(card => card.classList.remove('selected'));
            event.currentTarget.classList.add('selected');
            document.getElementById('newAddressForm').style.display = 'block';
            document.querySelectorAll('input[name="addressId"]').forEach(radio => radio.checked = false);
        }

        function selectPayment(element) {
            document.querySelectorAll('.payment-option').forEach(option => option.classList.remove('selected'));
            element.classList.add('selected');
            element.querySelector('input[type="radio"]').checked = true;
        }
    </script>

</body>
</html>