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0dd6
 
 
 
 
 
 
 
 
 
 
 
 
 
ae4866a
60c0dd6
 
 
 
 
ae4866a
60c0dd6
 
 
 
 
 
 
 
ae4866a
60c0dd6
ae4866a
 
60c0dd6
 
 
 
 
 
 
 
 
 
 
ae4866a
 
 
 
60c0dd6
 
 
 
 
 
ae4866a
 
 
 
60c0dd6
 
 
 
 
 
 
ae4866a
60c0dd6
 
 
 
 
ae4866a
60c0dd6
 
 
 
 
 
 
 
ae4866a
60c0dd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Holiday service class
class RomanianHolidays {
  constructor() {
    this.baseUrl = 'https://date.nager.at/api/v3';
  }

  // Get all holidays for a year
  async getHolidays(year = new Date().getFullYear()) {
    try {
      const response = await fetch(`${this.baseUrl}/PublicHolidays/${year}/RO`);
      if (!response.ok) throw new Error('Network response was not ok');
      return await response.json();
    } catch (error) {
      console.error('Error fetching holidays:', error);
      return null;
    }
  }

  // Check if a specific date is a holiday
  async isHoliday(date = new Date()) {
    const year = date.getFullYear();
    const dateString = date.toISOString().split('T')[0]; // YYYY-MM-DD

    const holidays = await this.getHolidays(year);
    if (!holidays) return false;

    return holidays.some(holiday => holiday.date === dateString);
  }

  // Get upcoming holidays
  async getUpcomingHolidays(count = 5) {
    const currentYear = new Date().getFullYear();
    const holidays = await this.getHolidays(currentYear);

    if (!holidays) return [];

    const today = new Date().toISOString().split('T')[0];

    return holidays
      .filter(holiday => holiday.date >= today)
      .slice(0, count);
  }

  // Get holidays for multiple years
  async getHolidaysRange(startYear, endYear) {
    const years = Array.from({ length: endYear - startYear + 1 }, (_, i) => startYear + i);
    const promises = years.map(year => this.getHolidays(year));

    try {
      const results = await Promise.all(promises);
      return results.flat();
    } catch (error) {
      console.error('Error fetching holiday range:', error);
      return null;
    }
  }
}

// Global variables
const monthNamesRo = ["ianuarie","februarie","martie","aprilie","mai","iunie","iulie","august","septembrie","octombrie","noiembrie","decembrie"];
let currentYear = new Date().getFullYear();
let selectedYear = currentYear;
let selectedMonth = new Date().getMonth();
let leaveDays = [];
const refYear = 2017;
const minYear = refYear + 1;
const maxYear = 2037;

// Planner 2026 variables
let plannerYear = currentYear;
let plannerMonth = 0; // January
let plannerLeaveDays = [];
let plannerWorkedDays = 0;
let plannerTotalHours = 0;

// Initialize holiday service
const holidayService = new RomanianHolidays();

// Tab functionality
document.addEventListener('DOMContentLoaded', function() {
  const tabs = document.querySelectorAll('.tab');
  const tabContents = document.querySelectorAll('.tab-content');

  tabs.forEach(tab => {
    tab.addEventListener('click', () => {
      // Remove active class from all tabs and contents
      tabs.forEach(t => t.classList.remove('active'));
      tabContents.forEach(c => c.classList.remove('active'));

      // Add active class to clicked tab and corresponding content
      tab.classList.add('active');
      const tabId = tab.getAttribute('data-tab');
      document.getElementById(`${tabId}-tab`).classList.add('active');

      // If switching to planner 2026, update it
      if (tabId === 'planner2026') {
        updatePlanner2026();
      }
    });
  });
});

function handleUrlParams() {
  const urlParams = new URLSearchParams(window.location.search);
  if (urlParams.has('user')) localStorage.setItem('user', urlParams.get('user'));
  if (urlParams.has('tura')) localStorage.setItem('tura', urlParams.get('tura'));
  updateUserInfo();
}

