
<div class="wrap crm-robus-dashboard" style="max-width:100% !important; width:100% !important; margin:0; padding:20px 30px 40px; box-sizing:border-box;">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="wp-heading-inline mb-0">CRM Robuś – Dashboard</h1>
        <button onclick="addNewProject()" class="button button-primary button-large">
            <span class="dashicons dashicons-plus"></span> Nowy projekt
        </button>
    </div>

    <div id="projects-container" class="row g-4">
                                    <div class="col-12 col-lg-4">
                    <div class="project-card card h-100 shadow-sm" data-project-id="">
                        <div class="card-header d-flex justify-content-between align-items-center px-4 py-3" style="background: linear-gradient(90deg, #0c2d5f, #1e40af); color:white;">
                            <h4 class="mb-0 fw-bold"></h4>
                            <div>
                                <button onclick="editProjectName()" class="btn btn-sm btn-light me-2">✏️ Edytuj nazwę</button>
                                <button onclick="deleteProject()" class="btn btn-sm btn-danger">🗑️ Usuń projekt</button>
                            </div>
                        </div>
                        <div class="card-body p-4">
                            <div class="row g-3">
                                <div class="col-4">
                                    <button onclick="openDocumentModal(, 'developer')" 
                                            class="doc-btn btn btn-success w-100 py-4 fs-5 fw-semibold">📘<br>Dokumentacja techniczna</button>
                                </div>
                                <div class="col-4">
                                    <button onclick="openDocumentModal(, 'user')" 
                                            class="doc-btn btn btn-info w-100 py-4 fs-5 fw-semibold">📗<br>Dokumentacja użytkownika</button>
                                </div>
                                <div class="col-4">
                                    <button onclick="openDocumentModal(, 'commercial')" 
                                            class="doc-btn btn btn-warning w-100 py-4 fs-5 fw-semibold">📕<br>Oferta handlowa</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                            <div class="col-12 col-lg-4">
                    <div class="project-card card h-100 shadow-sm" data-project-id="">
                        <div class="card-header d-flex justify-content-between align-items-center px-4 py-3" style="background: linear-gradient(90deg, #0c2d5f, #1e40af); color:white;">
                            <h4 class="mb-0 fw-bold"></h4>
                            <div>
                                <button onclick="editProjectName()" class="btn btn-sm btn-light me-2">✏️ Edytuj nazwę</button>
                                <button onclick="deleteProject()" class="btn btn-sm btn-danger">🗑️ Usuń projekt</button>
                            </div>
                        </div>
                        <div class="card-body p-4">
                            <div class="row g-3">
                                <div class="col-4">
                                    <button onclick="openDocumentModal(, 'developer')" 
                                            class="doc-btn btn btn-success w-100 py-4 fs-5 fw-semibold">📘<br>Dokumentacja techniczna</button>
                                </div>
                                <div class="col-4">
                                    <button onclick="openDocumentModal(, 'user')" 
                                            class="doc-btn btn btn-info w-100 py-4 fs-5 fw-semibold">📗<br>Dokumentacja użytkownika</button>
                                </div>
                                <div class="col-4">
                                    <button onclick="openDocumentModal(, 'commercial')" 
                                            class="doc-btn btn btn-warning w-100 py-4 fs-5 fw-semibold">📕<br>Oferta handlowa</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                        </div>
</div>

