evandro26/prayer-intentions-tracker
0
1document.addEventListener('DOMContentLoaded', function() {2 // Load saved intentions3 loadIntentions();4 5 // Form submission6 document.getElementById('intentionForm').addEventListener('submit', function(e) {7 e.preventDefault();8 9 const name = document.getElementById('name').value;10 const type = document.getElementById('type').value;11 const date = document.getElementById('date').value;12 const notes = document.getElementById('notes').value;13 14 if (!name || !type) {15 alert('Por favor, preencha pelo menos o nome e o tipo de intenção.');16 return;17 }18 19 const intention = {20 id: Date.now(),21 name,22 type,23 date,24 notes,25 createdAt: new Date().toISOString()26 };27 28 saveIntention(intention);29 renderIntention(intention);30 31 // Reset form32 this.reset();33 });34});35 36function saveIntention(intention) {37 let intentions = JSON.parse(localStorage.getItem('intentions') || '[]');38 intentions.push(intention);39 localStorage.setItem('intentions', JSON.stringify(intentions));40}41 42function loadIntentions() {43 const intentions = JSON.parse(localStorage.getItem('intentions') || '[]');44 const container = document.getElementById('intentionsContainer');45 container.innerHTML = '';46 47 if (intentions.length === 0) {48 container.innerHTML = '<p class="text-gray-500">Nenhuma intenção registrada ainda.</p>';49 return;50 }51 52 // Group by type53 const grouped = {54 falecimento: [],55 nascimento: [],56 matrimonio: [],57 saude: [],58 gracas: []59 };60 61 intentions.forEach(intention => {62 grouped[intention.type].push(intention);63 });64 65 // Render each group66 for (const type in grouped) {67 if (grouped[type].length > 0) {68 renderIntentionGroup(type, grouped[type]);69 }70 }71 72 // Add print button if there are intentions73 if (intentions.length > 0) {74 const printBtn = document.createElement('button');75 printBtn.className = 'mt-6 inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-primary hover:bg-primary-dark';76printBtn.innerHTML = '<i data-feather="printer" class="mr-2"></i> Imprimir Todas as Intenções';77 printBtn.onclick = preparePrintModal;78 document.getElementById('intentionsContainer').appendChild(printBtn);79 feather.replace();80 }81}82 83function renderIntentionGroup(type, intentions) {84 const container = document.getElementById('intentionsContainer');85 const typeNames = {86 falecimento: 'Falecimento',87 nascimento: 'Aniversário de Nascimento',88 matrimonio: 'Aniversário de Matrimônio',89 saude: 'Recuperação da Saúde',90 gracas: 'Ação de Graças'91 };92 93 const groupDiv = document.createElement('div');94 groupDiv.className = 'bg-white rounded-lg shadow-md overflow-hidden';95 96 // Group header97 const header = document.createElement('div');98 header.className = 'bg-indigo-100 px-6 py-3 flex justify-between items-center';99 header.innerHTML = `100 <h3 class="text-lg font-medium text-indigo-800">${typeNames[type]}</h3>101 <button onclick="preparePrintModal('${type}')" class="text-indigo-600 hover:text-indigo-800 flex items-center text-sm">102 <i data-feather="printer" class="w-4 h-4 mr-1"></i> Imprimir103 </button>104 `;105 106 // Intentions list107 const list = document.createElement('div');108 list.className = 'divide-y divide-gray-200';109 110 intentions.forEach(intention => {111 const item = document.createElement('div');112 item.className = 'p-6 intention-card';113 item.innerHTML = `114 <div class="flex justify-between items-start">115 <div>116 <h4 class="text-lg font-medium text-gray-900">${intention.name}</h4>117 ${intention.date ? `<p class="text-sm text-gray-500 mt-1">Data: ${new Date(intention.date).toLocaleDateString('pt-BR')}</p>` : ''}118 ${intention.notes ? `<p class="text-sm text-gray-700 mt-2">${intention.notes}</p>` : ''}119 </div>120 <button onclick="deleteIntention(${intention.id})" class="text-red-600 hover:text-red-800">121 <i data-feather="trash-2"></i>122 </button>123</div>124 `;125 list.appendChild(item);126 });127 128 groupDiv.appendChild(header);129 groupDiv.appendChild(list);130 container.appendChild(groupDiv);131 feather.replace();132}133 134function renderIntention(intention) {135 const container = document.getElementById('intentionsContainer');136 137 // Check if there's a "no intentions" message138 if (container.firstChild && container.firstChild.textContent === 'Nenhuma intenção registrada ainda.') {139 container.innerHTML = '';140 }141 142 // Find the group for this intention type143 let groupDiv = Array.from(container.children).find(el => {144 return el.querySelector('h3')?.textContent === getTypeName(intention.type);145 });146 147 // If group doesn't exist, create it148 if (!groupDiv) {149 renderIntentionGroup(intention.type, []);150 groupDiv = Array.from(container.children).find(el => {151 return el.querySelector('h3')?.textContent === getTypeName(intention.type);152 });153 }154 155 // Add the new intention to the group156 const list = groupDiv.querySelector('.divide-y');157 const item = document.createElement('div');158 item.className = 'p-6 intention-card';159 item.innerHTML = `160 <div class="flex justify-between items-start">161 <div>162 <h4 class="text-lg font-medium text-gray-900">${intention.name}</h4>163 ${intention.date ? `<p class="text-sm text-gray-500 mt-1">Data: ${new Date(intention.date).toLocaleDateString('pt-BR')}</p>` : ''}164 ${intention.notes ? `<p class="text-sm text-gray-700 mt-2">${intention.notes}</p>` : ''}165 </div>166 <button onclick="deleteIntention(${intention.id})" class="text-red-500 hover:text-red-700">167 <i data-feather="trash-2"></i>168 </button>169 </div>170 `;171 list.appendChild(item);172 feather.replace();173 174 // Add print button if it's the first intention175 if (container.querySelectorAll('button[onclick^="preparePrintModal"]').length === 0) {176 const printBtn = document.createElement('button');177 printBtn.className = 'mt-6 inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-primary hover:bg-primary-dark';178printBtn.innerHTML = '<i data-feather="printer" class="mr-2"></i> Imprimir Todas as Intenções';179 printBtn.onclick = preparePrintModal;180 container.appendChild(printBtn);181 feather.replace();182 }183}184 185function getTypeName(type) {186 const typeNames = {187 falecimento: 'Falecimento',188 nascimento: 'Aniversário de Nascimento',189 matrimonio: 'Aniversário de Matrimônio',190 saude: 'Recuperação da Saúde',191 gracas: 'Ação de Graças'192 };193 return typeNames[type];194}195 196function deleteIntention(id) {197 if (!confirm('Tem certeza que deseja remover esta intenção?')) return;198 199 let intentions = JSON.parse(localStorage.getItem('intentions') || '[]');200 intentions = intentions.filter(i => i.id !== id);201 localStorage.setItem('intentions', JSON.stringify(intentions));202 loadIntentions();203}204 205function preparePrintModal(type = null) {206 const modal = document.getElementById('printModal');207 const content = document.getElementById('printContent');208 content.innerHTML = '';209 210 const intentions = JSON.parse(localStorage.getItem('intentions') || '[]');211 const filtered = type ? intentions.filter(i => i.type === type) : intentions;212 213 if (filtered.length === 0) {214 content.innerHTML = '<p class="text-gray-500">Nenhuma intenção para imprimir.</p>';215 modal.classList.remove('hidden');216 return;217 }218 219 // Group by type220 const grouped = {};221 filtered.forEach(intention => {222 if (!grouped[intention.type]) {223 grouped[intention.type] = [];224 }225 grouped[intention.type].push(intention);226 });227 228 // Create print content229 for (const type in grouped) {230 const section = document.createElement('div');231 section.className = 'print-section';232 233 const header = document.createElement('h3');234 header.className = 'text-xl font-bold mb-4 border-b pb-2';235 header.textContent = getTypeName(type);236 237 const list = document.createElement('ul');238 list.className = 'space-y-3';239 240 grouped[type].forEach(intention => {241 const item = document.createElement('li');242 item.className = 'text-gray-800';243 244 let content = `<strong>${intention.name}</strong>`;245 if (intention.date) {246 content += ` - ${new Date(intention.date).toLocaleDateString('pt-BR')}`;247 }248 if (intention.notes) {249 content += `<div class="text-sm text-gray-600 ml-4">${intention.notes}</div>`;250 }251 252 item.innerHTML = content;253 list.appendChild(item);254 });255 256 section.appendChild(header);257 section.appendChild(list);258 content.appendChild(section);259 }260 261 modal.classList.remove('hidden');262}263 264function closePrintModal() {265 document.getElementById('printModal').classList.add('hidden');266}