AuditSpace

Sign in to your account

วางแผนงาน (Audit Calendar)

Auto-saved

สาขาทั้งหมด

0

ยังไม่วางแผน

0

ตรวจตามรอบ

0

ตรวจประเภทอื่น

0

แผนงานที่บันทึกแล้ว

ประจำเดือนนี้

0
`; const blob = new Blob([html], { type: 'application/vnd.ms-excel;charset=utf-8;' }); const url = URL.createObjectURL(blob); const link = document.createElement("a"); link.setAttribute("href", url); const shortMonths = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']; link.setAttribute("download", `AuditPlan_Calendar_${shortMonths[calMonth]}_${calYear}.xls`); document.body.appendChild(link); link.click(); document.body.removeChild(link); } // --- HISTORY LOGIC --- function renderHistoryTable() { const tbody = document.getElementById('history-list-body'); tbody.innerHTML = ''; const visiblePlans = (currentUser && currentUser.role === 'auditor') ? plans.filter(p => p.auditorId === currentUser.id) : plans; const historyMap = {}; // Seed with all monthStatuses (support legacy YYYY-MM by ignoring or converting) Object.keys(monthStatuses).forEach(k => { if (k.includes('_')) { historyMap[k] = { plans: [] }; } }); visiblePlans.forEach(p => { if(p.date && p.auditorId) { const [y, m] = p.date.split('-'); const k = `${y}-${m}_${p.auditorId}`; if (!historyMap[k]) historyMap[k] = { plans: [] }; historyMap[k].plans.push(p); } }); let historyKeys = Object.keys(historyMap); if (currentUser && currentUser.role === 'auditor') { historyKeys = historyKeys.filter(k => k.endsWith(`_${currentUser.id}`)); } // Sort descending by date, then auditor historyKeys.sort((a,b) => b.localeCompare(a)); const monthsNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; if (historyKeys.length === 0) { tbody.innerHTML = `ยังไม่มีประวัติแผนงาน`; return; } historyKeys.forEach(key => { const parts = key.split('_'); const [y, m] = parts[0].split('-'); const auditorId = parts[1]; const monthName = monthsNames[parseInt(m) - 1]; const displayTitle = `${monthName} ${y}`; const status = monthStatuses[key] || 'draft'; let statusHtml = ''; if(status === 'draft') statusHtml = `กำลังจัดทำ`; else if(status === 'pending') statusHtml = `รออนุมัติ`; else if(status === 'approved') statusHtml = `อนุมัติแล้ว`; else if(status === 'edit_requested') statusHtml = `ขอแก้ไขแผนงาน`; const count = historyMap[key].plans.length; const u = dbUsers.find(x => x.id == auditorId); const auditorName = u ? u.name : 'Unknown'; tbody.innerHTML += ` ${displayTitle} ${auditorName} ${count} แผน ${statusHtml} `; }); } async function clearMonthPlan(monthKey) { if (!confirm('คุณต้องการยกเลิกและลบแผนงานทั้งหมดในเดือนนี้ใช่หรือไม่?\n(การกระทำนี้ไม่สามารถย้อนกลับได้)')) return; try { await fetch(API_BASE_URL + '/api/plans?monthKey=' + monthKey, { method: 'DELETE' }); await loadData(); addNotification('ยกเลิกแผนงานเรียบร้อยแล้ว', 'success'); renderHistoryTable(); renderCalendar(); } catch (e) { console.error(e); alert('เกิดข้อผิดพลาดในการลบแผนงาน'); } } let isHistoryMode = false; let currentHistoryMonthKey = ''; let currentCalendarAuditorId = null; function viewHistoryMonth(year, monthIndex, auditorId) { isHistoryMode = true; calYear = parseInt(year); calMonth = parseInt(monthIndex); currentCalendarAuditorId = auditorId; renderCalendar(); // Move calendar to modal const calNode = document.getElementById('view-calendar'); const modalContainer = document.getElementById('modal-view-calendar-content'); modalContainer.appendChild(calNode); calNode.style.display = 'flex'; // Hide KPIs inside the modal (optional, based on request "see only calendar") const kpiSection = document.getElementById('kpi-section'); if(kpiSection) kpiSection.style.display = 'none'; // Show modal const modal = document.getElementById('modal-view-calendar'); modal.classList.remove('hidden'); setTimeout(() => { modal.classList.remove('opacity-0'); modalContainer.classList.remove('scale-95'); }, 10); } function closeHistoryModal() { const modal = document.getElementById('modal-view-calendar'); const modalContainer = document.getElementById('modal-view-calendar-content'); modal.classList.add('opacity-0'); modalContainer.classList.add('scale-95'); setTimeout(() => { modal.classList.add('hidden'); // Move calendar back to main const calNode = document.getElementById('view-calendar'); document.querySelector('main').appendChild(calNode); calNode.style.display = 'none'; // Restore KPIs const kpiSection = document.getElementById('kpi-section'); if(kpiSection) kpiSection.style.display = 'grid'; isHistoryMode = false; currentCalendarAuditorId = null; renderCalendar(); // re-render back to standard view }, 300); } // Close notifications dropdown and custom dropdowns when clicking outside document.addEventListener('click', function(event) { const notifDropdown = document.getElementById('notif-dropdown'); const bellBtn = document.getElementById('btn-bell'); if (notifDropdown && !notifDropdown.classList.contains('hidden')) { if (!notifDropdown.contains(event.target) && (!bellBtn || !bellBtn.contains(event.target))) { notifDropdown.classList.add('hidden'); } } const customYearDropdown = document.getElementById('custom-year-dropdown'); const customYearList = document.getElementById('custom-year-list'); if (customYearDropdown && customYearList && !customYearList.classList.contains('hidden')) { if (!customYearDropdown.contains(event.target)) { customYearList.classList.add('hidden'); } } }); function toggleSidebar() { const sidebar = document.getElementById('sidebar'); const overlay = document.getElementById('mobile-overlay'); if (sidebar.classList.contains('-translate-x-full')) { sidebar.classList.remove('-translate-x-full'); overlay.classList.remove('hidden'); } else { sidebar.classList.add('-translate-x-full'); overlay.classList.add('hidden'); } } window.onload = init;