{% extends "base.html" %}

{% block title %}Katalog firm - Norda Biznes Hub{% endblock %}

{% block extra_css %}
<style>
    /* Search Bar */
    .search-section {
        margin-bottom: var(--spacing-2xl);
    }

    .search-bar {
        display: flex;
        gap: var(--spacing-md);
        max-width: 800px;
        margin: 0 auto;
    }

    .search-input {
        flex: 1;
        padding: var(--spacing-md);
        border: 1px solid var(--border);
        border-radius: var(--radius);
        font-size: var(--font-size-base);
    }

    .search-input:focus {
        outline: none;
        border-color: var(--primary);
        box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
    }

    /* Category Filter */
    .category-filter {
        display: flex;
        gap: var(--spacing-sm);
        flex-wrap: wrap;
        justify-content: center;
        margin-bottom: var(--spacing-xl);
    }

    .category-badge {
        padding: var(--spacing-sm) var(--spacing-md);
        background-color: var(--background);
        border: 1px solid var(--border);
        border-radius: var(--radius-lg);
        color: var(--text-secondary);
        text-decoration: none;
        font-size: var(--font-size-sm);
        font-weight: 500;
        transition: var(--transition);
        cursor: pointer;
    }

    .category-badge:hover,
    .category-badge.active {
        background-color: var(--primary);
        color: white;
        border-color: var(--primary);
    }

    /* Company Grid */
    .companies-grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
        gap: var(--spacing-lg);
        margin-bottom: var(--spacing-2xl);
    }

    .company-card {
        background-color: var(--surface);
        border-radius: var(--radius-lg);
        padding: var(--spacing-lg);
        box-shadow: var(--shadow);
        transition: var(--transition);
        display: flex;
        flex-direction: column;
        height: 100%;
    }

    .company-card:hover {
        box-shadow: var(--shadow-lg);
        transform: translateY(-2px);
    }

    .company-logo {
        width: 100%;
        height: 80px;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: var(--spacing-md);
        background: var(--background);
        border-radius: var(--radius-md);
        overflow: hidden;
    }

    .company-logo img {
        max-width: 100%;
        max-height: 100%;
        object-fit: contain;
    }

    .company-header {
        margin-bottom: var(--spacing-md);
    }

    .company-category {
        display: inline-block;
        padding: var(--spacing-xs) var(--spacing-sm);
        background-color: var(--background);
        color: var(--text-secondary);
        font-size: var(--font-size-sm);
        border-radius: var(--radius);
        margin-bottom: var(--spacing-sm);
    }

    .company-name {
        font-size: var(--font-size-xl);
        font-weight: 600;
        color: var(--text-primary);
        margin-bottom: var(--spacing-sm);
        text-decoration: none;
        display: block;
    }

    .company-name:hover {
        color: var(--primary);
    }

    .company-description {
        color: var(--text-secondary);
        font-size: var(--font-size-sm);
        line-height: 1.6;
        margin-bottom: var(--spacing-md);
        flex: 1;
    }

    .company-contact {
        display: flex;
        flex-direction: column;
        gap: var(--spacing-xs);
        font-size: var(--font-size-sm);
        color: var(--text-secondary);
        padding-top: var(--spacing-md);
        border-top: 1px solid var(--border);
    }

    .company-contact-item {
        display: flex;
        align-items: center;
        gap: var(--spacing-sm);
    }

    .company-contact a {
        color: var(--primary);
        text-decoration: none;
    }

    .company-contact a:hover {
        text-decoration: underline;
    }

    .empty-state {
        text-align: center;
        padding: var(--spacing-2xl);
        color: var(--text-secondary);
    }

    .empty-state svg {
        opacity: 0.3;
        margin-bottom: var(--spacing-md);
    }

    /* Loading State */
    .loading {
        text-align: center;
        padding: var(--spacing-2xl);
        color: var(--text-secondary);
    }

    @media (max-width: 768px) {
        .companies-grid {
            grid-template-columns: 1fr;
        }

        .search-bar {
            flex-direction: column;
        }
    }
</style>
{% endblock %}

{% block content %}
<!-- Header -->
<div style="background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%); color: white; padding: var(--spacing-xl); border-radius: var(--radius-lg); margin-bottom: var(--spacing-xl); text-align: center;">
    <h1 style="font-size: var(--font-size-3xl); margin-bottom: var(--spacing-sm); font-weight: 700;">
        Katalog firm Norda Biznes
    </h1>
    <p style="font-size: var(--font-size-lg); opacity: 0.9;">
        {{ total_companies }} firm członkowskich • {{ total_categories }} branż
    </p>
</div>

