File size: 2,741 Bytes
c70c429
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Shared utility functions
function formatDate(date) {
    const d = new Date(date);
    return d.toLocaleDateString('en-US', {
        year: 'numeric',
        month: 'short',
        day: 'numeric'
    });
}

function generateUniqueId() {
    return Date.now().toString(36) + Math.random().toString(36).substr(2);
}

function exportToExcel(data, fileName) {
    const ws = XLSX.utils.json_to_sheet(data);
    const wb = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
    XLSX.writeFile(wb, `${fileName}.xlsx`);
}

function shareToWhatsApp(data) {
    let message = "Stock Report:\n\n";
    data.forEach(item => {
        message += `${item.department}: ${item.quantity} ${item.size || ''}\n`;
    });
    const url = `https://wa.me/?text=${encodeURIComponent(message)}`;
    window.open(url, '_blank');
}

// Initialize tooltips
function initTooltips() {
    const tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
    tooltipTriggerList.map(function (tooltipTriggerEl) {
        return new bootstrap.Tooltip(tooltipTriggerEl);
    });
}

// Data validation
function validateNumberInput(input) {
    return !isNaN(parseFloat(input)) && isFinite(input) && parseFloat(input) > 0;
}

// LocalStorage operations
function backupToJSON() {
    const data = {
        production: JSON.parse(localStorage.getItem('productionData') || '[]'),
        stockOut: JSON.parse(localStorage.getItem('stockOutData') || '[]'),
        materials: JSON.parse(localStorage.getItem('materials') || '{}')
    };
    return JSON.stringify(data);
}

function restoreFromJSON(json) {
    try {
        const data = JSON.parse(json);
        if (data.production) localStorage.setItem('productionData', JSON.stringify(data.production));
        if (data.stockOut) localStorage.setItem('stockOutData', JSON.stringify(data.stockOut));
        if (data.materials) localStorage.setItem('materials', JSON.stringify(data.materials));
        return true;
    } catch (e) {
        console.error("Restore failed:", e);
        return false;
    }
}

// Initialize app when DOM is loaded
document.addEventListener('DOMContentLoaded', function() {
    initTooltips();
    
    // Toggle dark mode
    const darkModeToggle = document.getElementById('darkModeToggle');
    if (darkModeToggle) {
        darkModeToggle.addEventListener('click', function() {
            document.documentElement.classList.toggle('dark');
            localStorage.setItem('darkMode', document.documentElement.classList.contains('dark'));
        });
    }
    
    // Check for saved dark mode preference
    if (localStorage.getItem('darkMode') === 'true') {
        document.documentElement.classList.add('dark');
    }
});