{% extends "base.html" %}

{% block content %}
<section class="panel">
    <div class="title-row">
        <div>
            <h1>{{ lead.name }}</h1>
            <p class="muted">Lead #{{ lead.id }} del job #{{ lead.job_id }}</p>
        </div>
        <a class="button-link" href="/jobs/{{ lead.job_id }}">Volver al job</a>
    </div>
    <div class="stats">
        <div class="stat"><strong>{{ lead.score }}</strong><span>Score</span></div>
        <div class="stat"><strong>{{ lead.review_status }}</strong><span>Revision</span></div>
        <div class="stat"><strong>{{ lead.category_group or "-" }}</strong><span>Grupo</span></div>
        <div class="stat"><strong>{{ lead.review_count or 0 }}</strong><span>Resenas</span></div>
    </div>
</section>

<section class="grid">
    <article class="panel">
        <h2>Datos</h2>
        <dl class="details">
            <dt>Keyword origen</dt><dd>{{ lead.source_keyword }}</dd>
            <dt>Keywords detectadas</dt><dd>{{ lead.matched_keywords_json | join(", ") }}</dd>
            <dt>Telefono</dt><dd>{{ lead.phone or "-" }}</dd>
            <dt>Web</dt><dd>{% if lead.website %}<a href="{{ lead.website }}" target="_blank">{{ lead.website }}</a>{% else %}-{% endif %}</dd>
            <dt>Email</dt><dd>{{ lead.email or "-" }}</dd>
            <dt>Facebook</dt><dd>{% if lead.facebook_url %}<a href="{{ lead.facebook_url }}" target="_blank">Abrir</a>{% else %}-{% endif %}</dd>
            <dt>Instagram</dt><dd>{% if lead.instagram_url %}<a href="{{ lead.instagram_url }}" target="_blank">Abrir</a>{% else %}-{% endif %}</dd>
            <dt>LinkedIn</dt><dd>{% if lead.linkedin_url %}<a href="{{ lead.linkedin_url }}" target="_blank">Abrir</a>{% else %}-{% endif %}</dd>
            <dt>Direccion</dt><dd>{{ lead.address or "-" }}</dd>
            <dt>Ciudad</dt><dd>{{ lead.city or "-" }}</dd>
            <dt>Provincia</dt><dd>{{ lead.province or "-" }}</dd>
            <dt>Pais</dt><dd>{{ lead.country or "-" }}</dd>
            <dt>Maps</dt><dd>{% if lead.maps_url %}<a href="{{ lead.maps_url }}" target="_blank">Abrir en Maps</a>{% else %}-{% endif %}</dd>
            <dt>Clave banco</dt><dd>{{ lead.duplicate_group_key or "-" }}</dd>
            <dt>Origen banco</dt><dd>{{ lead.duplicate_scope or "-" }}</dd>
            <dt>Motivo coincidencia</dt><dd>{{ lead.duplicate_reason or "-" }}</dd>
            <dt>Confianza</dt><dd>{{ lead.duplicate_confidence or "-" }}</dd>
            <dt>Fusionado en</dt><dd>{% if lead.merged_into_lead_id %}<a href="/leads/{{ lead.merged_into_lead_id }}">Lead #{{ lead.merged_into_lead_id }}</a>{% else %}-{% endif %}</dd>
        </dl>

        {% if reference_lead %}
            <div class="alert">
                Coincidencia historica con
                <a href="/leads/{{ reference_lead.id }}">Lead #{{ reference_lead.id }}</a>:
                {{ reference_lead.name }}
            </div>
        {% endif %}
    </article>

    <article class="panel">
        <h2>Revision manual</h2>
        <form action="/leads/{{ lead.id }}/review" method="post" class="form">
            <label>Estado
                <select name="review_status">
                    <option value="pending" {% if lead.review_status == "pending" %}selected{% endif %}>pending</option>
                    <option value="approved" {% if lead.review_status == "approved" %}selected{% endif %}>approved</option>
                    <option value="discarded" {% if lead.review_status == "discarded" %}selected{% endif %}>discarded</option>
                    <option value="merged" {% if lead.review_status == "merged" %}selected{% endif %}>merged</option>
                </select>
            </label>
            <label>Notas
                <textarea name="notes" rows="8">{{ lead.notes or "" }}</textarea>
            </label>
            <button type="submit">Guardar revision</button>
        </form>

        {% if lead.duplicate_group_key %}
            <form action="/groups/{{ lead.id }}/merge" method="post" class="form">
                <input type="hidden" name="next_url" value="/leads/{{ lead.id }}">
                <button type="submit">Fusionar este grupo usando este lead como principal</button>
            </form>
        {% endif %}

        <h2>Coincidencias del grupo</h2>
        {% if peers %}
            <ul class="list">
                {% for peer in peers %}
                    <li>
                        <a href="/leads/{{ peer.id }}">Lead #{{ peer.id }}</a> - {{ peer.name }}
                        {% if peer.id == lead.id %}<strong>(actual)</strong>{% endif %}
                        {% if peer.merged_into_lead_id %}<span class="muted"> fusionado en #{{ peer.merged_into_lead_id }}</span>{% endif %}
                    </li>
                {% endfor %}
            </ul>
        {% else %}
            <p class="muted">Sin coincidencias agrupadas todavia.</p>
        {% endif %}

        {% if merged_children %}
            <h2>Secundarios fusionados</h2>
            <ul class="list">
                {% for peer in merged_children %}
                    <li><a href="/leads/{{ peer.id }}">Lead #{{ peer.id }}</a> - {{ peer.name }}</li>
                {% endfor %}
            </ul>
        {% endif %}
    </article>
</section>
{% endblock %}