function updateUserInfo() {
  const storedUser = localStorage.getItem('user');
  const storedTura = localStorage.getItem('tura');
  const userInfoElement = document.getElementById('userInfo');
  if (storedUser || storedTura) {
    let info = 'Configurare: ';
    if (storedUser) info += `Utilizator: ${storedUser} `;
    if (storedTura) info += `Tură: ${storedTura}`;
    userInfoElement.textContent = info;
    userInfoElement.style.display = 'block';
  } else {
    userInfoElement.textContent = 'Eroare!';
    userInfoElement.style.display = 'block';
  }
}

function getTuraFromUrl() {
  let tura = 2;
  const urlParams = new URLSearchParams(window.location.search);
  if (urlParams.has("tura")) {
    tura = parseInt(urlParams.get("tura"));
  } else if (urlParams.has("user")) {
    const users = ["ljc1q", "xxtoo", "fras0", "l3hb4"];
    const idx = users.indexOf(urlParams.get("user"));
    if (idx >= 0) tura = idx + 1;
  } else {
    const storedTura = localStorage.getItem('tura');
    const storedUser = localStorage.getItem('user');
    if (storedTura) {
      tura = parseInt(storedTura);
    } else if (storedUser) {
      const users = ["ljc1q", "xxtoo", "fras0", "l3hb4"];
      const idx = users.indexOf(storedUser);
      if (idx >= 0) tura = idx + 1;
    }
  }
  if (tura % 2 === 0) tura = 6 - tura;
  return tura;
}

let plannerShift = getTuraFromUrl(); // Default shift
document.getElementById("shift-planner").textContent = plannerShift;

function isPWA() {
  return window.navigator.standalone === true || 
    window.matchMedia('(display-mode: standalone)').matches ||
    window.matchMedia('(display-mode: fullscreen)').matches;
}

function createYearButtons() {
  const yearButtonsContainer = document.getElementById('yearButtons');
  yearButtonsContainer.innerHTML = '';
  for (let year = currentYear - 3; year <= currentYear + 2; year++) {
    const button = document.createElement('button');
    button.className = 'year-btn';
    if (year === selectedYear) button.classList.add('active');
    button.textContent = year;
    button.onclick = function () {
      selectedYear = year;
      saveToLocalStorage();
      updateYearButtons();
      document.getElementById("holidayResult").innerHTML = ``;
      updateCalendar();
    };
    yearButtonsContainer.appendChild(button);
  }
}

function updateYearButtons() {
  const buttons = document.querySelectorAll('.year-btn');
  buttons.forEach(button => {
    if (parseInt(button.textContent) === selectedYear)
      button.classList.add('active');
    else
      button.classList.remove('active');
  });
}

function populateMonthSelect() {
  const monthSelect = document.getElementById('monthSelect');
  monthSelect.innerHTML = '';
  for (let month = 0; month < 12; month++) {
    const option = document.createElement('option');
    option.value = month;
    option.textContent = monthNamesRo[month];
    if (month === selectedMonth) option.selected = true;
    monthSelect.appendChild(option);
  }
  monthSelect.addEventListener('change', function () {
    selectedMonth = parseInt(monthSelect.value);
    saveToLocalStorage();
    document.getElementById("holidayResult").innerHTML = ``;
    updateCalendar();
  });
}

function loadFromLocalStorage() {
  const storedYear = localStorage.getItem('selectedYear');
  const storedMonth = localStorage.getItem('selectedMonth');
  const storedLeaveDays = localStorage.getItem('leaveDays');

  if (storedYear) selectedYear = parseInt(storedYear);
  if (storedMonth) selectedMonth = parseInt(storedMonth);
  if (storedLeaveDays) leaveDays = JSON.parse(storedLeaveDays);

  // Load planner 2026 data
  const storedPlannerMonth = localStorage.getItem('plannerMonth');
  const storedPlannerShift = localStorage.getItem('plannerShift');
  const storedPlannerLeaveDays = localStorage.getItem('plannerLeaveDays');

  if (storedPlannerMonth) plannerMonth = parseInt(storedPlannerMonth);
  if (storedPlannerShift) plannerShift = parseInt(storedPlannerShift);
  if (storedPlannerLeaveDays) plannerLeaveDays = JSON.parse(storedPlannerLeaveDays);
}

