-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
217 lines (187 loc) · 7.62 KB
/
Copy pathscript.js
File metadata and controls
217 lines (187 loc) · 7.62 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
document.addEventListener("DOMContentLoaded", function () {
const serviceTypeSelect = document.getElementById("service_type");
const rateTypeSelect = document.getElementById("rate_type");
const rentalDurationSelect = document.getElementById("rental_duration");
function updateAmount() {
const rateType = rateTypeSelect.value;
const rentalDuration = rentalDurationSelect.value;
if (rateType && rentalDuration) {
fetch('get_amount.php', {
method: 'POST',
body: new URLSearchParams({ rate_type: rateType, duration: rentalDuration }),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(response => response.json())
.then(data => {
if (data.amount !== undefined) {
const amount = parseFloat(data.amount);
if (!isNaN(amount)) {
document.getElementById("amount").value = amount.toFixed(2) + "р.";
enablePaymentButton();
} else {
console.error('Invalid amount value:', data.amount);
}
} else {
console.error('No amount found for the selected rate and duration.');
}
})
.catch(error => {
console.error('Error loading amount:', error);
});
}
}
serviceTypeSelect.addEventListener("change", function () {
const serviceType = serviceTypeSelect.value;
const allowedRateTypes = {
"Рабочее место": ["Гость", "Резидент", "Агентство"],
"Переговорка": ["1 час", "Полдня", "День"],
"Конференц-зал": ["1час", "2 часа", "3 часа", "4 часа", "5 часов", "6 часов", "7 часов", "8 часов"]
};
updateRateTypes(allowedRateTypes[serviceType]);
});
rateTypeSelect.addEventListener("change", function () {
updateRentalDurations(serviceTypeSelect.value, rateTypeSelect.value);
});
rentalDurationSelect.addEventListener("change", updateAmount);
function updateRateTypes(allowedTypes) {
rateTypeSelect.innerHTML = '';
fetch('get_rates.php', {
method: 'POST',
body: new URLSearchParams({ service_type: serviceTypeSelect.value }),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(response => response.json())
.then(data => {
if (data.rates && data.rates.length > 0) {
displayRates(data.rates, allowedTypes);
} else {
console.error('No rates found for the selected service.');
}
})
.catch(error => {
console.error('Error loading rates:', error);
});
}
function displayRates(rates, allowedRateTypes) {
rates.forEach(rate => {
const rateType = rate.rate_type;
if (allowedRateTypes.includes(rateType)) {
const option = document.createElement('option');
option.text = rateType;
option.value = rateType;
rateTypeSelect.add(option);
}
});
updateRentalDurations(serviceTypeSelect.value, rateTypeSelect.value);
}
function updateRentalDurations(serviceType, rateType) {
rentalDurationSelect.innerHTML = '';
fetch('get_durations.php', {
method: 'POST',
body: new URLSearchParams({ service_type: serviceType, rate_type: rateType }),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(response => response.json())
.then(data => {
if (data.durations && data.durations.length > 0) {
data.durations.forEach(duration => {
const option = document.createElement('option');
option.text = duration;
option.value = duration;
rentalDurationSelect.add(option);
});
updateAmount();
} else {
console.error('No durations found for the selected service and rate.');
}
})
.catch(error => {
console.error('Error loading durations:', error);
});
}
function enablePaymentButton() {
const paymentButton = document.getElementById("payment-button");
if (paymentButton) {
paymentButton.disabled = false;
}
}
});
function clearFormFields() {
document.getElementById("full_name").value = "";
document.getElementById("email").value = "";
document.getElementById("phone").value = "";
document.getElementById("activity_type").value = "";
document.getElementById("service_type").value = "";
document.getElementById("rate_type").value = "";
document.getElementById("rental_duration").value = "";
document.getElementById("amount").value = "";
}
function validateForm() {
const full_name = document.getElementById("full_name").value;
const email = document.getElementById("email").value;
const phone = document.getElementById("phone").value;
const activity_type = document.getElementById("activity_type").value;
const serviceType = document.getElementById("service_type").value;
const rateType = document.getElementById("rate_type").value;
const rentalDuration = document.getElementById("rental_duration").value;
if (full_name.trim() === "" || email.trim() === "" || phone.trim() === "" || activity_type.trim() === "" || rentalDuration === "") {
alert("Пожалуйста, заполните все обязательные поля.");
return false;
}
if (!validateEmail(email)) {
alert("Пожалуйста, введите корректный e-mail.");
return false;
}
if (!validatePhone(phone)) {
alert("Пожалуйста, введите корректный телефон (10 цифр).");
return false;
}
if ((serviceType === "Рабочее место" || serviceType === "Переговорка") && !rateType) {
alert("Пожалуйста, выберите тариф.");
return false;
}
if (serviceType === "Конференц-зал" && !/^\d+\sчас(ов)?$/.test(rentalDuration)) {
alert("Пожалуйста, выберите продолжительность аренды в формате 'N час' или 'N часов'.");
return false;
}
return true;
}
function validateEmail(email) {
const re = /\S+@\S+\.\S+/;
return re.test(email);
}
function validatePhone(phone) {
const re = /^\d{10}$/;
return re.test(phone);
}
function submitPayment() {
if (!validateForm()) {
return;
}
const form = document.getElementById("payment-form");
const formData = new FormData(form);
fetch('process_payment.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert("Платеж успешно отправлен.");
clearFormFields();
} else {
alert("Ошибка: " + data.error);
}
})
.catch(error => {
console.error(error);
alert("Ошибка при отправке платежа.");
});
}
document.getElementById("rental_duration").addEventListener("change", updateAmount);