{% extends "base.html" %}

{% block title %}Social Media Publisher - Norda Biznes Partner{% 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);
    }

    .header-actions {
        display: flex;
        gap: var(--spacing-sm);
        align-items: center;
    }

    .stats-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
        gap: var(--spacing-md);
        margin-bottom: var(--spacing-lg);
    }

    .stat-card {
        background: var(--surface);
        padding: var(--spacing-md) var(--spacing-lg);
        border-radius: var(--radius);
        box-shadow: var(--shadow-sm);
        text-align: center;
    }

    .stat-card.total { border-top: 3px solid var(--primary); }
    .stat-card.draft { border-top: 3px solid var(--text-secondary); }
    .stat-card.approved { border-top: 3px solid var(--info, #0ea5e9); }
    .stat-card.scheduled { border-top: 3px solid var(--warning); }
    .stat-card.published { border-top: 3px solid var(--success); }
    .stat-card.failed { border-top: 3px solid var(--error); }

    .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);
    }

    .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);
    }

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

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

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

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

    .content-preview {
        max-width: 300px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        color: var(--text-primary);
    }

    .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-badge.draft { background: var(--surface-secondary, #f1f5f9); color: var(--text-secondary); }
    .status-badge.approved { background: #e0f2fe; color: #0369a1; }
    .status-badge.scheduled { background: var(--warning-bg); color: var(--warning); }
    .status-badge.published { background: var(--success-bg); color: var(--success); }
    .status-badge.failed { background: var(--error-bg); color: var(--error); }

    .type-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);
    }

    .engagement-cell {
        font-size: var(--font-size-sm);
        color: var(--text-secondary);
        white-space: nowrap;
    }

    .engagement-cell span {
        margin-right: 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);
    }

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

{% block content %}
<div class="container">
    <div class="admin-header">
        <h1>Social Media Publisher</h1>
        <div class="header-actions">
            <a href="{{ url_for('admin.social_publisher_settings') }}" class="btn btn-secondary">Ustawienia</a>
            <a href="{{ url_for('admin.social_publisher_new') }}" class="btn btn-primary">+ Nowy Post</a>
        </div>
    </div>

    <!-- Statystyki -->
    <div class="stats-grid">
        <div class="stat-card total">
            <div class="stat-value">{{ stats.total or 0 }}</div>
            <div class="stat-label">Wszystkie</div>
        </div>
        <div class="stat-card draft">
            <div class="stat-value">{{ stats.draft or 0 }}</div>
            <div class="stat-label">Szkice</div>
        </div>
        <div class="stat-card approved">
            <div class="stat-value">{{ stats.approved or 0 }}</div>
            <div class="stat-label">Zatwierdzone</div>
        </div>
        <div class="stat-card scheduled">
            <div class="stat-value">{{ stats.scheduled or 0 }}</div>
            <div class="stat-label">Zaplanowane</div>
        </div>
        <div class="stat-card published">
            <div class="stat-value">{{ stats.published or 0 }}</div>
            <div class="stat-label">Opublikowane</div>
        </div>
        <div class="stat-card failed">
            <div class="stat-value">{{ stats.failed or 0 }}</div>
            <div class="stat-label">Bledy</div>
        </div>
    </div>

    <!-- Filtry -->
    <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>
                <option value="draft" {% if status_filter == 'draft' %}selected{% endif %}>Szkic</option>
                <option value="approved" {% if status_filter == 'approved' %}selected{% endif %}>Zatwierdzony</option>
                <option value="scheduled" {% if status_filter == 'scheduled' %}selected{% endif %}>Zaplanowany</option>
                <option value="published" {% if status_filter == 'published' %}selected{% endif %}>Opublikowany</option>
                <option value="failed" {% if status_filter == 'failed' %}selected{% endif %}>Błąd</option>
            </select>
        </div>
        <div class="filter-group">
            <label for="type-filter">Typ:</label>
            <select id="type-filter" onchange="applyFilters()">
                <option value="all" {% if type_filter == 'all' %}selected{% endif %}>Wszystkie</option>
                {% for key, label in post_types.items() %}
                <option value="{{ key }}" {% if type_filter == key %}selected{% endif %}>{{ label }}</option>
                {% endfor %}
            </select>
        </div>
    </div>

    <!-- Tabela postow -->
    <div class="section">
        {% if posts %}
        <table class="posts-table">
            <thead>
                <tr>
                    <th>Typ</th>
                    <th>Tresc</th>
                    <th>Firma</th>
                    <th>Status</th>
                    <th>Data</th>
                    <th>Engagement</th>
                    <th>Akcje</th>
                </tr>
            </thead>
            <tbody>
                {% for post in posts %}
                <tr>
                    <td>
                        <span class="type-badge">{{ post_types.get(post.post_type, post.post_type) }}</span>
                    </td>
                    <td class="content-preview">
                        <a href="{{ url_for('admin.social_publisher_edit', post_id=post.id) }}" style="color: var(--text-primary); text-decoration: none;">
                            {{ post.content[:80] }}{% if post.content|length > 80 %}...{% endif %}
                        </a>
                    </td>
                    <td>{{ post.company.name if post.company else '-' }}</td>
                    <td>
                        <span class="status-badge {{ post.status }}">
                            {% if post.status == 'draft' %}Szkic
                            {% elif post.status == 'approved' %}Zatwierdzony
                            {% elif post.status == 'scheduled' %}Zaplanowany
                            {% elif post.status == 'published' %}Opublikowany
                            {% elif post.status == 'failed' %}Błąd
                            {% else %}{{ post.status }}{% endif %}
                        </span>
                    </td>
                    <td style="white-space: nowrap; font-size: var(--font-size-sm); color: var(--text-secondary);">
                        {% if post.published_at %}
                            {{ post.published_at.strftime('%Y-%m-%d %H:%M') }}
                        {% elif post.scheduled_at %}
                            {{ post.scheduled_at.strftime('%Y-%m-%d %H:%M') }}
                        {% else %}
                            {{ post.created_at.strftime('%Y-%m-%d %H:%M') if post.created_at else '-' }}
                        {% endif %}
                    </td>
                    <td class="engagement-cell">
                        {% if post.status == 'published' and (post.engagement_likes or post.engagement_comments or post.engagement_shares) %}
                            <span title="Polubienia">&#128077; {{ post.engagement_likes or 0 }}</span>
                            <span title="Komentarze">&#128172; {{ post.engagement_comments or 0 }}</span>
                            <span title="Udostepnienia">&#128257; {{ post.engagement_shares or 0 }}</span>
                        {% else %}
                            -
                        {% endif %}
                    </td>
                    <td class="actions-cell">
                        <a href="{{ url_for('admin.social_publisher_edit', post_id=post.id) }}" class="btn btn-secondary btn-small">
                            Edytuj
                        </a>
                        {% if post.status == 'draft' %}
                        <form method="POST" action="{{ url_for('admin.social_publisher_approve', post_id=post.id) }}" style="display:inline;">
                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                            <button type="submit" class="btn btn-info btn-small" style="background: #0ea5e9; color: white; border: none;">Zatwierdz</button>
                        </form>
                        {% endif %}
                        {% if post.status in ['draft', 'approved'] %}
                        <button class="btn btn-success btn-small" onclick="publishPost({{ post.id }})">Publikuj</button>
                        {% endif %}
                        {% if post.status != 'published' %}
                        <button class="btn btn-error btn-small" onclick="deletePost({{ post.id }})">Usun</button>
                        {% endif %}
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        {% else %}
        <div class="empty-state">
            <p>Brak postow{% if status_filter != 'all' or type_filter != 'all' %} pasujacych do filtrow{% endif %}.</p>
            <p style="margin-top: var(--spacing-md);">
                <a href="{{ url_for('admin.social_publisher_new') }}" class="btn btn-primary">Utworz pierwszy post</a>
            </p>
        </div>
        {% endif %}
    </div>