function saveToLocalStorage() {
  localStorage.setItem('selectedYear', selectedYear);
  localStorage.setItem('selectedMonth', selectedMonth);
  localStorage.setItem('leaveDays', JSON.stringify(leaveDays));

  // Save planner 2026 data
  localStorage.setItem('plannerMonth', plannerMonth);
  localStorage.setItem('plannerShift', plannerShift);
  localStorage.setItem('plannerLeaveDays', JSON.stringify(plannerLeaveDays));
}

function initializeControls() {
  handleUrlParams();
  loadFromLocalStorage();
  createYearButtons();
  populateMonthSelect();

  // Initialize planner 2026 controls
  document.getElementById('prev-year').addEventListener('click', () => {
    plannerYear = plannerYear - 1;
    if (plannerYear < minYear)
      plannerYear = minYear;
    updatePlanner2026();
  });

  document.getElementById('next-year').addEventListener('click', () => {
    plannerYear = plannerYear + 1;
    if (plannerYear > maxYear)
      plannerYear = maxYear;
    updatePlanner2026();
  });

  // Initialize planner 2026 controls
  document.getElementById('prev-month').addEventListener('click', () => {
    plannerMonth = (plannerMonth - 1 + 12) % 12;
    updatePlanner2026();
  });

  document.getElementById('next-month').addEventListener('click', () => {
    plannerMonth = (plannerMonth + 1) % 12;
    updatePlanner2026();
  });

  // Initialize optimize button
  document.getElementById('optimize-leave').addEventListener('click', optimizeLeaveDays);

  if (!isPWA()) {
    const urlParams = new URLSearchParams(window.location.search);
    const storedUser = localStorage.getItem('user');
    const storedTura = localStorage.getItem('tura');
    let urlChanged = false;
    if (storedUser && !urlParams.has('user')) { urlParams.set('user', storedUser); urlChanged = true; }
    if (storedTura && !urlParams.has('tura')) { urlParams.set('tura', storedTura); urlChanged = true; }
    if (urlChanged) {
      const newUrl = `${window.location.pathname}?${urlParams.toString()}`;
      window.history.replaceState({}, '', newUrl);
    }
  }
}

function toggleLeaveDay(year, month, day) {
  const dateKey = `${year}-${month}-${day}`;
  const index = leaveDays.indexOf(dateKey);

  if (index === -1) {
    leaveDays.push(dateKey);
  } else {
    leaveDays.splice(index, 1);
  }

  saveToLocalStorage();
  updateCalendar();
}

function updateCalendar() {
  const daysInMonth = new Date(selectedYear, selectedMonth + 1, 0).getDate();
  const firstDay = (new Date(selectedYear, selectedMonth, 1).getDay() + 6) % 7;
  let tura = getTuraFromUrl();
  const date0 = new Date(refYear, 0, 1);
  let fakeDayOfYear = Math.ceil((new Date(selectedYear, selectedMonth, 1) - date0) / 86400000);

  let html = `<table><tr><th>lun</th><th>mar</th><th>mie</th><th>joi</th><th>vin</th><th>sâm</th><th>dum</th></tr><tr>`;
  let dayCount = 1;

  for (let i = 0; i < 42; i++) {
    if (i >= firstDay && dayCount <= daysInMonth) {
      const dateKey = `${selectedYear}-${selectedMonth}-${dayCount}`;
      const isLeaveDay = leaveDays.includes(dateKey);

      let dayClass = isLeaveDay ? 'doy4' : `doy${(fakeDayOfYear + tura) % 4}`;

      html += `<td class="${dayClass}" data-day="${dayCount}">${dayCount}</td>`;
      fakeDayOfYear++; 
      dayCount++;
    } else {
      html += `<td class="unclickable"></td>`;
    }

    if (i % 7 === 6 && dayCount <= daysInMonth) html += `</tr><tr>`;
    if (dayCount > daysInMonth && i % 7 === 6) break;
  }

  html += `</tr></table>`;
  document.getElementById("calendarContainer").innerHTML = html;

  const ldcount = leaveDays.length;
  const currentMonth = `${selectedYear}-${selectedMonth}-`;
  const currentmlc = leaveDays.filter(day => day.startsWith(currentMonth)).length;
  const mlc2 = ldcount ? `(${currentmlc}/${ldcount})`: ``;
  document.getElementById("selectedMonth").innerHTML = `Calendar ${monthNamesRo[selectedMonth]} ${selectedYear} ${mlc2}`;

  const dayCells = document.querySelectorAll('#calendarContainer td[data-day]');
  dayCells.forEach(cell => {
    cell.addEventListener('click', function() {
      const day = parseInt(this.getAttribute('data-day'));
      toggleLeaveDay(selectedYear, selectedMonth, day);
    });
  });
}

