<?php
/* Smarty version 4.5.6, created on 2025-12-20 23:17:17
  from '/var/www/html/tivi/panel/templates/partials/entity_notes.tpl' */

/* @var Smarty_Internal_Template $_smarty_tpl */
if ($_smarty_tpl->_decodeProperties($_smarty_tpl, array (
  'version' => '4.5.6',
  'unifunc' => 'content_6947206dbe6d71_17684692',
  'has_nocache_code' => false,
  'file_dependency' => 
  array (
    '5f2b30e563583ef1f8574ff439ed919d8999f906' => 
    array (
      0 => '/var/www/html/tivi/panel/templates/partials/entity_notes.tpl',
      1 => 1766268947,
      2 => 'file',
    ),
  ),
  'includes' => 
  array (
  ),
),false)) {
function content_6947206dbe6d71_17684692 (Smarty_Internal_Template $_smarty_tpl) {
?>
<div class="entity-notes-container" data-entity-type="<?php echo $_smarty_tpl->tpl_vars['entityType']->value;?>
" data-entity-id="<?php echo $_smarty_tpl->tpl_vars['entityId']->value;?>
">
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h6 class="card-title mb-0">
                <i class="bi bi-sticky me-2"></i>Notatki
                <span class="badge bg-secondary notes-count">0</span>
            </h6>
            <button type="button" class="btn btn-sm btn-primary btn-add-note">
                <i class="bi bi-plus-lg"></i>
            </button>
        </div>
        <div class="card-body p-0">
            <div class="notes-list">
                <div class="text-center py-4 text-muted notes-empty">
                    <i class="bi bi-sticky d-block mb-2" style="font-size: 2rem;"></i>
                    Brak notatek
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Modal dodawania/edycji notatki -->
<div class="modal fade" id="noteModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Notatka</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <input type="hidden" id="noteId" value="">
                <input type="hidden" id="noteEntityType" value="">
                <input type="hidden" id="noteEntityId" value="">
                
                <div class="mb-3">
                    <label class="form-label">Tytuł (opcjonalnie)</label>
                    <input type="text" id="noteTitle" class="form-control" placeholder="Tytuł notatki">
                </div>
                
                <div class="mb-3">
                    <label class="form-label">Treść <span class="text-danger">*</span></label>
                    <textarea id="noteContent" class="form-control" rows="4" placeholder="Treść notatki..." required></textarea>
                </div>
                
                <div class="mb-3">
                    <label class="form-label">Kolor</label>
                    <div class="d-flex gap-2" id="noteColors">
                        <button type="button" class="btn btn-sm note-color active" data-color="default" style="background: #6c757d; width: 30px; height: 30px; border-radius: 50%;"></button>
                        <button type="button" class="btn btn-sm note-color" data-color="red" style="background: #dc3545; width: 30px; height: 30px; border-radius: 50%;"></button>
                        <button type="button" class="btn btn-sm note-color" data-color="orange" style="background: #fd7e14; width: 30px; height: 30px; border-radius: 50%;"></button>
                        <button type="button" class="btn btn-sm note-color" data-color="yellow" style="background: #ffc107; width: 30px; height: 30px; border-radius: 50%;"></button>
                        <button type="button" class="btn btn-sm note-color" data-color="green" style="background: #198754; width: 30px; height: 30px; border-radius: 50%;"></button>
                        <button type="button" class="btn btn-sm note-color" data-color="blue" style="background: #0d6efd; width: 30px; height: 30px; border-radius: 50%;"></button>
                        <button type="button" class="btn btn-sm note-color" data-color="purple" style="background: #6f42c1; width: 30px; height: 30px; border-radius: 50%;"></button>
                        <button type="button" class="btn btn-sm note-color" data-color="pink" style="background: #d63384; width: 30px; height: 30px; border-radius: 50%;"></button>
                    </div>
                </div>
                
                <div class="form-check">
                    <input type="checkbox" id="notePinned" class="form-check-input">
                    <label class="form-check-label" for="notePinned">Przypnij na górze</label>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Anuluj</button>
                <button type="button" class="btn btn-primary" id="saveNoteBtn">Zapisz</button>
            </div>
        </div>
    </div>
</div>

<style>
.note-item {
    padding: 10px 15px;
    border-bottom: 1px solid var(--border-color);
    position: relative;
}
.note-item:last-child {
    border-bottom: none;
}
.note-item.pinned {
    background: rgba(var(--bs-primary-rgb), 0.05);
}
.note-item .note-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 5px;
}
.note-item .note-title {
    font-weight: 600;
    margin: 0;
    font-size: 0.9rem;
}
.note-item .note-content {
    font-size: 0.85rem;
    color: var(--text-muted);
    white-space: pre-wrap;
    word-break: break-word;
}
.note-item .note-meta {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-top: 5px;
}
.note-item .note-actions {
    display: flex;
    gap: 5px;
}
.note-item .note-actions .btn {
    padding: 2px 6px;
    font-size: 0.75rem;
}
.note-color-indicator {
    width: 4px;
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
}
.note-color.active {
    box-shadow: 0 0 0 3px rgba(255,255,255,0.5), 0 0 0 5px currentColor;
}
</style>

<?php echo '<script'; ?>
>

document.addEventListener('DOMContentLoaded', function() {

    const baseUrl = '<?php echo $_smarty_tpl->tpl_vars['baseUrl']->value;?>
';

    
    // Inicjalizacja dla każdego kontenera notatek
    document.querySelectorAll('.entity-notes-container').forEach(container => {
        const entityType = container.dataset.entityType;
        const entityId = container.dataset.entityId;
        
        // Załaduj notatki
        loadNotes(container, entityType, entityId);
        
        // Przycisk dodawania
        container.querySelector('.btn-add-note').addEventListener('click', function() {
            openNoteModal(null, entityType, entityId);
        });
    });
    
    // Wybór koloru
    document.querySelectorAll('#noteColors .note-color').forEach(btn => {
        btn.addEventListener('click', function() {
            document.querySelectorAll('#noteColors .note-color').forEach(b => b.classList.remove('active'));
            this.classList.add('active');
        });
    });
    
    // Zapisz notatkę
    document.getElementById('saveNoteBtn').addEventListener('click', saveNote);
    
    // Funkcje
    function loadNotes(container, entityType, entityId) {
        fetch(baseUrl + '/entity-notes/list?entity_type=' + entityType + '&entity_id=' + entityId)
            .then(r => r.json())
            .then(data => {
                if (data.success) {
                    renderNotes(container, data.notes);
                    container.querySelector('.notes-count').textContent = data.count;
                }
            });
    }
    
    function renderNotes(container, notes) {
        const list = container.querySelector('.notes-list');
        const empty = container.querySelector('.notes-empty');
        
        if (notes.length === 0) {
            list.innerHTML = '';
            list.appendChild(empty.cloneNode(true));
            return;
        }
        
        const colors = {
            default: '#6c757d', red: '#dc3545', orange: '#fd7e14', yellow: '#ffc107',
            green: '#198754', blue: '#0d6efd', purple: '#6f42c1', pink: '#d63384'
        };
        
        list.innerHTML = notes.map(note => 
            '<div class="note-item ' + (note.is_pinned ? 'pinned' : '') + '" data-id="' + note.id + '">' +
                '<div class="note-color-indicator" style="background: ' + (colors[note.color] || colors.default) + '"></div>' +
                '<div class="note-header">' +
                    '<div>' +
                        (note.is_pinned ? '<i class="bi bi-pin-fill text-primary me-1"></i>' : '') +
                        (note.title ? '<span class="note-title">' + escapeHtml(note.title) + '</span>' : '') +
                    '</div>' +
                    '<div class="note-actions">' +
                        '<button class="btn btn-outline-secondary btn-pin" title="' + (note.is_pinned ? 'Odepnij' : 'Przypnij') + '">' +
                            '<i class="bi bi-pin' + (note.is_pinned ? '-fill' : '') + '"></i>' +
                        '</button>' +
                        '<button class="btn btn-outline-primary btn-edit" title="Edytuj">' +
                            '<i class="bi bi-pencil"></i>' +
                        '</button>' +
                        '<button class="btn btn-outline-danger btn-delete" title="Usuń">' +
                            '<i class="bi bi-trash"></i>' +
                        '</button>' +
                    '</div>' +
                '</div>' +
                '<div class="note-content">' + escapeHtml(note.content) + '</div>' +
                '<div class="note-meta">' +
                    '<i class="bi bi-person me-1"></i>' + note.username + ' • ' +
                    '<i class="bi bi-clock me-1"></i>' + formatDate(note.updated_at) +
                '</div>' +
            '</div>'
        ).join('');
        
        // Event listeners dla przycisków
        list.querySelectorAll('.btn-pin').forEach((btn, i) => {
            btn.addEventListener('click', () => togglePin(notes[i].id, container));
        });
        list.querySelectorAll('.btn-edit').forEach((btn, i) => {
            btn.addEventListener('click', () => openNoteModal(notes[i], container.dataset.entityType, container.dataset.entityId));
        });
        list.querySelectorAll('.btn-delete').forEach((btn, i) => {
            btn.addEventListener('click', () => deleteNote(notes[i].id, container));
        });
    }
    
    function openNoteModal(note, entityType, entityId) {
        document.getElementById('noteId').value = note ? note.id : '';
        document.getElementById('noteEntityType').value = entityType;
        document.getElementById('noteEntityId').value = entityId;
        document.getElementById('noteTitle').value = note ? note.title || '' : '';
        document.getElementById('noteContent').value = note ? note.content : '';
        document.getElementById('notePinned').checked = note ? note.is_pinned : false;
        
        // Reset koloru
        document.querySelectorAll('#noteColors .note-color').forEach(b => b.classList.remove('active'));
        var colorBtn = document.querySelector('#noteColors .note-color[data-color="' + (note ? note.color : 'default') + '"]');
        if (colorBtn) colorBtn.classList.add('active');
        
        document.querySelector('#noteModal .modal-title').textContent = note ? 'Edytuj notatkę' : 'Nowa notatka';
        
        new bootstrap.Modal(document.getElementById('noteModal')).show();
    }
    
    function saveNote() {
        var noteId = document.getElementById('noteId').value;
        var entityType = document.getElementById('noteEntityType').value;
        var entityId = document.getElementById('noteEntityId').value;
        var content = document.getElementById('noteContent').value.trim();
        
        if (!content) {
            alert('Treść notatki jest wymagana');
            return;
        }
        
        var formData = new FormData();
        formData.append('entity_type', entityType);
        formData.append('entity_id', entityId);
        formData.append('title', document.getElementById('noteTitle').value);
        formData.append('content', content);
        formData.append('color', document.querySelector('#noteColors .note-color.active').dataset.color);
        formData.append('is_pinned', document.getElementById('notePinned').checked ? 1 : 0);
        
        if (noteId) {
            formData.append('id', noteId);
        }
        
        var url = noteId ? baseUrl + '/entity-notes/update' : baseUrl + '/entity-notes/add';
        
        fetch(url, { method: 'POST', body: formData })
            .then(r => r.json())
            .then(data => {
                if (data.success) {
                    bootstrap.Modal.getInstance(document.getElementById('noteModal')).hide();
                    // Odśwież notatki w odpowiednim kontenerze
                    var container = document.querySelector('.entity-notes-container[data-entity-type="' + entityType + '"][data-entity-id="' + entityId + '"]');
                    if (container) loadNotes(container, entityType, entityId);
                } else {
                    alert(data.error || 'Wystąpił błąd');
                }
            });
    }
    
    function togglePin(noteId, container) {
        var formData = new FormData();
        formData.append('id', noteId);
        
        fetch(baseUrl + '/entity-notes/toggle-pin', { method: 'POST', body: formData })
            .then(r => r.json())
            .then(data => {
                if (data.success) {
                    loadNotes(container, container.dataset.entityType, container.dataset.entityId);
                }
            });
    }
    
    function deleteNote(noteId, container) {
        if (!confirm('Usunąć tę notatkę?')) return;
        
        var formData = new FormData();
        formData.append('id', noteId);
        
        fetch(baseUrl + '/entity-notes/delete', { method: 'POST', body: formData })
            .then(r => r.json())
            .then(data => {
                if (data.success) {
                    loadNotes(container, container.dataset.entityType, container.dataset.entityId);
                } else {
                    alert(data.error || 'Wystąpił błąd');
                }
            });
    }
    
    function escapeHtml(text) {
        var div = document.createElement('div');
        div.textContent = text;
        return div.innerHTML;
    }
    
    function formatDate(dateStr) {
        var date = new Date(dateStr);
        return date.toLocaleDateString('pl-PL') + ' ' + date.toLocaleTimeString('pl-PL', {hour: '2-digit', minute: '2-digit'});
    }
});

<?php echo '</script'; ?>
>
<?php }
}
