Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ // Dark mode toggle
+ const darkModeToggle = document.getElementById('darkModeToggle');
+ const prefersDark = window.matchMedia('(prefers-color-scheme: dark)');
+
+ function setTheme(isDark) {
+ document.documentElement.setAttribute('data-theme', isDark ? 'dark' : 'light');
+ localStorage.setItem('theme', isDark ? 'dark' : 'light');
+ }
+
+ // Initialize theme
+ const savedTheme = localStorage.getItem('theme');
+ if (savedTheme) {
+ setTheme(savedTheme === 'dark');
+ } else {
+ setTheme(prefersDark.matches);
+ }
+
+ darkModeToggle.addEventListener('click', () => {
+ const isDark = document.documentElement.getAttribute('data-theme') === 'dark';
+ setTheme(!isDark);
+ });
+
+ // Batch modal
+ const batchModal = document.getElementById('batchModal');
+ const addBatchBtn = document.getElementById('addBatch');
+ const cancelBatchBtn = document.getElementById('cancelBatch');
+ const batchForm = document.getElementById('batchForm');
+
+ addBatchBtn.addEventListener('click', () => {
+ batchModal.showModal();
+ });
+
+ cancelBatchBtn.addEventListener('click', () => {
+ batchModal.close();
+ batchForm.reset();
+ });
+
+ // Sample data management
+ let batches = JSON.parse(localStorage.getItem('batches')) || [];
+
+ function generateBatchId() {
+ return 'B' + Date.now().toString().slice(-6);
+ }
+
+ function updateTable() {
+ const tbody = document.getElementById('batchTableBody');
+ tbody.innerHTML = '';
+
+ batches.forEach(batch => {
+ const tr = document.createElement('tr');
+ tr.innerHTML = `
+
${batch.id} | +
${batch.model} | +
${batch.quantity} | +
${new Date(batch.startDate).toLocaleDateString()} | +
${batch.status} | + `;
+ tbody.appendChild(tr);
+ });
+
+ updateStats();
+ }
+
+ function updateStats() {
+ const today = new Date().toDateString();
+ const todayCount = batches.filter(b =>
+ new Date(b.startDate).toDateString() === today
+ ).length;
+
+ const weekAgo = new Date();
+ weekAgo.setDate(weekAgo.getDate() - 7);
+ const weeklyCount = batches.filter(b =>
+ new Date(b.startDate) >= weekAgo
+ ).length;
+
+ const completed = batches.filter(b => b.status === 'Completed').length;
+ const completionRate = batches.length ?
+ Math.round((completed / batches.length) * 100) : 0;
+
+ document.getElementById('todayCount').textContent = todayCount;
+ document.getElementById('weeklyCount').textContent = weeklyCount;
+ document.getElementById('completionRate').textContent = `${completionRate}%`;
+ }
+
+ batchForm.addEventListener('submit', (e) => {
+ e.preventDefault();
+
+ const newBatch = {
+ id: generateBatchId(),
+ model: document.getElementById('modelNumber').value,
+ quantity: document.getElementById('quantity').value,
+ startDate: new Date().toISOString(),
+ status: 'Pending'
+ };
+
+ batches.unshift(newBatch);
+ localStorage.setItem('batches', JSON.stringify(batches));
+
+ updateTable();
+ batchModal.close();
+ batchForm.reset();
+ });
+
+ // Initial render
+ updateTable();
+
+ // Expose updateStatus to global scope
+ window.updateStatus = (batchId) => {
+ const batch = batches.find(b => b.id === batchId);
+ if (batch) {
+ const statuses = ['Pending', 'In Progress', 'Completed'];
+ const currentIndex = statuses.indexOf(batch.status);
+ batch.status = statuses[(currentIndex + 1) % statuses.length];
+
+ localStorage.setItem('batches', JSON.stringify(batches));
+ updateTable();
+ }
+ };
+ });