function getEasterDate(year) {
  const a = year % 4, b = year % 7, c = year % 19;
  const d = (19 * c + 15) % 30;
  const e = (2 * a + 4 * b - d + 34) % 7;
  const month = Math.floor((d + e + 114) / 31);
  const day = ((d + e + 114) % 31) + 1;
  let date = new Date(year, month - 1, day);
  date.setDate(date.getDate() + 13);
  return date;
}

function getDayName(date) {
  const days = ['duminică', 'luni', 'marți', 'miercuri', 'joi', 'vineri', 'sâmbătă'];
  return days[date.getDay()];
}

function findClosestWorkShift(holidayDate, tura) {
  const date0 = new Date(2024, 0, 1);
  let closest = null, minDistance = Infinity;
  for (let offset = -10; offset <= 10; offset++) {
    let d = new Date(holidayDate);
    d.setDate(d.getDate() + offset);
    const daysDiff = Math.ceil((d - date0) / 86400000);
    const shift = (daysDiff + tura) % 4;
    if (shift === 2 || shift === 3) {
      let dist = Math.abs(offset);
      if (dist < minDistance) {
        minDistance = dist;
        closest = {
          date: d,
          shift: shift === 2 ? 'de zi' : 'de noapte',
          dayName: getDayName(d)
        };
      }
    }
  }
  return closest;
}

function checkHoliday(type) {
  const currentDate = new Date();
  const currentYear = currentDate.getFullYear();
  const tura = getTuraFromUrl();
  let date, name = '';

  if (type === "lcrt") { 
    date = new Date(); 
  } else if (type === "lless") { 
    date = new Date(selectedYear, selectedMonth - 1, new Date().getDate()); 
  } else if (type === "lgrt") {
    date = new Date(selectedYear, selectedMonth + 1, new Date().getDate());
    if (date.getFullYear() > currentYear + 2) date = new Date(currentYear + 2, 11, new Date().getDate());
  } else if (type === "lurm") { 
    date = new Date(currentYear, new Date().getMonth() + 1, 15); 
  } else if (type === "curm") {
    date = new Date(selectedYear, selectedMonth, 15);
    if (leaveDays) {
      while (true) {
        date = new Date(date.getFullYear(), date.getMonth() + 1, 15);
        if (date.getFullYear() > currentYear + 2) {
          date = new Date(currentYear - 1, 0, 15);
        }
        if (date.getFullYear() === selectedYear && date.getMonth() === selectedMonth) {
          break;
        }
        const currentMonth = `${date.getFullYear()}-${date.getMonth()}-`;
        const currentmlc = leaveDays.filter(day => day.startsWith(currentMonth)).length;
        if (currentmlc) break;  
      }
    }
  } else {
    for (let year = currentYear; year <= currentYear + 10; year++) {
      if (type === 'paste') { 
        date = getEasterDate(year); 
        name = 'Paște'; 
      } else if (type === 'craciun') { 
        date = new Date(year, 11, 25); 
        name = 'Crăciun'; 
      } else if (type === 'revelion') { 
        date = new Date(year, 0, 1); 
        name = 'Revelion'; 
      }
      if (date > currentDate) break;
    }
  }

  const closest = findClosestWorkShift(date, tura);
  if (closest) {
    const m = monthNamesRo[closest.date.getMonth()];
    const y = closest.date.getFullYear();
    const d = closest.date.getDate();
    const text = `De ${name} ${y} sunt ${closest.shift} ${closest.dayName}, ${d} ${m} ${y}`;
    if (name) {
      document.getElementById('holidayResult').textContent = text;
    } else {
      document.getElementById("holidayResult").innerHTML = ``;
    }
    selectedYear = y;
    selectedMonth = closest.date.getMonth();
  }

  saveToLocalStorage();
  updateYearButtons();
  populateMonthSelect();
  updateCalendar();
}

