-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.js
198 lines (158 loc) · 7.11 KB
/
calendar.js
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
document.addEventListener('DOMContentLoaded', function() {
const monthNameElement = document.getElementById('month-name1');
const daysElement = document.getElementById('ds');
const prevMonthButton = document.getElementById('prev-month1');
const nextMonthButton = document.getElementById('next-month1');
const selectedDateInput = document.getElementById('selected-date');
const months = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
];
let currentDate = new Date();
function renderCalendar(date) {
const year = date.getFullYear();
const month = date.getMonth();
// Set the month name
monthNameElement.textContent = `${months[month]} ${year}`;
// Clear previous days
daysElement.innerHTML = '';
// Get first and last day of the month
const firstDay = new Date(year, month, 1).getDay();
const lastDate = new Date(year, month + 1, 0).getDate();
// Get today's date without time
const today = new Date();
today.setHours(0, 0, 0, 0);
// Calculate the date for seven days from now
// Create day elements
for (let i = 1; i <= lastDate; i++) {
const dateToCheck = new Date(year, month, i);
const dayOfWeek = dateToCheck.getDay();
// Skip Saturday (6) and Sunday (0)
if (dayOfWeek === 0 || dayOfWeek === 6) {
continue;
}
const dayDiv = document.createElement('div');
dayDiv.className = 'dsy';
dayDiv.textContent = i;
// Check if the day is in the past or within the next seven days
if (dateToCheck < today ) {
dayDiv.classList.add('disabled');
} else {
// Add click event listener to non-disabled days
(function(dayDiv, year, month, i) {
const dateStr = `${year}-${String(month + 1).padStart(2, '0')}-${String(i).padStart(2, '0')}`;
fetch(`restriction.php?date=${dateStr}`)
.then(response => response.json())
.then(data => {
if (data.isFull || data.restrictedDates.includes(dateStr)) {
dayDiv.classList.add('disabled');
} else {
dayDiv.addEventListener('click', () => {
selectedDateInput.value = dateStr;
});
}
})
.catch(error => console.error('Error fetching date info:', error));
})(dayDiv, year, month, i);
}
daysElement.appendChild(dayDiv);
}
}
function changeMonth(delta) {
currentDate.setMonth(currentDate.getMonth() + delta);
renderCalendar(currentDate);
}
prevMonthButton.addEventListener('click', () => changeMonth(-1));
nextMonthButton.addEventListener('click', () => changeMonth(1));
// Initial render
renderCalendar(currentDate);
});
document.addEventListener('DOMContentLoaded', function() {
const monthNameElement = document.getElementById('month-name-2');
const daysElement = document.getElementById('ds-2');
const prevMonthButton = document.getElementById('prev-month-2');
const nextMonthButton = document.getElementById('next-month-2');
const selectedDateInput = document.getElementById('selected-date-2');
const timeSelection = document.getElementById('time-selection');
const months = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
];
let currentDate = new Date();
function renderCalendar(date) {
const year = date.getFullYear();
const month = date.getMonth();
// Set the month name
monthNameElement.textContent = `${months[month]} ${year}`;
// Clear previous days
daysElement.innerHTML = '';
// Get first and last day of the month
const firstDay = new Date(year, month, 1).getDay();
const lastDate = new Date(year, month + 1, 0).getDate();
// Get today's date without time
const today = new Date();
today.setHours(0, 0, 0, 0);
// Calculate the date for seven days from now
const sevenDaysFromNow = new Date(today);
sevenDaysFromNow.setDate(sevenDaysFromNow.getDate() + 7);
// Create day elements
for (let i = 1; i <= lastDate; i++) {
const dateToCheck = new Date(year, month, i);
const dayOfWeek = dateToCheck.getDay();
// Skip Saturday (6) and Sunday (0)
if (dayOfWeek === 0 || dayOfWeek === 6) {
continue;
}
const dayDiv = document.createElement('div');
dayDiv.className = 'dsy';
dayDiv.textContent = i;
// Check if the day is in the past or within the next seven days
if (dateToCheck < today || dateToCheck <= sevenDaysFromNow) {
dayDiv.classList.add('disabled');
} else {
// Add click event listener to non-disabled days
(function(dayDiv, year, month, i) {
const dateStr = `${year}-${String(month + 1).padStart(2, '0')}-${String(i).padStart(2, '0')}`;
fetch(`restriction.php?date=${dateStr}`)
.then(response => response.json())
.then(data => {
if (data.isFull || data.restrictedDates.includes(dateStr)) {
dayDiv.classList.add('disabled');
} else {
dayDiv.addEventListener('click', () => {
fetchAvailableTimes(dateStr);
selectedDateInput.value = dateStr;
});
}
})
.catch(error => console.error('Error fetching date info:', error));
})(dayDiv, year, month, i);
}
daysElement.appendChild(dayDiv);
}
}
function changeMonth(delta) {
currentDate.setMonth(currentDate.getMonth() + delta);
renderCalendar(currentDate);
}
prevMonthButton.addEventListener('click', () => changeMonth(-1));
nextMonthButton.addEventListener('click', () => changeMonth(1));
// Initial render
renderCalendar(currentDate);
function fetchAvailableTimes(date) {
fetch(`availtime.php?date=${date}`)
.then(response => response.json())
.then(data => {
// Clear previous options
timeSelection.innerHTML = '';
// Populate with new options
data.availableTimes.forEach(time => {
const option = document.createElement('option');
option.value = time;
option.textContent = time;
timeSelection.appendChild(option);
});
})
.catch(error => console.error('Error fetching available times:', error));
}
});