{% extends "base.html" %}

{% block title %}{{ classified.title }} - Norda Biznes Hub{% endblock %}

{% block extra_css %}
<style>
    .classified-container {
        max-width: 800px;
        margin: 0 auto;
    }

    .classified-card {
        background: var(--surface);
        border-radius: var(--radius-lg);
        padding: var(--spacing-xl);
        box-shadow: var(--shadow);
        margin-bottom: var(--spacing-xl);
    }

    .classified-card.szukam {
        border-top: 4px solid var(--warning);
    }

    .classified-card.oferuje {
        border-top: 4px solid var(--success);
    }

    .classified-header {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        margin-bottom: var(--spacing-lg);
    }

    .classified-type {
        display: inline-block;
        padding: 4px 12px;
        border-radius: var(--radius-sm);
        font-size: var(--font-size-sm);
        font-weight: 600;
        text-transform: uppercase;
    }

    .classified-type.szukam {
        background: #fef3c7;
        color: #92400e;
    }

    .classified-type.oferuje {
        background: #dcfce7;
        color: #166534;
    }

    .classified-category {
        font-size: var(--font-size-sm);
        color: var(--text-secondary);
        text-transform: uppercase;
        margin-left: var(--spacing-sm);
    }

    .classified-title {
        font-size: var(--font-size-2xl);
        font-weight: 600;
        color: var(--text-primary);
        margin-bottom: var(--spacing-lg);
    }

    .classified-description {
        line-height: 1.8;
        color: var(--text-primary);
        margin-bottom: var(--spacing-xl);
        white-space: pre-wrap;
    }

    .classified-details {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: var(--spacing-lg);
        padding: var(--spacing-lg);
        background: var(--background);
        border-radius: var(--radius);
        margin-bottom: var(--spacing-xl);
    }

    .detail-item {
        display: flex;
        align-items: flex-start;
        gap: var(--spacing-sm);
    }

    .detail-item svg {
        width: 20px;
        height: 20px;
        color: var(--primary);
        flex-shrink: 0;
        margin-top: 2px;
    }

    .detail-label {
        font-size: var(--font-size-sm);
        color: var(--text-secondary);
    }

    .detail-value {
        font-weight: 500;
        color: var(--text-primary);
    }

    .author-card {
        display: flex;
        align-items: center;
        gap: var(--spacing-lg);
        padding: var(--spacing-lg);
        background: var(--background);
        border-radius: var(--radius);
    }

    .author-avatar {
        width: 56px;
        height: 56px;
        border-radius: 50%;
        background: var(--primary);
        color: white;
        display: flex;
        align-items: center;
        justify-content: center;
        font-weight: 600;
        font-size: var(--font-size-lg);
    }

    .author-info {
        flex: 1;
    }

    .author-name {
        font-weight: 600;
        color: var(--text-primary);
        margin-bottom: var(--spacing-xs);
    }

    .author-company {
        font-size: var(--font-size-sm);
        color: var(--text-secondary);
    }

    .back-link {
        display: inline-flex;
        align-items: center;
        gap: var(--spacing-xs);
        color: var(--text-secondary);
        text-decoration: none;
        margin-bottom: var(--spacing-lg);
    }

    .back-link:hover {
        color: var(--primary);
    }

    .stats-bar {
        display: flex;
        justify-content: space-between;
        font-size: var(--font-size-sm);
        color: var(--text-secondary);
        padding-top: var(--spacing-lg);
        border-top: 1px solid var(--border);
        margin-top: var(--spacing-lg);
    }

    .close-btn {
        margin-left: auto;
    }
</style>
{% endblock %}

{% block content %}
<div class="classified-container">
    <a href="{{ url_for('classifieds_index') }}" class="back-link">
        <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
            <path d="M19 12H5M12 19l-7-7 7-7"/>
        </svg>
        Powrot do tablicy
    </a>

    <div class="classified-card {{ classified.listing_type }}">
        <div class="classified-header">
            <div>
                <span class="classified-type {{ classified.listing_type }}">{{ 'Szukam' if classified.listing_type == 'szukam' else 'Oferuje' }}</span>
                <span class="classified-category">{{ classified.category }}</span>
            </div>
            {% if classified.author_id == current_user.id %}
            <button class="btn btn-secondary btn-sm close-btn" onclick="closeClassified()">Zamknij ogloszenie</button>
            {% endif %}
        </div>

        <h1 class="classified-title">{{ classified.title }}</h1>

        <div class="classified-description">{{ classified.description }}</div>

        {% if classified.budget_info or classified.location_info %}
        <div class="classified-details">
            {% if classified.budget_info %}
            <div class="detail-item">
                <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                    <path d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
                </svg>
                <div>
                    <div class="detail-label">Budzet / Cena</div>
                    <div class="detail-value">{{ classified.budget_info }}</div>
                </div>
            </div>
            {% endif %}

            {% if classified.location_info %}
            <div class="detail-item">
                <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                    <path d="M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z"></path>
                    <circle cx="12" cy="10" r="3"></circle>
                </svg>
                <div>
                    <div class="detail-label">Lokalizacja</div>
                    <div class="detail-value">{{ classified.location_info }}</div>
                </div>
            </div>
            {% endif %}
        </div>
        {% endif %}

        <div class="author-card">
            <div class="author-avatar">
                {{ (classified.author.name or classified.author.email)[0].upper() }}
            </div>
            <div class="author-info">
                <div class="author-name">{{ classified.author.name or classified.author.email.split('@')[0] }}</div>
                {% if classified.company %}
                <div class="author-company">{{ classified.company.name }}</div>
                {% endif %}
            </div>
            {% if classified.author_id != current_user.id %}
            <a href="{{ url_for('messages_new', to=classified.author_id) }}" class="btn btn-primary">Skontaktuj sie</a>
            {% endif %}
        </div>

        <div class="stats-bar">
            <span>{{ classified.views_count }} wyswietlen</span>
            <span>Dodano: {{ classified.created_at.strftime('%d.%m.%Y %H:%M') }}</span>
            {% if classified.expires_at %}
            <span>Wygasa: {{ classified.expires_at.strftime('%d.%m.%Y') }}</span>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
const csrfToken = '{{ csrf_token() }}';

async function closeClassified() {
    if (!confirm('Czy na pewno chcesz zamknac to ogloszenie?')) {
        return;
    }

    try {
        const response = await fetch('{{ url_for("classifieds_close", classified_id=classified.id) }}', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': csrfToken
            }
        });

        const data = await response.json();
        if (data.success) {
            window.location.href = '{{ url_for("classifieds_index") }}';
        } else {
            alert(data.error || 'Wystapil blad');
        }
    } catch (error) {
        alert('Blad polaczenia');
    }
}
{% endblock %}