// Planner functionality
const daysInDecember = 31;
const saturdays = [6, 13, 20, 27];
const sundays = [7, 14, 21, 28];
const hoursPerWorkedDay = 12;
const hoursPerLeaveDay = 8;
let totalHours = 0;
const tura = getTuraFromUrl();
let leaveDaysPlanner = 0;
let decemberHolidays = [1, 25, 26];

// Function to get December holidays for a specific year
async function getDecemberHolidays(year = 2025) {
  try {
    const allHolidays = await holidayService.getHolidays(year);
    if (!allHolidays) {
      console.warn('Could not fetch holidays, using fallback');
      return [1, 25, 26]; // Fallback to hardcoded values
    }

    // Filter holidays that are in December and extract the day
    const decemberHolidays = allHolidays
      .filter(holiday => {
        const month = parseInt(holiday.date.split('-')[1]);
        return month === 12;
      })
      .map(holiday => parseInt(holiday.date.split('-')[2]));

    console.log('December holidays:', decemberHolidays);
    return decemberHolidays;
  } catch (error) {
    console.error('Error getting December holidays:', error);
    return [1, 25, 26]; // Fallback to hardcoded values
  }
}

async function updateHolidays() {
  decemberHolidays = await getDecemberHolidays(2025);
  return decemberHolidays;
}

function updateStats() {
  const totalHoursDisplay = document.getElementById("total-hours");
  const leaveDaysDisplay = document.getElementById("leave-days");

  totalHoursDisplay.textContent = totalHours;
  leaveDaysDisplay.textContent = leaveDaysPlanner;
}

async function renderPlanner() {
  const calendar = document.querySelector(".planner-calendar");

  // Update holidays before rendering
  await updateHolidays();

  calendar.innerHTML = ``;
  totalHours = 0;
  leaveDaysPlanner = 0;

  for (let day = 1; day <= daysInDecember; day++) {
    const dayElement = document.createElement("div");
    dayElement.classList.add("combined-day");
    dayElement.textContent = day;

    // Determine initial day type
    const isHoliday = decemberHolidays.includes(day) || saturdays.includes(day) || sundays.includes(day);
    const isWorkDay = ((day + 7 - plannerShift) % 4 < 2);

    if (isHoliday) {
      dayElement.classList.add("holiday");
    }
    if (isWorkDay) {
      dayElement.classList.add("workday");
      totalHours += hoursPerWorkedDay;
    } else {
      dayElement.classList.add("day-off");
    }

    dayElement.addEventListener("click", () => toggleDayStatus(dayElement, day));
    calendar.appendChild(dayElement);
  }
  updateStats();
}

function toggleDayStatus(dayElement, day) {
  const isHoliday = decemberHolidays.includes(day) || saturdays.includes(day) || sundays.includes(day);
  const isWorkDay = ((day + 7 - plannerShift) % 4 < 2);
  if (isWorkDay) {
    if (dayElement.classList.contains("holiday")) {
      if (dayElement.classList.contains("workday")) {
        dayElement.classList.remove("workday");
        dayElement.classList.add("leave");
        totalHours -= hoursPerWorkedDay;
      } else if (dayElement.classList.contains("leave")) {
        dayElement.classList.remove("leave");
        dayElement.classList.add("workday");
        totalHours += hoursPerWorkedDay;
      }
    } else {
      if (dayElement.classList.contains("workday")) {
        dayElement.classList.remove("workday");
        dayElement.classList.add("leave");
        totalHours -= 4;
        leaveDaysPlanner += 1;
      } else if (dayElement.classList.contains("leave")) {
        dayElement.classList.remove("leave");
        dayElement.classList.add("workday");
        totalHours += 4;
        leaveDaysPlanner -= 1;
      }
    }
  } else {
    if (dayElement.classList.contains("holiday")) {
      if (dayElement.classList.contains("leave")) {
        dayElement.classList.remove("leave");
      } else {
        dayElement.classList.add("leave");
      }
    } else {
      if (dayElement.classList.contains("leave")) {
        dayElement.classList.remove("leave");
        totalHours -= 8;
        leaveDaysPlanner -= 1;
      } else {
        dayElement.classList.add("leave");
        totalHours += 8;
        leaveDaysPlanner += 1;
      }
    }
  }

  updateStats();
}

