-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
472 lines (392 loc) · 15.5 KB
/
script.js
File metadata and controls
472 lines (392 loc) · 15.5 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// إعداد Supabase
const SUPABASE_URL = 'https://kqpxoplowdyfhzstkvwk.supabase.co';
const SUPABASE_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImtxcHhvcGxvd2R5Zmh6c3RrdndrIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTc0MTU3OTYsImV4cCI6MjA3Mjk5MTc5Nn0.DJuCG-Ou0QAi8FhR-3rYbr539xLO8MOvMrHwDvg6yZI';
const supabase = supabase.createClient(SUPABASE_URL, SUPABASE_KEY);
// حالة التطبيق
let isDarkMode = false;
let items = [];
let transactions = [];
// تهيئة التطبيق
document.addEventListener('DOMContentLoaded', async function() {
await initApp();
setupEventListeners();
});
// تهيئة التطبيق
async function initApp() {
try {
updateStatus('جاري تحميل البيانات...');
// تحميل العناصر والمعاملات
await loadItems();
await loadTransactions();
// تحديث واجهة المستخدم
refreshItemsList();
setupDefaultDates();
updateStatus('جاهز - متصل بقاعدة البيانات');
} catch (error) {
console.error('Error initializing app:', error);
updateStatus('خطأ في تحميل البيانات');
}
}
// تحميل العناصر
async function loadItems() {
const { data, error } = await supabase
.from('items')
.select('*')
.order('name');
if (error) throw error;
items = data || [];
}
// تحميل المعاملات
async function loadTransactions() {
const { data, error } = await supabase
.from('transactions')
.select('*')
.order('timestamp', { ascending: false });
if (error) throw error;
transactions = data || [];
}
// إضافة معاملة جديدة
async function addTransaction() {
const itemName = document.getElementById('itemName').value.trim();
const type = document.getElementById('transactionType').value;
const quantity = parseInt(document.getElementById('quantity').value);
if (!itemName) {
alert('يرجى إدخال اسم الصنف');
return;
}
if (quantity <= 0) {
alert('يرجى إدخال كمية صحيحة');
return;
}
try {
updateStatus('جاري إضافة المعاملة...');
// إضافة المعاملة
const { data, error } = await supabase
.from('transactions')
.insert([
{
item_name: itemName,
type: type,
quantity: quantity,
timestamp: new Date().toISOString()
}
]);
if (error) throw error;
// إضافة الصنف إذا كان جديداً
if (!items.some(item => item.name === itemName)) {
const { error: itemError } = await supabase
.from('items')
.insert([{ name: itemName }]);
if (!itemError) {
items.push({ name: itemName });
}
}
// إعادة تحميل البيانات
await loadTransactions();
refreshItemsList();
// تحديث التقرير
generateReport();
// مسح الحقول
document.getElementById('itemName').value = '';
document.getElementById('quantity').value = '1';
updateStatus('تمت إضافة المعاملة بنجاح');
// إشعار
showNotification(`تمت إضافة ${type === 'supply' ? 'توريد' : 'استهلاك'} للصنف ${itemName}`);
} catch (error) {
console.error('Error adding transaction:', error);
updateStatus('خطأ في إضافة المعاملة');
}
}
// توليد التقرير
function generateReport() {
const startDate = document.getElementById('startDate').value;
const endDate = document.getElementById('endDate').value;
if (!startDate || !endDate) {
alert('يرجى تحديد تاريخ البداية والنهاية');
return;
}
const reportData = {};
let totalSupply = 0;
let totalConsume = 0;
// معالجة البيانات
transactions.forEach(transaction => {
const transactionDate = transaction.timestamp.split('T')[0];
if (transactionDate >= startDate && transactionDate <= endDate) {
const itemName = transaction.item_name;
if (!reportData[itemName]) {
reportData[itemName] = { supply: 0, consume: 0 };
}
if (transaction.type === 'supply') {
reportData[itemName].supply += transaction.quantity;
totalSupply += transaction.quantity;
} else {
reportData[itemName].consume += transaction.quantity;
totalConsume += transaction.quantity;
}
}
});
// عرض التقرير
displayReport(reportData, totalSupply, totalConsume, startDate, endDate);
}
// عرض التقرير
function displayReport(reportData, totalSupply, totalConsume, startDate, endDate) {
const tableBody = document.getElementById('reportTableBody');
const reportSummary = document.getElementById('reportSummary');
// مسح المحتوى السابق
tableBody.innerHTML = '';
// إضافة البيانات
Object.keys(reportData).forEach(itemName => {
const data = reportData[itemName];
const balance = data.supply - data.consume;
const row = tableBody.insertRow();
// إضافة خلايا الصف
row.insertCell(0).textContent = itemName;
row.insertCell(1).textContent = data.supply;
row.insertCell(2).textContent = data.consume;
const balanceCell = row.insertCell(3);
balanceCell.textContent = balance;
// تلوين الخلايا حسب الرصيد
if (balance < 0) {
balanceCell.classList.add('balance-negative');
} else if (balance < 10) {
balanceCell.classList.add('balance-low');
}
});
// إضافة صف المجموع
if (Object.keys(reportData).length > 0) {
const totalRow = tableBody.insertRow();
totalRow.classList.add('total-row');
totalRow.insertCell(0).textContent = 'المجموع';
totalRow.insertCell(1).textContent = totalSupply;
totalRow.insertCell(2).textContent = totalConsume;
totalRow.insertCell(3).textContent = totalSupply - totalConsume;
// تنسيق صف المجموع
totalRow.cells[0].style.fontWeight = 'bold';
totalRow.cells[1].style.fontWeight = 'bold';
totalRow.cells[2].style.fontWeight = 'bold';
totalRow.cells[3].style.fontWeight = 'bold';
totalRow.style.backgroundColor = '#e3f2fd';
}
// تحديث ملخص التقرير
reportSummary.innerHTML = `
<h3>تقرير المخزون من ${startDate} إلى ${endDate}</h3>
<p>عدد الأصناف: ${Object.keys(reportData).length} |
إجمالي التوريد: ${totalSupply} |
إجمالي الاستهلاك: ${totalConsume}</p>
`;
}
// تحديث قائمة العناصر
function refreshItemsList() {
const itemsList = document.getElementById('itemsList');
itemsList.innerHTML = '';
items.forEach(item => {
const option = document.createElement('option');
option.value = item.name;
itemsList.appendChild(option);
});
}
// إعداد التواريخ الافتراضية
function setupDefaultDates() {
const today = new Date();
const oneWeekAgo = new Date();
oneWeekAgo.setDate(today.getDate() - 7);
document.getElementById('startDate').value = oneWeekAgo.toISOString().split('T')[0];
document.getElementById('endDate').value = today.toISOString().split('T')[0];
}
// تحديث حالة النظام
function updateStatus(message) {
document.getElementById('statusText').textContent = message;
}
// إظهار الإشعار
function showNotification(message) {
if ('Notification' in window && Notification.permission === 'granted') {
new Notification('نظام مخزون رسلان', {
body: message,
icon: '/favicon.ico'
});
}
}
// إعداد مستمعي الأحداث
function setupEventListeners() {
document.getElementById('addTransactionBtn').addEventListener('click', addTransaction);
document.getElementById('generateReportBtn').addEventListener('click', generateReport);
document.getElementById('darkModeBtn').addEventListener('click', toggleDarkMode);
document.getElementById('exportBtn').addEventListener('click', exportData);
document.getElementById('importBtn').addEventListener('click', importData);
// توليد التقرير تلقائياً عند التحميل
generateReport();
}
// تبديل الوضع الليلي
function toggleDarkMode() {
isDarkMode = !isDarkMode;
document.body.classList.toggle('dark-mode', isDarkMode);
const button = document.getElementById('darkModeBtn');
button.textContent = isDarkMode ? 'الوضع النهاري' : 'الوضع الليلي';
button.style.background = isDarkMode ? '#f39c12' : '#34495e';
button.style.color = 'white';
// تطبيق الألوان على جميع العناصر
applyDarkModeStyles(isDarkMode);
// حفظ الإعدادات
localStorage.setItem('darkMode', isDarkMode);
}
// تطبيق أنماط الوضع الليلي على جميع العناصر
function applyDarkModeStyles(isDark) {
const elementsToStyle = [
'.container',
'.transaction-section',
'.report-section',
'.header',
'.status-bar',
'table',
'th',
'tr',
'td'
];
const styles = document.getElementById('dark-mode-styles');
if (!styles) {
const styleElement = document.createElement('style');
styleElement.id = 'dark-mode-styles';
document.head.appendChild(styleElement);
}
if (isDark) {
document.getElementById('dark-mode-styles').textContent = `
.dark-mode .container {
background: #2c3e50 !important;
color: #ecf0f1 !important;
}
.dark-mode .transaction-section,
.dark-mode .report-section {
background: #34495e !important;
color: #ecf0f1 !important;
border-color: #4a6278 !important;
}
.dark-mode .header {
background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%) !important;
}
.dark-mode .status-bar {
background: #2c3e50 !important;
color: #ecf0f1 !important;
}
.dark-mode table {
background: #34495e !important;
color: #ecf0f1 !important;
border-color: #4a6278 !important;
}
.dark-mode th {
background: #2c3e50 !important;
color: #ecf0f1 !important;
border-color: #4a6278 !important;
}
.dark-mode tr {
background: #3b4f63 !important;
color: #ecf0f1 !important;
border-color: #4a6278 !important;
}
.dark-mode tr:nth-child(even) {
background: #34495e !important;
}
.dark-mode tr:hover {
background: #4a6278 !important;
}
.dark-mode td {
background: inherit !important;
color: inherit !important;
border-color: #4a6278 !important;
}
.dark-mode .form-group input,
.dark-mode .form-group select,
.dark-mode .filter-group input {
background: #2c3e50 !important;
color: #ecf0f1 !important;
border-color: #4a6278 !important;
}
.dark-mode .report-summary {
background: #3b4f63 !important;
color: #ecf0f1 !important;
border-color: #3498db !important;
}
`;
} else {
document.getElementById('dark-mode-styles').textContent = '';
}
}
// تحميل الإعدادات
function loadSettings() {
const savedDarkMode = localStorage.getItem('darkMode');
if (savedDarkMode === 'true') {
isDarkMode = true;
document.body.classList.add('dark-mode');
document.getElementById('darkModeBtn').textContent = 'الوضع النهاري';
document.getElementById('darkModeBtn').style.background = '#f39c12';
document.getElementById('darkModeBtn').style.color = 'white';
applyDarkModeStyles(true);
}
}
// تصدير البيانات
async function exportData() {
try {
const { data, error } = await supabase
.from('transactions')
.select('*');
if (error) throw error;
const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'inventory_export.json';
a.click();
URL.revokeObjectURL(url);
updateStatus('تم تصدير البيانات بنجاح');
} catch (error) {
console.error('Error exporting data:', error);
updateStatus('خطأ في تصدير البيانات');
}
}
// استيراد البيانات
async function importData() {
const input = document.createElement('input');
input.type = 'file';
input.accept = '.json';
input.onchange = async (e) => {
const file = e.target.files[0];
if (!file) return;
try {
const content = await file.text();
const data = JSON.parse(content);
updateStatus('جاري استيراد البيانات...');
const { error } = await supabase
.from('transactions')
.insert(data);
if (error) throw error;
// إعادة تحميل البيانات
await loadTransactions();
generateReport();
updateStatus('تم استيراد البيانات بنجاح');
showNotification('تم استيراد البيانات بنجاح');
} catch (error) {
console.error('Error importing data:', error);
updateStatus('خطأ في استيراد البيانات');
}
};
input.click();
}
// تحميل الإعدادات المحفوظة
function loadSettings() {
const savedDarkMode = localStorage.getItem('darkMode');
if (savedDarkMode === 'true') {
isDarkMode = true;
document.body.classList.add('dark-mode');
document.getElementById('darkModeBtn').textContent = 'الوضع النهاري';
}
}
// طلب الإذن للإشعارات
if ('Notification' in window) {
Notification.requestPermission();
}
// تحميل الإعدادات عند البدء
loadSettings();
// تحديث استخدام الذاكرة (محاكاة)
setInterval(() => {
const memoryUsage = Math.random() * 10 + 5;
document.getElementById('memoryUsage').textContent = `الذاكرة: ${memoryUsage.toFixed(1)} MB`;
}, 5000);