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

{% block title %}Payment Checkout | Host in Sri Lanka{% endblock %}

{% block content %}

<style>
    /* =========================================================
       HOSTING PARTNER PAYMENT CHECKOUT — PREMIUM
    ========================================================= */

    .hosting-checkout-page {
        min-height: 100vh;
        padding: 4rem 0 5rem;
        background:
            radial-gradient(circle at top left, rgba(255, 193, 7, 0.12), transparent 30%),
            radial-gradient(circle at top right, rgba(116, 49, 41, 0.10), transparent 32%),
            linear-gradient(180deg, #fffaf5 0%, #fff4e8 42%, #ffffff 100%);
    }

    .hosting-checkout-hero {
        position: relative;
        overflow: hidden;
        border-radius: 38px;
        padding: 2.6rem 2.4rem;
        margin-bottom: 2rem;
        color: #ffffff;
        background:
            radial-gradient(circle at top right, rgba(255, 211, 107, 0.22), transparent 32%),
            linear-gradient(135deg, #2d1716 0%, #743129 56%, #a7601b 100%);
        box-shadow: 0 30px 68px rgba(58, 30, 24, 0.20);
    }

    .hosting-checkout-hero::before {
        content: "";
        position: absolute;
        top: -110px;
        right: -85px;
        width: 300px;
        height: 300px;
        border-radius: 50%;
        background: rgba(255, 255, 255, 0.07);
        pointer-events: none;
    }

    .hosting-checkout-hero::after {
        content: "";
        position: absolute;
        bottom: -150px;
        left: -110px;
        width: 360px;
        height: 360px;
        border-radius: 50%;
        background: rgba(255, 193, 7, 0.10);
        pointer-events: none;
    }

    .hosting-checkout-hero-inner {
        position: relative;
        z-index: 2;
    }

    .hosting-checkout-kicker {
        display: inline-flex;
        align-items: center;
        gap: 0.5rem;
        padding: 0.52rem 1rem;
        border-radius: 999px;
        background: rgba(255, 255, 255, 0.14);
        border: 1px solid rgba(255, 255, 255, 0.22);
        color: #ffe7aa;
        font-size: 0.84rem;
        font-weight: 900;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        margin-bottom: 1rem;
    }

    .hosting-checkout-hero h1 {
        font-size: clamp(2.15rem, 4vw, 3.45rem);
        font-weight: 950;
        line-height: 1.1;
        letter-spacing: -0.045em;
        margin-bottom: 0.75rem;
    }

    .hosting-checkout-hero p {
        max-width: 850px;
        margin-bottom: 0;
        color: rgba(255, 255, 255, 0.88);
        font-size: 1.03rem;
        line-height: 1.8;
    }

    .hosting-checkout-hero-actions {
        display: flex;
        flex-wrap: wrap;
        gap: 0.85rem;
        margin-top: 1.6rem;
    }

    .hosting-checkout-hero-actions .btn {
        border-radius: 999px;
        font-weight: 850;
        padding: 0.82rem 1.35rem;
        min-height: 48px;
    }

    .hosting-btn-gold {
        background: #ffc107;
        color: #2d1716;
        border: none;
    }

    .hosting-btn-gold:hover {
        background: #ffe082;
        color: #2d1716;
        transform: translateY(-1px);
    }

    .hosting-btn-light-outline {
        background: rgba(255, 255, 255, 0.10);
        color: #ffffff;
        border: 1px solid rgba(255, 255, 255, 0.42);
    }

    .hosting-btn-light-outline:hover {
        background: #ffffff;
        color: #2d1716;
    }

    .hosting-checkout-card {
        border-radius: 34px;
        background: rgba(255, 255, 255, 0.98);
        border: 1px solid rgba(239, 226, 211, 0.95);
        box-shadow: 0 24px 56px rgba(58, 30, 24, 0.10);
        overflow: hidden;
        margin-bottom: 1.5rem;
    }

    .hosting-checkout-card-header {
        padding: 1.55rem 1.7rem 1.1rem;
        border-bottom: 1px solid #efe2d3;
        background:
            radial-gradient(circle at top right, rgba(255, 193, 7, 0.08), transparent 28%),
            linear-gradient(180deg, #ffffff 0%, #fffaf5 100%);
    }

    .hosting-checkout-card-header h2,
    .hosting-checkout-card-header h3 {
        color: #2d1716;
        font-weight: 950;
        margin-bottom: 0.35rem;
    }

    .hosting-checkout-card-header p {
        color: #7a655d;
        line-height: 1.65;
        margin-bottom: 0;
    }

    .hosting-checkout-card-body {
        padding: 1.6rem;
    }

    .hosting-checkout-table {
        margin-bottom: 0;
    }

    .hosting-checkout-table thead th {
        color: #7a655d;
        font-size: 0.78rem;
        font-weight: 900;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        border-bottom-color: #efe2d3;
        white-space: nowrap;
    }

    .hosting-checkout-table tbody td {
        border-color: #f0e4d6;
        vertical-align: middle;
        color: #2d1716;
        padding-top: 1rem;
        padding-bottom: 1rem;
    }

    .hosting-checkout-item-title {
        font-weight: 900;
        color: #2d1716;
        margin-bottom: 0.25rem;
    }

    .hosting-checkout-item-desc {
        color: #7a655d;
        font-size: 0.9rem;
        line-height: 1.55;
        margin-bottom: 0;
    }

    .hosting-wallet-row {
        background: #fffaf5;
    }

    .hosting-wallet-row td {
        border-top: 1px solid #efe2d3;
    }

    .hosting-wallet-toggle-wrap {
        padding: 0.4rem 0;
    }

    .hosting-wallet-toggle-wrap .form-check-input:checked {
        background-color: #a7601b;
        border-color: #a7601b;
    }

    .hosting-wallet-coverage {
        color: #7a655d;
        line-height: 1.6;
    }

    .hosting-total-row td {
        border-top: 2px solid #efe2d3;
        font-size: 1.05rem;
    }

    .hosting-total-label {
        color: #2d1716;
        font-weight: 950;
    }

    .hosting-total-amount {
        color: #2d1716;
        font-weight: 950;
        font-size: 1.25rem;
    }

    .payment-method-list {
        display: grid;
        gap: 0.9rem;
    }

    .payment-method-card {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 1rem;
        border: 1.5px solid #efe2d3;
        border-radius: 1.15rem;
        padding: 0.95rem 1rem;
        background: #ffffff;
        cursor: pointer;
        transition: all 0.2s ease-in-out;
        min-height: 78px;
    }

    .payment-method-card:hover {
        border-color: #d8bfa3;
        box-shadow: 0 0.45rem 1rem rgba(58, 30, 24, 0.08);
    }

    .payment-method-left {
        display: flex;
        align-items: center;
        gap: 0.9rem;
        min-width: 0;
    }

    .payment-method-icon {
        width: 46px;
        height: 46px;
        border-radius: 0.9rem;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.2rem;
        flex-shrink: 0;
    }

    .stripe-card .payment-method-icon {
        background: #eef4ff;
        color: #635bff;
    }

    .paypal-card .payment-method-icon {
        background: #eef7ff;
        color: #0070ba;
    }

    .payment-method-text {
        min-width: 0;
    }

    .payment-method-title {
        font-weight: 900;
        color: #2d1716;
        line-height: 1.2;
    }

    .payment-method-subtitle {
        font-size: 0.9rem;
        color: #7a655d;
        line-height: 1.35;
        margin-top: 0.15rem;
    }

    .payment-method-right {
        display: flex;
        align-items: center;
        gap: 0.65rem;
        flex-shrink: 0;
    }

    .payment-method-status {
        font-size: 0.78rem;
        font-weight: 850;
        color: #047857;
        background: #ecfdf5;
        border: 1px solid #a7f3d0;
        border-radius: 999px;
        padding: 0.3rem 0.65rem;
    }

    .payment-method-check {
        color: #047857;
        font-size: 1.15rem;
        opacity: 0;
        transition: opacity 0.2s ease-in-out;
        line-height: 1;
    }

    .btn-check:checked + .payment-method-card {
        border-color: #a7601b;
        background: #fffaf5;
        box-shadow: 0 0 0 3px rgba(167, 96, 27, 0.12);
    }

    .btn-check:checked + .payment-method-card .payment-method-check {
        opacity: 1;
    }

    .hosting-payment-summary-box {
        border-radius: 24px;
        padding: 1.1rem;
        background: #fffaf5;
        border: 1px solid #efe2d3;
        margin-bottom: 1rem;
    }

    .hosting-payment-summary-row {
        display: flex;
        justify-content: space-between;
        gap: 1rem;
        color: #6f5b53;
        line-height: 1.6;
        margin-bottom: 0.55rem;
    }

    .hosting-payment-summary-row:last-child {
        margin-bottom: 0;
        padding-top: 0.7rem;
        border-top: 1px solid #efe2d3;
        color: #2d1716;
        font-weight: 950;
    }

    .hosting-security-note {
        border-radius: 22px;
        background: #fff4e4;
        border: 1px solid #efd7ae;
        color: #7a4c18;
        padding: 1rem;
        line-height: 1.65;
        font-size: 0.92rem;
        margin-top: 1rem;
    }

    .hosting-security-note strong {
        color: #2d1716;
    }

    .hosting-pay-button {
        border-radius: 999px;
        min-height: 52px;
        font-weight: 900;
        background: linear-gradient(135deg, #321816, #a7601b);
        color: #ffffff;
        border: none;
        box-shadow: 0 14px 28px rgba(167, 96, 27, 0.20);
    }

    .hosting-pay-button:hover {
        color: #2d1716;
        background: linear-gradient(135deg, #ffc107, #ffe082);
        transform: translateY(-2px);
    }

    .hosting-back-button {
        border-radius: 999px;
        min-height: 48px;
        font-weight: 850;
    }

    @media (max-width: 991.98px) {
        .hosting-checkout-page {
            padding-top: 2.8rem;
        }

        .hosting-checkout-hero {
            padding: 2rem 1.45rem;
            border-radius: 30px;
        }
    }

    @media (max-width: 767.98px) {
        .hosting-checkout-card-body,
        .hosting-checkout-card-header {
            padding-left: 1.15rem;
            padding-right: 1.15rem;
        }

        .hosting-checkout-hero-actions {
            flex-direction: column;
            align-items: stretch;
        }

        .hosting-checkout-hero-actions .btn {
            width: 100%;
        }

        .payment-method-card {
            padding: 0.9rem;
            min-height: 74px;
        }

        .payment-method-icon {
            width: 42px;
            height: 42px;
            font-size: 1.1rem;
        }

        .payment-method-title {
            font-size: 0.98rem;
        }

        .payment-method-subtitle {
            font-size: 0.83rem;
        }

        .payment-method-status {
            font-size: 0.75rem;
            padding: 0.25rem 0.55rem;
        }
    }
</style>


<section class="hosting-checkout-page">
    <div class="container">

        <!-- HERO -->
        <div class="hosting-checkout-hero">
            <div class="hosting-checkout-hero-inner">

                <span class="hosting-checkout-kicker">
                    <i class="bi bi-credit-card-2-front-fill"></i>
                    Hosting Partner Checkout
                </span>

                <h1>Payment Checkout</h1>

                <p>
                    Review your selected hosting partner payment items and choose a payment method
                    to continue securely.
                </p>

                <div class="hosting-checkout-hero-actions">
                    <a href="{% url 'hosting_app:host_payment_center' %}"
                       class="btn hosting-btn-light-outline">
                        <i class="bi bi-arrow-left-circle me-2"></i>
                        Back to Payment Center
                    </a>

                    <a href="{% url 'hosting_app:host_dashboard' %}"
                       class="btn hosting-btn-light-outline">
                        <i class="bi bi-speedometer2 me-2"></i>
                        Dashboard
                    </a>

                    <a href="{% url 'hosting_app:host_banking_details' %}"
                       class="btn hosting-btn-gold">
                        <i class="bi bi-bank2 me-2"></i>
                        Banking Details
                    </a>
                </div>

            </div>
        </div>


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

            <!-- SELECTED ITEMS -->
            <div class="col-lg-8">
                <div class="hosting-checkout-card">

                    <div class="hosting-checkout-card-header">
                        <div>
                            <h2>Selected Payable Items</h2>
                            <p>
                                These are the host-side payment items selected from your Hosting Partner Payment Center.
                            </p>
                        </div>
                    </div>

                    <div class="hosting-checkout-card-body">

                        <div class="table-responsive">
                            <table class="table hosting-checkout-table align-middle">
                                <thead>
                                    <tr>
                                        <th>Title</th>
                                        <th>Type</th>
                                        <th class="text-end">Amount</th>
                                    </tr>
                                </thead>

                                <tbody>
                                    {% for item in payable_items %}
                                        <tr>
                                            <td>
                                                <div class="hosting-checkout-item-title">
                                                    {{ item.title }}
                                                </div>

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

                                                {% if item.reference_code %}
                                                    <p class="hosting-checkout-item-desc">
                                                        Reference: {{ item.reference_code }}
                                                    </p>
                                                {% endif %}
                                            </td>

                                            <td>
                                                {{ item.get_payment_for_display }}
                                            </td>

                                            <td class="text-end fw-bold">
                                                USD {{ item.amount_usd|floatformat:2 }}
                                            </td>
                                        </tr>
                                    {% endfor %}

                                    <!-- Wallet toggle row -->
                                    <tr class="hosting-wallet-row" id="wallet-row">
                                        <td colspan="2">
                                            <div class="hosting-wallet-toggle-wrap">
                                                <div class="form-check form-switch">
                                                    <input class="form-check-input"
                                                           type="checkbox"
                                                           id="useWalletToggle">

                                                    <label class="form-check-label fw-semibold"
                                                           for="useWalletToggle">
                                                        Use Wallet Balance
                                                        (<span id="wallet-balance">USD {{ wallet.balance_usd|floatformat:2 }}</span>)
                                                    </label>
                                                </div>

                                                <div id="wallet-coverage"
                                                     class="hosting-wallet-coverage small mt-1 d-none">
                                                    Wallet covers
                                                    <strong id="covered-amount">USD 0.00</strong>,
                                                    remaining
                                                    <strong id="remaining-amount">USD 0.00</strong>
                                                    via selected payment method.
                                                </div>
                                            </div>
                                        </td>

                                        <td class="text-end fw-bold text-primary">
                                            USD {{ wallet.balance_usd|floatformat:2 }}
                                        </td>
                                    </tr>
                                </tbody>

                                <tfoot>
                                    <tr class="hosting-total-row">
                                        <td colspan="2" class="text-end">
                                            <span class="hosting-total-label" id="total-label">
                                                Total Payment
                                            </span>
                                        </td>

                                        <td class="text-end">
                                            <span class="hosting-total-amount" id="total-card-amount">
                                                USD {{ total_amount|floatformat:2 }}
                                            </span>
                                        </td>
                                    </tr>
                                </tfoot>
                            </table>
                        </div>

                        <div class="hosting-security-note">
                            <strong>Note:</strong>
                            This checkout is for Hosting Partner payments such as optional featured visibility.
                            Future guest event booking earnings will be handled separately through your hosting wallet.
                        </div>

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


            <!-- PAYMENT METHOD -->
            <div class="col-lg-4">
                <div class="hosting-checkout-card">

                    <div class="hosting-checkout-card-header">
                        <div>
                            <h3>Choose Payment Method</h3>
                            <p>
                                Select how you want to complete this payment.
                            </p>
                        </div>
                    </div>

                    <div class="hosting-checkout-card-body">

                        <form method="post" action="{% url 'hosting_app:host_payment_checkout_pay' %}">
                            {% csrf_token %}

                            <input type="hidden" name="use_wallet" id="useWalletInput" value="">

                            {% for item in payable_items %}
                                <input type="hidden" name="selected_items" value="{{ item.id }}">
                            {% endfor %}

                            <div class="mb-4">
                                <label class="form-label fw-bold mb-3">
                                    Payment Method
                                </label>

                                <div class="payment-method-list">

                                    <input
                                        class="btn-check"
                                        type="radio"
                                        name="payment_method"
                                        id="payStripe"
                                        value="stripe"
                                        checked
                                    >

                                    <label class="payment-method-card stripe-card" for="payStripe">
                                        <div class="payment-method-left">
                                            <div class="payment-method-icon">
                                                <i class="bi bi-credit-card-2-front-fill"></i>
                                            </div>

                                            <div class="payment-method-text">
                                                <div class="payment-method-title">Stripe</div>
                                                <div class="payment-method-subtitle">
                                                    Pay securely with card
                                                </div>
                                            </div>
                                        </div>

                                        <div class="payment-method-right">
                                            <span class="payment-method-status">Available</span>

                                            <span class="payment-method-check">
                                                <i class="bi bi-check-circle-fill"></i>
                                            </span>
                                        </div>
                                    </label>


                                    <input
                                        class="btn-check"
                                        type="radio"
                                        name="payment_method"
                                        id="payPaypal"
                                        value="paypal"
                                    >

                                    <label class="payment-method-card paypal-card" for="payPaypal">
                                        <div class="payment-method-left">
                                            <div class="payment-method-icon">
                                                <i class="bi bi-paypal"></i>
                                            </div>

                                            <div class="payment-method-text">
                                                <div class="payment-method-title">PayPal</div>
                                                <div class="payment-method-subtitle">
                                                    Pay securely with your PayPal account
                                                </div>
                                            </div>
                                        </div>

                                        <div class="payment-method-right">
                                            <span class="payment-method-status">Available</span>

                                            <span class="payment-method-check">
                                                <i class="bi bi-check-circle-fill"></i>
                                            </span>
                                        </div>
                                    </label>

                                </div>
                            </div>

                            <!-- Secure Checkout Trust Badge -->
                            <div class="checkout-secure-trust-wrap">
                                <div class="checkout-secure-trust-badge">
                                    <img src="{% static 'expat_life/images/secure_https_badge.png' %}"
                                        alt="Secure HTTPS SSL TLS Protected"
                                        class="checkout-secure-trust-img">

                                    <div class="checkout-secure-trust-text">
                                        <strong>Secure Checkout</strong>
                                        <span>Your payment is protected through SSL/TLS encrypted connection.</span>
                                    </div>
                                </div>
                            </div>

                            <div class="hosting-payment-summary-box">
                                <div class="hosting-payment-summary-row">
                                    <span>Selected Items</span>
                                    <strong>{{ payable_items|length }}</strong>
                                </div>

                                <div class="hosting-payment-summary-row">
                                    <span>Wallet Balance</span>
                                    <strong>USD {{ wallet.balance_usd|floatformat:2 }}</strong>
                                </div>

                                <div class="hosting-payment-summary-row">
                                    <span id="sidebar-total-label">Total Payment</span>
                                    <strong id="sidebar-total">USD {{ total_amount|floatformat:2 }}</strong>
                                </div>
                            </div>

                            <button type="submit" class="btn hosting-pay-button w-100">
                                <i class="bi bi-lock-fill me-2"></i>
                                Continue to Payment
                            </button>

                            <a href="{% url 'hosting_app:host_payment_center' %}"
                               class="btn btn-outline-secondary hosting-back-button w-100 mt-2">
                                <i class="bi bi-arrow-left-circle me-2"></i>
                                Back to Payment Center
                            </a>

                        </form>

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

        </div>

    </div>
</section>


<script>
    document.addEventListener('DOMContentLoaded', function () {
        const subtotal = parseFloat("{{ total_amount|floatformat:2 }}");
        const walletBalance = parseFloat("{{ wallet.balance_usd|floatformat:2 }}");

        const toggle = document.getElementById('useWalletToggle');
        const totalLabel = document.getElementById('total-label');
        const sidebarTotalLabel = document.getElementById('sidebar-total-label');
        const totalCardAmount = document.getElementById('total-card-amount');
        const sidebarTotal = document.getElementById('sidebar-total');
        const coverageDiv = document.getElementById('wallet-coverage');
        const coveredAmount = document.getElementById('covered-amount');
        const remainingAmount = document.getElementById('remaining-amount');
        const useWalletInput = document.getElementById('useWalletInput');

        function money(value) {
            return 'USD ' + value.toFixed(2);
        }

        function updateTotal() {
            let due = subtotal;
            let covered = 0;

            if (toggle.checked) {
                covered = Math.min(subtotal, walletBalance);
                due = subtotal - covered;

                totalLabel.textContent = 'Remaining to pay';
                sidebarTotalLabel.textContent = 'Remaining to pay';

                coverageDiv.classList.remove('d-none');
                coveredAmount.textContent = money(covered);
                remainingAmount.textContent = money(due);
            } else {
                totalLabel.textContent = 'Total Payment';
                sidebarTotalLabel.textContent = 'Total Payment';
                coverageDiv.classList.add('d-none');
            }

            totalCardAmount.textContent = money(due);
            sidebarTotal.textContent = money(due);
            useWalletInput.value = toggle.checked ? '1' : '0';
        }

        if (toggle) {
            toggle.addEventListener('change', updateTotal);
            updateTotal();
        }
    });
</script>

{% endblock %}