// Planner 2026 functionality
async function updatePlanner2026() {
  const calendar = document.getElementById("planner-2026-calendar");
  const monthDisplay = document.getElementById("current-month");
  const yearDisplay = document.getElementById("current-year");
  const shiftDisplay = document.getElementById("shift-planner");
  const workedDaysDisplay = document.getElementById("worked-days");
  const leaveDaysDisplay = document.getElementById("leave-days-2026");
  const totalHoursDisplay = document.getElementById("total-hours-2026");

  // Update display
  monthDisplay.textContent = `${monthNamesRo[plannerMonth]}`;
  yearDisplay.textContent = `${plannerYear}`;
  plannerShift = getTuraFromUrl();
  if (plannerShift % 2 === 0) {
    plannerShift = 6 - plannerShift;
  }
  shiftDisplay.textContent = plannerShift;

  // Get holidays for the current month
  const holidays = await getHolidaysForMonth(plannerYear, plannerMonth);

  // Calculate days in month
  const daysInMonth = new Date(plannerYear, plannerMonth + 1, 0).getDate();
  const firstDay = (new Date(plannerYear, plannerMonth, 1).getDay() + 6) % 7;

  let targetHours = 0;
  for (let day = 1; day <= daysInMonth; day++) {
    if (!holidays.includes(day) && (new Date(plannerYear, plannerMonth, day).getDay() % 6) != 0)
      targetHours += 8;
  }
  document.getElementById("target-hours-2026").textContent = targetHours;
  // Reset stats
  plannerWorkedDays = 0;
  plannerTotalHours = 0;
  let currentLeaveDays = 0;

  // Clear calendar
  calendar.innerHTML = '';

  // Add empty cells for days before the first day of the month
  for (let i = 0; i < firstDay; i++) {
    const emptyDay = document.createElement("div");
    emptyDay.classList.add("combined-day");
    emptyDay.classList.add("unclickable");
    calendar.appendChild(emptyDay);
  }

  // Add days of the month
  for (let day = 1; day <= daysInMonth; day++) {
    const dayElement = document.createElement("div");
    dayElement.classList.add("combined-day");

    // Add day number
    const dayNumber = document.createElement("div");
    dayNumber.classList.add("day-number");
    dayNumber.textContent = day;
    dayElement.appendChild(dayNumber);

    // Calculate shift
    const date0 = new Date(refYear, 0, 1);
    const currentDate = new Date(plannerYear, plannerMonth, day);
    const daysDiff = Math.ceil((currentDate - date0) / 86400000);
    let shift2 = plannerShift;
    if (shift2 % 2 === 0)
      shift2 = 6 - shift2;
    const shift = (daysDiff + shift2) % 4;

    // Add shift info
    const shiftInfo = document.createElement("div");
    shiftInfo.classList.add("day-shift");

    let shiftText = '';
    let isWorkDay = false;

    if (shift === 0 || shift === 1) {
      shiftText = 'Repaus';
      dayElement.classList.add("day-off");
    } else if (shift === 2) {
      shiftText = 'Zi';
      dayElement.classList.add("workday");
      isWorkDay = true;
    } else if (shift === 3) {
      shiftText = 'Noapte';
      dayElement.classList.add("workday");
      isWorkDay = true;
    }

    shiftInfo.textContent = shiftText;
    dayElement.appendChild(shiftInfo);

    // Check if holiday
    const isHoliday = holidays.includes(day) || 
      currentDate.getDay() === 0 || // Sunday
      currentDate.getDay() === 6;   // Saturday

    if (isHoliday) {
      dayElement.classList.add("holiday");
    }

    // Check if leave day
    const dateKey = `${plannerYear}-${plannerMonth}-${day}`;
    const isLeaveDay = plannerLeaveDays.includes(dateKey);

    if (isLeaveDay) {
      dayElement.classList.add("leave");
      if (!isHoliday) {
        currentLeaveDays++;
      }

      // Calculate hours for leave day
      if (!isHoliday && !isWorkDay) {
        plannerTotalHours += 8; // 8 hours for leave on holiday/day off
      } else if (!isHoliday && isWorkDay) {
        plannerTotalHours += 8; // 4 hours for leave on work day
      }
    } else if (isWorkDay) {
      plannerWorkedDays++;
      plannerTotalHours += 12; // 12 hours for work day
    }

    // Add click event
    dayElement.addEventListener('click', () => togglePlannerDay(plannerYear, plannerMonth, day));

    calendar.appendChild(dayElement);
  }

  // Update stats
  workedDaysDisplay.textContent = plannerWorkedDays;
  leaveDaysDisplay.textContent = currentLeaveDays;
  totalHoursDisplay.textContent = plannerTotalHours;

  // Save to localStorage
  saveToLocalStorage();
}

