-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
1010 lines (852 loc) · 34.1 KB
/
index.html
File metadata and controls
1010 lines (852 loc) · 34.1 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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Worker Shift Scheduler</title>
<style>
:root {
--primary-color: #667eea;
--secondary-color: #764ba2;
--text-color: #333;
--bg-color: white;
--input-bg: white;
--input-border: #e9ecef;
--header-bg: linear-gradient(135deg, #2c3e50, #34495e);
--section-bg: #f8f9fa;
--shadow-color: rgba(0, 0, 0, 0.2);
}
[data-theme="dark"] {
--text-color: #e9ecef;
--bg-color: #1a1a1a;
--input-bg: #2d2d2d;
--input-border: #404040;
--header-bg: linear-gradient(135deg, #0d1117, #161b22);
--section-bg: #2d2d2d;
--shadow-color: rgba(0, 0, 0, 0.4);
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
line-height: 1.6;
color: var(--text-color);
background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
min-height: 100vh;
padding: 20px;
transition: all 0.3s ease;
}
.container {
max-width: 1200px;
margin: 0 auto;
background: var(--bg-color);
border-radius: 12px;
box-shadow: 0 10px 30px var(--shadow-color);
overflow: hidden;
transition: all 0.3s ease;
}
.header {
background: var(--header-bg);
color: white;
padding: 30px;
text-align: center;
position: relative;
}
.header h1 {
font-size: 2.5rem;
margin-bottom: 10px;
font-weight: 300;
}
.header p {
font-size: 1.1rem;
opacity: 0.9;
}
.content {
display: grid;
grid-template-columns: 1fr 2fr;
gap: 0;
min-height: 600px;
}
.input-section {
background: var(--section-bg);
padding: 30px;
border-right: 1px solid var(--input-border);
transition: all 0.3s ease;
}
.output-section {
padding: 30px;
background: var(--bg-color);
transition: all 0.3s ease;
}
.form-group {
margin-bottom: 25px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: 600;
color: var(--text-color);
}
input, textarea, select {
width: 100%;
padding: 12px;
border: 2px solid var(--input-border);
border-radius: 8px;
font-size: 14px;
transition: border-color 0.3s ease;
background: var(--input-bg);
color: var(--text-color);
}
input:focus, textarea:focus, select:focus {
outline: none;
border-color: #667eea;
}
textarea {
resize: vertical;
min-height: 100px;
font-family: inherit;
}
.btn {
background: linear-gradient(135deg, #667eea, #764ba2);
color: white;
border: none;
padding: 14px 28px;
border-radius: 8px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
width: 100%;
}
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}
.btn:active {
transform: translateY(0);
}
.btn:disabled {
background: #bdc3c7;
cursor: not-allowed;
transform: none;
box-shadow: none;
}
.schedule-output {
background: var(--section-bg);
border-radius: 8px;
padding: 25px;
min-height: 400px;
max-height: 600px;
overflow-y: auto;
border: 2px solid var(--input-border);
transition: all 0.3s ease;
}
.schedule-round {
margin-bottom: 25px;
padding-bottom: 20px;
border-bottom: 1px solid #e9ecef;
}
.schedule-round:last-child {
border-bottom: none;
margin-bottom: 0;
}
.round-header {
font-size: 1.2rem;
font-weight: 600;
color: var(--text-color);
margin-bottom: 15px;
padding-bottom: 8px;
border-bottom: 2px solid var(--primary-color);
}
.shift-pair {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 15px;
margin-bottom: 8px;
background: var(--bg-color);
border-radius: 6px;
border-left: 4px solid var(--primary-color);
transition: all 0.3s ease;
}
.shift-label {
font-weight: 600;
color: #2c3e50;
}
.shift-workers {
color: #7f8c8d;
}
.stats-section {
background: var(--section-bg);
border-radius: 8px;
padding: 20px;
margin-top: 25px;
border-left: 4px solid var(--primary-color);
transition: all 0.3s ease;
}
.stats-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
margin-top: 15px;
}
.stat-item {
text-align: center;
padding: 15px;
background: var(--bg-color);
border-radius: 6px;
box-shadow: 0 2px 5px var(--shadow-color);
transition: all 0.3s ease;
}
.stat-value {
font-size: 1.5rem;
font-weight: 700;
color: var(--text-color);
margin-bottom: 5px;
}
.stat-label {
font-size: 0.9rem;
color: #7f8c8d;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.error-message {
background: #ffeaa7;
color: #d63031;
padding: 15px;
border-radius: 8px;
margin-bottom: 20px;
border-left: 4px solid #fdcb6e;
display: none;
}
.success-message {
background: #55efc4;
color: #00b894;
padding: 15px;
border-radius: 8px;
margin-bottom: 20px;
border-left: 4px solid #00b894;
display: none;
}
.loading {
display: none;
text-align: center;
padding: 20px;
color: #667eea;
}
.spinner {
border: 3px solid #f3f3f3;
border-top: 3px solid #667eea;
border-radius: 50%;
width: 30px;
height: 30px;
animation: spin 1s linear infinite;
margin: 0 auto 10px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.worker-names-hint {
font-size: 0.85rem;
color: #7f8c8d;
margin-top: 5px;
}
@media (max-width: 768px) {
.content {
grid-template-columns: 1fr;
}
.input-section {
border-right: none;
border-bottom: 1px solid var(--input-border);
}
.header h1 {
font-size: 2rem;
}
}
/* Dark mode toggle */
.theme-toggle {
position: absolute;
top: 20px;
right: 20px;
background: rgba(255, 255, 255, 0.2);
border: none;
border-radius: 50%;
width: 40px;
height: 40px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-size: 1.2rem;
transition: all 0.3s ease;
}
.theme-toggle:hover {
background: rgba(255, 255, 255, 0.3);
transform: scale(1.1);
}
/* Algorithm descriptions */
.algorithm-description {
margin-top: 8px;
padding: 12px;
background: var(--section-bg);
border-radius: 6px;
border-left: 3px solid var(--primary-color);
font-size: 0.9rem;
color: var(--text-color);
display: none;
}
.algorithm-description.active {
display: block;
}
/* Button group */
.button-group {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-top: 10px;
}
.btn-secondary {
background: linear-gradient(135deg, #6c757d, #495057);
}
.btn-secondary:hover {
box-shadow: 0 5px 15px rgba(108, 117, 125, 0.4);
}
.btn-tertiary {
background: linear-gradient(135deg, #28a745, #20c997);
}
.btn-tertiary:hover {
box-shadow: 0 5px 15px rgba(40, 167, 69, 0.4);
}
/* Print styles */
@media print {
body {
background: white !important;
padding: 0 !important;
font-size: 12pt;
}
.container {
box-shadow: none !important;
border-radius: 0 !important;
max-width: none !important;
margin: 0 !important;
}
.header {
background: #2c3e50 !important;
color: white !important;
page-break-after: avoid;
}
.input-section {
display: none !important;
}
.content {
grid-template-columns: 1fr !important;
display: block !important;
}
.schedule-output {
max-height: none !important;
border: none !important;
background: white !important;
padding: 20px !important;
}
.schedule-round {
page-break-inside: avoid;
page-break-after: auto;
margin-bottom: 30px;
padding-bottom: 15px;
border-bottom: 2px solid #e9ecef;
}
.schedule-round:last-child {
border-bottom: none;
}
.round-header {
font-size: 14pt !important;
font-weight: 600;
color: #2c3e50 !important;
margin-bottom: 15px;
padding-bottom: 8px;
border-bottom: 2px solid #667eea;
page-break-after: avoid;
}
.shift-pair {
display: flex;
justify-content: space-between;
align-items: center;
padding: 8px 12px;
margin-bottom: 6px;
background: white !important;
border-radius: 4px;
border-left: 3px solid #667eea;
page-break-inside: avoid;
}
.theme-toggle, .btn {
display: none !important;
}
.stats-section {
background: #f8f9fa !important;
border-left: 4px solid #667eea !important;
page-break-inside: avoid;
page-break-before: auto;
margin-top: 40px;
padding: 25px !important;
}
.stats-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 12px;
margin-top: 15px;
}
.stat-item {
text-align: center;
padding: 12px;
background: white !important;
border-radius: 4px;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
page-break-inside: avoid;
}
.stat-value {
font-size: 16pt !important;
font-weight: 700;
color: #2c3e50 !important;
margin-bottom: 4px;
}
.stat-label {
font-size: 9pt !important;
color: #7f8c8d !important;
text-transform: uppercase;
letter-spacing: 0.5px;
}
/* Ensure proper page breaks */
h2, h3 {
page-break-after: avoid;
}
/* Add page margins for better printing */
@page {
margin: 1cm;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>Worker Shift Scheduler</h1>
<p>Generate fair and balanced work schedules for your team</p>
<button class="theme-toggle" id="themeToggle" title="Toggle dark mode">
🌙
</button>
</div>
<div class="content">
<div class="input-section">
<div class="error-message" id="errorMessage"></div>
<div class="success-message" id="successMessage"></div>
<div class="form-group">
<label for="numWorkers">Number of Workers *</label>
<input type="number" id="numWorkers" min="2" max="50" value="4" required>
</div>
<div class="form-group">
<label for="workerNames">Worker Names (Optional)</label>
<textarea id="workerNames" placeholder="Enter names, one per line or comma-separated Example: John Smith Jane Doe Mike Johnson"></textarea>
<div class="worker-names-hint">
Leave empty to use generic names (Worker 1, Worker 2, etc.)
</div>
</div>
<div class="form-group">
<label for="algorithm">Scheduling Algorithm</label>
<select id="algorithm">
<option value="standard">Standard Round-Robin</option>
<option value="circle" selected>Circle Method</option>
</select>
<div class="algorithm-description" id="standardDescription">
<strong>Standard Round-Robin:</strong> Uses mathematical indexing with modulo operations. More computationally elegant but harder to understand. Best for performance with large worker counts.
</div>
<div class="algorithm-description" id="circleDescription">
<strong>Circle Method:</strong> Uses physical rotation of the worker array. More intuitive and visually clear. Easier to debug and understand. Recommended for most use cases.
</div>
</div>
<div class="button-group">
<button class="btn" id="generateBtn">
Generate Schedule
</button>
<button class="btn btn-secondary" id="clearBtn">
Clear Schedule
</button>
</div>
<div class="button-group" style="margin-top: 10px;">
<button class="btn btn-tertiary" id="printBtn">
Print Schedule
</button>
</div>
<div class="loading" id="loading">
<div class="spinner"></div>
Generating schedule...
</div>
<div class="footer" style="margin-top: 30px; padding-top: 20px; border-top: 1px solid var(--input-border);">
<div class="last-updated" id="lastUpdated" style="text-align: center; color: var(--text-color); font-size: 0.9rem; opacity: 0.7;">
Last Updated: <span id="timestamp"></span>
</div>
</div>
</div>
<div class="output-section">
<h2 style="margin-bottom: 20px; color: #2c3e50;">Generated Schedule</h2>
<div class="schedule-output" id="scheduleOutput">
<div style="text-align: center; color: #7f8c8d; padding: 50px;">
<p>Enter the number of workers and click "Generate Schedule" to create your schedule.</p>
</div>
</div>
<div class="stats-section" id="statsSection" style="display: none;">
<h3 style="margin-bottom: 15px; color: #2c3e50;">Schedule Statistics</h3>
<div class="stats-grid" id="statsGrid">
<!-- Stats will be populated here -->
</div>
</div>
</div>
</div>
</div>
<script>
// Include the schedule generator functions directly
function generateSchedule(numWorkers) {
if (numWorkers < 2) {
throw new Error("Number of workers must be at least 2");
}
const workers = Array.from({length: numWorkers}, (_, i) => i + 1);
const isOdd = numWorkers % 2 !== 0;
if (isOdd) {
workers.push('BYE');
numWorkers += 1;
}
const totalRounds = numWorkers - 1;
const shiftsPerRound = numWorkers / 2;
const schedule = [];
for (let round = 0; round < totalRounds; round++) {
const roundPairs = [];
roundPairs.push([workers[0], workers[numWorkers - 1]]);
for (let i = 1; i < shiftsPerRound; i++) {
const firstIndex = 1 + ((round + i - 1) % (numWorkers - 1));
const secondIndex = 1 + ((round + numWorkers - 2 - i) % (numWorkers - 1));
roundPairs.push([workers[firstIndex], workers[secondIndex]]);
}
if (isOdd) {
const filteredPairs = roundPairs.filter(pair => !pair.includes('BYE'));
schedule.push(filteredPairs);
} else {
schedule.push(roundPairs);
}
}
return schedule;
}
function generateScheduleCircleMethod(numWorkers) {
if (numWorkers < 2) {
throw new Error("Number of workers must be at least 2");
}
let workers = Array.from({length: numWorkers}, (_, i) => i + 1);
const isOdd = numWorkers % 2 !== 0;
if (isOdd) {
workers.push('BYE');
numWorkers += 1;
}
const totalRounds = numWorkers - 1;
const schedule = [];
for (let round = 0; round < totalRounds; round++) {
const roundPairs = [];
for (let i = 0; i < numWorkers / 2; i++) {
roundPairs.push([workers[i], workers[numWorkers - 1 - i]]);
}
if (isOdd) {
const filteredPairs = roundPairs.filter(pair => !pair.includes('BYE'));
schedule.push(filteredPairs);
} else {
schedule.push(roundPairs);
}
const fixed = workers[0];
const rotated = [fixed, workers[numWorkers - 1], ...workers.slice(1, numWorkers - 1)];
workers = rotated;
}
return schedule;
}
function formatSchedule(schedule, workerNames = null) {
if (workerNames && workerNames.length !== Math.max(...schedule.flat(2).filter(x => typeof x === 'number'))) {
console.warn("Worker names count doesn't match schedule");
workerNames = null;
}
const result = [];
result.push("=== WORKER SCHEDULE ===");
result.push("");
schedule.forEach((round, roundIndex) => {
result.push(`Round ${roundIndex + 1}:`);
round.forEach((pair, pairIndex) => {
const pairNames = pair.map(worker =>
workerNames ? (worker === 'BYE' ? 'BYE' : workerNames[worker - 1]) : `Worker ${worker}`
);
result.push(` Shift ${pairIndex + 1}: ${pairNames[0]} & ${pairNames[1]}`);
});
result.push('');
});
return result.join('\n');
}
function getScheduleStats(schedule) {
const allPairs = new Set();
const workerShifts = {};
const allWorkers = [...new Set(schedule.flat(2).filter(x => typeof x === 'number'))];
allWorkers.forEach(worker => { workerShifts[worker] = 0; });
schedule.forEach(round => {
round.forEach(pair => {
if (!pair.includes('BYE')) {
const sortedPair = [...pair].sort((a, b) => a - b);
allPairs.add(`${sortedPair[0]}-${sortedPair[1]}`);
workerShifts[pair[0]]++;
workerShifts[pair[1]]++;
}
});
});
const totalRounds = schedule.length;
const shiftsPerRound = schedule[0] ? schedule[0].length : 0;
const totalWorkers = allWorkers.length;
return {
totalWorkers,
totalRounds,
shiftsPerRound,
totalUniquePairs: allPairs.size,
expectedPairs: totalWorkers * (totalWorkers - 1) / 2,
workerShifts,
isComplete: allPairs.size === totalWorkers * (totalWorkers - 1) / 2
};
}
// UI Functions
function showError(message) {
const errorEl = document.getElementById('errorMessage');
errorEl.textContent = message;
errorEl.style.display = 'block';
document.getElementById('successMessage').style.display = 'none';
}
function showSuccess(message) {
const successEl = document.getElementById('successMessage');
successEl.textContent = message;
successEl.style.display = 'block';
document.getElementById('errorMessage').style.display = 'none';
}
function hideMessages() {
document.getElementById('errorMessage').style.display = 'none';
document.getElementById('successMessage').style.display = 'none';
}
function parseWorkerNames(input) {
if (!input.trim()) return null;
// Split by newlines or commas, trim each name, and filter out empty strings
const names = input.split(/[\n,]/)
.map(name => name.trim())
.filter(name => name.length > 0);
return names.length > 0 ? names : null;
}
function generateScheduleUI() {
hideMessages();
const numWorkersInput = document.getElementById('numWorkers');
const workerNamesInput = document.getElementById('workerNames');
const algorithmSelect = document.getElementById('algorithm');
const generateBtn = document.getElementById('generateBtn');
const loadingEl = document.getElementById('loading');
const numWorkers = parseInt(numWorkersInput.value);
const workerNames = parseWorkerNames(workerNamesInput.value);
const algorithm = algorithmSelect.value;
// Validation
if (isNaN(numWorkers) || numWorkers < 2) {
showError('Please enter a valid number of workers (minimum 2)');
return;
}
if (numWorkers > 50) {
showError('Maximum number of workers is 50 for optimal performance');
return;
}
if (workerNames && workerNames.length !== numWorkers) {
showError(`Number of worker names (${workerNames.length}) doesn't match number of workers (${numWorkers})`);
return;
}
// Show loading
generateBtn.disabled = true;
loadingEl.style.display = 'block';
// Simulate async operation for better UX
setTimeout(() => {
try {
let schedule;
if (algorithm === 'circle') {
schedule = generateScheduleCircleMethod(numWorkers);
} else {
schedule = generateSchedule(numWorkers);
}
displaySchedule(schedule, workerNames);
displayStats(schedule);
showSuccess(`Schedule generated successfully for ${numWorkers} workers!`);
} catch (error) {
showError('Error generating schedule: ' + error.message);
clearOutput();
} finally {
generateBtn.disabled = false;
loadingEl.style.display = 'none';
}
}, 100);
}
function displaySchedule(schedule, workerNames) {
const outputEl = document.getElementById('scheduleOutput');
if (schedule.length === 0) {
outputEl.innerHTML = '<div style="text-align: center; color: #7f8c8d; padding: 50px;">No schedule could be generated.</div>';
return;
}
let html = '';
schedule.forEach((round, roundIndex) => {
html += `<div class="schedule-round">`;
html += `<div class="round-header">Round ${roundIndex + 1}</div>`;
round.forEach((pair, pairIndex) => {
const pairNames = pair.map(worker =>
workerNames ? (worker === 'BYE' ? 'BYE' : workerNames[worker - 1]) : `Worker ${worker}`
);
html += `<div class="shift-pair">`;
html += `<span class="shift-label">Shift ${pairIndex + 1}</span>`;
html += `<span class="shift-workers">${pairNames[0]} & ${pairNames[1]}</span>`;
html += `</div>`;
});
html += `</div>`;
});
outputEl.innerHTML = html;
}
function displayStats(schedule) {
const statsSection = document.getElementById('statsSection');
const statsGrid = document.getElementById('statsGrid');
if (schedule.length === 0) {
statsSection.style.display = 'none';
return;
}
const stats = getScheduleStats(schedule);
let statsHtml = `
<div class="stat-item">
<div class="stat-value">${stats.totalWorkers}</div>
<div class="stat-label">Total Workers</div>
</div>
<div class="stat-item">
<div class="stat-value">${stats.totalRounds}</div>
<div class="stat-label">Total Rounds</div>
</div>
<div class="stat-item">
<div class="stat-value">${stats.shiftsPerRound}</div>
<div class="stat-label">Shifts per Round</div>
</div>
<div class="stat-item">
<div class="stat-value">${stats.totalUniquePairs}</div>
<div class="stat-label">Unique Pairs</div>
</div>
<div class="stat-item">
<div class="stat-value">${stats.isComplete ? '✓' : '✗'}</div>
<div class="stat-label">Complete Schedule</div>
</div>
`;
statsGrid.innerHTML = statsHtml;
statsSection.style.display = 'block';
}
function clearOutput() {
document.getElementById('scheduleOutput').innerHTML =
'<div style="text-align: center; color: #7f8c8d; padding: 50px;">Enter the number of workers and click "Generate Schedule" to create your schedule.</div>';
document.getElementById('statsSection').style.display = 'none';
}
// Dark mode functionality
function toggleDarkMode() {
const currentTheme = document.documentElement.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
document.documentElement.setAttribute('data-theme', newTheme);
const themeToggle = document.getElementById('themeToggle');
themeToggle.textContent = newTheme === 'dark' ? '☀️' : '🌙';
// Save preference to localStorage
localStorage.setItem('theme', newTheme);
}
// Initialize theme from localStorage or system preference
function initializeTheme() {
const savedTheme = localStorage.getItem('theme');
const systemPrefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
let theme = savedTheme || (systemPrefersDark ? 'dark' : 'light');
document.documentElement.setAttribute('data-theme', theme);
const themeToggle = document.getElementById('themeToggle');
themeToggle.textContent = theme === 'dark' ? '☀️' : '🌙';
}
// Algorithm description functionality
function updateAlgorithmDescription() {
const algorithm = document.getElementById('algorithm').value;
// Hide all descriptions
document.querySelectorAll('.algorithm-description').forEach(desc => {
desc.classList.remove('active');
});
// Show selected description
const selectedDesc = document.getElementById(algorithm + 'Description');
if (selectedDesc) {
selectedDesc.classList.add('active');
}
}
// Print schedule functionality
function printSchedule() {
const scheduleOutput = document.getElementById('scheduleOutput');
const statsSection = document.getElementById('statsSection');
if (scheduleOutput.innerHTML.includes('Enter the number of workers')) {
showError('No schedule to print. Please generate a schedule first.');
return;
}
// Show success message
showSuccess('Opening print dialog...');
// Use setTimeout to ensure the message is visible before print
setTimeout(() => {
window.print();
}, 500);
}
// Auto-sync worker count with names
function syncWorkerCount() {
const workerNamesInput = document.getElementById('workerNames');
const numWorkersInput = document.getElementById('numWorkers');
const names = parseWorkerNames(workerNamesInput.value);
if (names && names.length > 0) {
// Update the number of workers to match the names count
numWorkersInput.value = names.length;
}
}
// Update timestamp function
function updateTimestamp() {
const now = new Date();
const timestampEl = document.getElementById('timestamp');
const options = {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
timeZoneName: 'short'
};
timestampEl.textContent = now.toLocaleString('en-US', options);
}
// Clear schedule functionality
function clearSchedule() {
clearOutput();
hideMessages();
showSuccess('Schedule cleared successfully!');
}
// Event listeners
document.getElementById('generateBtn').addEventListener('click', generateScheduleUI);
document.getElementById('clearBtn').addEventListener('click', clearSchedule);
document.getElementById('printBtn').addEventListener('click', printSchedule);
document.getElementById('themeToggle').addEventListener('click', toggleDarkMode);
document.getElementById('algorithm').addEventListener('change', updateAlgorithmDescription);
document.getElementById('workerNames').addEventListener('blur', syncWorkerCount);
// Allow Enter key to trigger generation
document.getElementById('numWorkers').addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
generateScheduleUI();
}
});