{% extends "base.html" %}

{% block title %}Posiedzenie {{ meeting.meeting_identifier }} - Strefa RADA{% endblock %}

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

    .meeting-header {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        margin-bottom: var(--spacing-xl);
        padding-bottom: var(--spacing-lg);
        border-bottom: 1px solid var(--border-color);
    }

    .meeting-title-section h1 {
        font-size: var(--font-size-2xl);
        color: var(--text-primary);
        margin-bottom: var(--spacing-sm);
    }

    .meeting-meta {
        display: flex;
        flex-wrap: wrap;
        gap: var(--spacing-lg);
        color: var(--text-secondary);
        font-size: var(--font-size-sm);
    }

    .meeting-meta span {
        display: flex;
        align-items: center;
        gap: 6px;
    }

    .meeting-meta svg {
        width: 16px;
        height: 16px;
    }

    .meeting-actions {
        display: flex;
        flex-direction: column;
        gap: var(--spacing-sm);
        align-items: flex-end;
    }

    .meeting-status {
        display: inline-flex;
        align-items: center;
        padding: 6px 14px;
        border-radius: var(--radius-full);
        font-size: var(--font-size-sm);
        font-weight: 600;
    }

    .meeting-status.draft {
        background: #fef3c7;
        color: #92400e;
    }

    .meeting-status.agenda_published {
        background: #dbeafe;
        color: #1e40af;
    }

    .meeting-status.protocol_draft {
        background: #e0e7ff;
        color: #3730a3;
    }

    .meeting-status.protocol_published {
        background: #d1fae5;
        color: #065f46;
    }

    .action-buttons {
        display: flex;
        gap: var(--spacing-sm);
    }

    .btn-action {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        padding: 8px 16px;
        border-radius: var(--radius-md);
        font-size: var(--font-size-sm);
        text-decoration: none;
        transition: all 0.2s;
        border: none;
        cursor: pointer;
    }

    .btn-action svg {
        width: 16px;
        height: 16px;
    }

    .btn-edit {
        background: var(--bg-secondary);
        color: var(--text-primary);
    }

    .btn-edit:hover {
        background: var(--bg-tertiary);
    }

    .btn-publish {
        background: var(--success);
        color: white;
    }

    .btn-publish:hover {
        background: #059669;
    }

    .inline-form {
        display: inline;
    }

    .btn-back {
        background: var(--bg-secondary);
        color: var(--text-secondary);
    }

    .btn-back:hover {
        background: var(--bg-tertiary);
    }

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

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

    .back-link svg {
        width: 16px;
        height: 16px;
    }

    .meeting-section {
        background: white;
        border: 1px solid var(--border-color);
        border-radius: var(--radius-lg);
        padding: var(--spacing-xl);
        margin-bottom: var(--spacing-lg);
    }

    .meeting-section h2 {
        font-size: var(--font-size-lg);
        color: var(--text-primary);
        margin-bottom: var(--spacing-lg);
        padding-bottom: var(--spacing-sm);
        border-bottom: 1px solid var(--border-color);
        display: flex;
        align-items: center;
        gap: var(--spacing-sm);
    }

    .meeting-section h2 svg {
        width: 20px;
        height: 20px;
        color: #f59e0b;
    }

    .info-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: var(--spacing-md);
    }

    .info-item {
        padding: var(--spacing-sm);
    }

    .info-item label {
        display: block;
        font-size: var(--font-size-sm);
        color: var(--text-muted);
        margin-bottom: 4px;
    }

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

    /* Agenda */
    .agenda-list {
        list-style: none;
        padding: 0;
        margin: 0;
    }

    .agenda-list li {
        display: flex;
        gap: var(--spacing-md);
        padding: var(--spacing-sm) 0;
        border-bottom: 1px solid var(--border-color);
    }

    .agenda-list li:last-child {
        border-bottom: none;
    }

    .agenda-time {
        font-size: var(--font-size-sm);
        color: var(--text-muted);
        min-width: 100px;
    }

    .agenda-title {
        flex: 1;
        color: var(--text-primary);
    }

    /* Attendance */
    .attendance-table {
        width: 100%;
        border-collapse: collapse;
    }

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

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

    .attendance-status {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        font-size: var(--font-size-sm);
        padding: 4px 10px;
        border-radius: var(--radius-full);
    }

    .attendance-status.present {
        background: #d1fae5;
        color: #065f46;
    }

    .attendance-status.absent {
        background: #fee2e2;
        color: #991b1b;
    }

    .attendance-status.unknown {
        background: #f3f4f6;
        color: #6b7280;
    }

    .attendance-status svg {
        width: 14px;
        height: 14px;
    }

    .quorum-info {
        margin-top: var(--spacing-md);
        padding: var(--spacing-md);
        border-radius: var(--radius-md);
        font-size: var(--font-size-sm);
    }

    .quorum-info.confirmed {
        background: #d1fae5;
        color: #065f46;
    }

    .quorum-info.not-confirmed {
        background: #fee2e2;
        color: #991b1b;
    }

    /* Proceedings */
    .proceeding-item {
        margin-bottom: var(--spacing-lg);
        padding-bottom: var(--spacing-lg);
        border-bottom: 1px solid var(--border-color);
    }

    .proceeding-item:last-child {
        border-bottom: none;
        margin-bottom: 0;
        padding-bottom: 0;
    }

    .proceeding-item h3 {
        font-size: var(--font-size-base);
        color: var(--text-primary);
        margin-bottom: var(--spacing-sm);
    }

    .proceeding-item h4 {
        font-size: var(--font-size-sm);
        color: var(--text-muted);
        margin-top: var(--spacing-sm);
        margin-bottom: var(--spacing-xs);
    }

    .proceeding-item p,
    .proceeding-text {
        color: var(--text-secondary);
        line-height: 1.6;
    }

    .proceeding-text p {
        margin-bottom: var(--spacing-sm);
    }

    .proceeding-list {
        color: var(--text-secondary);
        line-height: 1.6;
        padding-left: var(--spacing-lg);
        margin: var(--spacing-xs) 0;
    }

    .proceeding-list li {
        margin-bottom: var(--spacing-xs);
    }

    .tasks-list li {
        color: var(--text-primary);
    }

    /* Empty state */
    .empty-note {
        color: var(--text-muted);
        font-style: italic;
    }

    /* Print buttons */
    .print-buttons {
        display: flex;
        gap: var(--spacing-sm);
        margin-top: var(--spacing-md);
    }

    .btn-print {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        padding: 8px 16px;
        background: var(--bg-secondary);
        color: var(--text-primary);
        border: 1px solid var(--border-color);
        border-radius: var(--radius-md);
        font-size: var(--font-size-sm);
        cursor: pointer;
        transition: all 0.2s;
    }

    .btn-print:hover {
        background: var(--bg-tertiary);
        border-color: var(--primary);
    }

    a.btn-print {
        text-decoration: none;
        color: var(--text-primary);
    }

    .btn-print svg {
        width: 16px;
        height: 16px;
    }

    /* Print styles */
    @media print {
        body * {
            visibility: hidden;
        }
        .print-area, .print-area * {
            visibility: visible;
        }
        .print-area {
            position: absolute;
            left: 0;
            top: 0;
            width: 100%;
            padding: 20px;
        }
        .meeting-header, .meeting-actions, .action-buttons,
        .back-link, .btn-print, .print-buttons, nav, footer,
        .meeting-section:not(.print-section) {
            display: none !important;
        }
        .print-section {
            page-break-inside: avoid;
            border: none !important;
            box-shadow: none !important;
        }
        .print-header {
            text-align: center;
            margin-bottom: 30px;
            padding-bottom: 20px;
            border-bottom: 2px solid #333;
        }
        .print-header h1 {
            font-size: 18pt;
            margin-bottom: 10px;
        }
        .print-header p {
            font-size: 12pt;
            color: #666;
        }
        .attendance-status {
            background: none !important;
            padding: 0 !important;
        }
        .attendance-status.present { color: #000 !important; }
        .attendance-status.absent { color: #666 !important; }
        .attendance-status.unknown { color: #999 !important; }
    }
</style>
{% endblock %}

{% block content %}
<div class="meeting-container">
    <a href="{{ url_for('board.index') }}" class="back-link">
        <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
            <path d="M15 19l-7-7 7-7"/>
        </svg>
        Powrót do listy posiedzeń
    </a>

    <div class="meeting-header">
        <div class="meeting-title-section">
            <h1>Posiedzenie Rady Izby nr {{ meeting.meeting_identifier }}</h1>
            <div class="meeting-meta">
                {% if meeting.meeting_date %}
                <span>
                    <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                        <path d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"/>
                    </svg>
                    {{ meeting.meeting_date.strftime('%d.%m.%Y') }}
                </span>
                {% endif %}
                {% if meeting.start_time %}
                <span>
                    <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                        <path d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/>
                    </svg>
                    {{ meeting.start_time.strftime('%H:%M') }}{% if meeting.end_time %} - {{ meeting.end_time.strftime('%H:%M') }}{% endif %}
                </span>
                {% endif %}
                <span>
                    <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                        <path d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z"/>
                        <path d="M15 11a3 3 0 11-6 0 3 3 0 016 0z"/>
                    </svg>
                    {{ meeting.location or 'Siedziba Izby' }}
                </span>
            </div>
        </div>

        <div class="meeting-actions">
            <span class="meeting-status {{ meeting.status }}">{{ meeting.status_label }}</span>

            {% if can_manage %}
            <div class="action-buttons">
                <a href="{{ url_for('board.meeting_edit', meeting_id=meeting.id) }}" class="btn-action btn-edit">
                    <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                        <path d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
                    </svg>
                    Edytuj
                </a>

                {% if meeting.status == 'draft' %}
                <form action="{{ url_for('board.meeting_publish_agenda', meeting_id=meeting.id) }}" method="POST" class="inline-form">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                    <button type="submit" class="btn-action btn-publish" onclick="return confirm('Czy na pewno chcesz opublikować program posiedzenia?')">
                        <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                            <path d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                        </svg>
                        Opublikuj program
                    </button>
                </form>
                {% elif meeting.status in ['agenda_published', 'protocol_draft'] %}
                <form action="{{ url_for('board.meeting_publish_protocol', meeting_id=meeting.id) }}" method="POST" class="inline-form">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                    <button type="submit" class="btn-action btn-publish" onclick="return confirm('Czy na pewno chcesz opublikować protokół?')">
                        <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                            <path d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                        </svg>
                        Opublikuj protokół
                    </button>
                </form>
                {% endif %}
            </div>
            {% endif %}

            <!-- Print & PDF buttons - visible for all members -->
            <div class="print-buttons">
                <a href="{{ url_for('board.meeting_pdf_agenda', meeting_id=meeting.id) }}" class="btn-print">
                    <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                        <path d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"/>
                    </svg>
                    Pobierz program (PDF)
                </a>
                {% if meeting.proceedings %}
                <a href="{{ url_for('board.meeting_pdf_protocol', meeting_id=meeting.id) }}" class="btn-print">
                    <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                        <path d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"/>
                    </svg>
                    Pobierz protokół (PDF)
                </a>
                {% endif %}
                <button type="button" class="btn-print" onclick="printAgenda()">
                    <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                        <path d="M17 17h2a2 2 0 002-2v-4a2 2 0 00-2-2H5a2 2 0 00-2 2v4a2 2 0 002 2h2m2 4h6a2 2 0 002-2v-4a2 2 0 00-2-2H9a2 2 0 00-2 2v4a2 2 0 002 2zm8-12V5a2 2 0 00-2-2H9a2 2 0 00-2 2v4h10z"/>
                    </svg>
                    Drukuj program
                </button>
                {% if meeting.proceedings %}
                <button type="button" class="btn-print" onclick="printProtocol()">
                    <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                        <path d="M17 17h2a2 2 0 002-2v-4a2 2 0 00-2-2H5a2 2 0 00-2 2v4a2 2 0 002 2h2m2 4h6a2 2 0 002-2v-4a2 2 0 00-2-2H9a2 2 0 00-2 2v4a2 2 0 002 2zm8-12V5a2 2 0 00-2-2H9a2 2 0 00-2 2v4h10z"/>
                    </svg>
                    Drukuj protokół
                </button>
                {% endif %}
            </div>
        </div>
    </div>

    <!-- Basic Info -->
    <div class="meeting-section">
        <h2>
            <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                <path d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
            </svg>
            Dane posiedzenia
        </h2>

        <div class="info-grid">
            <div class="info-item">
                <label>Prowadzący</label>
                <span class="value">{{ meeting.chairperson.name if meeting.chairperson else '—' }}</span>
            </div>
            <div class="info-item">
                <label>Protokolant</label>
                <span class="value">{{ meeting.secretary.name if meeting.secretary else '—' }}</span>
            </div>
            <div class="info-item">
                <label>Goście</label>
                <span class="value">{{ meeting.guests or 'brak' }}</span>
            </div>
        </div>
    </div>

    <!-- Agenda -->
    <div class="meeting-section" id="agenda">
        <h2>
            <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                <path d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-3 7h3m-3 4h3m-6-4h.01M9 16h.01"/>
            </svg>
            Program posiedzenia
        </h2>

        {% if meeting.agenda_items %}
        <ol class="agenda-list">
            {% for item in meeting.agenda_items %}
            <li>
                <span class="agenda-time">
                    {% if item.time_start %}{{ item.time_start }}{% endif %}
                    {% if item.time_end %} - {{ item.time_end }}{% endif %}
                </span>
                <span class="agenda-title">{{ item.title }}</span>
            </li>
            {% endfor %}
        </ol>
        {% else %}
        <p class="empty-note">Program posiedzenia nie został jeszcze uzupełniony.</p>
        {% endif %}
    </div>

    <!-- Attendance -->
    <div class="meeting-section">
        <h2>
            <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                <path d="M17 21v-2a4 4 0 00-4-4H5a4 4 0 00-4 4v2"/>
                <circle cx="9" cy="7" r="4"/>
                <path d="M23 21v-2a4 4 0 00-3-3.87"/>
                <path d="M16 3.13a4 4 0 010 7.75"/>
            </svg>
            Lista obecności
        </h2>

        {% if meeting.attendance %}
        <table class="attendance-table">
            <thead>
                <tr>
                    <th>Lp.</th>
                    <th>Imię i nazwisko</th>
                    <th>Inicjały</th>
                    <th>Obecność</th>
                </tr>
            </thead>
            <tbody>
                {% for member in board_members %}
                {% set att = meeting.attendance.get(member.id|string, {}) %}
                {% set status = att.get('status', 'unknown') %}
                <tr>
                    <td>{{ loop.index }}</td>
                    <td>{{ member.name or member.email.split('@')[0] }}</td>
                    <td>{{ att.get('initials', '') }}</td>
                    <td>
                        {% if status == 'present' or att.get('present') %}
                        <span class="attendance-status present">
                            <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                                <path d="M5 13l4 4L19 7"/>
                            </svg>
                            obecny
                        </span>
                        {% elif status == 'absent' %}
                        <span class="attendance-status absent">
                            <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                                <path d="M6 18L18 6M6 6l12 12"/>
                            </svg>
                            nieobecny
                        </span>
                        {% else %}
                        <span class="attendance-status unknown">
                            <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                                <path d="M8.228 9c.549-1.165 2.03-2 3.772-2 2.21 0 4 1.343 4 3 0 1.4-1.278 2.575-3.006 2.907-.542.104-.994.54-.994 1.093m0 3h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
                            </svg>
                            nieoznaczony
                        </span>
                        {% endif %}
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>

        {% if meeting.quorum_count is not none %}
        <div class="quorum-info {% if meeting.quorum_confirmed %}confirmed{% else %}not-confirmed{% endif %}">
            <strong>Kworum:</strong> {{ meeting.quorum_count }} / {{ board_members|length }} obecnych
            {% if meeting.quorum_confirmed %}
            — Kworum osiągnięte
            {% else %}
            — Brak kworum
            {% endif %}
        </div>
        {% endif %}
        {% else %}
        <p class="empty-note">Lista obecności nie została jeszcze uzupełniona.</p>
        {% endif %}
    </div>

    <!-- Proceedings -->
    {% if meeting.proceedings %}
    <div class="meeting-section" id="proceedings">
        <h2>
            <svg fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24">
                <path d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
            </svg>
            Przebieg posiedzenia i ustalenia
        </h2>

        {% for proc in meeting.proceedings or [] %}
        {% set agenda_item = meeting.agenda_items[proc.agenda_item] if meeting.agenda_items and proc.agenda_item < meeting.agenda_items|length else none %}
        <div class="proceeding-item">
            <h3>Ad. {{ proc.agenda_item + 1 }}. {{ proc.title or (agenda_item.title if agenda_item else 'Punkt programu') }}</h3>

            {% set discussion_text = proc.discussion or proc.discussed %}
            {% if discussion_text %}
            <h4>Omówiono:</h4>
            <div class="proceeding-text">{{ discussion_text|replace('\n\n', '</p><p>')|replace('\n', '<br>')|safe }}</div>
            {% endif %}

            {% if proc.decisions is iterable and proc.decisions is not string and proc.decisions|length > 0 %}
            <h4>Ustalono / decyzje:</h4>
            <ul class="proceeding-list">
                {% for decision in proc.decisions %}
                <li>{{ decision }}</li>
                {% endfor %}
            </ul>
            {% elif proc.decisions is string and proc.decisions %}
            <h4>Ustalono / decyzje:</h4>
            <p>{{ proc.decisions }}</p>
            {% endif %}

            {% if proc.tasks is iterable and proc.tasks is not string and proc.tasks|length > 0 %}
            <h4>Zadania:</h4>
            <ul class="proceeding-list tasks-list">
                {% for task in proc.tasks %}
                <li>{{ task }}</li>
                {% endfor %}
            </ul>
            {% endif %}
        </div>
        {% endfor %}
    </div>
    {% endif %}
</div>
{% endblock %}

{% block extra_js %}
function printAgenda() {
    const meetingId = '{{ meeting.meeting_identifier }}';
    const meetingDate = '{{ meeting.meeting_date.strftime("%d.%m.%Y") if meeting.meeting_date else "" }}';
    const location = '{{ meeting.location or "Siedziba Izby" }}';
    const chairperson = '{{ meeting.chairperson.name if meeting.chairperson else "—" }}';

    // Collect agenda items
    const agendaItems = [
        {% for item in meeting.agenda_items or [] %}
        { time: '{{ item.time_start }}{% if item.time_end %} - {{ item.time_end }}{% endif %}', title: '{{ item.title|e }}' },
        {% endfor %}
    ];

    // Create print window
    const printWindow = window.open('', '_blank');
    printWindow.document.write(`
        <html>
        <head>
            <title>Program Posiedzenia ${meetingId}</title>
            <style>
                body { font-family: 'Segoe UI', Arial, sans-serif; padding: 40px; max-width: 800px; margin: 0 auto; }
                .header { text-align: center; margin-bottom: 40px; padding-bottom: 20px; border-bottom: 2px solid #333; }
                .header h1 { font-size: 20pt; margin: 0 0 10px 0; }
                .header p { color: #666; margin: 5px 0; }
                .info-row { display: flex; justify-content: space-between; margin-bottom: 20px; padding: 10px; background: #f5f5f5; }
                .agenda-table { width: 100%; border-collapse: collapse; margin-top: 20px; }
                .agenda-table th, .agenda-table td { border: 1px solid #ddd; padding: 10px; text-align: left; }
                .agenda-table th { background: #f5f5f5; }
                .agenda-table td:first-child { width: 100px; text-align: center; }
                @media print { body { padding: 20px; } }
            </style>
        </head>
        <body>
            <div class="header">
                <h1>Program Posiedzenia Rady Izby Przedsiębiorców NORDA</h1>
                <p>Posiedzenie nr ${meetingId}</p>
                <p>${meetingDate} | ${location}</p>
            </div>
            <div class="info-row">
                <span><strong>Prowadzący:</strong> ${chairperson}</span>
            </div>
            <table class="agenda-table">
                <thead>
                    <tr><th>Godzina</th><th>Punkt programu</th></tr>
                </thead>
                <tbody>
                    ${agendaItems.map((item, i) => `<tr><td>${item.time || '—'}</td><td>${i+1}. ${item.title}</td></tr>`).join('')}
                </tbody>
            </table>
        </body>
        </html>
    `);
    printWindow.document.close();
    printWindow.onload = function() {
        printWindow.print();
    };
}

function printProtocol() {
    const meetingId = '{{ meeting.meeting_identifier }}';
    const meetingDate = '{{ meeting.meeting_date.strftime("%d.%m.%Y") if meeting.meeting_date else "" }}';
    const startTime = '{{ meeting.start_time.strftime("%H:%M") if meeting.start_time else "" }}';
    const endTime = '{{ meeting.end_time.strftime("%H:%M") if meeting.end_time else "" }}';
    const location = '{{ meeting.location or "Siedziba Izby" }}';
    const chairperson = '{{ meeting.chairperson.name if meeting.chairperson else "—" }}';
    const secretary = '{{ meeting.secretary.name if meeting.secretary else "—" }}';
    const guests = '{{ meeting.guests or "brak" }}';

    // Collect attendance
    const attendance = [
        {% for member in board_members %}
        {% set att = meeting.attendance.get(member.id|string, {}) %}
        {% set status = att.get('status', 'unknown') %}
        { name: '{{ member.name or member.email.split("@")[0] }}', initials: '{{ att.get("initials", "") }}', status: '{{ "obecny" if status == "present" or att.get("present") else ("nieobecny" if status == "absent" else "—") }}' },
        {% endfor %}
    ];

    // Collect agenda items
    const agendaItems = [
        {% for item in meeting.agenda_items or [] %}
        '{{ item.title|e }}',
        {% endfor %}
    ];

    // Collect proceedings
    const proceedings = [
        {% for proc in meeting.proceedings or [] %}
        {
            index: {{ proc.agenda_item }},
            title: `{{ (proc.title or '')|e }}`,
            discussion: `{{ (proc.discussion or proc.discussed or '')|e|replace('\n', '\\n') }}`,
            decisions: [{% for d in proc.decisions or [] %}`{{ d|e }}`,{% endfor %}],
            tasks: [{% for t in proc.tasks or [] %}`{{ t|e }}`,{% endfor %}]
        },
        {% endfor %}
    ];

    // Create print window
    const printWindow = window.open('', '_blank');
    printWindow.document.write(`
        <html>
        <head>
            <title>Protokół Posiedzenia ${meetingId}</title>
            <style>
                body { font-family: 'Segoe UI', Arial, sans-serif; padding: 40px; max-width: 800px; margin: 0 auto; line-height: 1.6; }
                .header { text-align: center; margin-bottom: 30px; padding-bottom: 20px; border-bottom: 2px solid #333; }
                .header h1 { font-size: 18pt; margin: 0 0 10px 0; }
                .header p { color: #666; margin: 5px 0; }
                .section { margin-bottom: 25px; }
                .section h2 { font-size: 14pt; border-bottom: 1px solid #ddd; padding-bottom: 5px; }
                .info-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 20px; }
                .info-item { padding: 8px; background: #f9f9f9; }
                .info-item strong { display: block; font-size: 10pt; color: #666; }
                table { width: 100%; border-collapse: collapse; margin: 15px 0; }
                th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
                th { background: #f5f5f5; font-size: 11pt; }
                .proceeding { margin-bottom: 20px; padding: 15px; background: #fafafa; border-left: 3px solid #2563eb; }
                .proceeding h3 { margin: 0 0 10px 0; font-size: 12pt; }
                .proceeding h4 { margin: 10px 0 5px 0; font-size: 11pt; color: #666; }
                .proceeding p { margin: 0 0 8px 0; white-space: pre-wrap; }
                .proceeding ul { margin: 5px 0; padding-left: 20px; }
                .proceeding li { margin-bottom: 3px; }
                .signature-section { margin-top: 50px; display: flex; justify-content: space-between; }
                .signature { width: 40%; text-align: center; padding-top: 50px; border-top: 1px solid #333; }
                @media print { body { padding: 20px; } .proceeding { page-break-inside: avoid; } }
            </style>
        </head>
        <body>
            <div class="header">
                <h1>PROTOKÓŁ</h1>
                <p>z Posiedzenia Rady Izby Przedsiębiorców NORDA</p>
                <p>Posiedzenie nr ${meetingId}</p>
            </div>

            <div class="section">
                <div class="info-grid">
                    <div class="info-item"><strong>Data</strong>${meetingDate}</div>
                    <div class="info-item"><strong>Godzina</strong>${startTime}${endTime ? ' - ' + endTime : ''}</div>
                    <div class="info-item"><strong>Miejsce</strong>${location}</div>
                    <div class="info-item"><strong>Prowadzący</strong>${chairperson}</div>
                    <div class="info-item"><strong>Protokolant</strong>${secretary}</div>
                    <div class="info-item"><strong>Goście</strong>${guests}</div>
                </div>
            </div>

            <div class="section">
                <h2>Lista obecności</h2>
                <table>
                    <thead><tr><th>Lp.</th><th>Imię i nazwisko</th><th>Inicjały</th><th>Obecność</th></tr></thead>
                    <tbody>
                        ${attendance.map((m, i) => `<tr><td>${i+1}</td><td>${m.name}</td><td>${m.initials}</td><td>${m.status}</td></tr>`).join('')}
                    </tbody>
                </table>
                <p><strong>Kworum:</strong> ${attendance.filter(m => m.status === 'obecny').length} / ${attendance.length} obecnych</p>
            </div>

            <div class="section">
                <h2>Przebieg posiedzenia</h2>
                ${proceedings.map(p => `
                    <div class="proceeding">
                        <h3>Ad. ${p.index + 1}. ${p.title || agendaItems[p.index] || 'Punkt programu'}</h3>
                        ${p.discussion ? `<h4>Omówiono:</h4><p>${p.discussion.replace(/\\n/g, '<br>')}</p>` : ''}
                        ${p.decisions.length > 0 ? `<h4>Ustalono / decyzje:</h4><ul>${p.decisions.map(d => '<li>' + d + '</li>').join('')}</ul>` : ''}
                        ${p.tasks.length > 0 ? `<h4>Zadania:</h4><ul>${p.tasks.map(t => '<li>' + t + '</li>').join('')}</ul>` : ''}
                    </div>
                `).join('')}
            </div>

            <div class="signature-section">
                <div class="signature">Prowadzący posiedzenie<br><small>${chairperson}</small></div>
                <div class="signature">Protokolant<br><small>${secretary}</small></div>
            </div>
        </body>
        </html>
    `);
    printWindow.document.close();
    printWindow.onload = function() {
        printWindow.print();
    };
}
{% endblock %}
