{% extends 'hosting_app/base_hosting.html' %}
{% load static %}

{% block title %}Hosting Client Payment Center | Host in Sri Lanka{% endblock %}

{% block content %}

<style>
    /* =========================================================
       HOSTING CLIENT PAYMENT CENTER — CHECKOUT ENABLED
    ========================================================= */

    .hosting-client-payment-page {
        min-height: 100vh;
        padding: 4rem 0 7rem;
        background:
            radial-gradient(circle at top left, rgba(255, 193, 7, 0.10), transparent 30%),
            radial-gradient(circle at top right, rgba(116, 49, 41, 0.10), transparent 32%),
            linear-gradient(180deg, #fffaf5 0%, #ffffff 48%, #fff7ed 100%);
        color: #2c1b17;
    }

    .client-payment-hero {
        border-radius: 36px;
        padding: 2.7rem 2.35rem;
        color: #ffffff;
        background:
            radial-gradient(circle at top right, rgba(255, 193, 7, 0.22), transparent 35%),
            linear-gradient(145deg, #2e1817 0%, #6d2e28 64%, #a3601b 100%);
        box-shadow: 0 30px 70px rgba(53, 27, 22, 0.20);
        margin-bottom: 2rem;
        position: relative;
        overflow: hidden;
    }

    .client-payment-hero::before {
        content: "";
        position: absolute;
        width: 260px;
        height: 260px;
        border-radius: 999px;
        right: -80px;
        top: -80px;
        background: rgba(255, 193, 7, 0.16);
        pointer-events: none;
    }

    .client-payment-hero > * {
        position: relative;
        z-index: 2;
    }

    .client-payment-kicker {
        display: inline-flex;
        align-items: center;
        gap: 0.5rem;
        padding: 0.52rem 1rem;
        border-radius: 999px;
        background: rgba(255, 255, 255, 0.13);
        border: 1px solid rgba(255, 255, 255, 0.20);
        color: #ffd36b;
        font-size: 0.84rem;
        font-weight: 900;
        text-transform: uppercase;
        letter-spacing: 0.055em;
        margin-bottom: 1rem;
    }

    .client-payment-hero h1 {
        font-size: clamp(2.15rem, 4vw, 3.65rem);
        font-weight: 950;
        line-height: 1.06;
        letter-spacing: -0.05em;
        margin-bottom: 0.8rem;
    }

    .client-payment-hero p {
        color: rgba(255, 255, 255, 0.84);
        line-height: 1.8;
        max-width: 900px;
        margin-bottom: 1.4rem;
    }

    .client-payment-actions {
        display: flex;
        flex-wrap: wrap;
        gap: 0.85rem;
    }

    .client-payment-actions .btn {
        border-radius: 999px;
        min-height: 50px;
        font-weight: 950;
        padding: 0.8rem 1.3rem;
    }

    .client-btn-gold {
        background: linear-gradient(135deg, #ffc83f, #df9219);
        color: #26160d;
        border: none;
        font-weight: 950 !important;
        box-shadow: 0 18px 34px rgba(223, 146, 25, 0.30);
        transition: all 0.22s ease;
    }

    .client-btn-gold:hover,
    .client-btn-gold:focus {
        background: linear-gradient(135deg, #2e1817, #6d2e28);
        color: #ffffff !important;
        transform: translateY(-2px);
        box-shadow: 0 22px 42px rgba(54, 28, 23, 0.28);
    }

    .client-btn-gold:disabled {
        opacity: 0.55;
        cursor: not-allowed;
        transform: none;
        box-shadow: none;
    }

    .client-btn-glass {
        background: rgba(255, 255, 255, 0.10);
        color: #ffffff;
        border: 1px solid rgba(255, 255, 255, 0.34);
        backdrop-filter: blur(10px);
    }

    .client-btn-glass:hover {
        background: rgba(255, 255, 255, 0.17);
        color: #ffffff;
    }

    .client-payment-stats-grid {
        display: grid;
        grid-template-columns: repeat(4, minmax(0, 1fr));
        gap: 1rem;
        margin-bottom: 1.5rem;
    }

    .client-payment-stat-card {
        border-radius: 26px;
        padding: 1.15rem;
        background: linear-gradient(145deg, #fffaf4 0%, #ffffff 100%);
        border: 1px solid rgba(124, 74, 50, 0.10);
        box-shadow: 0 14px 30px rgba(55, 31, 22, 0.06);
        position: relative;
        overflow: hidden;
    }

    .client-payment-stat-card::before {
        content: "";
        position: absolute;
        top: -50px;
        right: -50px;
        width: 120px;
        height: 120px;
        border-radius: 999px;
        background: rgba(255, 193, 7, 0.11);
        pointer-events: none;
    }

    .client-payment-stat-card > * {
        position: relative;
        z-index: 2;
    }

    .client-payment-stat-icon {
        width: 46px;
        height: 46px;
        border-radius: 16px;
        background: #fff0d6;
        color: #a75e17;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        font-size: 1.25rem;
        margin-bottom: 0.75rem;
    }

    .client-payment-stat-card span {
        display: block;
        color: #8b6550;
        font-size: 0.76rem;
        font-weight: 950;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        margin-bottom: 0.35rem;
    }

    .client-payment-stat-card strong {
        color: #38231d;
        font-size: 1.45rem;
        font-weight: 950;
    }

    .client-payment-card {
        border-radius: 32px;
        background: linear-gradient(145deg, #fffaf4 0%, #ffffff 100%);
        border: 1px solid rgba(124, 74, 50, 0.10);
        box-shadow: 0 18px 42px rgba(55, 31, 22, 0.07);
        overflow: hidden;
        margin-bottom: 1.4rem;
    }

    .client-payment-card-header {
        padding: 1.45rem 1.55rem 1rem;
        border-bottom: 1px solid rgba(124, 74, 50, 0.10);
        background:
            radial-gradient(circle at top right, rgba(255, 193, 7, 0.08), transparent 30%),
            linear-gradient(145deg, #fffaf4 0%, #ffffff 100%);
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        gap: 1rem;
        flex-wrap: wrap;
    }

    .client-payment-card-header h2,
    .client-payment-card-header h3 {
        color: #38231d;
        font-weight: 950;
        margin-bottom: 0.35rem;
    }

    .client-payment-card-header p {
        color: #78625a;
        line-height: 1.65;
        margin-bottom: 0;
    }

    .client-payment-card-body {
        padding: 1.55rem;
    }

    .client-payable-list {
        display: grid;
        gap: 1rem;
    }

    .client-payable-card {
        border-radius: 26px;
        background:
            radial-gradient(circle at top right, rgba(255, 193, 7, 0.05), transparent 30%),
            #ffffff;
        border: 1px solid rgba(124, 74, 50, 0.10);
        box-shadow: 0 12px 28px rgba(55, 31, 22, 0.05);
        padding: 1.15rem;
        transition: all 0.22s ease;
        cursor: pointer;
        margin-bottom: 0;
    }

    .client-payable-card:hover {
        transform: translateY(-2px);
        border-color: rgba(167, 94, 23, 0.28);
        box-shadow: 0 18px 38px rgba(55, 31, 22, 0.09);
    }

    .client-payable-card.is-selected {
        border-color: #a75e17;
        box-shadow: 0 0 0 3px rgba(167, 94, 23, 0.12), 0 18px 38px rgba(55, 31, 22, 0.09);
        background:
            radial-gradient(circle at top right, rgba(255, 193, 7, 0.10), transparent 32%),
            #ffffff;
    }

    .client-payable-check {
        margin-top: 0.35rem;
        width: 1.15rem;
        height: 1.15rem;
        border-color: #d7bfa5;
        flex-shrink: 0;
        cursor: pointer;
    }

    .client-payable-check:checked {
        background-color: #a75e17;
        border-color: #a75e17;
    }

    .client-payable-title {
        color: #38231d;
        font-weight: 950;
        font-size: 1.05rem;
        margin-bottom: 0.35rem;
    }

    .client-payable-desc {
        color: #78625a;
        line-height: 1.6;
        font-size: 0.92rem;
        margin-bottom: 0.8rem;
    }

    .client-payable-meta {
        display: flex;
        flex-wrap: wrap;
        gap: 0.45rem;
        margin-bottom: 0.85rem;
    }

    .client-payable-chip {
        display: inline-flex;
        align-items: center;
        gap: 0.32rem;
        border-radius: 999px;
        padding: 0.34rem 0.58rem;
        background: #fff7e8;
        border: 1px solid #efd8b7;
        color: #6f4f3f;
        font-size: 0.75rem;
        font-weight: 850;
    }

    .client-payable-chip.paid {
        background: #ecfdf5;
        border-color: #a7f3d0;
        color: #047857;
    }

    .client-payable-chip.due {
        background: #fffbeb;
        border-color: #fde68a;
        color: #92400e;
    }

    .client-payable-chip.cancelled {
        background: #fef2f2;
        border-color: #fecaca;
        color: #b91c1c;
    }

    .client-payment-amount {
        color: #38231d;
        font-weight: 950;
        font-size: 1.25rem;
        white-space: nowrap;
    }

    .client-selected-summary {
        position: sticky;
        bottom: 1rem;
        z-index: 10;
        margin-top: 1.2rem;
        border-radius: 28px;
        padding: 1rem 1.15rem;
        background:
            radial-gradient(circle at top right, rgba(255, 193, 7, 0.16), transparent 34%),
            linear-gradient(135deg, #2e1817, #6d2e28);
        color: #ffffff;
        box-shadow: 0 22px 44px rgba(46, 24, 23, 0.26);
    }

    .client-selected-summary span {
        color: rgba(255, 255, 255, 0.74);
        font-size: 0.82rem;
        font-weight: 850;
    }

    .client-selected-summary strong {
        color: #ffffff;
        font-size: 1.28rem;
        font-weight: 950;
    }

    .client-selected-summary .btn {
        border-radius: 999px;
        font-weight: 950;
        min-height: 46px;
    }

    .client-booking-card {
        border-radius: 24px;
        background: #ffffff;
        border: 1px solid rgba(124, 74, 50, 0.10);
        padding: 1rem;
        margin-bottom: 0.8rem;
    }

    .client-booking-card h4 {
        color: #38231d;
        font-weight: 950;
        font-size: 1rem;
        margin-bottom: 0.4rem;
    }

    .client-booking-card p {
        color: #78625a;
        font-size: 0.9rem;
        line-height: 1.6;
        margin-bottom: 0.75rem;
    }

    .client-empty-card {
        border-radius: 28px;
        padding: 2.4rem 1.4rem;
        text-align: center;
        background: #ffffff;
        border: 1px solid rgba(124, 74, 50, 0.10);
    }

    .client-empty-icon {
        width: 78px;
        height: 78px;
        border-radius: 28px;
        background: #fff0d6;
        color: #a75e17;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        font-size: 2.2rem;
        margin-bottom: 1rem;
    }

    .client-empty-card h3 {
        color: #38231d;
        font-weight: 950;
    }

    .client-empty-card p {
        color: #78625a;
        line-height: 1.7;
        max-width: 620px;
        margin-left: auto;
        margin-right: auto;
    }

    .client-side-profile-grid {
        display: grid;
        gap: 0.8rem;
    }

    .client-side-profile-item {
        border-radius: 22px;
        padding: 1rem;
        background: #fff7e8;
        border: 1px solid #efd8b7;
    }

    .client-side-profile-item span {
        display: block;
        color: #8b6550;
        font-size: 0.76rem;
        font-weight: 950;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        margin-bottom: 0.35rem;
    }

    .client-side-profile-item strong {
        color: #38231d;
        font-weight: 900;
        word-break: break-word;
    }

    .client-payment-note {
        border-radius: 24px;
        padding: 1rem;
        background: #fffbeb;
        border: 1px solid #fde68a;
        color: #92400e;
        line-height: 1.65;
        font-weight: 750;
    }

    .client-payment-mini-total {
        display: inline-flex;
        align-items: center;
        gap: 0.45rem;
        border-radius: 999px;
        padding: 0.45rem 0.8rem;
        background: #fff7e8;
        border: 1px solid #efd8b7;
        color: #6f4f3f;
        font-size: 0.82rem;
        font-weight: 900;
    }

    @media (max-width: 1199.98px) {
        .client-payment-stats-grid {
            grid-template-columns: repeat(2, minmax(0, 1fr));
        }
    }

    @media (max-width: 767.98px) {
        .hosting-client-payment-page {
            padding-top: 2.8rem;
        }

        .client-payment-hero {
            padding: 2rem 1.35rem;
            border-radius: 30px;
        }

        .client-payment-actions {
            flex-direction: column;
            align-items: stretch;
        }

        .client-payment-actions .btn {
            width: 100%;
        }

        .client-payment-stats-grid {
            grid-template-columns: 1fr;
        }

        .client-payment-card-header,
        .client-payment-card-body {
            padding-left: 1.15rem;
            padding-right: 1.15rem;
        }

        .client-payable-card .client-payable-row {
            flex-direction: column;
        }

        .client-payment-amount {
            white-space: normal;
        }

        .client-selected-summary {
            bottom: 0.65rem;
            border-radius: 24px;
        }

        .client-selected-summary .d-flex {
            flex-direction: column;
            align-items: stretch !important;
        }
    }

    @media (max-width: 575.98px) {
        .hosting-client-payment-page > .container {
            padding-left: 1.25rem !important;
            padding-right: 1.25rem !important;
        }
    }
</style>


<section class="hosting-client-payment-page">
    <div class="container">

        <!-- HERO -->
        <div class="client-payment-hero">
            <span class="client-payment-kicker">
                <i class="bi bi-wallet2"></i>
                Hosting Client Payment Center
            </span>

            <h1>
                Payments & booking orders
            </h1>

            <p>
                Select your due hosting payments and continue to checkout. Your booking order will be confirmed
                after successful payment through Stripe or PayPal.
            </p>

            <div class="client-payment-actions">
                <a href="{% url 'hosting_app:public_host_venue_list' %}"
                   class="btn client-btn-gold">
                    <i class="bi bi-search-heart-fill me-2"></i>
                    Browse Host Venues
                </a>

                <a href="{% url 'hosting_app:hosting_client_dashboard' %}"
                   class="btn client-btn-glass">
                    <i class="bi bi-speedometer2 me-2"></i>
                    Client Dashboard
                </a>
            </div>
        </div>


        <!-- STATS -->
        <div class="client-payment-stats-grid">

            <div class="client-payment-stat-card">
                <div class="client-payment-stat-icon">
                    <i class="bi bi-exclamation-circle-fill"></i>
                </div>
                <span>Due Items</span>
                <strong>{{ due_items|length }}</strong>
            </div>

            <div class="client-payment-stat-card">
                <div class="client-payment-stat-icon">
                    <i class="bi bi-cash-coin"></i>
                </div>
                <span>Total Due</span>
                <strong>USD {{ due_total|floatformat:2 }}</strong>
            </div>

            <div class="client-payment-stat-card">
                <div class="client-payment-stat-icon">
                    <i class="bi bi-check-circle-fill"></i>
                </div>
                <span>Paid Total</span>
                <strong>USD {{ paid_total|floatformat:2 }}</strong>
            </div>

            <div class="client-payment-stat-card">
                <div class="client-payment-stat-icon">
                    <i class="bi bi-calendar-heart-fill"></i>
                </div>
                <span>Bookings</span>
                <strong>{{ bookings|length }}</strong>
            </div>

        </div>


        <div class="row g-4 align-items-start">

            <!-- MAIN PAYMENTS -->
            <div class="col-lg-8">

                <!-- DUE ITEMS -->
                <div class="client-payment-card">
                    <div class="client-payment-card-header">
                        <div>
                            <h2>Due Payments</h2>
                            <p>
                                Select one or more due items and continue to checkout.
                            </p>
                        </div>

                        {% if due_items %}
                            <span class="client-payment-mini-total">
                                <i class="bi bi-cash-stack"></i>
                                Total Due: USD {{ due_total|floatformat:2 }}
                            </span>
                        {% endif %}
                    </div>

                    <div class="client-payment-card-body">

                        {% if due_items %}
                            <form method="post"
                                  action="{% url 'hosting_app:hosting_client_payment_checkout' %}"
                                  id="hostingClientPaymentForm">
                                {% csrf_token %}

                                <div class="client-payable-list">
                                    {% for item in due_items %}
                                        <label class="client-payable-card"
                                               for="payable_{{ item.id }}"
                                               data-payable-card>
                                            <div class="d-flex justify-content-between gap-3 align-items-start client-payable-row">

                                                <div class="d-flex gap-3 align-items-start flex-grow-1">
                                                    <div class="pt-1">
                                                        <input type="checkbox"
                                                               name="selected_items"
                                                               value="{{ item.id }}"
                                                               id="payable_{{ item.id }}"
                                                               class="form-check-input client-payable-check"
                                                               data-payable-checkbox
                                                               data-amount="{{ item.amount_usd }}">
                                                    </div>

                                                    <div class="flex-grow-1">
                                                        <h3 class="client-payable-title">
                                                            {{ item.title }}
                                                        </h3>

                                                        {% if item.description %}
                                                            <p class="client-payable-desc">
                                                                {{ item.description }}
                                                            </p>
                                                        {% endif %}

                                                        <div class="client-payable-meta">
                                                            <span class="client-payable-chip due">
                                                                <i class="bi bi-exclamation-circle-fill"></i>
                                                                {{ item.get_payment_status_display }}
                                                            </span>

                                                            <span class="client-payable-chip">
                                                                <i class="bi bi-tag-fill"></i>
                                                                {{ item.get_payment_for_display }}
                                                            </span>

                                                            {% if item.amount_state %}
                                                                <span class="client-payable-chip">
                                                                    <i class="bi bi-shield-check"></i>
                                                                    {{ item.get_amount_state_display }}
                                                                </span>
                                                            {% endif %}

                                                            {% if item.reference_code %}
                                                                <span class="client-payable-chip">
                                                                    <i class="bi bi-hash"></i>
                                                                    {{ item.reference_code }}
                                                                </span>
                                                            {% endif %}
                                                        </div>
                                                    </div>
                                                </div>

                                                <div class="text-lg-end">
                                                    <div class="client-payment-amount">
                                                        USD {{ item.amount_usd|floatformat:2 }}
                                                    </div>
                                                </div>

                                            </div>
                                        </label>
                                    {% endfor %}
                                </div>

                                <div class="client-selected-summary">
                                    <div class="d-flex justify-content-between align-items-center gap-3">
                                        <div>
                                            <span>Selected Payments</span>
                                            <strong class="d-block">
                                                <span id="selectedItemCount">0</span> item(s) ·
                                                USD <span id="selectedPaymentTotal">0.00</span>
                                            </strong>
                                        </div>

                                        <button type="submit"
                                                class="btn client-btn-gold"
                                                id="continueCheckoutBtn"
                                                disabled>
                                            <i class="bi bi-credit-card-fill me-2"></i>
                                            Continue to Checkout
                                        </button>
                                    </div>
                                </div>
                            </form>
                        {% else %}
                            <div class="client-empty-card">
                                <div class="client-empty-icon">
                                    <i class="bi bi-check-circle-fill"></i>
                                </div>

                                <h3>No due payments</h3>

                                <p>
                                    You currently have no due hosting client payments. When you create a booking,
                                    its payable item will appear here.
                                </p>

                                <a href="{% url 'hosting_app:public_host_venue_list' %}"
                                   class="btn client-btn-gold">
                                    <i class="bi bi-search-heart-fill me-2"></i>
                                    Browse Venues
                                </a>
                            </div>
                        {% endif %}

                    </div>
                </div>


                <!-- PAID ITEMS -->
                <div class="client-payment-card">
                    <div class="client-payment-card-header">
                        <div>
                            <h3>Paid Payments</h3>
                            <p>
                                Completed payment records for your hosting experience bookings.
                            </p>
                        </div>

                        {% if paid_items %}
                            <span class="client-payment-mini-total">
                                <i class="bi bi-check-circle-fill"></i>
                                Paid: USD {{ paid_total|floatformat:2 }}
                            </span>
                        {% endif %}
                    </div>

                    <div class="client-payment-card-body">

                        {% if paid_items %}
                            <div class="client-payable-list">
                                {% for item in paid_items %}
                                    <div class="client-payable-card">
                                        <div class="d-flex justify-content-between gap-3 align-items-start client-payable-row">

                                            <div>
                                                <h3 class="client-payable-title">
                                                    {{ item.title }}
                                                </h3>

                                                {% if item.description %}
                                                    <p class="client-payable-desc">
                                                        {{ item.description }}
                                                    </p>
                                                {% endif %}

                                                <div class="client-payable-meta">
                                                    <span class="client-payable-chip paid">
                                                        <i class="bi bi-check-circle-fill"></i>
                                                        {{ item.get_payment_status_display }}
                                                    </span>

                                                    <span class="client-payable-chip">
                                                        <i class="bi bi-tag-fill"></i>
                                                        {{ item.get_payment_for_display }}
                                                    </span>

                                                    {% if item.reference_code %}
                                                        <span class="client-payable-chip">
                                                            <i class="bi bi-hash"></i>
                                                            {{ item.reference_code }}
                                                        </span>
                                                    {% endif %}
                                                </div>
                                            </div>

                                            <div class="client-payment-amount">
                                                USD {{ item.amount_usd|floatformat:2 }}
                                            </div>

                                        </div>
                                    </div>
                                {% endfor %}
                            </div>
                        {% else %}
                            <p class="text-muted mb-0">
                                No paid hosting client payments yet.
                            </p>
                        {% endif %}

                    </div>
                </div>

            </div>


            <!-- SIDEBAR -->
            <div class="col-lg-4">

                <!-- PROFILE -->
                <div class="client-payment-card">
                    <div class="client-payment-card-header">
                        <h3>Client Profile</h3>
                        <p>
                            Account details connected to your hosting payments.
                        </p>
                    </div>

                    <div class="client-payment-card-body">
                        <div class="client-side-profile-grid">

                            <div class="client-side-profile-item">
                                <span>Full Name</span>
                                <strong>{{ hosting_client_profile.full_name }}</strong>
                            </div>

                            <div class="client-side-profile-item">
                                <span>Email</span>
                                <strong>{{ hosting_client_profile.email }}</strong>
                            </div>

                            <div class="client-side-profile-item">
                                <span>Phone</span>
                                <strong>{{ hosting_client_profile.phone_number }}</strong>
                            </div>

                            <div class="client-side-profile-item">
                                <span>WhatsApp</span>
                                <strong>{{ hosting_client_profile.whatsapp_number|default:"Not provided" }}</strong>
                            </div>

                        </div>
                    </div>
                </div>


                <!-- BOOKINGS -->
                <div class="client-payment-card">
                    <div class="client-payment-card-header">
                        <h3>Booking Orders</h3>
                        <p>
                            Your related booking records.
                        </p>
                    </div>

                    <div class="client-payment-card-body">

                        {% if bookings %}
                            {% for booking in bookings %}
                                <div class="client-booking-card">
                                    <h4>
                                        {{ booking.get_event_type_display }}
                                    </h4>

                                    <p>
                                        {{ booking.venue_name_snapshot }}<br>
                                        {{ booking.preferred_event_date }} · {{ booking.number_of_guests }} guests
                                    </p>

                                    <div class="client-payable-meta mb-3">
                                        <span class="client-payable-chip {% if booking.payment_status == 'paid' %}paid{% elif booking.payment_status == 'due' or booking.payment_status == 'unpaid' %}due{% endif %}">
                                            <i class="bi bi-credit-card-fill"></i>
                                            {{ booking.get_payment_status_display }}
                                        </span>

                                        <span class="client-payable-chip">
                                            <i class="bi bi-list-check"></i>
                                            {{ booking.get_status_display }}
                                        </span>
                                    </div>

                                    <a href="{% url 'hosting_app:local_client_booking_review' booking.id %}"
                                       class="btn btn-outline-dark rounded-pill fw-bold w-100">
                                        Review Booking
                                    </a>
                                </div>
                            {% endfor %}
                        {% else %}
                            <p class="text-muted mb-0">
                                No booking records found.
                            </p>
                        {% endif %}

                    </div>
                </div>


                <!-- NOTE -->
                <div class="client-payment-card">
                    <div class="client-payment-card-header">
                        <h3>Payment Note</h3>
                        <p>
                            How payments are handled.
                        </p>
                    </div>

                    <div class="client-payment-card-body">
                        <div class="client-payment-note">
                            Select one or more due items and continue to checkout. After Stripe or PayPal
                            payment is completed, the related booking will be marked as paid and confirmed.
                        </div>
                    </div>
                </div>

            </div>

        </div>

    </div>
</section>


<script>
    document.addEventListener('DOMContentLoaded', function () {
        const checkboxes = document.querySelectorAll('[data-payable-checkbox]');
        const selectedItemCount = document.getElementById('selectedItemCount');
        const selectedPaymentTotal = document.getElementById('selectedPaymentTotal');
        const continueCheckoutBtn = document.getElementById('continueCheckoutBtn');

        function updateSelectedSummary() {
            let count = 0;
            let total = 0;

            checkboxes.forEach(function (checkbox) {
                const card = checkbox.closest('[data-payable-card]');
                const amount = parseFloat(checkbox.dataset.amount || '0');

                if (checkbox.checked) {
                    count += 1;
                    total += amount;

                    if (card) {
                        card.classList.add('is-selected');
                    }
                } else {
                    if (card) {
                        card.classList.remove('is-selected');
                    }
                }
            });

            if (selectedItemCount) {
                selectedItemCount.textContent = count;
            }

            if (selectedPaymentTotal) {
                selectedPaymentTotal.textContent = total.toFixed(2);
            }

            if (continueCheckoutBtn) {
                continueCheckoutBtn.disabled = count === 0;
            }
        }

        checkboxes.forEach(function (checkbox) {
            checkbox.addEventListener('change', updateSelectedSummary);
        });

        updateSelectedSummary();
    });
</script>

{% endblock %}