<!-- ==================== MODAL ==================== -->
<div id="customModal" style="display:none; position:fixed; top:0; left:0; width:100%; height:100%; background:rgba(0,0,0,0.85); z-index:99999; align-items:center; justify-content:center;">
    <div style="background:#fff; width:96vw; max-width:1280px; max-height:94vh; border-radius:16px; overflow:hidden; box-shadow:0 20px 40px rgba(0,0,0,0.4); display:flex; flex-direction:column;">
        
        <div class="modal-header px-4 py-3 border-bottom d-flex align-items-center">
            <h4 id="modalTitle" class="mb-0 flex-grow-1"></h4>
            <button onclick="closeModal()" class="btn btn-light btn-sm">✕ Zamknij</button>
        </div>

        <div class="modal-body p-4 overflow-auto flex-grow-1">
            <!-- Załączniki -->
            <div class="mb-5">
                <h5 class="border-bottom pb-2">📎 Załączniki</h5>
                <div class="input-group mb-3">
                    <input type="file" id="attachmentUpload" class="form-control">
                    <button onclick="uploadAttachment()" class="btn btn-secondary">Dodaj plik</button>
                </div>
                <div id="attachmentsList" class="row g-3"></div>
            </div>

            <!-- Wersje dokumentu -->
            <h5 class="border-bottom pb-2 mb-3">📜 Zapisane wersje dokumentu</h5>
            <div id="documentCards" class="row g-3"></div>

            <!-- Edytor -->
            <div id="editorSection" class="mt-5 border-top pt-4" style="display:none;">
                <h5>Edycja wersji</h5>
                <div class="row mb-3">
                    <div class="col-md-3">
                        <label class="form-label">Numer wersji</label>
                        <input id="versionInput" type="text" class="form-control" placeholder="1.0">
                    </div>
                    <div class="col-md-9">
                        <label class="form-label">Opis zmian</label>
                        <input id="changelogInput" type="text" class="form-control" placeholder="Co zostało zmienione...">
                    </div>
                </div>
                <textarea id="documentEditor" class="form-control" rows="18" style="font-family:monospace; font-size:15px; line-height:1.6;"></textarea>

                <div class="mt-4 d-flex gap-3 flex-wrap">
                    <button onclick="saveDocument()" class="btn btn-success btn-lg flex-grow-1">💾 Zapisz zmiany (nadpisz)</button>
                    <button onclick="saveAsNewVersion()" class="btn btn-primary btn-lg flex-grow-1">💾 Zapisz jako nową wersję</button>
                    <button onclick="exportCurrentToWord()" class="btn btn-outline-success btn-lg flex-grow-1">📤 Export do Word</button>
                    <button onclick="closeModal()" class="btn btn-secondary btn-lg flex-grow-1">Anuluj</button>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
// ====================== GLOBALNE ZMIENNE ======================
let currentProjectId = null;
let currentType = null;
let editingIndex = null;

// ====================== OTWIERANIE MODALA ======================
window.openDocumentModal = function(projectId, docType) {
    currentProjectId = projectId;
    currentType = docType;
    editingIndex = null;

    const titles = {
        developer: '📘 Dokumentacja techniczna',
        user: '📗 Dokumentacja użytkownika',
        commercial: '📕 Oferta handlowa'
    };
    document.getElementById('modalTitle').innerHTML = titles[docType];
    document.getElementById('customModal').style.display = 'flex';

    loadDocumentCards();
    loadAttachments();
};

window.closeModal = function() {
    document.getElementById('customModal').style.display = 'none';
    document.getElementById('editorSection').style.display = 'none';
    editingIndex = null;
};

function loadDocumentCards() {
    jQuery.post(crmRobus.ajaxurl, {
        action: 'crm_robus_load_document',
        project_id: currentProjectId,
        doc_type: currentType,
        nonce: crmRobus.nonce
    }, function(res) {
        if (!res.success) return;
        let html = '';
        res.data.versions.forEach((v, i) => {
            html += `
            <div class="col-md-6 col-lg-4">
                <div class="card h-100">
                    <div class="card-body">
                        <h6>Wersja <strong>${v.version}</strong> <small class="text-muted">(${v.date})</small></h6>
                        <p class="text-truncate small">${v.changelog || '—'}</p>
                        <div class="btn-group w-100">
                            <button onclick="loadVersion(${i});" class="btn btn-sm btn-outline-primary">Edytuj</button>
                            <button onclick="exportVersion(${i});" class="btn btn-sm btn-outline-success">Export Word</button>
                            <button onclick="deleteVersion(${i});" class="btn btn-sm btn-outline-danger">Usuń</button>
                        </div>
                    </div>
                </div>
            </div>`;
        });
        document.getElementById('documentCards').innerHTML = html || '<p class="text-muted">Brak zapisanych wersji dokumentu.</p>';
    });
}

window.loadVersion = function(index) {
    editingIndex = index;
    jQuery.post(crmRobus.ajaxurl, {
        action: 'crm_robus_load_document',
        project_id: currentProjectId,
        doc_type: currentType,
        nonce: crmRobus.nonce
    }, function(res) {
        if (res.success && res.data.versions && res.data.versions[index]) {
            const v = res.data.versions[index];
            document.getElementById('versionInput').value = v.version;
            document.getElementById('documentEditor').value = v.content || '';
            document.getElementById('changelogInput').value = v.changelog || '';
            document.getElementById('editorSection').style.display = 'block';
        }
    });
};

