/**
* CSV Manager UI
* User interface for CRUD operations on CSV files
*/
import {
loadCSV,
toCSV,
downloadCSV,
getAvailableCSVFiles,
validateCSVData
} from '../utils/csv-manager.js';
// Current state
let currentCSVData = [];
let currentFilename = '';
let currentFileId = '';
let editingIndex = -1;
/**
* Open CSV Manager Modal
*/
export function openCSVManager() {
const modal = document.getElementById('csvManagerModal');
const select = document.getElementById('csvFileSelect');
// Populate file selector
const files = getAvailableCSVFiles();
select.innerHTML = '';
files.forEach(file => {
const option = document.createElement('option');
option.value = file.id;
option.textContent = `${file.icon} ${file.name} - ${file.description}`;
select.appendChild(option);
});
modal.classList.add('active');
}
/**
* Close CSV Manager Modal
*/
export function closeCSVManager() {
const modal = document.getElementById('csvManagerModal');
modal.classList.remove('active');
// Reset state
currentCSVData = [];
currentFilename = '';
currentFileId = '';
// Reset UI
document.getElementById('csvFileSelect').value = '';
document.getElementById('csvContent').innerHTML = `
📁
Selecione um arquivo CSV para começar
Você poderá visualizar, editar, adicionar e remover registros
`;
// Hide buttons
document.getElementById('btnAddRecord').style.display = 'none';
document.getElementById('btnDownload').style.display = 'none';
}
/**
* Load selected CSV file
*/
export async function loadSelectedCSV() {
const select = document.getElementById('csvFileSelect');
const fileId = select.value;
if (!fileId) {
closeCSVManager();
openCSVManager();
return;
}
const files = getAvailableCSVFiles();
const file = files.find(f => f.id === fileId);
if (!file) return;
// Show loading
document.getElementById('csvContent').innerHTML = `
⏳
Carregando ${file.name}...
`;
try {
// Load CSV
const data = await loadCSV(file.filename);
// Update state
currentCSVData = data;
currentFilename = file.filename;
currentFileId = fileId;
// Render table
renderCSVTable(data, file);
// Show buttons
document.getElementById('btnAddRecord').style.display = 'inline-block';
document.getElementById('btnDownload').style.display = 'inline-block';
} catch (error) {
document.getElementById('csvContent').innerHTML = `
❌
Erro ao carregar arquivo
${error.message}
`;
}
}
/**
* Render CSV data as table
* @param {Array