{% extends 'property_manager_app/base_property_manager.html' %}
{% load static %}

{% block title %}Wallet Top-up | Home in Sri Lanka{% endblock %}

{% block content %}

<style>
    /* =========================================================
       PROPERTY CARE WALLET TOP-UP
       Based on Property Care payment center theme
    ========================================================= */

    .pm-wallet-page {
        min-height: 100vh;
        background:
            radial-gradient(circle at top left, rgba(251, 191, 36, 0.08), transparent 30%),
            radial-gradient(circle at top right, rgba(15, 118, 110, 0.08), transparent 32%),
            linear-gradient(180deg, #f8fafc 0%, #ffffff 100%);
    }

    .pm-wallet-hero {
        border-radius: 32px;
        background:
            radial-gradient(circle at top right, rgba(251, 191, 36, 0.18), transparent 34%),
            linear-gradient(135deg, #052e25 0%, #0f766e 100%);
        color: #ffffff;
        padding: 2.4rem 2.2rem;
        box-shadow: 0 24px 55px rgba(15, 23, 42, 0.16);
    }

    .pm-wallet-mini-title {
        display: inline-flex;
        align-items: center;
        gap: 0.45rem;
        padding: 0.46rem 0.92rem;
        border-radius: 999px;
        background: rgba(255, 255, 255, 0.14);
        border: 1px solid rgba(255, 255, 255, 0.22);
        color: #fef3c7;
        font-size: 0.84rem;
        font-weight: 900;
        text-transform: uppercase;
        letter-spacing: 0.045em;
        margin-bottom: 1rem;
    }

    .pm-wallet-hero h1 {
        font-size: clamp(2.05rem, 4vw, 3.35rem);
        font-weight: 900;
        letter-spacing: -0.04em;
        line-height: 1.08;
        margin-bottom: 0.85rem;
    }

    .pm-wallet-hero p {
        color: rgba(255, 255, 255, 0.88);
        line-height: 1.75;
        max-width: 900px;
        margin-bottom: 0;
    }

    .pm-wallet-hero-actions {
        display: flex;
        flex-wrap: wrap;
        gap: 0.75rem;
        margin-top: 1.45rem;
    }

    .pm-wallet-hero-actions .btn {
        border-radius: 999px;
        font-weight: 800;
        min-height: 46px;
        padding: 0.75rem 1.2rem;
    }

    .pm-btn-outline-light {
        border: 1px solid rgba(255, 255, 255, 0.55);
        color: #ffffff;
        background: rgba(255, 255, 255, 0.08);
    }

    .pm-btn-outline-light:hover {
        background: #ffffff;
        color: #052e25;
    }

    .pm-btn-gold {
        border: none;
        background: #fbbf24;
        color: #052e25;
    }

    .pm-btn-gold:hover {
        background: #fde68a;
        color: #052e25;
        transform: translateY(-1px);
    }

    .pm-overview-strip {
        border: 1px solid #dbe4ee;
        border-radius: 28px;
        background: rgba(255, 255, 255, 0.96);
        box-shadow: 0 14px 30px rgba(15, 23, 42, 0.06);
        padding: 1.35rem 1.4rem;
    }

    .pm-overview-item {
        height: 100%;
        border-radius: 20px;
        background: #f8fafc;
        border: 1px solid #e2e8f0;
        padding: 1rem;
    }

    .pm-overview-label {
        color: #64748b;
        font-size: 0.82rem;
        text-transform: uppercase;
        letter-spacing: 0.045em;
        font-weight: 900;
        margin-bottom: 0.35rem;
    }

    .pm-overview-value {
        color: #0f172a;
        font-size: 1.3rem;
        font-weight: 900;
        margin-bottom: 0;
    }

    .pm-wallet-card {
        border-radius: 28px;
        background: #ffffff;
        border: 1px solid #e2e8f0;
        box-shadow: 0 16px 36px rgba(15, 23, 42, 0.07);
        overflow: hidden;
    }

    .pm-wallet-card-header {
        padding: 1.55rem 1.6rem 1.05rem;
        border-bottom: 1px solid #e2e8f0;
        background:
            radial-gradient(circle at top right, rgba(251, 191, 36, 0.08), transparent 28%),
            linear-gradient(180deg, #ffffff 0%, #f8fafc 100%);
    }

    .pm-wallet-card-header h2,
    .pm-wallet-card-header h3 {
        color: #0f172a;
        font-weight: 950;
        margin-bottom: 0.35rem;
    }

    .pm-wallet-card-header p {
        color: #64748b;
        line-height: 1.7;
        margin-bottom: 0;
    }

    .pm-wallet-card-body {
        padding: 1.6rem;
    }

    .pm-wallet-balance-box {
        border-radius: 26px;
        padding: 1.35rem;
        color: #ffffff;
        background:
            radial-gradient(circle at top right, rgba(251, 191, 36, 0.16), transparent 34%),
            linear-gradient(135deg, #052e25 0%, #0f766e 100%);
        margin-bottom: 1rem;
    }

    .pm-wallet-balance-box span {
        display: block;
        color: rgba(255, 255, 255, 0.72);
        font-size: 0.78rem;
        font-weight: 900;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        margin-bottom: 0.35rem;
    }

    .pm-wallet-balance-box strong {
        display: block;
        color: #ffffff;
        font-size: 2.35rem;
        font-weight: 950;
        letter-spacing: -0.04em;
        line-height: 1;
    }

    .pm-wallet-form-group {
        margin-bottom: 1.35rem;
    }

    .pm-wallet-form-group label {
        display: block;
        color: #0f172a;
        font-weight: 850;
        margin-bottom: 0.5rem;
    }

    .pm-wallet-form-group input,
    .pm-wallet-form-group select,
    .pm-wallet-form-group textarea {
        border-radius: 18px;
        border: 1px solid #dbe4ee;
        background: #f8fafc;
        min-height: 52px;
        color: #0f172a;
        transition: all 0.2s ease;
    }

    .pm-wallet-form-group input:focus,
    .pm-wallet-form-group select:focus,
    .pm-wallet-form-group textarea:focus {
        border-color: #0f766e;
        background: #ffffff;
        box-shadow: 0 0 0 0.22rem rgba(15, 118, 110, 0.13);
    }

    .pm-wallet-field-error {
        color: #dc2626;
        font-size: 0.86rem;
        font-weight: 750;
        margin-top: 0.4rem;
    }

    .pm-quick-amount-grid {
        display: grid;
        grid-template-columns: repeat(4, minmax(0, 1fr));
        gap: 0.75rem;
        margin-top: 0.85rem;
    }

    .pm-quick-amount-btn {
        border-radius: 18px;
        border: 1px solid #dbe4ee;
        background: #ffffff;
        color: #0f172a;
        padding: 0.8rem 0.75rem;
        font-weight: 900;
        transition: 0.2s ease;
    }

    .pm-quick-amount-btn:hover {
        border-color: #0f766e;
        background: #ecfdf5;
        color: #0f766e;
        transform: translateY(-1px);
    }

    .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 #dbe4ee;
        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: #94a3b8;
        box-shadow: 0 0.45rem 1rem rgba(15, 23, 42, 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-title {
        font-weight: 900;
        color: #0f172a;
        line-height: 1.2;
    }

    .payment-method-subtitle {
        font-size: 0.9rem;
        color: #64748b;
        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: #0f766e;
        background: #f0fdfa;
        box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.12);
    }

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

    .pm-wallet-submit {
        min-height: 52px;
        border-radius: 0.9rem;
        background: #1f7a4d;
        color: #ffffff;
        border: 1px solid #1f7a4d;
        font-weight: 900;
        box-shadow: 0 0.35rem 0.75rem rgba(31, 122, 77, 0.25);
        transition: all 0.2s ease-in-out;
    }

    .pm-wallet-submit:hover,
    .pm-wallet-submit:focus {
        background: #19633f;
        border-color: #19633f;
        color: #ffffff;
        transform: translateY(-1px);
        box-shadow: 0 0.5rem 1rem rgba(31, 122, 77, 0.35);
    }

    .pm-note-box {
        border-radius: 24px;
        padding: 1rem 1.1rem;
        background: #fffbeb;
        border: 1px solid #fde68a;
        color: #92400e;
        line-height: 1.7;
    }

    .pm-note-box strong {
        color: #0f172a;
    }

    .pm-side-stat {
        border-radius: 22px;
        padding: 1.05rem;
        background: #f8fafc;
        border: 1px solid #e2e8f0;
        margin-bottom: 0.85rem;
    }

    .pm-side-stat span {
        display: block;
        color: #64748b;
        font-size: 0.78rem;
        font-weight: 900;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        margin-bottom: 0.35rem;
    }

    .pm-side-stat strong {
        color: #0f172a;
        font-weight: 950;
        font-size: 1.08rem;
    }

    @media (max-width: 575.98px) {
        .pm-wallet-hero {
            padding: 2rem 1.35rem;
            border-radius: 28px;
        }

        .pm-wallet-card-body,
        .pm-wallet-card-header {
            padding-left: 1.15rem;
            padding-right: 1.15rem;
        }

        .pm-wallet-hero-actions {
            flex-direction: column;
            align-items: stretch;
        }

        .pm-wallet-hero-actions .btn {
            width: 100%;
        }

        .pm-quick-amount-grid {
            grid-template-columns: repeat(2, minmax(0, 1fr));
        }

        .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,
        .payment-method-status {
            font-size: 0.78rem;
        }
    }
</style>


<section class="pm-wallet-page py-5">
    <div class="container">

        <!-- =====================================================
             HERO
        ====================================================== -->
        <div class="pm-wallet-hero mb-4">
            <span class="pm-wallet-mini-title">
                <i class="bi bi-wallet2"></i>
                Property Care Wallet
            </span>

            <h1>
                Top Up Your Property Care Wallet
            </h1>

            <p>
                Add funds to your Property Care wallet so you can handle future monthly plan fees,
                maintenance-related charges, approved service requests, and other property care payments
                more conveniently.
            </p>

            <div class="pm-wallet-hero-actions">
                <a href="{% url 'property_manager_app:property_manager_dashboard' %}"
                   class="btn pm-btn-outline-light">
                    <i class="bi bi-speedometer2 me-2"></i>
                    Dashboard
                </a>

                <a href="{% url 'property_manager_app:property_care_payment_center' %}"
                   class="btn pm-btn-outline-light">
                    <i class="bi bi-credit-card-2-front-fill me-2"></i>
                    Payment Center
                </a>

                <a href="{% url 'property_manager_app:managed_property_list' %}"
                   class="btn pm-btn-gold">
                    <i class="bi bi-houses-fill me-2"></i>
                    My Properties
                </a>
            </div>
        </div>


        <!-- =====================================================
             OVERVIEW STRIP
        ====================================================== -->
        <div class="pm-overview-strip mb-4">
            <div class="row g-3">

                <div class="col-md-4">
                    <div class="pm-overview-item">
                        <div class="pm-overview-label">Account</div>
                        <div class="pm-overview-value">
                            {{ profile.user.get_full_name|default:profile.user.username }}
                        </div>
                    </div>
                </div>

                <div class="col-md-4">
                    <div class="pm-overview-item">
                        <div class="pm-overview-label">Wallet Type</div>
                        <div class="pm-overview-value">
                            {{ account_label|default:"Property Care Wallet" }}
                        </div>
                    </div>
                </div>

                <div class="col-md-4">
                    <div class="pm-overview-item">
                        <div class="pm-overview-label">Current Balance</div>
                        <div class="pm-overview-value">
                            USD {{ wallet.balance_usd|floatformat:2 }}
                        </div>
                    </div>
                </div>

            </div>
        </div>


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

            <!-- =====================================================
                 TOP-UP FORM
            ====================================================== -->
            <div class="col-lg-8">
                <div class="pm-wallet-card">

                    <div class="pm-wallet-card-header">
                        <h2>Top-up Details</h2>
                        <p>
                            Enter the amount you want to add to your wallet and choose your preferred payment method.
                        </p>
                    </div>

                    <div class="pm-wallet-card-body">
                        <form method="post">
                            {% csrf_token %}

                            {% if form.non_field_errors %}
                                <div class="alert alert-danger rounded-4">
                                    {% for error in form.non_field_errors %}
                                        <div>{{ error }}</div>
                                    {% endfor %}
                                </div>
                            {% endif %}

                            <!-- Amount -->
                            <div class="pm-wallet-form-group">
                                <label for="{{ form.amount_usd.id_for_label }}">
                                    Top-up Amount USD
                                </label>

                                {{ form.amount_usd }}

                                {% for error in form.amount_usd.errors %}
                                    <div class="pm-wallet-field-error">{{ error }}</div>
                                {% endfor %}

                                <div class="pm-quick-amount-grid">
                                    <button type="button" class="pm-quick-amount-btn" data-amount="25">USD 25</button>
                                    <button type="button" class="pm-quick-amount-btn" data-amount="50">USD 50</button>
                                    <button type="button" class="pm-quick-amount-btn" data-amount="100">USD 100</button>
                                    <button type="button" class="pm-quick-amount-btn" data-amount="250">USD 250</button>
                                </div>
                            </div>


                            <!-- Payment Method -->
                            <div class="pm-wallet-form-group">
                                <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"
                                        {% if form.payment_method.value == "stripe" or not form.payment_method.value %}checked{% endif %}
                                    >

                                    <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>
                                                <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"
                                        {% if form.payment_method.value == "paypal" %}checked{% endif %}
                                    >

                                    <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>
                                                <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>

                                {% for error in form.payment_method.errors %}
                                    <div class="pm-wallet-field-error">{{ error }}</div>
                                {% endfor %}
                            </div>


                            <div class="pm-note-box mb-4">
                                <strong>Important:</strong>
                                Wallet top-up payments are credited to your Property Care wallet only after payment confirmation.
                                You can use your wallet balance for eligible future Property Care payments where wallet usage is enabled.
                            </div>

                            <button type="submit" class="btn pm-wallet-submit w-100">
                                <i class="bi bi-lock-fill me-2"></i>
                                Continue to Secure Payment
                            </button>
                        </form>
                    </div>

                </div>
            </div>


            <!-- =====================================================
                 SIDE INFO
            ====================================================== -->
            <div class="col-lg-4">

                <div class="pm-wallet-card mb-4">
                    <div class="pm-wallet-card-header">
                        <h3>Wallet Summary</h3>
                        <p>
                            Your wallet helps you manage future property care payments more smoothly.
                        </p>
                    </div>

                    <div class="pm-wallet-card-body">
                        <div class="pm-wallet-balance-box">
                            <span>Available Balance</span>
                            <strong>USD {{ wallet.balance_usd|floatformat:2 }}</strong>
                        </div>

                        <div class="pm-side-stat">
                            <span>Wallet Status</span>
                            <strong>
                                {% if wallet.is_active %}
                                    Active
                                {% else %}
                                    Inactive
                                {% endif %}
                            </strong>
                        </div>

                        <div class="pm-side-stat">
                            <span>Profile Type</span>
                            <strong>Property Care Owner</strong>
                        </div>

                        <div class="pm-side-stat">
                            <span>Minimum Top-up</span>
                            <strong>USD 5.00</strong>
                        </div>
                    </div>
                </div>


                <div class="pm-wallet-card mb-4">
                    <div class="pm-wallet-card-header">
                        <h3>What can wallet funds be used for?</h3>
                        <p>
                            Wallet funds can support eligible Property Care payments in the future.
                        </p>
                    </div>

                    <div class="pm-wallet-card-body">
                        <ul class="mb-0 ps-3 text-muted" style="line-height: 1.85;">
                            <li>Monthly Property Care plan fees where wallet usage is enabled.</li>
                            <li>Approved repair, inspection, and maintenance service charges.</li>
                            <li>Future property-related service payments inside the Home in Sri Lanka ecosystem.</li>
                            <li>Convenient balance usage for repeat property care payments.</li>
                        </ul>
                    </div>
                </div>


                <div class="pm-wallet-card">
                    <div class="pm-wallet-card-header">
                        <h3>Quick Links</h3>
                        <p>
                            Continue managing your Property Care account.
                        </p>
                    </div>

                    <div class="pm-wallet-card-body">
                        <div class="d-grid gap-2">
                            <a href="{% url 'property_manager_app:property_care_payment_center' %}"
                               class="btn btn-dark rounded-pill fw-bold">
                                <i class="bi bi-credit-card-2-front-fill me-2"></i>
                                Payment Center
                            </a>

                            <a href="{% url 'property_manager_app:property_manager_dashboard' %}"
                               class="btn btn-outline-dark rounded-pill fw-bold">
                                <i class="bi bi-speedometer2 me-2"></i>
                                Dashboard
                            </a>

                            <a href="{% url 'property_manager_app:managed_property_list' %}"
                               class="btn btn-outline-dark rounded-pill fw-bold">
                                <i class="bi bi-houses-fill me-2"></i>
                                My Properties
                            </a>
                        </div>
                    </div>
                </div>

            </div>

        </div>

    </div>
</section>


<script>
    document.addEventListener('DOMContentLoaded', function () {
        const amountInput = document.getElementById('{{ form.amount_usd.id_for_label }}');
        const quickButtons = document.querySelectorAll('.pm-quick-amount-btn');

        quickButtons.forEach(function (button) {
            button.addEventListener('click', function () {
                const amount = button.dataset.amount || '';
                if (amountInput) {
                    amountInput.value = amount;
                    amountInput.focus();
                }
            });
        });
    });
</script>

{% endblock %}