{% extends "base.html" %}

{% block title %}Zarzadzanie Ogloszeniami - Norda Biznes Hub{% endblock %}

{% block extra_css %}
<style>
    .admin-header {
        margin-bottom: var(--spacing-xl);
        display: flex;
        justify-content: space-between;
        align-items: center;
        flex-wrap: wrap;
        gap: var(--spacing-md);
    }

    .admin-header h1 {
        font-size: var(--font-size-3xl);
        color: var(--text-primary);
    }

    .filters-row {
        display: flex;
        gap: var(--spacing-md);
        margin-bottom: var(--spacing-lg);
        flex-wrap: wrap;
        align-items: center;
    }

    .filter-group {
        display: flex;
        align-items: center;
        gap: var(--spacing-xs);
    }

    .filter-group label {
        font-weight: 500;
        color: var(--text-secondary);
        font-size: var(--font-size-sm);
    }

    .filter-group select {
        padding: var(--spacing-xs) var(--spacing-sm);
        border: 1px solid var(--border);
        border-radius: var(--radius);
        background: var(--surface);
        font-size: var(--font-size-sm);
    }

    .section {
        background: var(--surface);
        padding: var(--spacing-xl);
        border-radius: var(--radius-lg);
        box-shadow: var(--shadow);
    }

    .announcements-table {
        width: 100%;
        border-collapse: collapse;
    }

    .announcements-table th,
    .announcements-table td {
        padding: var(--spacing-md);
        text-align: left;
        border-bottom: 1px solid var(--border);
    }

    .announcements-table th {
        font-weight: 600;
        color: var(--text-secondary);
        font-size: var(--font-size-sm);
        text-transform: uppercase;
        background: var(--background);
    }

    .announcements-table tr:hover {
        background: var(--background);
    }

    .status-badge {
        display: inline-block;
        padding: var(--spacing-xs) var(--spacing-sm);
        border-radius: var(--radius-full);
        font-size: var(--font-size-xs);
        font-weight: 600;
    }

    .status-published { background: var(--success-bg); color: var(--success); }
    .status-draft { background: var(--warning-bg); color: var(--warning); }
    .status-archived { background: var(--surface-secondary); color: var(--text-secondary); }
    .status-expired { background: var(--error-bg); color: var(--error); }

    .category-badge {
        display: inline-block;
        padding: var(--spacing-xs) var(--spacing-sm);
        border-radius: var(--radius-sm);
        font-size: var(--font-size-xs);
        font-weight: 500;
        background: var(--primary-bg);
        color: var(--primary);
    }

    .category-event { background: #e0f2fe; color: #0369a1; }
    .category-opportunity { background: #dcfce7; color: #15803d; }
    .category-member_news { background: #fef3c7; color: #b45309; }
    .category-partnership { background: #f3e8ff; color: #7c3aed; }

    .pinned-icon {
        color: var(--warning);
        margin-left: var(--spacing-xs);
    }

    .featured-icon {
        color: var(--primary);
        margin-left: var(--spacing-xs);
    }

    .btn-small {
        padding: var(--spacing-xs) var(--spacing-sm);
        font-size: var(--font-size-xs);
    }

    .actions-cell {
        white-space: nowrap;
        display: flex;
        gap: var(--spacing-xs);
    }

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

    .stats-row {
        display: flex;
        gap: var(--spacing-lg);
        margin-bottom: var(--spacing-lg);
        flex-wrap: wrap;
    }

    .stat-card {
        background: var(--surface);
        padding: var(--spacing-md) var(--spacing-lg);
        border-radius: var(--radius);
        box-shadow: var(--shadow-sm);
        display: flex;
        flex-direction: column;
        min-width: 120px;
    }

    .stat-value {
        font-size: var(--font-size-2xl);
        font-weight: 700;
        color: var(--primary);
    }

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

    .title-cell {
        max-width: 300px;
    }

    .title-cell a {
        color: var(--text-primary);
        text-decoration: none;
    }

    .title-cell a:hover {
        color: var(--primary);
    }

    .views-count {
        color: var(--text-secondary);
        font-size: var(--font-size-sm);
    }

    @media (max-width: 768px) {
        .announcements-table {
            font-size: var(--font-size-sm);
        }
        .announcements-table th:nth-child(4),
        .announcements-table td:nth-child(4),
        .announcements-table th:nth-child(5),
        .announcements-table td:nth-child(5) {
            display: none;
        }
    }
</style>
{% endblock %}

{% block content %}
<div class="container">
    <div class="admin-header">
        <h1>Zarzadzanie Ogloszeniami</h1>
        <a href="{{ url_for('admin_announcements_new') }}" class="btn btn-primary">
            + Nowe ogloszenie
        </a>
    </div>

    <!-- Filters -->
    <div class="filters-row">
        <div class="filter-group">
            <label for="status-filter">Status:</label>
            <select id="status-filter" onchange="applyFilters()">
                <option value="all" {% if status_filter == 'all' %}selected{% endif %}>Wszystkie</option>
                {% for status in statuses %}
                <option value="{{ status }}" {% if status_filter == status %}selected{% endif %}>
                    {{ status_labels.get(status, status) }}
                </option>
                {% endfor %}
            </select>
        </div>
        <div class="filter-group">
            <label for="category-filter">Kategoria:</label>
            <select id="category-filter" onchange="applyFilters()">
                <option value="all" {% if category_filter == 'all' %}selected{% endif %}>Wszystkie</option>
                {% for cat in categories %}
                <option value="{{ cat }}" {% if category_filter == cat %}selected{% endif %}>
                    {{ category_labels.get(cat, cat) }}
                </option>
                {% endfor %}
            </select>
        </div>
    </div>

    <div class="section">
        {% if announcements %}
        <table class="announcements-table">
            <thead>
                <tr>
                    <th>Tytul</th>
                    <th>Kategoria</th>
                    <th>Status</th>
                    <th>Autor</th>
                    <th>Utworzono</th>
                    <th>Wyswietlenia</th>
                    <th>Akcje</th>
                </tr>
            </thead>
            <tbody>
                {% for ann in announcements %}
                <tr>
                    <td class="title-cell">
                        <a href="{{ url_for('admin_announcements_edit', id=ann.id) }}">
                            {{ ann.title }}
                        </a>
                        {% if ann.is_pinned %}<span class="pinned-icon" title="Przypiete">&#128204;</span>{% endif %}
                        {% if ann.is_featured %}<span class="featured-icon" title="Wyroznienie">&#11088;</span>{% endif %}
                    </td>
                    <td>
                        <span class="category-badge category-{{ ann.category }}">
                            {{ category_labels.get(ann.category, ann.category) }}
                        </span>
                    </td>
                    <td>
                        {% if ann.status == 'draft' %}
                        <span class="status-badge status-draft">Szkic</span>
                        {% elif ann.status == 'archived' %}
                        <span class="status-badge status-archived">Zarchiwizowane</span>
                        {% elif ann.expires_at and ann.expires_at < now %}
                        <span class="status-badge status-expired">Wygaslo</span>
                        {% else %}
                        <span class="status-badge status-published">Opublikowane</span>
                        {% endif %}
                    </td>
                    <td>{{ ann.author.name if ann.author else '-' }}</td>
                    <td>{{ ann.created_at.strftime('%Y-%m-%d %H:%M') if ann.created_at else '-' }}</td>
                    <td class="views-count">{{ ann.views_count or 0 }}</td>
                    <td class="actions-cell">
                        <a href="{{ url_for('admin_announcements_edit', id=ann.id) }}" class="btn btn-secondary btn-small">
                            Edytuj
                        </a>
                        {% if ann.status == 'draft' %}
                        <button class="btn btn-success btn-small" onclick="publishAnnouncement({{ ann.id }})">
                            Publikuj
                        </button>
                        {% elif ann.status == 'published' %}
                        <button class="btn btn-warning btn-small" onclick="archiveAnnouncement({{ ann.id }})">
                            Archiwizuj
                        </button>
                        {% endif %}
                        <button class="btn btn-error btn-small" onclick="deleteAnnouncement({{ ann.id }})">
                            Usun
                        </button>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        {% else %}
        <div class="empty-state">
            <p>Brak ogloszen{% if status_filter != 'all' or category_filter != 'all' %} pasujacych do filtrow{% endif %}. Utworz pierwsze ogloszenie klikajac przycisk powyzej.</p>
        </div>
        {% endif %}
    </div>
</div>

<!-- Universal Confirm Modal -->
<div class="modal-overlay" id="confirmModal">
    <div class="modal" style="max-width: 420px; background: var(--surface); border-radius: var(--radius-lg); padding: var(--spacing-xl);">
        <div style="text-align: center; margin-bottom: var(--spacing-lg);">
            <div class="modal-icon" id="confirmModalIcon" style="font-size: 3em; margin-bottom: var(--spacing-md);">&#10067;</div>
            <h3 id="confirmModalTitle" style="margin-bottom: var(--spacing-sm);">Potwierdzenie</h3>
            <p class="modal-description" id="confirmModalMessage" style="color: var(--text-secondary);"></p>
        </div>
        <div class="modal-actions" style="display: flex; gap: var(--spacing-sm); justify-content: center;">
            <button type="button" class="btn btn-secondary" id="confirmModalCancel">Anuluj</button>
            <button type="button" class="btn btn-primary" id="confirmModalOk">OK</button>
        </div>
    </div>
</div>

<div id="toastContainer" style="position: fixed; top: 80px; right: 20px; z-index: 1100; display: flex; flex-direction: column; gap: 10px;"></div>

<style>
.modal-overlay#confirmModal { display: none; position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.5); z-index: 1050; align-items: center; justify-content: center; }
.modal-overlay#confirmModal.active { display: flex; }
.toast { padding: 12px 20px; border-radius: var(--radius); background: var(--surface); border-left: 4px solid var(--primary); box-shadow: 0 4px 12px rgba(0,0,0,0.15); display: flex; align-items: center; gap: 10px; animation: toastIn 0.3s ease; }
.toast.success { border-left-color: var(--success); }
.toast.error { border-left-color: var(--error); }
@keyframes toastIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
@keyframes toastOut { from { opacity: 1; } to { opacity: 0; } }
</style>
{% endblock %}

{% block extra_js %}
    const now = new Date();
    let confirmResolve = null;

    function applyFilters() {
        const status = document.getElementById('status-filter').value;
        const category = document.getElementById('category-filter').value;
        let url = '{{ url_for("admin_announcements") }}?';
        if (status !== 'all') url += 'status=' + status + '&';
        if (category !== 'all') url += 'category=' + category + '&';
        window.location.href = url;
    }

    function showConfirm(message, options = {}) {
        return new Promise(resolve => {
            confirmResolve = resolve;
            document.getElementById('confirmModalIcon').innerHTML = options.icon || '&#10067;';
            document.getElementById('confirmModalTitle').textContent = options.title || 'Potwierdzenie';
            document.getElementById('confirmModalMessage').innerHTML = message;
            document.getElementById('confirmModalOk').textContent = options.okText || 'OK';
            document.getElementById('confirmModalOk').className = 'btn ' + (options.okClass || 'btn-primary');
            document.getElementById('confirmModal').classList.add('active');
        });
    }

    function closeConfirm(result) {
        document.getElementById('confirmModal').classList.remove('active');
        if (confirmResolve) { confirmResolve(result); confirmResolve = null; }
    }

    document.getElementById('confirmModalOk').addEventListener('click', () => closeConfirm(true));
    document.getElementById('confirmModalCancel').addEventListener('click', () => closeConfirm(false));
    document.getElementById('confirmModal').addEventListener('click', e => { if (e.target.id === 'confirmModal') closeConfirm(false); });

    function showToast(message, type = 'info', duration = 4000) {
        const container = document.getElementById('toastContainer');
        const icons = { success: '&#10003;', error: '&#10007;', warning: '&#9888;', info: '&#8505;' };
        const toast = document.createElement('div');
        toast.className = `toast ${type}`;
        toast.innerHTML = `<span style="font-size:1.2em">${icons[type]||'&#8505;'}</span><span>${message}</span>`;
        container.appendChild(toast);
        setTimeout(() => { toast.style.animation = 'toastOut 0.3s ease forwards'; setTimeout(() => toast.remove(), 300); }, duration);
    }

    async function publishAnnouncement(id) {
        const confirmed = await showConfirm('Czy na pewno chcesz opublikowac to ogloszenie?', {
            icon: '&#128227;',
            title: 'Publikacja ogloszenia',
            okText: 'Publikuj',
            okClass: 'btn-success'
        });
        if (!confirmed) return;

        try {
            const response = await fetch('/admin/announcements/' + id + '/publish', {
                method: 'POST',
                headers: {
                    'X-CSRFToken': '{{ csrf_token() }}'
                }
            });
            const data = await response.json();
            if (data.success) {
                showToast('Ogloszenie zostalo opublikowane', 'success');
                setTimeout(() => location.reload(), 1500);
            } else {
                showToast('Blad: ' + data.error, 'error');
            }
        } catch (err) {
            showToast('Blad: ' + err, 'error');
        }
    }

    async function archiveAnnouncement(id) {
        const confirmed = await showConfirm('Czy na pewno chcesz zarchiwizowac to ogloszenie?', {
            icon: '&#128230;',
            title: 'Archiwizacja ogloszenia',
            okText: 'Archiwizuj',
            okClass: 'btn-warning'
        });
        if (!confirmed) return;

        try {
            const response = await fetch('/admin/announcements/' + id + '/archive', {
                method: 'POST',
                headers: {
                    'X-CSRFToken': '{{ csrf_token() }}'
                }
            });
            const data = await response.json();
            if (data.success) {
                showToast('Ogloszenie zostalo zarchiwizowane', 'success');
                setTimeout(() => location.reload(), 1500);
            } else {
                showToast('Blad: ' + data.error, 'error');
            }
        } catch (err) {
            showToast('Blad: ' + err, 'error');
        }
    }

    async function deleteAnnouncement(id) {
        const confirmed = await showConfirm('Czy na pewno chcesz usunac to ogloszenie? Ta operacja jest nieodwracalna.', {
            icon: '&#128465;',
            title: 'Usuwanie ogloszenia',
            okText: 'Usun',
            okClass: 'btn-error'
        });
        if (!confirmed) return;

        try {
            const response = await fetch('/admin/announcements/' + id + '/delete', {
                method: 'POST',
                headers: {
                    'X-CSRFToken': '{{ csrf_token() }}'
                }
            });
            const data = await response.json();
            if (data.success) {
                showToast('Ogloszenie zostalo usuniete', 'success');
                setTimeout(() => location.reload(), 1500);
            } else {
                showToast('Blad: ' + data.error, 'error');
            }
        } catch (err) {
            showToast('Blad: ' + err, 'error');
        }
    }
{% endblock %}
