File size: 6,456 Bytes
da273f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
957b832
da273f4
 
 
 
957b832
da273f4
957b832
da273f4
 
 
 
957b832
da273f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
957b832
 
 
 
 
 
 
 
 
 
da273f4
 
 
957b832
da273f4
 
 
 
 
 
957b832
 
da273f4
 
 
 
957b832
 
da273f4
 
 
 
 
 
957b832
 
 
 
da273f4
 
957b832
 
 
 
 
 
 
 
 
da273f4
 
 
957b832
 
 
 
 
 
 
 
 
da273f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Initialize documents array
let documents = [];
let isAuthenticated = false;
let editingDocumentId = null;
let selectedFile = null;
// Sample data from PDF
const pdfData = [
    {
        "number": "1",
        "title": "Политика в области качества и пищевой безопасности АО «ПРОГРЕСС»",
        "code": "",
        "approvalDate": "2025-08-04",
        "scope": "Все подразделения АО \"ПРОГРЕСС\", г. Липецк",
        "url": "",
        "location": ""
    }
];
// Initialize PDF data if localStorage is empty
function initializePdfData() {
    const savedDocuments = localStorage.getItem('documents');
    if (!savedDocuments && pdfData.length > 0) {
        documents = pdfData.map((doc, index) => ({
            id: Date.now() + index,  // Unique ID based on timestamp
            ...doc
        }));
        saveToLocalStorage();
        showNotification('Данные из PDF успешно загружены!');
        renderDocuments();
    }
}
// Calculate update date (3 years from approval)
function calculateUpdateDate(approvalDate) {
    const date = new Date(approvalDate);
    date.setFullYear(date.getFullYear() + 3);
    return date;
}

// Get document status based on approval date
function getDocumentStatus(approvalDate) {
    const updateDate = calculateUpdateDate(approvalDate);
    const today = new Date();
    const timeDiff = updateDate.getTime() - today.getTime();
    const daysDiff = Math.ceil(timeDiff / (1000 * 3600 * 24));
    const monthsDiff = Math.floor(daysDiff / 30);
    
    if (daysDiff <= 0) {
        return { status: 'expired', days: daysDiff, months: monthsDiff };
    } else if (monthsDiff <= 3) {
        return { status: 'danger', days: daysDiff, months: monthsDiff };
    } else if (monthsDiff <= 6) {
        return { status: 'warning', days: daysDiff, months: monthsDiff };
    } else {
        return { status: 'normal', days: daysDiff, months: monthsDiff };
    }
}

// Get status text and class
function getStatusText(statusInfo) {
    switch (statusInfo.status) {
        case 'expired':
            return { 
                text: 'ПРОСРОЧЕН', 
                class: 'expired', 
                fullText: 'Документ требует срочной актуализации! Просрочен.' 
            };
        case 'danger':
            return { 
                text: `${statusInfo.months} мес.`, 
                class: 'danger', 
                fullText: `Срочная актуализация! Осталось ${statusInfo.months} месяцев (${statusInfo.days} дней)` 
            };
        case 'warning':
            return { 
                text: `${statusInfo.months} мес.`, 
                class: 'warning', 
                fullText: `Требуется актуализация в ближайшее время. Осталось ${statusInfo.months} месяцев` 
            };
        default:
            return { 
                text: `Актуален`, 
                class: 'normal', 
                fullText: `Документ актуален. Актуализация через ${statusInfo.months} месяцев` 
            };
    }
}

// Save to localStorage
function saveToLocalStorage() {
    localStorage.setItem('documents', JSON.stringify(documents));
}
// Load from localStorage
function loadFromLocalStorage() {
    try {
        const savedDocuments = localStorage.getItem('documents');
        if (savedDocuments) {
            documents = JSON.parse(savedDocuments);
        } else {
            documents = [];
            saveToLocalStorage();
        }
    } catch (e) {
        console.error('Error loading documents:', e);
        documents = [];
        saveToLocalStorage();
    }
    renderDocuments();
}
// Render documents list
function renderDocuments() {
    const container = document.getElementById('documentsContainer');
    const emptyState = document.getElementById('emptyState');
    
    if (!container || !emptyState) return;
    
    // Clear existing documents (keep header and empty state)
    const header = container.querySelector('.grid.bg-gray-800');
    container.innerHTML = '';
    if (header) container.appendChild(header);
    if (emptyState) container.appendChild(emptyState);
    if (!documents || documents.length === 0) {
        emptyState.classList.remove('hidden');
        return;
    }
    
    emptyState.classList.add('hidden');
    
    if (!Array.isArray(documents)) {
        console.error('Documents is not an array');
        return;
    }
    // Sort documents by status priority
    const sortedDocuments = [...documents].sort((a, b) => {
        try {
            const statusA = getDocumentStatus(a.approvalDate);
            const statusB = getDocumentStatus(b.approvalDate);
            const priority = { 'expired': 0, 'danger': 1, 'warning': 2, 'normal': 3 };
            return priority[statusA.status] - priority[statusB.status];
        } catch (e) {
            console.error('Error sorting documents:', e);
            return 0;
        }
    });
    // Render each document
    sortedDocuments.forEach(doc => {
        try {
            if (!doc.approvalDate) {
                console.warn('Document missing approvalDate:', doc);
                return;
            }
            
            const statusInfo = getDocumentStatus(doc.approvalDate);
            const statusText = getStatusText(statusInfo);
const row = document.createElement('div');
        row.className = 'document-row';
        row.innerHTML = `
            <div class="col-span-1 font-medium">${doc.number}</div>
            <div class="col-span-5">${doc.title}</div>
            <div class="col-span-2 text-gray-600">${doc.code || '-'}</div>
            <div class="col-span-2">${doc.approvalDate}</div>
            <div class="col-span-2">
                <span class="status-indicator ${statusText.class}"></span>
                <span class="text-sm">${statusText.text}</span>
            </div>
            
            <div class="document-details">
                <div class="mb-2"><span class="font-medium">Область действия:</span> ${doc.scope}</div>
                <div class="mb-2"><span class="font-medium">Место хранения:</span> ${doc.location || 'Не указано'}</div>
                <div class="p-2 rounded bg-gray-100 mb-2 text-sm">