<!-- Search Section -->
<div class="search-section">
    <form action="{{ url_for('search') }}" method="GET" class="search-bar">
        <input
            type="search"
            name="q"
            class="search-input"
            placeholder="Szukaj firm po nazwie, usłudze lub słowie kluczowym..."
            aria-label="Search companies"
        >
        <button type="submit" class="btn btn-primary">
            <svg width="20" height="20" fill="none" stroke="currentColor" stroke-width="2">
                <circle cx="9" cy="9" r="7"/>
                <path d="M14 14l5 5"/>
            </svg>
            Szukaj
        </button>
    </form>
</div>

<!-- Category Filter -->
{% if categories %}
<div class="category-filter">
    <button class="category-badge active" onclick="filterCategory('all')">
        Wszystkie ({{ total_companies }})
    </button>
    {% for category in categories %}
    {% set count = companies|selectattr('category_id', 'equalto', category.id)|list|length %}
    {% if count > 0 %}
    <button class="category-badge" onclick="filterCategory('{{ category.slug }}')">
        {{ category.name }} ({{ count }})
    </button>
    {% endif %}
    {% endfor %}
</div>
{% endif %}

<!-- Companies Grid -->
{% if companies %}
<div class="companies-grid" id="companiesGrid">
    {% for company in companies %}
    <div class="company-card" data-category="{{ company.category.slug if company.category else 'brak' }}">
        <a href="{{ url_for('company_detail', company_id=company.id) }}" class="company-logo">
            <img src="{{ url_for('static', filename='img/companies/' ~ company.slug ~ '.webp') }}"
                 alt="{{ company.name }}"
                 onerror="if(!this.dataset.triedSvg){this.dataset.triedSvg='1';this.src=this.src.replace('.webp','.svg')}else{this.parentElement.style.display='none'}">
        </a>
        <div class="company-header">
            {% if company.category %}
            <span class="company-category">{{ company.category.name }}</span>
            {% endif %}
            <a href="{{ url_for('company_detail', company_id=company.id) }}" class="company-name">
                {{ company.name }}
            </a>
        </div>

        <div class="company-description">
            {{ company.description_short|truncate(150) if company.description_short else 'Brak opisu' }}
        </div>

        <div class="company-contact">
            {% if company.website %}
            <div class="company-contact-item">
                <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2">
                    <circle cx="8" cy="8" r="7"/>
                    <path d="M1 8h14M8 1a11 11 0 0 1 0 14 11 11 0 0 1 0-14"/>
                </svg>
                <a href="{{ company.website }}" target="_blank" rel="noopener noreferrer">
                    {{ company.website|replace('https://', '')|replace('http://', '')|replace('www.', '')|truncate(30) }}
                </a>
            </div>
            {% endif %}

            {% if company.phone %}
            <div class="company-contact-item">
                <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2">
                    <path d="M3 2a1 1 0 0 0-1 1v10a1 1 0 0 0 1 1h10a1 1 0 0 0 1-1V3a1 1 0 0 0-1-1H3z"/>
                    <path d="M6 6l4 4M10 6l-4 4"/>
                </svg>
                <a href="tel:{{ company.phone }}">{{ company.phone }}</a>
            </div>
            {% endif %}

            {% if company.address_city %}
            <div class="company-contact-item">
                <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2">
                    <path d="M8 2l6 6-6 6-6-6 6-6z"/>
                </svg>
                {{ company.address_city }}
            </div>
            {% endif %}
        </div>
    </div>
    {% endfor %}
</div>
{% else %}
<div class="empty-state">
    <svg width="120" height="120" viewBox="0 0 120 120" fill="none">
        <circle cx="60" cy="60" r="50" stroke="currentColor" stroke-width="4"/>
        <path d="M40 50h40M40 70h40" stroke="currentColor" stroke-width="4" stroke-linecap="round"/>
    </svg>
    <h2>Brak firm w katalogu</h2>
    <p>Nie znaleziono żadnych firm w systemie.</p>
</div>
{% endif %}

{% endblock %}

{% block extra_js %}
    // Category filter
    function filterCategory(slug) {
        const cards = document.querySelectorAll('.company-card');
        const badges = document.querySelectorAll('.category-badge');

        // Update active badge
        badges.forEach(badge => {
            badge.classList.remove('active');
            if (badge.textContent.toLowerCase().includes(slug) ||
                (slug === 'all' && badge.textContent.includes('Wszystkie'))) {
                badge.classList.add('active');
            }
        });

        // Filter cards
        cards.forEach(card => {
            if (slug === 'all') {
                card.style.display = 'flex';
            } else {
                const cardCategory = card.getAttribute('data-category');
                card.style.display = cardCategory === slug ? 'flex' : 'none';
            }
        });
    }

    // Smooth scroll to companies on search
    const urlParams = new URLSearchParams(window.location.search);
    if (urlParams.get('q')) {
        document.getElementById('companiesGrid')?.scrollIntoView({ behavior: 'smooth' });
    }
{% endblock %}