</div>

<!-- 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 id="confirmModalMessage" style="color: var(--text-secondary);"></p>
        </div>
        <div 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 %}
    function applyFilters() {
        const status = document.getElementById('status-filter').value;
        const type = document.getElementById('type-filter').value;
        let url = '{{ url_for("admin.social_publisher_list") }}?';
        if (status !== 'all') url += 'status=' + status + '&';
        if (type !== 'all') url += 'type=' + type + '&';
        window.location.href = url;
    }

    let confirmResolve = null;

    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 publishPost(id) {
        const confirmed = await showConfirm('Czy na pewno chcesz opublikować ten post na Facebook?', {
            icon: '&#128227;',
            title: 'Publikacja posta',
            okText: 'Publikuj',
            okClass: 'btn-success'
        });
        if (!confirmed) return;

        try {
            const response = await fetch('{{ url_for("admin.social_publisher_publish", post_id=0) }}'.replace('/0/', '/' + id + '/'), {
                method: 'POST',
                headers: { 'X-CSRFToken': '{{ csrf_token() }}' }
            });
            const data = await response.json();
            if (data.success) {
                showToast('Post został opublikowany na Facebook', 'success');
                setTimeout(() => location.reload(), 1500);
            } else {
                showToast('Błąd: ' + (data.error || 'Nieznany błąd'), 'error');
            }
        } catch (err) {
            showToast('Błąd połączenia: ' + err.message, 'error');
        }
    }

    async function deletePost(id) {
        const confirmed = await showConfirm('Czy na pewno chcesz usunąć ten post? Ta operacja jest nieodwracalna.', {
            icon: '&#128465;',
            title: 'Usuwanie posta',
            okText: 'Usun',
            okClass: 'btn-error'
        });
        if (!confirmed) return;

        try {
            const response = await fetch('{{ url_for("admin.social_publisher_delete", post_id=0) }}'.replace('/0/', '/' + id + '/'), {
                method: 'POST',
                headers: { 'X-CSRFToken': '{{ csrf_token() }}' }
            });
            const data = await response.json();
            if (data.success) {
                showToast('Post został usunięty', 'success');
                setTimeout(() => location.reload(), 1500);
            } else {
                showToast('Błąd: ' + (data.error || 'Nieznany błąd'), 'error');
            }
        } catch (err) {
            showToast('Błąd połączenia: ' + err.message, 'error');
        }
    }
{% endblock %}