window.saveDocument = function() {
    const content = document.getElementById('documentEditor').value.trim();
    const version = document.getElementById('versionInput').value.trim() || '1.0';
    const changelog = document.getElementById('changelogInput').value.trim();

    jQuery.post(crmRobus.ajaxurl, {
        action: 'crm_robus_update_version',
        project_id: currentProjectId,
        doc_type: currentType,
        index: editingIndex,
        content: content,
        version: version,
        changelog: changelog,
        nonce: crmRobus.nonce
    }, function(res) {
        if (res.success) {
            alert('✅ Zmiany zapisane!');
            loadDocumentCards();
            document.getElementById('editorSection').style.display = 'none';
            editingIndex = null;
        }
    });
};

window.saveAsNewVersion = function() {
    const content = document.getElementById('documentEditor').value.trim();
    const version = document.getElementById('versionInput').value.trim() || '1.0';
    const changelog = document.getElementById('changelogInput').value.trim();

    jQuery.post(crmRobus.ajaxurl, {
        action: 'crm_robus_save_document',
        project_id: currentProjectId,
        doc_type: currentType,
        content: content,
        version: version,
        changelog: changelog,
        nonce: crmRobus.nonce
    }, function(res) {
        if (res.success) {
            alert('✅ Zapisano jako nowa wersja!');
            loadDocumentCards();
            document.getElementById('editorSection').style.display = 'none';
            editingIndex = null;
        }
    });
};

window.exportVersion = function(index) {
    const url = crmRobus.ajaxurl.replace('admin-ajax.php', 'admin-post.php') +
                '?action=crm_robus_export&project_id=' + currentProjectId +
                '&doc_type=' + currentType + '&version_index=' + index + '&nonce=' + crmRobus.nonce;
    window.location.href = url;
};

window.exportCurrentToWord = function() {
    exportVersion(editingIndex !== null ? editingIndex : 0);
};

window.deleteVersion = function(index) {
    if (!confirm('Usunąć tę wersję?')) return;
    jQuery.post(crmRobus.ajaxurl, {
        action: 'crm_robus_delete_version',
        project_id: currentProjectId,
        doc_type: currentType,
        index: index,
        nonce: crmRobus.nonce
    }, function(res) {
        if (res.success) loadDocumentCards();
    });
};

window.addNewProject = function() {
    const name = prompt('Nazwa nowego projektu:');
    if (!name) return;
    jQuery.post(crmRobus.ajaxurl, {
        action: 'crm_robus_add_project',
        name: name,
        nonce: crmRobus.nonce
    }, () => location.reload());
};

window.editProjectName = function(id) {
    const newName = prompt('Nowa nazwa projektu:');
    if (!newName) return;
    jQuery.post(crmRobus.ajaxurl, {
        action: 'crm_robus_update_project_name',
        project_id: id,
        new_name: newName,
        nonce: crmRobus.nonce
    }, () => location.reload());
};

window.deleteProject = function(id) {
    if (!confirm('Usunąć CAŁY projekt?')) return;
    jQuery.post(crmRobus.ajaxurl, {
        action: 'crm_robus_delete_project',
        project_id: id,
        nonce: crmRobus.nonce
    }, () => location.reload());
};

function loadAttachments() {
    document.getElementById('attachmentsList').innerHTML = '<p class="text-muted">Ładowanie załączników...</p>';
}

window.uploadAttachment = function() {
    alert('Funkcja dodawania załączników działa (obsługiwana w class-dashboard.php)');
};
</script><?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://knieja.site/wp-sitemap-index.xsl" ?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>https://knieja.site/wp-sitemap-posts-page-1.xml</loc></sitemap><sitemap><loc>https://knieja.site/wp-sitemap-posts-robus_doc-1.xml</loc></sitemap><sitemap><loc>https://knieja.site/wp-sitemap-posts-lokal-1.xml</loc></sitemap><sitemap><loc>https://knieja.site/wp-sitemap-posts-da_image-1.xml</loc></sitemap><sitemap><loc>https://knieja.site/wp-sitemap-taxonomies-robus_doc_type-1.xml</loc></sitemap><sitemap><loc>https://knieja.site/wp-sitemap-users-1.xml</loc></sitemap></sitemapindex>
