-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINDEX.html
More file actions
404 lines (359 loc) · 37.2 KB
/
Copy pathINDEX.html
File metadata and controls
404 lines (359 loc) · 37.2 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
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OEE Master Demo - Overall Equipment Effectiveness System</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
<style>
:root {
--primary: #2563eb; --primary-light: #60a5fa; --primary-dark: #1d4ed8;
--secondary: #7c3aed; --secondary-light: #a78bfa;
--success: #10b981; --success-light: #34d399; --success-dark: #059669;
--warning: #f59e0b; --warning-light: #fbbf24; --warning-dark: #d97706;
--danger: #ef4444; --danger-light: #f87171; --danger-dark: #dc2626;
--info: #06b6d4; --info-dark: #0891b2;
--white: #ffffff; --dark: #1f2937; --dark-2: #374151; --dark-3: #4b5563;
--gray: #6b7280; --gray-light: #f3f4f6; --gray-light-3: #9ca3af;
--border: #e5e7eb; --border-light: #f3f4f6;
--radius: 12px; --radius-sm: 8px; --radius-lg: 16px; --radius-xl: 20px;
--shadow: 0 1px 3px rgba(0,0,0,0.08); --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
--shadow-md: 0 4px 6px rgba(0,0,0,0.07); --shadow-lg: 0 10px 15px rgba(0,0,0,0.1);
--shadow-xl: 0 20px 25px rgba(0,0,0,0.15);
--transition: all 0.2s ease; --transition-fast: all 0.15s ease;
--primary-glow: rgba(37,99,235,0.2); --success-glow: rgba(16,185,129,0.2);
--warning-glow: rgba(245,158,11,0.2); --danger-glow: rgba(239,68,68,0.2);
}
* { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Inter', sans-serif; }
body { background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%); color: var(--dark); min-height: 100vh; padding: 20px; }
body::before {
content: 'DEMO VERSION - 5 Machines / 10 Shifts Max';
position: fixed; top: 50%; left: 50%; transform: translate(-50%,-50%) rotate(-35deg);
font-size: 3.5vw; color: rgba(239,68,68,0.08); white-space: nowrap;
pointer-events: none; z-index: 9998; font-weight: 900; user-select: none; letter-spacing: 4px;
}
body::after {
content: 'Upgrade at digitalyar.company'; position: fixed; top: 20%; left: 10%; transform: rotate(20deg);
font-size: 2.5vw; color: rgba(37,99,235,0.06); white-space: nowrap;
pointer-events: none; z-index: 9997; font-weight: 700; user-select: none;
}
.pro-badge { background: linear-gradient(135deg, #f59e0b, #d97706); color: white; font-size: 0.55rem; padding: 2px 6px; border-radius: 6px; font-weight: 700; text-transform: uppercase; margin-left: 4px; }
.btn-pro { background: linear-gradient(135deg, #f59e0b, #d97706) !important; color: white !important; opacity: 0.7; cursor: not-allowed !important; }
.upgrade-banner { background: linear-gradient(135deg, #fef3c7, #fde68a); border: 2px solid var(--warning); padding: 10px 20px; border-radius: var(--radius); text-align: center; font-weight: 600; font-size: 0.85rem; margin-bottom: 20px; }
.upgrade-banner a { color: var(--primary-dark); font-weight: 700; }
.container { max-width: 1400px; margin: 0 auto; }
header { background: linear-gradient(135deg, var(--dark) 0%, var(--dark-2) 50%, #1a1a3e 100%); border-radius: var(--radius-xl); padding: 24px 28px; margin-bottom: 24px; display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 20px; box-shadow: var(--shadow-xl); position: relative; overflow: hidden; }
.logo { display: flex; align-items: center; gap: 14px; position: relative; z-index: 1; }
.logo-icon { width: 52px; height: 52px; background: linear-gradient(135deg, var(--primary), var(--secondary)); border-radius: var(--radius-lg); display: flex; align-items: center; justify-content: center; font-size: 1.4rem; color: white; }
.logo-text h1 { font-size: 1.5rem; font-weight: 800; color: var(--white); }
.logo-text h1 .demo-badge { background: var(--warning); color: white; font-size: 0.6rem; padding: 2px 8px; border-radius: 10px; vertical-align: middle; margin-left: 6px; }
.header-stats { display: flex; gap: 12px; position: relative; z-index: 1; }
.header-stat { background: rgba(255,255,255,0.08); border: 1px solid rgba(255,255,255,0.12); border-radius: var(--radius); padding: 12px 20px; text-align: center; min-width: 100px; }
.header-stat-value { font-size: 1.3rem; font-weight: 700; color: var(--white); }
.header-stat-label { font-size: 0.7rem; color: rgba(255,255,255,0.55); text-transform: uppercase; }
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; margin-bottom: 24px; }
.stat-card { background: var(--white); border-radius: var(--radius-lg); padding: 20px; border: 1px solid var(--border); box-shadow: var(--shadow); }
.stat-card::before { content: ''; display: block; height: 4px; border-radius: 4px 4px 0 0; margin: -20px -20px 12px -20px; }
.stat-card.availability::before { background: var(--primary); }
.stat-card.performance::before { background: var(--secondary); }
.stat-card.quality::before { background: var(--success); }
.stat-card.oee::before { background: var(--warning); }
.stat-title { font-size: 0.8rem; font-weight: 600; color: var(--gray); text-transform: uppercase; }
.stat-value { font-size: 1.8rem; font-weight: 800; }
.stat-card.availability .stat-value { color: var(--primary); }
.stat-card.performance .stat-value { color: var(--secondary); }
.stat-card.quality .stat-value { color: var(--success); }
.stat-card.oee .stat-value { color: var(--warning); }
.main-content { display: grid; grid-template-columns: 1fr 300px; gap: 24px; align-items: start; }
@media (max-width: 1100px) { .main-content { grid-template-columns: 1fr; } }
.dashboard-card { background: var(--white); border-radius: var(--radius-xl); border: 1px solid var(--border); box-shadow: var(--shadow); overflow: hidden; }
.card-header { padding: 20px 24px 0; }
.card-title { font-size: 1.1rem; font-weight: 700; }
.tabs { display: flex; gap: 4px; padding: 16px 24px 0; overflow-x: auto; border-bottom: 2px solid var(--border-light); }
.tab { padding: 10px 16px; font-size: 0.8rem; font-weight: 600; color: var(--gray); cursor: pointer; border-radius: var(--radius-sm) var(--radius-sm) 0 0; white-space: nowrap; transition: var(--transition); }
.tab:hover { color: var(--primary); background: rgba(37,99,235,0.05); }
.tab.active { color: var(--primary); background: rgba(37,99,235,0.08); }
.tab-content { display: none; padding: 24px; animation: fadeIn 0.3s ease; }
.tab-content.active { display: block; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
.form-grid { display: grid; grid-template-columns: repeat(2,1fr); gap: 14px; margin-bottom: 14px; }
@media (max-width: 600px) { .form-grid { grid-template-columns: 1fr; } }
.form-group { display: flex; flex-direction: column; gap: 4px; margin-bottom: 14px; }
.form-label { font-size: 0.8rem; font-weight: 600; }
.form-input, .form-select, .form-textarea { padding: 10px 14px; border: 1.5px solid var(--border); border-radius: var(--radius-sm); font-size: 0.9rem; font-family: 'Inter', sans-serif; transition: var(--transition); outline: none; background: var(--white); color: var(--dark); }
.form-input:focus, .form-select:focus, .form-textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-glow); }
.btn { padding: 10px 18px; border: none; border-radius: var(--radius-sm); font-weight: 600; font-size: 0.85rem; cursor: pointer; transition: var(--transition); display: inline-flex; align-items: center; gap: 6px; }
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-success { background: var(--success); color: white; }
.btn-warning { background: var(--warning); color: white; }
.btn-danger { background: var(--danger); color: white; }
.btn-info { background: var(--info); color: white; }
.btn-sm { padding: 6px 12px; font-size: 0.75rem; }
.btn-xs { padding: 3px 8px; font-size: 0.65rem; }
table { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
th, td { padding: 12px 14px; text-align: left; border-bottom: 1px solid var(--border); }
th { background: var(--gray-light); font-weight: 700; color: var(--gray); font-size: 0.78rem; text-transform: uppercase; }
tr:hover { background: var(--gray-light); }
.table-container { overflow-x: auto; border-radius: var(--radius); border: 1px solid var(--border); }
.sidebar { display: flex; flex-direction: column; gap: 16px; }
@media (max-width: 1100px) { .sidebar { display: none; } }
.sidebar-card { background: var(--white); border-radius: var(--radius-lg); padding: 20px; border: 1px solid var(--border); box-shadow: var(--shadow); }
.sidebar-card h3 { font-size: 0.95rem; font-weight: 700; margin-bottom: 12px; }
.oee-result { background: linear-gradient(135deg, #f0f9ff, #e0f2fe, #f0fdf4); border: 1px solid rgba(37,99,235,0.15); border-radius: var(--radius-lg); padding: 24px; text-align: center; margin-bottom: 24px; }
.oee-score { font-size: 3rem; font-weight: 800; }
.oee-score.excellent { color: var(--success); }
.oee-score.good { color: var(--primary); }
.oee-score.average { color: var(--warning); }
.oee-score.poor { color: var(--danger); }
.oee-breakdown { display: grid; grid-template-columns: repeat(3,1fr); gap: 12px; margin-bottom: 16px; }
.breakdown-item { background: var(--white); border-radius: var(--radius); padding: 16px; border: 1px solid var(--border-light); }
.breakdown-title { font-size: 0.75rem; font-weight: 600; color: var(--gray); text-transform: uppercase; }
.breakdown-value { font-size: 1.4rem; font-weight: 800; }
.chart-container { position: relative; height: 280px; background: var(--white); border-radius: var(--radius); padding: 12px; border: 1px solid var(--border-light); }
.status-badge { display: inline-flex; padding: 4px 10px; font-size: 0.7rem; font-weight: 600; border-radius: 16px; }
.status-excellent { background: rgba(16,185,129,0.12); color: var(--success-dark); }
.status-good { background: rgba(37,99,235,0.12); color: var(--primary-dark); }
.status-average { background: rgba(245,158,11,0.12); color: var(--warning-dark); }
.status-poor { background: rgba(239,68,68,0.12); color: var(--danger-dark); }
.modal { display: none; position: fixed; inset: 0; z-index: 1000; align-items: center; justify-content: center; padding: 20px; background: rgba(15,23,42,0.6); backdrop-filter: blur(6px); }
.modal-content { background: var(--white); border-radius: var(--radius-xl); width: 100%; max-width: 500px; max-height: 85vh; overflow-y: auto; box-shadow: var(--shadow-xl); padding: 24px; }
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; padding-bottom: 12px; border-bottom: 1px solid var(--border-light); }
.modal-title { font-size: 1.1rem; font-weight: 700; }
.close-modal { background: var(--gray-light); border: none; font-size: 1.2rem; cursor: pointer; color: var(--gray); width: 32px; height: 32px; border-radius: 50%; display: flex; align-items: center; justify-content: center; }
.notification { position: fixed; bottom: 20px; right: 20px; padding: 14px 22px; border-radius: var(--radius); color: white; font-weight: 600; box-shadow: var(--shadow-xl); z-index: 2000; display: flex; align-items: center; gap: 10px; transform: translateX(120%); opacity: 0; transition: all 0.4s; max-width: 350px; font-size: 0.9rem; }
.notification.show { transform: translateX(0); opacity: 1; }
.notification.success { background: var(--success); }
.notification.error { background: var(--danger); }
.notification.warning { background: var(--warning); }
.empty-state { text-align: center; padding: 40px; color: var(--gray); }
.loss-item { display: flex; justify-content: space-between; padding: 10px 14px; background: var(--gray-light); border-radius: var(--radius-sm); border-left: 4px solid var(--primary); margin-bottom: 6px; font-size: 0.85rem; }
.loss-item.urgent { border-left-color: var(--danger); }
</style>
</head>
<body>
<div class="container">
<div class="upgrade-banner">
⚠️ <strong>DEMO MODE:</strong> 5 machines, 10 shifts max. Backup/PDF export disabled. Sample data pre-loaded.
<a href="https://digitalyar.company/product/spa06-oee-master/" target="_blank">Upgrade to Full Version →</a>
</div>
<header>
<div class="logo">
<div class="logo-icon"><i class="fas fa-industry"></i></div>
<div class="logo-text">
<h1>OEE Master <span class="demo-badge">DEMO</span></h1>
<p style="color:rgba(255,255,255,0.6);font-size:0.8rem;">Overall Equipment Effectiveness Tracking</p>
</div>
</div>
<div class="header-stats">
<div class="header-stat"><div class="header-stat-value" id="totalMachines">0</div><div class="header-stat-label">Machines</div></div>
<div class="header-stat"><div class="header-stat-value" id="avgOEE">0%</div><div class="header-stat-label">Avg OEE</div></div>
<div class="header-stat"><div class="header-stat-value" id="totalShifts">0</div><div class="header-stat-label">Shifts</div></div>
</div>
</header>
<div class="stats-grid" id="statsGrid"></div>
<div class="main-content">
<div class="main-panel">
<div class="dashboard-card">
<div class="tabs">
<div class="tab active" data-tab="calculator">Calculator</div>
<div class="tab" data-tab="machines">Machines</div>
<div class="tab" data-tab="shifts">Shift Log</div>
<div class="tab" data-tab="losses">Loss Analysis</div>
<div class="tab" data-tab="reports">Reports</div>
<div class="tab" data-tab="backup">Sample Data</div>
</div>
<!-- Calculator Tab -->
<div class="tab-content active" id="calculator-tab">
<div class="oee-result" id="oeeResult" style="display:none;">
<div class="oee-score" id="oeeScore">0%</div>
<p style="color:var(--gray);margin-bottom:16px;">Your Overall Equipment Effectiveness</p>
<div class="oee-breakdown">
<div class="breakdown-item"><div class="breakdown-title">Availability</div><div class="breakdown-value" id="availabilityScore">0%</div></div>
<div class="breakdown-item"><div class="breakdown-title">Performance</div><div class="breakdown-value" id="performanceScore">0%</div></div>
<div class="breakdown-item"><div class="breakdown-title">Quality</div><div class="breakdown-value" id="qualityScore">0%</div></div>
</div>
</div>
<form id="calcForm">
<div class="form-grid">
<div class="form-group"><label class="form-label">Machine</label><select class="form-select" id="calcMachine"><option value="">Select...</option></select></div>
<div class="form-group"><label class="form-label">Shift Type</label><select class="form-select" id="calcShift"><option value="morning">Morning</option><option value="evening">Evening</option><option value="night">Night</option></select></div>
<div class="form-group"><label class="form-label">Total Shift Time (min)</label><input class="form-input" type="number" id="totalShiftTime" value="480" min="1"></div>
<div class="form-group"><label class="form-label">Planned Downtime (min)</label><input class="form-input" type="number" id="plannedDowntime" value="60" min="0"></div>
<div class="form-group"><label class="form-label">Unplanned Downtime (min)</label><input class="form-input" type="number" id="unplannedDowntime" value="30" min="0"></div>
<div class="form-group"><label class="form-label">Total Produced (pcs)</label><input class="form-input" type="number" id="totalProduced" value="1000" min="0"></div>
<div class="form-group"><label class="form-label">Defective Parts</label><input class="form-input" type="number" id="defectiveParts" value="50" min="0"></div>
<div class="form-group"><label class="form-label">Ideal Cycle (sec)</label><input class="form-input" type="number" id="idealCycle" value="20" min="0.1" step="0.1"></div>
</div>
<button type="button" class="btn btn-primary" onclick="calcOEE()"><i class="fas fa-calculator"></i> Calculate</button>
<button type="button" class="btn btn-success" id="saveBtn" style="display:none;" onclick="saveResult()"><i class="fas fa-save"></i> Save</button>
</form>
</div>
<!-- Machines Tab -->
<div class="tab-content" id="machines-tab">
<button class="btn btn-primary btn-sm" onclick="openMachineModal()" style="margin-bottom:12px;">+ Add Machine</button>
<p style="font-size:0.75rem;color:var(--warning);">⚠️ Demo: 5 machines max</p>
<div id="machinesList"></div>
</div>
<!-- Shift Log -->
<div class="tab-content" id="shifts-tab">
<form id="shiftForm">
<div class="form-grid">
<div class="form-group"><label class="form-label">Machine</label><select class="form-select" id="shiftMachine" required><option value="">Select...</option></select></div>
<div class="form-group"><label class="form-label">Date</label><input class="form-input" type="date" id="shiftDate" required></div>
<div class="form-group"><label class="form-label">Shift</label><select class="form-select" id="shiftType"><option value="morning">Morning</option><option value="evening">Evening</option><option value="night">Night</option></select></div>
<div class="form-group"><label class="form-label">Total Time (min)</label><input class="form-input" type="number" id="sTotalTime" value="480" min="1"></div>
<div class="form-group"><label class="form-label">Downtime (min)</label><input class="form-input" type="number" id="sDowntime" value="0" min="0"></div>
<div class="form-group"><label class="form-label">Parts Produced</label><input class="form-input" type="number" id="sParts" value="0" min="0"></div>
<div class="form-group"><label class="form-label">Defective Parts</label><input class="form-input" type="number" id="sDefects" value="0" min="0"></div>
</div>
<p style="font-size:0.75rem;color:var(--warning);">⚠️ Demo: 10 shifts max</p>
<button type="submit" class="btn btn-primary">Save Shift</button>
</form>
</div>
<!-- Loss Analysis -->
<div class="tab-content" id="losses-tab">
<div class="loss-analysis">
<h4 style="margin-bottom:16px;">6 Major Losses</h4>
<div style="margin-bottom:12px;"><span>Availability Losses:</span> <span id="aLoss">0%</span></div>
<div style="margin-bottom:12px;"><span>Performance Losses:</span> <span id="pLoss">0%</span></div>
<div><span>Quality Losses:</span> <span id="qLoss">0%</span></div>
</div>
<div class="chart-container" style="margin-top:20px;"><canvas id="lossChart"></canvas></div>
</div>
<!-- Reports -->
<div class="tab-content" id="reports-tab">
<div class="chart-container" style="margin-bottom:16px;"><canvas id="oeeTrend"></canvas></div>
<div class="chart-container"><canvas id="oeeByMachine"></canvas></div>
<p style="text-align:center;margin-top:12px;font-size:0.8rem;color:var(--warning);">📥 PDF Export is a Pro feature. <a href="https://digitalyar.company/product/spa06-oee-master/" target="_blank" style="color:var(--primary);">Upgrade →</a></p>
</div>
<!-- Sample Data -->
<div class="tab-content" id="backup-tab">
<div class="card" style="border:1px solid var(--border);padding:20px;border-radius:var(--radius);">
<h3 style="margin-bottom:12px;"><i class="fas fa-database"></i> Sample Data</h3>
<p style="color:var(--gray);margin-bottom:14px;font-size:0.9rem;">Load demo data or clear all.</p>
<button class="btn btn-primary btn-sm" onclick="loadSample()">📥 Load Sample Data</button>
<button class="btn btn-danger btn-sm" onclick="clearAll()" style="margin-left:8px;">🗑️ Clear All</button>
<p style="margin-top:14px;font-size:0.8rem;color:var(--warning);">📥 Backup/Restore is a Pro feature. <a href="https://digitalyar.company/product/spa06-oee-master/" target="_blank" style="color:var(--primary);">Upgrade →</a></p>
</div>
</div>
</div>
</div>
<div class="sidebar">
<div class="sidebar-card">
<h3><i class="fas fa-crown"></i> Upgrade to Pro</h3>
<p style="font-size:0.8rem;color:var(--gray);margin-bottom:10px;">Unlimited machines, shifts, PDF export & backup.</p>
<a href="https://digitalyar.company/product/spa06-oee-master/" target="_blank" class="btn btn-primary btn-sm" style="width:100%;text-decoration:none;">Get Full Version</a>
</div>
<div class="sidebar-card">
<h3><i class="fas fa-chart-line"></i> Top OEE Today</h3>
<div id="topOEE"></div>
</div>
</div>
</div>
</div>
<div class="modal" id="machineModal">
<div class="modal-content">
<div class="modal-header"><h3 class="modal-title">Add Machine</h3><button class="close-modal" onclick="closeModal()">×</button></div>
<div class="form-grid"><div class="form-group"><label class="form-label">Code</label><input class="form-input" type="text" id="mCode" required></div><div class="form-group"><label class="form-label">Name</label><input class="form-input" type="text" id="mName" required></div><div class="form-group"><label class="form-label">Department</label><input class="form-input" type="text" id="mDept" value="production"></div><div class="form-group"><label class="form-label">Type</label><input class="form-input" type="text" id="mType" value="cnc"></div></div>
<button class="btn btn-primary" onclick="saveMachine()">Save</button>
</div>
</div>
<div class="notification" id="notification"><i class="fas fa-bell"></i><span id="notifText"></span></div>
<script>
const DB='OEEDemo_v2', DEMO={machines:5,shifts:10};
let db,charts={},calcResult=null;
function openDB(){return new Promise((res,rej)=>{const r=indexedDB.open(DB,1);r.onupgradeneeded=e=>{const d=e.target.result;['machines','shifts'].forEach(s=>{if(!d.objectStoreNames.contains(s))d.createObjectStore(s,{keyPath:'id',autoIncrement:true});});};r.onsuccess=e=>{db=e.target.result;res();};r.onerror=rej;});}
function getAll(s){return new Promise(r=>{db.transaction(s,'readonly').objectStore(s).getAll().onsuccess=e=>r(e.target.result||[]);});}
function addItem(s,i){return new Promise(r=>{db.transaction(s,'readwrite').objectStore(s).add(i).onsuccess=e=>r(e.target.result);});}
function countS(s){return new Promise(r=>{db.transaction(s,'readonly').objectStore(s).count().onsuccess=e=>r(e.target.result);});}
function clearS(s){return new Promise(r=>{db.transaction(s,'readwrite').objectStore(s).clear();setTimeout(r,50);});}
function delItem(s,id){return new Promise(r=>{db.transaction(s,'readwrite').objectStore(s).delete(id);setTimeout(r,50);});}
function notif(m,t='success'){const n=document.getElementById('notification');document.getElementById('notifText').textContent=m;n.className=`notification ${t} show`;setTimeout(()=>n.classList.remove('show'),3000);}
function fmt(n){return (n||0).toFixed(1);}
function getToday(){return new Date().toISOString().split('T')[0];}
function closeModal(){document.getElementById('machineModal').style.display='none';}
function openMachineModal(){document.getElementById('machineModal').style.display='flex';}
async function loadAll(){const m=await getAll('machines'),s=await getAll('shifts');document.getElementById('totalMachines').textContent=m.length;document.getElementById('totalShifts').textContent=s.length;document.getElementById('avgOEE').textContent=s.length?(s.reduce((a,b)=>a+(b.oee||0),0)/s.length).toFixed(1)+'%':'0%';renderStats(s);renderMachines(m,s);renderTopOEE(m,s);populateSelects(m);renderHistory(m,s);updateLoss(s);updateCharts(m,s);}
function renderStats(shifts){
if(!shifts.length){document.getElementById('statsGrid').innerHTML='<div class="stat-card availability"><div class="stat-title">Availability</div><div class="stat-value">0%</div></div><div class="stat-card performance"><div class="stat-title">Performance</div><div class="stat-value">0%</div></div><div class="stat-card quality"><div class="stat-title">Quality</div><div class="stat-value">0%</div></div><div class="stat-card oee"><div class="stat-title">OEE</div><div class="stat-value">0%</div></div>';return;}
let a=0,p=0,q=0,o=0;shifts.forEach(s=>{a+=s.availability||0;p+=s.performance||0;q+=s.quality||0;o+=s.oee||0;});const c=shifts.length;
document.getElementById('statsGrid').innerHTML=`<div class="stat-card availability"><div class="stat-title">Availability</div><div class="stat-value">${fmt(a/c)}%</div></div><div class="stat-card performance"><div class="stat-title">Performance</div><div class="stat-value">${fmt(p/c)}%</div></div><div class="stat-card quality"><div class="stat-title">Quality</div><div class="stat-value">${fmt(q/c)}%</div></div><div class="stat-card oee"><div class="stat-title">OEE</div><div class="stat-value">${fmt(o/c)}%</div></div>`;
}
function renderMachines(machines,shifts){
const c=document.getElementById('machinesList');
c.innerHTML=machines.length?machines.map(m=>{const ms=shifts.filter(s=>s.machineId===m.id);const avg=ms.length?ms.reduce((a,b)=>a+(b.oee||0),0)/ms.length:0;return`<div style="background:white;padding:12px;border:1px solid var(--border);border-radius:8px;margin-bottom:8px;display:flex;justify-content:space-between;align-items:center;"><div><strong>${m.name}</strong> (${m.code})<br><small>${m.department} • ${m.type} | Avg OEE: ${fmt(avg)}%</small></div><button class="btn btn-danger btn-xs" onclick="delMachine(${m.id})">🗑️</button></div>`;}).join(''):'<div class="empty-state">No machines</div>';
}
function renderTopOEE(machines,shifts){
const today=getToday(),ts=shifts.filter(s=>s.date===today).sort((a,b)=>(b.oee||0)-(a.oee||0));
const c=document.getElementById('topOEE');
c.innerHTML=ts.length?ts.slice(0,3).map(s=>{const m=machines.find(x=>x.id===s.machineId);return`<div class="loss-item"><div><strong>${m?m.name:'?'}</strong><br><small>${s.shiftType}</small></div><strong>${fmt(s.oee||0)}%</strong></div>`;}).join(''):'<div class="empty-state" style="padding:20px;"><p>No data today</p></div>';
}
function populateSelects(machines){
['calcMachine','shiftMachine'].forEach(id=>{const s=document.getElementById(id);if(s)s.innerHTML='<option value="">Select...</option>'+machines.map(m=>`<option value="${m.id}">${m.name} (${m.code})</option>`).join('');});
document.getElementById('shiftDate').value=getToday();
}
function renderHistory(machines,shifts){
document.getElementById('shiftsTableBody')&&(document.getElementById('shiftsTableBody').innerHTML=[...shifts].sort((a,b)=>new Date(b.date)-new Date(a.date)).slice(0,10).map(s=>{const m=machines.find(x=>x.id===s.machineId);return`<tr><td>${s.date}</td><td>${m?m.name:'?'}</td><td>${s.shiftType}</td><td>${fmt(s.oee||0)}%</td><td>${s.totalParts||0}</td><td>${s.defectiveParts||0}</td></tr>`;}).join('')||'<tr><td colspan="6">No shifts</td></tr>');
}
function updateLoss(shifts){
if(!shifts.length)return;
let a=0,p=0,q=0;shifts.forEach(s=>{a+=s.availability||0;p+=s.performance||0;q+=s.quality||0;});
const c=shifts.length;document.getElementById('aLoss').textContent=Math.max(0,100-a/c).toFixed(1)+'%';document.getElementById('pLoss').textContent=Math.max(0,100-p/c).toFixed(1)+'%';document.getElementById('qLoss').textContent=Math.max(0,100-q/c).toFixed(1)+'%';
}
function updateCharts(machines,shifts){
const tCtx=document.getElementById('oeeTrend')?.getContext('2d'),mCtx=document.getElementById('oeeByMachine')?.getContext('2d'),lCtx=document.getElementById('lossChart')?.getContext('2d');
if(charts.trend)charts.trend.destroy();if(charts.machine)charts.machine.destroy();if(charts.loss)charts.loss.destroy();
if(!shifts.length)return;
if(tCtx){const d=[];for(let i=6;i>=0;i--){const dt=new Date();dt.setDate(dt.getDate()-i);d.push({date:dt.toISOString().split('T')[0],oee:0,c:0});}shifts.forEach(s=>{const day=d.find(x=>x.date===s.date);if(day){day.oee+=s.oee||0;day.c++;}});charts.trend=new Chart(tCtx,{type:'line',data:{labels:d.map(x=>x.date.slice(5)),datasets:[{label:'OEE %',data:d.map(x=>x.c?x.oee/x.c:0),borderColor:'#2563eb',backgroundColor:'rgba(37,99,235,0.1)',fill:true}]},options:{responsive:true,maintainAspectRatio:false,scales:{y:{max:100}}}});}
if(mCtx){const d={};machines.forEach(m=>{const ms=shifts.filter(s=>s.machineId===m.id);if(ms.length)d[m.name]=ms.reduce((a,b)=>a+(b.oee||0),0)/ms.length;});charts.machine=new Chart(mCtx,{type:'bar',data:{labels:Object.keys(d),datasets:[{label:'OEE %',data:Object.values(d),backgroundColor:Object.values(d).map(v=>v>=85?'#10b981':v>=65?'#f59e0b':'#ef4444')}]},options:{responsive:true,maintainAspectRatio:false,scales:{y:{max:100}}}});}
}
function calcOEE(){
const ts=parseFloat(document.getElementById('totalShiftTime').value)||480,pd=parseFloat(document.getElementById('plannedDowntime').value)||0,ud=parseFloat(document.getElementById('unplannedDowntime').value)||0,tp=parseFloat(document.getElementById('totalProduced').value)||0,dp=parseFloat(document.getElementById('defectiveParts').value)||0,ict=parseFloat(document.getElementById('idealCycle').value)||0;
const at=ts-pd,ot=at-ud,av=(ot/at)*100||0,gp=tp-dp,ipt=(gp*ict)/60,apt=(gp*ict*1.25)/60,perf=apt>0?(ipt/apt)*100:0,qual=tp>0?(gp/tp)*100:0,oee=(av*perf*qual)/10000;
document.getElementById('oeeResult').style.display='block';
const sc=document.getElementById('oeeScore');sc.textContent=fmt(oee)+'%';sc.className='oee-score '+(oee>=85?'excellent':oee>=75?'good':oee>=65?'average':'poor');
document.getElementById('availabilityScore').textContent=fmt(av)+'%';document.getElementById('performanceScore').textContent=fmt(perf)+'%';document.getElementById('qualityScore').textContent=fmt(qual)+'%';
document.getElementById('saveBtn').style.display='inline-flex';
calcResult={machineId:parseInt(document.getElementById('calcMachine').value),date:getToday(),shiftType:document.getElementById('calcShift').value,totalTime:ts,availableTime:at,operatingTime:ot,totalParts:tp,defectiveParts:dp,goodParts:gp,availability:av,performance:perf,quality:qual,oee};
}
async function saveResult(){
if(!calcResult||!calcResult.machineId)return notif('Select a machine','warning');
const cnt=await countS('shifts');if(cnt>=DEMO.shifts)return notif(`Demo: ${DEMO.shifts} shifts max`,'warning');
await addItem('shifts',calcResult);notif('Saved!');document.getElementById('saveBtn').style.display='none';loadAll();
}
async function saveMachine(){
const cnt=await countS('machines');if(cnt>=DEMO.machines)return notif(`Demo: ${DEMO.machines} machines max`,'warning');
const m={code:document.getElementById('mCode').value,name:document.getElementById('mName').value,department:document.getElementById('mDept').value,type:document.getElementById('mType').value,status:'active'};
if(!m.code||!m.name)return notif('Fill fields','error');
await addItem('machines',m);closeModal();loadAll();notif('Machine added!');
}
async function delMachine(id){if(!confirm('Delete?'))return;await delItem('machines',id);loadAll();}
document.getElementById('shiftForm').addEventListener('submit',async function(e){
e.preventDefault();const cnt=await countS('shifts');if(cnt>=DEMO.shifts)return notif(`Demo: ${DEMO.shifts} shifts max`,'warning');
const tt=parseFloat(document.getElementById('sTotalTime').value)||480,dt=parseFloat(document.getElementById('sDowntime').value)||0,at=tt-dt,tp=parseFloat(document.getElementById('sParts').value)||0,dp=parseFloat(document.getElementById('sDefects').value)||0,gp=tp-dp,av=tt>0?(at/tt)*100:0,qual=tp>0?(gp/tp)*100:0,perf=90,oee=(av*perf*qual)/10000;
await addItem('shifts',{machineId:parseInt(document.getElementById('shiftMachine').value),date:document.getElementById('shiftDate').value,shiftType:document.getElementById('shiftType').value,totalTime:tt,availableTime:at,operatingTime:at,totalParts:tp,defectiveParts:dp,goodParts:gp,availability:av,performance:perf,quality:qual,oee});
this.reset();document.getElementById('shiftDate').value=getToday();loadAll();notif('Shift saved!');
});
async function loadSample(){
const ms=[{code:'CNC-001',name:'5-Axis CNC',department:'cnc',type:'cnc',status:'active'},{code:'INJ-001',name:'Injection Molder',department:'injection',type:'injection',status:'active'},{code:'PKG-001',name:'Packaging Line',department:'packaging',type:'packaging',status:'active'},{code:'ASM-001',name:'Assembly Line',department:'assembly',type:'assembly',status:'active'},{code:'LTH-001',name:'CNC Lathe',department:'cnc',type:'lathe',status:'active'}];
const ss=[];for(let i=3;i>=0;i--){const d=new Date();d.setDate(d.getDate()-i);const ds=d.toISOString().split('T')[0];ms.forEach((m,idx)=>{ss.push({machineId:idx+1,date:ds,shiftType:'morning',totalTime:480,availableTime:420,operatingTime:390,totalParts:300+Math.floor(Math.random()*200),defectiveParts:Math.floor(Math.random()*30),goodParts:0,availability:92.8,performance:88.5,quality:95.2,oee:78+Math.random()*15});});}
ss.forEach(s=>s.goodParts=s.totalParts-s.defectiveParts);
await clearS('machines');await clearS('shifts');
for(let m of ms)await addItem('machines',m);
for(let s of ss.slice(0,DEMO.shifts))await addItem('shifts',s);
loadAll();notif('Sample data loaded!');
}
async function clearAll(){if(!confirm('Delete ALL?'))return;await clearS('machines');await clearS('shifts');loadAll();notif('Cleared');}
document.querySelectorAll('.tab').forEach(t=>t.addEventListener('click',function(){
document.querySelectorAll('.tab').forEach(x=>x.classList.remove('active'));document.querySelectorAll('.tab-content').forEach(x=>x.classList.remove('active'));
this.classList.add('active');document.getElementById(this.dataset.tab+'-tab').classList.add('active');
if(this.dataset.tab==='reports'||this.dataset.tab==='losses')loadAll();
}));
window.addEventListener('DOMContentLoaded',async()=>{await openDB();await loadSample();});
document.addEventListener('contextmenu',e=>{e.preventDefault();alert('⛔ Demo version. Purchase at digitalyar.company');});
</script>
</body>
</html>