async function getHolidaysForMonth(year, month) {
  try {
    const allHolidays = await holidayService.getHolidays(year);
    if (!allHolidays) {
      console.warn('Could not fetch holidays, using fallback');
      // Return fallback holidays for each month
      return getFallbackHolidays(year, month);
    }

    // Filter holidays that are in the specified month and extract the day
    const monthHolidays = allHolidays
      .filter(holiday => {
        const holidayMonth = parseInt(holiday.date.split('-')[1]);
        return holidayMonth === month + 1; // API uses 1-based months
      })
      .map(holiday => parseInt(holiday.date.split('-')[2]));

    return monthHolidays;
  } catch (error) {
    console.error('Error getting holidays for month:', error);
    return getFallbackHolidays(year, month);
  }
}

function getFallbackHolidays(year, month) {
  // Fallback holidays for Romania
  const holidays = {
    0: [1, 2, 24], // January: 1, 2 (Revelion), 24 (Unirea Principatelor Române)
    3: [], // April: Easter is dynamic, will be handled separately
    4: [1], // May: 1 (Ziua Muncii)
    5: [1], // June: 1 (Ziua Copilului)
    7: [15], // August: 15 (Adormirea Maicii Domnului)
    10: [1, 30], // November: 1 (Ziua Națională), 30 (Sf. Andrei)
    11: [1, 25, 26] // December: 1 (Ziua Națională), 25, 26 (Crăciun)
  };

  // Add Easter for April if applicable
  if (month === 3) {
    const easter = getEasterDate(year);
    if (easter.getMonth() === 3) {
      holidays[3].push(easter.getDate());
      holidays[3].push(easter.getDate() + 1); // Easter Monday
    }
  }

  // Add Easter for May if applicable (when Easter is in late April)
  if (month === 4) {
    const easter = getEasterDate(year);
    if (easter.getMonth() === 3 && easter.getDate() > 25) {
      holidays[4].push(easter.getDate() + 1); // Easter Monday
    }
  }

  return holidays[month] || [];
}

function togglePlannerDay(year, month, day) {
  const dateKey = `${year}-${month}-${day}`;
  const index = plannerLeaveDays.indexOf(dateKey);

  if (index === -1) {
    plannerLeaveDays.push(dateKey);
  } else {
    plannerLeaveDays.splice(index, 1);
  }

  updatePlanner2026();
}

