Repository navigation
Expand file tree
/
Copy pathadmin.js
More file actions
180 lines (167 loc) · 6.51 KB
/
Copy pathadmin.js
File metadata and controls
180 lines (167 loc) · 6.51 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
// TechStore – Admin Dashboard Logic
let editingProductId = null;
function renderAdmin() {
const orders = getOrders();
const totalRevenue = orders.reduce((a, o) => a + o.total, 0);
const lowStock = PRODUCTS.filter(p => p.stock <= 10).length;
document.getElementById('adminMetrics').innerHTML = `
<div class="metric-card">
<div class="metric-label">Total Revenue</div>
<div class="metric-value">₹${(totalRevenue / 1000).toFixed(0)}K</div>
</div>
<div class="metric-card">
<div class="metric-label">Products</div>
<div class="metric-value">${PRODUCTS.length}</div>
</div>
<div class="metric-card">
<div class="metric-label">Orders</div>
<div class="metric-value">${orders.length}</div>
</div>
<div class="metric-card">
<div class="metric-label">Low Stock Items</div>
<div class="metric-value" style="color:#E24B4A">${lowStock}</div>
</div>`;
adminTab('products', document.querySelector('.tab'));
}
function adminTab(tab, el) {
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
if (el) el.classList.add('active');
const c = document.getElementById('adminTabContent');
if (tab === 'products') {
c.innerHTML = `
<div class="table-wrap">
<table class="data-table">
<thead><tr>
<th>Product</th><th>Category</th><th>Price</th><th>Stock</th><th>Rating</th><th>Actions</th>
</tr></thead>
<tbody>
${PRODUCTS.map(p => `
<tr>
<td>
<div style="display:flex;align-items:center;gap:10px">
<span style="font-size:20px">${p.emoji}</span>
<span class="fw-medium">${p.name}</span>
</div>
</td>
<td>${p.cat}</td>
<td>₹${p.price.toLocaleString()}</td>
<td class="${p.stock <= 10 ? 'stock-low' : ''}">${p.stock}</td>
<td>${p.rating}★</td>
<td>
<div style="display:flex;gap:6px">
<button class="btn btn-secondary btn-sm" onclick="openEditProduct(${p.id})">Edit</button>
<button class="btn btn-danger btn-sm" onclick="deleteProduct(${p.id})">Delete</button>
</div>
</td>
</tr>`).join('')}
</tbody>
</table>
</div>`;
} else {
const orders = getOrders();
c.innerHTML = `
<div class="table-wrap">
<table class="data-table">
<thead><tr>
<th>Order ID</th><th>Items</th><th>Total</th><th>Date</th><th>Status</th><th>Update</th>
</tr></thead>
<tbody>
${orders.map(o => `
<tr>
<td class="fw-medium">${o.id}</td>
<td>${o.items.map(i => i.name).join(', ')}</td>
<td>₹${o.total.toLocaleString()}</td>
<td>${o.date}</td>
<td><span class="status-badge status-${o.status.toLowerCase()}">${o.status}</span></td>
<td>
<select onchange="adminUpdateOrder('${o.id}',this.value)" class="status-select">
${['Processing','Shipped','Delivered','Cancelled'].map(s =>
`<option${s === o.status ? ' selected' : ''}>${s}</option>`
).join('')}
</select>
</td>
</tr>`).join('')}
</tbody>
</table>
</div>`;
}
}
function adminUpdateOrder(id, status) {
updateOrderStatus(id, status);
showToast('Order ' + id + ' → ' + status);
}
function openAddProduct() {
editingProductId = null;
document.getElementById('addProductTitle').textContent = 'Add Product';
renderProductForm(null);
document.getElementById('addProductModal').classList.remove('hidden');
}
function openEditProduct(id) {
editingProductId = id;
const p = PRODUCTS.find(x => x.id === id);
document.getElementById('addProductTitle').textContent = 'Edit Product';
renderProductForm(p);
document.getElementById('addProductModal').classList.remove('hidden');
}
function renderProductForm(p) {
document.getElementById('addProductForm').innerHTML = `
<div class="form-group">
<label>Product Name</label>
<input id="pName" value="${p ? p.name : ''}">
</div>
<div class="form-row">
<div class="form-group">
<label>Category</label>
<input id="pCat" value="${p ? p.cat : ''}">
</div>
<div class="form-group">
<label>Emoji Icon</label>
<input id="pEmoji" value="${p ? p.emoji : '📦'}" style="width:80px">
</div>
</div>
<div class="form-row">
<div class="form-group">
<label>Price (₹)</label>
<input id="pPrice" type="number" value="${p ? p.price : ''}">
</div>
<div class="form-group">
<label>Stock</label>
<input id="pStock" type="number" value="${p ? p.stock : ''}">
</div>
</div>
<div class="form-group">
<label>Description</label>
<input id="pDesc" value="${p ? p.desc : ''}">
</div>
<div class="btn-row">
<button class="btn btn-secondary" onclick="document.getElementById('addProductModal').classList.add('hidden')">Cancel</button>
<button class="btn btn-primary" onclick="saveProduct()">Save Product</button>
</div>`;
}
function saveProduct() {
const name = document.getElementById('pName').value.trim();
const cat = document.getElementById('pCat').value.trim();
const price = parseInt(document.getElementById('pPrice').value);
const stock = parseInt(document.getElementById('pStock').value);
const emoji = document.getElementById('pEmoji').value.trim();
const desc = document.getElementById('pDesc').value.trim();
if (!name || !price || isNaN(price)) { showToast('Please fill in required fields'); return; }
if (editingProductId) {
const p = PRODUCTS.find(x => x.id === editingProductId);
if (p) Object.assign(p, { name, cat, price, stock: isNaN(stock) ? p.stock : stock, emoji, desc });
showToast('Product updated!');
} else {
PRODUCTS.push({
id: Date.now(), name, cat, price, stock: isNaN(stock) ? 0 : stock,
emoji, desc, rating: 4.5, reviews: 0, specs: {}
});
showToast('Product added!');
}
document.getElementById('addProductModal').classList.add('hidden');
renderAdmin();
}
function deleteProduct(id) {
if (!confirm('Delete this product?')) return;
const i = PRODUCTS.findIndex(p => p.id === id);
if (i > -1) { PRODUCTS.splice(i, 1); showToast('Product deleted'); renderAdmin(); }
}