{% extends "base.html" %}

{% block styles %}
{{ super() }}
<style>
    .proposal-card {
        border: 2px solid #e2e8f0 !important; /* como en PDF */
        border-left: 6px solid #0b99fd !important; /* acento lateral tipo PDF */
        border-radius: 12px !important;
        transition: all 0.25s ease;
        background-color: #f8fafc !important; /* card gris clara como Datos del Cliente */
        overflow: hidden;
        box-shadow: 0 2px 6px rgba(0,0,0,0.06);
    }
    .proposal-card:hover {
        transform: translateY(-3px);
        box-shadow: 0 12px 24px rgba(11,153,253,0.25);
    }
    .proposal-card .card-header {
        background-color: #0b99fd !important; /* header azul corporativo como en PDF */
        color: #ffffff !important;
        padding: 10px 15px;
        border-bottom: none !important;
    }
    .proposal-card .card-body {
        background-color: #f8fafc !important;
    }
    .proposal-card .supplier-logo {
        max-height: 40px;
        max-width: 120px;
        object-fit: contain;
    }
    .proposal-card .savings-badge {
        font-size: 0.9rem;
        padding: 0.5rem 1rem;
        border-radius: 9999px;
    }
    .proposal-card .main-figures {
        padding: 1.5rem;
        background: linear-gradient(135deg, rgba(11,153,253,0.10) 0%, rgba(11,153,253,0.06) 100%);
    }
    .proposal-card .main-figures .figure-value {
        font-size: 2.9rem;
        font-weight: 800;
        color: #0b99fd;
    }
    .proposal-card .main-figures .figure-label {
        font-size: 0.9rem;
        color: #64748b;
    }
    .proposal-card .price-details table {
        font-size: 0.9rem;
    }
    .proposal-card .price-details th {
        font-weight: 500;
        color: #475569;
    }
    .proposal-card .price-details td {
        color: #1e293b;
        font-weight: 500;
    }
    /* Card del asesor (preview) */
    .advisor-card {
        background-color: #f8fafc;
        border: 1px solid #e2e8f0;
        border-radius: 12px;
        padding: 1rem 1.25rem;
    }
    .advisor-photo {
        width: 96px !important;
        height: 96px !important;
        max-width: 96px !important;
        max-height: 96px !important;
        object-fit: cover;
        border-radius: 8px;
        border: 3px solid #fff;
        box-shadow: 0 4px 8px rgba(0,0,0,0.08);
        display: block;
    }
    /* Selector más específico por si hay reglas globales que afectan a img */
    .advisor-card img.advisor-photo {
        width: 96px !important;
        height: 96px !important;
        max-width: 96px !important;
        max-height: 96px !important;
    }
    .commercial-info-box {
        background-color: #f1f5f9;
        border-radius: 12px;
        padding: 1.5rem;
        display: flex;
        align-items: center;
        gap: 1.5rem;
    }
    .commercial-info-box .avatar {
        width: 80px;
        height: 80px;
        border-radius: 50%;
        object-fit: cover;
        border: 3px solid #fff;
        box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    }
    /* ===== Estilo tipo PDF para Planes Recomendados ===== */
    .proposal-card .card-header h5 { color: #ffffff !important; letter-spacing: 0.3px; }
    .rp-badge { background: rgba(255,255,255,0.18); color: #ffffff; border: 1px solid rgba(255,255,255,0.35); padding: 0.35rem 0.8rem; border-radius: 9999px; font-weight: 600; }
    .rp-badge.alt { background: rgba(255,255,255,0.12); }

    .rp-slab { margin: 10px 0; text-align: center; padding: 10px; border-radius: 5px; font-weight: bold; font-size: 18px; transition: transform .2s ease, box-shadow .2s ease; }
    .rp-slab:hover { transform: translateY(-1px); box-shadow: 0 8px 18px rgba(11,153,253,0.18); }
    .rp-savings { background: #bd05dc; color: #ffffff; }
    .rp-savings.alt { background: #bd05dc; }
    .rp-cost { background: #0b99fd; color: #ffffff; }
    .rp-percent { text-align: center; color: #0b99fd; font-weight: 600; margin-top: 6px; }
    .rp-percent.alt { color: #bd05dc; }

    .rp-price-table { width: 100%; border-collapse: collapse; margin: 10px 0; }
    .rp-price-table th { background: #0b99fd; color: #ffffff; padding: 8px; text-align: left; }
    .rp-price-table td { padding: 8px; border-bottom: 1px solid #dee2e6; }

    /* Variantes y bloques extra */
    .rp-slab .amount-lg { font-size: 26px; font-weight: 800; letter-spacing: .3px; }
    .rp-overcost { color: #ffffff; background: #bd05dc; background-image: repeating-linear-gradient(45deg, rgba(255,255,255,0.16) 0, rgba(255,255,255,0.16) 10px, rgba(189,5,220,0.35) 10px, rgba(189,5,220,0.35) 20px); }
    .rp-extra-info { text-align: center; color: #0b99fd; font-weight: 600; margin-top: 6px; }

    /* Secciones internas como cards */
    .rp-section { border: 1px solid #e2e8f0; border-radius: 8px; background: linear-gradient(180deg, rgba(11,153,253,0.04), rgba(11,153,253,0.02)); margin-bottom: 12px; transition: transform .2s ease, box-shadow .2s ease; }
    .rp-section:hover { transform: translateY(-1px); box-shadow: 0 6px 14px rgba(11,153,253,0.15); }
    .rp-section .rp-section-header { background: #0b99fd; color: #ffffff; padding: 8px 12px; border-top-left-radius: 8px; border-top-right-radius: 8px; font-weight: 700; }
    .rp-section .rp-section-body { padding: 10px 12px; }
</style>
{% endblock %}

{% block title %}{{ title }}{% endblock %}

{% block content %}
<div class="container-fluid" style="background: linear-gradient(135deg, #e0f2fe 0%, #dbeafe 100%); min-height: 100vh; padding: 120px 0 40px 0;">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-lg-10">
                <div class="card shadow-lg border-0" style="border-radius: 20px; overflow: hidden;">
                    <div class="card-body p-4 p-md-5">
                        <!-- Header -->
                        <div class="text-center mb-4">
                            <h2 class="fw-bold mb-2" style="color: #1e3a8a;">Estudio de ahorro Energético personalizado</h2>
                            <p class="text-muted">Preparada especialmente para ti.</p>
                        </div>

                        <!-- Información del Comercial (en card con foto a la derecha) -->
                        <div class="advisor-card mb-4">
                            <div class="d-flex align-items-center" style="gap: 12px;">
                                <img src="{{ commercial.profile_image_url or url_for('static', filename='assets/img/avatars/default-avatar.png') }}" alt="Foto de perfil de {{ commercial.first_name }}" class="advisor-photo" width="96" height="96" style="width:96px !important; height:96px !important; max-width:96px !important; max-height:96px !important; object-fit:cover;">
                                <div class="flex-grow-1">
                                    <h5 class="fw-bold mb-1">{{ commercial.first_name }} {{ commercial.last_name }}</h5>
                                    <p class="text-muted mb-2">Tu asesor energético</p>
                                    <a href="mailto:{{ commercial.email }}" class="text-primary text-decoration-none small">
                                        <i class="fas fa-envelope me-1"></i> {{ commercial.email }}
                                    </a>
                                </div>
                            </div>
                        </div>

                        <!-- Información del cliente -->
                        <div class="client-info mb-4">
                            <h5 class="fw-bold mb-3 text-secondary"><i class="fas fa-user-circle me-2"></i>Datos del Cliente</h5>
                            <div class="row bg-light p-3" style="border-radius: 12px;">
                                <div class="col-md-6 mb-2 mb-md-0">
                                    <p class="mb-1"><strong class="text-muted">Cliente:</strong> {{ invoice_data.get('client_name', 'N/A') }}</p>
                                    <p class="mb-1"><strong class="text-muted">Dirección:</strong> {{ invoice_data.get('supply_address', 'N/A') }}</p>
                                    <p class="mb-1"><strong class="text-muted">Email:</strong> {{ client_email or 'N/A' }}</p>
                                </div>
                                <div class="col-md-6">
                                    <p class="mb-0"><strong class="text-muted">Comercializadora:</strong> {{ invoice_data.get('provider_name', 'N/A') }}</p>
                                    <p class="mb-0"><strong class="text-muted">CUPS:</strong> {{ invoice_data.get('cups', 'N/A') }}</p>
                                    <p class="mb-0"><strong class="text-muted">Tarifa:</strong> {{ invoice_data.get('tariff_type', 'N/A') }}</p>
                                </div>
                            </div>
                        </div>

                        <!-- Comentario personalizado -->
                        {% if proposal_data.custom_comment %}
                        <div class="mb-4">
                            <div class="alert alert-info" style="border-radius: 12px;">
                                <h6 class="fw-bold mb-2"><i class="fas fa-comment-dots me-2"></i>Informacion relevante</h6>
                                <p class="mb-0 fst-italic">"{{ proposal_data.custom_comment }}"</p>
                            </div>
                        </div>
                        {% endif %}

                        <!-- Planes seleccionados -->
                        <div class="mb-4">
                            <h5 class="fw-bold mb-3 text-secondary"><i class="fas fa-star me-2"></i>Planes Recomendados</h5>
                            {% for plan in proposal_data.selected_plans %}
                            <div class="proposal-card mb-4">
                                <div class="card-header d-flex justify-content-between align-items-center">
                                    <h5 class="fw-bold mb-0">{{ plan.supplier }}</h5>
                                    {% if plan.is_savings %}
                                        <span class="rp-badge">Opción Ahorro</span>
                                    {% else %}
                                        <span class="rp-badge alt">Opción Alternativa</span>
                                    {% endif %}
                                </div>
                                <div class="card-body p-0">
                                    <!-- Resumen de costes en card interna -->
                                    <div class="rp-section">
                                        <div class="rp-section-header">Resumen de costes</div>
                                        <div class="rp-section-body">
                                            <div class="row text-center">
                                                <div class="col-6 border-end">
                                                    <div class="figure-label">COSTE MENSUAL ACTUAL</div>
                                                    {% set current_monthly = (invoice_data.get('current_annual_cost', 0) / 12) if invoice_data.get('current_annual_cost') else 0 %}
                                                    <div class="figure-value">{{ "%.2f"|format(current_monthly) }}€</div>
                                                    <small class="text-muted">IVA no incluido</small>
                                                </div>
                                                <div class="col-6">
                                                    <div class="figure-label">PROPUESTA COSTE MENSUAL</div>
                                                    <div class="figure-value">{{ "%.2f"|format(plan.ole_cost) }}€</div>
                                                    <small class="text-muted">IVA no incluido</small>
                                                </div>
                                            </div>
                                            <div class="row mt-2">
                                                <div class="col-12">
                                                    {% if plan.is_savings %}
                                                        <div class="rp-slab rp-savings">AHORRO ANUAL: <span class="amount-lg">{{ plan.yearly_savings|round|int }}€</span></div>
                                                        <div class="rp-percent">({{ "%.1f"|format(plan.savings_percentage) }}% de ahorro)</div>
                                                    {% else %}
                                                        <div class="rp-slab rp-overcost">SOBRECOSTE ANUAL: <span class="amount-lg">+{{ plan.yearly_loss|round|int }}€</span></div>
                                                        <div class="rp-percent alt">(+{{ "%.1f"|format(plan.savings_percentage) }}% de sobrecoste)</div>
                                                    {% endif %}
                                                    <div class="rp-slab rp-cost">NUEVO COSTE MENSUAL: <span class="amount-lg">{{ "%.2f"|format(plan.ole_cost) }}€</span></div>
                                                    <div class="rp-extra-info">Permanencia: <strong>{{ plan.contract_duration }} meses</strong> <span class="mx-1">|</span> Plan: <strong>{{ plan.name }}</strong></div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="price-details p-4">
                                        <div class="row">
                                            <div class="col-lg-6 mb-3 mb-lg-0">
                                                <h6 class="fw-bold small text-uppercase">Precios Energía (€/kWh)</h6>
                                                <table class="rp-price-table">
                                                    <thead>
                                                        <tr><th>Período</th><th>Precio</th></tr>
                                                    </thead>
                                                    <tbody>
                                                    {% for period in ['P1', 'P2', 'P3', 'P4', 'P5', 'P6'] %}
                                                        {% if plan.energy_prices[period] is defined and plan.energy_prices[period] and plan.energy_prices[period] > 0 %}
                                                        <tr><td>{{ period }}</td><td>{{ "%.5f"|format(plan.energy_prices[period]) }}</td></tr>
                                                        {% endif %}
                                                    {% endfor %}
                                                    </tbody>
                                                </table>
                                            </div>
                                            <div class="col-lg-6">
                                                <h6 class="fw-bold small text-uppercase">Precios Potencia (€/kW/día)</h6>
                                                <table class="rp-price-table">
                                                    <thead>
                                                        <tr><th>Período</th><th>Precio</th></tr>
                                                    </thead>
                                                    <tbody>
                                                    {% for period in ['P1', 'P2', 'P3', 'P4', 'P5', 'P6'] %}
                                                        {% if plan.power_prices[period] is defined and plan.power_prices[period] and plan.power_prices[period] > 0 %}
                                                        <tr><td>{{ period }}</td><td>{{ "%.5f"|format(plan.power_prices[period]) }}</td></tr>
                                                        {% endif %}
                                                    {% endfor %}
                                                    </tbody>
                                                </table>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            {% endfor %}
                        </div>

                        <!-- Acciones -->
                        <div class="text-center mt-5">
                            <div class="row">
                                <div class="col-md-4 mb-2">
                                    <a href="{{ url_for('commercial_invoice.generate_proposal') }}" class="btn btn-outline-secondary btn-lg w-100">
                                        <i class="fas fa-edit me-2"></i>Editar
                                    </a>
                                </div>
                                <div class="col-md-4 mb-2">
                                    <button class="btn btn-info btn-lg w-100 text-white" onclick="window.print()">
                                        <i class="fas fa-print me-2"></i>Imprimir
                                    </button>
                                </div>
                                <div class="col-md-4 mb-2">
                                    <button class="btn btn-primary btn-lg w-100" onclick="downloadPDF()">
                                        <i class="fas fa-download me-2"></i>Descargar PDF
                                    </button>
                                </div>
                            </div>
                        </div>

                        <!-- Nota legal -->
                        <div class="text-center mt-4">
                            <p class="text-muted small">
                                <i class="fas fa-info-circle me-1"></i>
                                Esta es una simulación basada en su consumo y precios actuales. Los precios y condiciones están sujetos a cambios según las condiciones del mercado.
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
function downloadPDF() {
    window.location.href = "{{ url_for('commercial_invoice.download_proposal') }}";
}
</script>

{% endblock %}