// Schedule optimization algorithm from schedule-2026.js
async function optimizeLeaveDays() {
  const resultElement = document.getElementById('optimization-result');
  resultElement.textContent = 'Se calculează...';
  resultElement.className = 'optimization-result calculating';

  try {
    const year = plannerYear;
    const month = plannerMonth;
    
    // Get holidays for the current month
    const holidays = await getHolidaysForMonth(year, month);
    
    // Add weekends to holidays
    const daysInMonth = new Date(year, month + 1, 0).getDate();
    for (let day = 1; day <= daysInMonth; day++) {
      const date = new Date(year, month, day);
      if (date.getDay() === 0 || date.getDay() === 6) { // Sunday or Saturday
        if (!holidays.includes(day)) {
          holidays.push(day);
        }
      }
    }
    
    holidays.sort((a, b) => a - b);
    
    // Calculate target hours
    let targetHours = 0;
    for (let day = 1; day <= daysInMonth; day++) {
      if (!holidays.includes(day) && (new Date(year, month, day).getDay() % 6) !== 0) {
        targetHours += 8;
      }
    }
    
    const refDate = new Date(refYear, 0, 0);
    let best = [-Infinity, -Infinity];
    let bestChoice = [-1, -1];
    
    // Try all possible leave periods
    for (let startDay = 1; startDay <= daysInMonth - 1; startDay++) {
      for (let endDay = startDay + 1; endDay <= daysInMonth; endDay++) {
        let score = [0, 0];
        const leaves = [];

        // Create leave period
        for (let day = startDay; day <= endDay; day++) {
          leaves.push(day);
        }

        score[1] = leaves.length;
        let countHours = 0;

        // Calculate hours for this configuration
        for (let day = 1; day <= daysInMonth; day++) {
            const date1 = new Date(year, month, day);
            const ecart = Math.round((date1 - refDate) / 86400000) + 7 - plannerShift;
          // Check if it's a work day and not on leave
          if (((ecart % 4) < 2) && !leaves.includes(day)) {
            countHours += 12;
          } else if (!holidays.includes(day) && leaves.includes(day)) {
            // Leave day on regular day
            countHours += 8;
          }
          // Holidays and weekends don't contribute to hours
        }

        // Only consider configurations that match target hours
        if (countHours === targetHours) {
          // Calculate score: count work days that fall on holidays
          for (let day = 1; day <= daysInMonth; day++) {
            const date1 = new Date(year, month, day);
            //const ecart = Math.round((date1 - refDate) / 86400000);
            const ecart = Math.round((date1 - refDate) / 86400000) + 7 - plannerShift;
            
            // If it's a holiday, a work day, and not on leave
            if (holidays.includes(day) && ((ecart % 4) < 2) && !leaves.includes(day)) {
              score[0] += 1;
            }
          }
        }

        // Update best choice
        if (score[0] > best[0] || (score[0] === best[0] && score[1] > best[1])) {
          best[0] = score[0];
          best[1] = score[1];
          bestChoice[0] = startDay;
          bestChoice[1] = endDay;
        }
      }
    }

    // Apply the best leave period
    if (bestChoice[0] !== -1 && bestChoice[1] !== -1) {
      // Clear existing leave days for this month
      plannerLeaveDays = plannerLeaveDays.filter(day => {
        const [y, m, d] = day.split('-').map(Number);
        return !(y === year && m === month);
      });

      // Add new leave days
      for (let day = bestChoice[0]; day <= bestChoice[1]; day++) {
        const dateKey = `${year}-${month}-${day}`;
        plannerLeaveDays.push(dateKey);
      }

      // Update the planner
      updatePlanner2026();

      resultElement.textContent = `Concediu optimizat: zilele ${bestChoice[0]}-${bestChoice[1]} ${monthNamesRo[month]}`;
      resultElement.className = 'optimization-result success';
    } else {
      resultElement.textContent = 'Nu s-a găsit o soluție optimă';
      resultElement.className = 'optimization-result error';
    }
  } catch (error) {
    console.error('Error optimizing leave days:', error);
    resultElement.textContent = 'Eroare la optimizare';
    resultElement.className = 'optimization-result error';
  }
}

// Initialize both apps
window.addEventListener('load', function() {
  initializeControls();
  updateCalendar();
  renderPlanner();
  updatePlanner2026();
});

document.addEventListener('visibilitychange', function() {
  if (!document.hidden) updateUserInfo();
});

if ("serviceWorker" in navigator) {
  window.addEventListener('load', function() {
    navigator.serviceWorker.register("sw.js");
  });
}