{% extends 'affiliates_app/base_affiliate.html' %}
{% load static %}

{% block title %}Edit Affiliate Profile{% endblock %}

{% block content %}
<style>
    .affiliate-edit-section {
        background:
            radial-gradient(circle at top left, rgba(25, 135, 84, 0.12), transparent 28%),
            radial-gradient(circle at bottom right, rgba(13, 110, 253, 0.08), transparent 30%),
            linear-gradient(135deg, #f8fafc 0%, #eefaf4 100%);
        min-height: 100vh;
    }

    .affiliate-edit-card {
        border-radius: 28px;
        overflow: hidden;
        background: #ffffff;
        box-shadow: 0 20px 55px rgba(15, 23, 42, 0.10);
    }

    .affiliate-edit-header {
        background: linear-gradient(135deg, #064e3b 0%, #047857 58%, #0f766e 100%);
        padding: 2rem 2.2rem;
        color: #ffffff;
        position: relative;
        overflow: hidden;
    }

    .affiliate-edit-header::after {
        content: "";
        position: absolute;
        width: 210px;
        height: 210px;
        border-radius: 50%;
        right: -80px;
        top: -80px;
        background: rgba(255, 255, 255, 0.12);
    }

    .affiliate-edit-header-content {
        position: relative;
        z-index: 2;
    }

    .affiliate-edit-kicker {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        background: rgba(255, 255, 255, 0.15);
        border: 1px solid rgba(255, 255, 255, 0.18);
        color: rgba(255, 255, 255, 0.92);
        padding: 7px 13px;
        border-radius: 50rem;
        font-size: 0.82rem;
        font-weight: 700;
        margin-bottom: 0.9rem;
    }

    .affiliate-edit-header h2 {
        font-weight: 800;
        letter-spacing: -0.03em;
        margin-bottom: 0.45rem;
    }

    .affiliate-edit-header p {
        margin-bottom: 0;
        color: rgba(255, 255, 255, 0.82);
        max-width: 680px;
    }

    .affiliate-edit-body {
        padding: 2.2rem;
    }

    .affiliate-form-panel {
        background: #ffffff;
        border: 1px solid #e7f2ec;
        border-radius: 22px;
        padding: 1.5rem;
    }

    .affiliate-form-panel-title {
        display: flex;
        align-items: center;
        gap: 10px;
        font-weight: 800;
        color: #064e3b;
        margin-bottom: 1.25rem;
    }

    .affiliate-form-panel-title i {
        color: #047857;
        font-size: 1.2rem;
    }

    .affiliate-field-wrap {
        position: relative;
        height: 100%;
    }

    .affiliate-edit-card .form-label {
        font-weight: 800;
        color: #1f2937;
        font-size: 0.9rem;
        margin-bottom: 0.48rem;
        letter-spacing: -0.01em;
    }

    /* Main form element styling */
    .affiliate-form-panel input:not([type="checkbox"]):not([type="radio"]),
    .affiliate-form-panel select,
    .affiliate-form-panel textarea,
    .affiliate-edit-card .form-control,
    .affiliate-edit-card .form-select {
        width: 100%;
        min-height: 50px;
        border-radius: 16px !important;
        border: 1px solid #dbe7e1 !important;
        background:
            linear-gradient(180deg, #ffffff 0%, #fbfdfc 100%) !important;
        color: #0f172a;
        padding: 0.78rem 0.95rem;
        font-size: 0.96rem;
        font-weight: 500;
        line-height: 1.45;
        outline: none;
        box-shadow: 0 8px 18px rgba(15, 23, 42, 0.025);
        transition: border-color 0.2s ease, box-shadow 0.2s ease, background 0.2s ease, transform 0.2s ease;
        appearance: auto;
    }

    .affiliate-form-panel input:not([type="checkbox"]):not([type="radio"]):hover,
    .affiliate-form-panel select:hover,
    .affiliate-form-panel textarea:hover,
    .affiliate-edit-card .form-control:hover,
    .affiliate-edit-card .form-select:hover {
        border-color: #a8d5c0 !important;
        background: #ffffff !important;
        box-shadow: 0 10px 22px rgba(15, 23, 42, 0.045);
    }

    .affiliate-form-panel input:not([type="checkbox"]):not([type="radio"]):focus,
    .affiliate-form-panel select:focus,
    .affiliate-form-panel textarea:focus,
    .affiliate-edit-card .form-control:focus,
    .affiliate-edit-card .form-select:focus {
        border-color: #047857 !important;
        background: #ffffff !important;
        box-shadow:
            0 0 0 0.22rem rgba(4, 120, 87, 0.12),
            0 12px 24px rgba(15, 23, 42, 0.055);
        transform: translateY(-1px);
    }

    .affiliate-form-panel input::placeholder,
    .affiliate-form-panel textarea::placeholder {
        color: #94a3b8;
        font-weight: 400;
    }

    .affiliate-form-panel textarea,
    .affiliate-edit-card textarea.form-control {
        min-height: 130px;
        resize: vertical;
    }

    /* Select field premium arrow feel */
    .affiliate-form-panel select,
    .affiliate-edit-card .form-select {
        cursor: pointer;
        background-color: #ffffff !important;
    }

    /* Date, number, email, tel consistency */
    .affiliate-form-panel input[type="date"],
    .affiliate-form-panel input[type="number"],
    .affiliate-form-panel input[type="email"],
    .affiliate-form-panel input[type="tel"],
    .affiliate-form-panel input[type="url"],
    .affiliate-form-panel input[type="text"],
    .affiliate-form-panel input[type="password"] {
        letter-spacing: 0;
    }

    /* Checkbox and radio styling */
    .affiliate-form-panel input[type="checkbox"],
    .affiliate-form-panel input[type="radio"] {
        width: 1.12rem;
        height: 1.12rem;
        margin-right: 0.45rem;
        accent-color: #047857;
        cursor: pointer;
    }

    .affiliate-edit-card .form-check {
        background: linear-gradient(180deg, #ffffff 0%, #fbfdfc 100%);
        border: 1px solid #dbe7e1;
        border-radius: 16px;
        padding: 0.9rem 1rem 0.9rem 2.7rem;
        box-shadow: 0 8px 18px rgba(15, 23, 42, 0.025);
    }

    .affiliate-edit-card .form-check-label {
        font-weight: 600;
        color: #334155;
    }

    .affiliate-edit-card .form-check-input {
        margin-top: 0.28rem;
        border-radius: 6px;
        border-color: #b7d8c8;
    }

    .affiliate-edit-card .form-check-input:checked {
        background-color: #047857;
        border-color: #047857;
    }

    .affiliate-edit-card .form-check-input:focus {
        box-shadow: 0 0 0 0.22rem rgba(4, 120, 87, 0.12);
        border-color: #047857;
    }

    /* File input */
    .affiliate-form-panel input[type="file"],
    .affiliate-edit-card input[type="file"] {
        width: 100%;
        padding: 0.62rem;
        border-radius: 16px !important;
        background: linear-gradient(180deg, #ffffff 0%, #fbfdfc 100%) !important;
        border: 1px solid #dbe7e1 !important;
        color: #475569;
        box-shadow: 0 8px 18px rgba(15, 23, 42, 0.025);
    }

    .affiliate-form-panel input[type="file"]::file-selector-button,
    .affiliate-edit-card input[type="file"]::file-selector-button {
        border: none;
        background: linear-gradient(135deg, #047857, #10b981);
        color: #ffffff;
        padding: 10px 16px;
        margin-right: 12px;
        border-radius: 12px;
        font-weight: 800;
        cursor: pointer;
        transition: all 0.2s ease;
    }

    .affiliate-form-panel input[type="file"]::file-selector-button:hover,
    .affiliate-edit-card input[type="file"]::file-selector-button:hover {
        background: linear-gradient(135deg, #065f46, #059669);
    }

    /* Disabled / readonly */
    .affiliate-form-panel input:disabled,
    .affiliate-form-panel select:disabled,
    .affiliate-form-panel textarea:disabled,
    .affiliate-form-panel input[readonly],
    .affiliate-form-panel textarea[readonly] {
        background: #f1f5f9 !important;
        color: #64748b;
        cursor: not-allowed;
    }

    /* Help text */
    .affiliate-edit-card small.text-muted {
        font-size: 0.82rem;
        color: #64748b !important;
    }

    /* Errors */
    .text-danger.small {
        font-weight: 700;
    }

    .affiliate-field-wrap:has(.text-danger) input,
    .affiliate-field-wrap:has(.text-danger) select,
    .affiliate-field-wrap:has(.text-danger) textarea {
        border-color: #ef4444 !important;
        box-shadow: 0 0 0 0.16rem rgba(239, 68, 68, 0.08);
    }

    /* Button/action area */
    .affiliate-edit-actions {
        background: #f8fafc;
        border: 1px solid #e7f2ec;
        border-radius: 22px;
        padding: 1rem;
        margin-top: 1.6rem;
    }

    .affiliate-edit-actions .btn {
        border-radius: 50rem;
        padding: 0.72rem 1.45rem;
        font-weight: 800;
    }

    .affiliate-edit-actions .btn-primary {
        background: linear-gradient(135deg, #047857, #059669);
        border: none;
        box-shadow: 0 10px 22px rgba(4, 120, 87, 0.20);
    }

    .affiliate-edit-actions .btn-primary:hover {
        background: linear-gradient(135deg, #065f46, #047857);
        transform: translateY(-1px);
    }

    .affiliate-edit-actions .btn-outline-secondary {
        border-color: #cbd5e1;
        color: #475569;
        background: #ffffff;
    }

    .affiliate-edit-actions .btn-outline-secondary:hover {
        background: #0f172a;
        color: #ffffff;
        border-color: #0f172a;
    }

    .affiliate-edit-note {
        background: linear-gradient(135deg, #f8fafc 0%, #f0fdf4 100%);
        border: 1px solid #dbe7e1;
        border-radius: 22px;
        padding: 1.25rem;
    }

    .affiliate-edit-note h5 {
        color: #064e3b;
        font-weight: 800;
    }

    .affiliate-edit-note-icon {
        width: 42px;
        height: 42px;
        border-radius: 15px;
        background: #dcfce7;
        color: #047857;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
        font-size: 1.2rem;
    }

    @media (max-width: 991.98px) {
        .affiliate-edit-body {
            padding: 1.6rem;
        }

        .affiliate-edit-header {
            padding: 1.8rem;
        }
    }

    @media (max-width: 767.98px) {
        .affiliate-edit-section {
            padding-top: 2rem !important;
            padding-bottom: 2rem !important;
        }

        .affiliate-edit-card {
            border-radius: 22px;
        }

        .affiliate-edit-header {
            padding: 1.5rem;
        }

        .affiliate-edit-header h2 {
            font-size: 1.55rem;
        }

        .affiliate-edit-header p {
            font-size: 0.95rem;
        }

        .affiliate-edit-body {
            padding: 1.1rem;
        }

        .affiliate-form-panel {
            padding: 1rem;
            border-radius: 18px;
        }

        .affiliate-form-panel input:not([type="checkbox"]):not([type="radio"]),
        .affiliate-form-panel select,
        .affiliate-form-panel textarea,
        .affiliate-edit-card .form-control,
        .affiliate-edit-card .form-select {
            min-height: 48px;
            border-radius: 14px !important;
            padding: 0.72rem 0.85rem;
            font-size: 0.95rem;
        }

        .affiliate-edit-actions {
            padding: 0.85rem;
            border-radius: 18px;
        }

        .affiliate-edit-actions .btn {
            width: 100%;
            padding: 0.78rem 1rem;
        }

        .affiliate-edit-actions > div:last-child {
            width: 100%;
        }

        .affiliate-edit-note {
            padding: 1rem;
            border-radius: 18px;
        }
    }

    @media (max-width: 575.98px) {
        .container {
            padding-left: 14px;
            padding-right: 14px;
        }

        .affiliate-edit-header {
            padding: 1.25rem;
        }

        .affiliate-edit-body {
            padding: 0.95rem;
        }

        .affiliate-form-panel-title {
            align-items: flex-start;
        }

        .affiliate-edit-kicker {
            font-size: 0.75rem;
        }

        .affiliate-form-panel textarea,
        .affiliate-edit-card textarea.form-control {
            min-height: 115px;
        }

        .affiliate-edit-note .d-flex {
            flex-direction: column;
        }
    }
</style>

<section class="affiliate-edit-section py-5">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-12 col-lg-10">

                <div class="card border-0 affiliate-edit-card">

                    <div class="affiliate-edit-header">
                        <div class="affiliate-edit-header-content">
                            <span class="affiliate-edit-kicker">
                                <i class="bi bi-person-gear"></i>
                                Affiliate Partner Account
                            </span>

                            <h2>Edit Affiliate Profile</h2>

                            <p>
                                Update your personal details, contact information, and payout details used for affiliate communication and commission processing.
                            </p>
                        </div>
                    </div>

                    <div class="affiliate-edit-body">

                        <form method="post">
                            {% csrf_token %}

                            <div class="affiliate-form-panel">
                                <h5 class="affiliate-form-panel-title">
                                    <i class="bi bi-pencil-square"></i>
                                    Profile Information
                                </h5>

                                <div class="row g-3 g-md-4">
                                    {% for field in form %}
                                        <div class="{% if field.name == 'payout_notes' or field.name == 'about_me' or field.name == 'description' or field.name == 'notes' %}col-12{% else %}col-12 col-md-6{% endif %}">
                                            <div class="affiliate-field-wrap">
                                                <label class="form-label" for="{{ field.id_for_label }}">
                                                    {{ field.label }}
                                                </label>

                                                {{ field }}

                                                {% if field.help_text %}
                                                    <small class="text-muted d-block mt-1">
                                                        {{ field.help_text }}
                                                    </small>
                                                {% endif %}

                                                {% for error in field.errors %}
                                                    <div class="text-danger small mt-1">
                                                        {{ error }}
                                                    </div>
                                                {% endfor %}
                                            </div>
                                        </div>
                                    {% endfor %}
                                </div>
                            </div>

                            <div class="affiliate-edit-actions d-flex gap-2 flex-wrap justify-content-between align-items-center">
                                <div class="text-muted small">
                                    <i class="bi bi-shield-check me-1"></i>
                                    Please check payout details carefully before saving.
                                </div>

                                <div class="d-flex gap-2 flex-wrap">
                                    <button type="submit" class="btn btn-primary">
                                        <i class="bi bi-check-circle me-1"></i>
                                        Save Changes
                                    </button>

                                    <a href="{% url 'affiliates_app:affiliate_profile' %}" class="btn btn-outline-secondary">
                                        <i class="bi bi-x-circle me-1"></i>
                                        Cancel
                                    </a>
                                </div>
                            </div>

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

                        <hr class="my-5">

                        <div class="affiliate-edit-note">
                            <div class="d-flex gap-3">
                                <div class="affiliate-edit-note-icon">
                                    <i class="bi bi-info-circle-fill"></i>
                                </div>

                                <div>
                                    <h5 class="mb-2">Profile Update Notes</h5>

                                    <p class="mb-2">
                                        <strong>Important:</strong>
                                        Please keep your payout details accurate so affiliate commissions can be processed correctly.
                                    </p>

                                    <p class="text-muted mb-0">
                                        If you update your bank account, WhatsApp number, or country, make sure the details match your official payment information.
                                    </p>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>

            </div>
        </div>
    </div>
</section>
{% endblock %}