-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
178 lines (152 loc) · 6.63 KB
/
index.js
File metadata and controls
178 lines (152 loc) · 6.63 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
// --- References to screen elements ---
const inputName = document.getElementById('inputName'); // Input field for material name
const inputQty = document.getElementById('inputQty'); // Input field for quantity
const inputMin = document.getElementById('inputMin'); // Input field for minimum stock
const inputUnit = document.getElementById('inputUnit'); // Input field for unit of measurement
const stockList = document.getElementById('stockList'); // Container for the list of items
const footerBar = document.getElementById('footerBar'); // Footer bar for alerts/status
const totalItemsEl = document.getElementById('totalItems'); // Element showing total items count
// --- References to Modal elements ---
const modalOverlay = document.getElementById('modalOverlay'); // Overlay background for modal
const modalTitle = document.getElementById('modalTitle'); // Title inside modal
const modalQty = document.getElementById('modalQty'); // Input field for quantity in modal
// --- State Variables ---
let items = JSON.parse(localStorage.getItem('db_estoque_obra')) || []; // Load items from localStorage or start empty
let currentItemId = null; // Stores the ID of the item currently being edited in the modal
// --- FUNCTION 1: Add Item ---
function addItem() {
const name = inputName.value.trim(); // Get and clean material name
const qty = parseFloat(inputQty.value); // Convert quantity to number
const min = parseFloat(inputMin.value); // Convert minimum stock to number
const unit = inputUnit.value; // Get unit of measurement
// Validation: name must not be empty
if (!name) {
alert("Please enter the material name.");
return;
}
// Create new item object
const newItem = {
id: Date.now(), // Unique ID based on timestamp
name: name,
qty: qty || 0, // Default to 0 if empty
min: min || 0, // Default to 0 if empty
unit: unit
};
// Add item to the beginning of the array (top of the list)
items.unshift(newItem);
saveToStorage(); // Save updated list to localStorage
renderScreen(); // Refresh screen with new item
resetForm(); // Clear form inputs
}
// --- FUNCTION 2: Render Screen ---
function renderScreen() {
stockList.innerHTML = ''; // Clear current list
let alerts = 0; // Counter for items needing attention
// If no items exist, show placeholder message
if (items.length === 0) {
stockList.innerHTML = '<p style="text-align:center; color:#999; padding:20px">No materials registered.</p>';
updateFooter(0);
return;
}
// Loop through items and build list
items.forEach(item => {
let statusClass = ''; // CSS class for stock status
// Apply color logic based on quantity
if (item.qty <= 0) {
statusClass = 'critical'; // Red for out of stock
alerts++;
} else if (item.qty <= item.min) {
statusClass = 'low'; // Yellow for low stock
alerts++;
}
// Create list item element
const li = document.createElement('li');
li.className = `stock-item ${statusClass}`;
li.innerHTML = `
<div class="item-info">
<h4>${item.name}</h4>
<span>Minimum: ${item.min} ${item.unit}</span>
</div>
<div class="item-actions">
<span class="qty-badge">${item.qty} <small>${item.unit}</small></span>
<button class="btn-move" onclick="openModal(${item.id})">Move</button>
<button class="btn-del" onclick="deleteItem(${item.id})">🗑️</button>
</div>
`;
stockList.appendChild(li);
});
// Update total items count
totalItemsEl.innerText = `${items.length} items registered`;
updateFooter(alerts); // Update footer with alerts
}
// --- FUNCTION 3: Modal Control ---
function openModal(id) {
const item = items.find(i => i.id === id); // Find item by ID
if (item) {
currentItemId = id; // Store current item ID
modalTitle.innerText = `Material: ${item.name} (Current: ${item.qty} ${item.unit})`;
modalQty.value = ''; // Clear modal input
modalOverlay.style.display = 'flex'; // Show modal
setTimeout(() => modalQty.focus(), 100); // Auto-focus input
}
}
function closeModal() {
modalOverlay.style.display = 'none'; // Hide modal
currentItemId = null; // Reset current item ID
}
function updateStock(type) {
const amount = parseFloat(modalQty.value); // Get entered amount
// Validate amount
if (!amount || amount <= 0) {
alert("Enter a valid quantity.");
return;
}
const index = items.findIndex(i => i.id === currentItemId); // Find item index
if (index !== -1) {
if (type === 'in') {
// Stock entry (add)
items[index].qty += amount;
} else {
// Stock exit (subtract with validation)
if (items[index].qty < amount) {
alert("Error: Insufficient stock!");
return;
}
items[index].qty -= amount;
}
saveToStorage(); // Save changes
renderScreen(); // Refresh screen
closeModal(); // Close modal
}
}
// --- FUNCTION 4: Delete and Save ---
function deleteItem(id) {
if (confirm("Are you sure you want to delete this item?")) {
items = items.filter(i => i.id !== id); // Remove item by ID
saveToStorage(); // Save updated list
renderScreen(); // Refresh screen
}
}
function saveToStorage() {
localStorage.setItem('db_estoque_obra', JSON.stringify(items)); // Save items to localStorage
}
function resetForm() {
inputName.value = ''; // Clear name field
inputQty.value = 0; // Reset quantity field
inputName.focus(); // Focus back on name input
}
function updateFooter(alertCount) {
if (alertCount > 0) {
footerBar.style.backgroundColor = '#c0392b'; // Red background for alerts
footerBar.innerText = `⚠️ ATTENTION: ${alertCount} items need restocking!`;
} else {
footerBar.style.backgroundColor = '#141618'; // Default background
footerBar.innerText = `✅ System Operational - Stock Regular`;
}
}
// Close modal when clicking outside of it
modalOverlay.addEventListener('click', (e) => {
if (e.target === modalOverlay) closeModal();
});
// --- Initialize screen on page load ---
renderScreen();