summaryrefslogtreecommitdiff
path: root/views/mods/mod_revocation/wp_footer.phtml
blob: 3c4f9c85ca51fedc51759476c57283cb7fccad67 (plain)
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
<?php

	declare(strict_types=1);

	/**
	 * @author: Daniel Schmitzer
	 * @date: 27.01.26
	 * @time: 09:26
	 */

	namespace wpsgRevocation;

?>

<?php if (\wpsg_ShopController::getShop()->get_option('wpsg_mod_revocation_view_flyout') === '1') { ?>
	
	<div id="wpsg_revocation_button">
		<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><title>Vertrag widerrufen</title><path d="M13.5,8H12V13L16.28,15.54L17,14.33L13.5,12.25V8M13,3A9,9 0 0,0 4,12H1L4.96,16.03L9,12H6A7,7 0 0,1 13,5A7,7 0 0,1 20,12A7,7 0 0,1 13,19C11.07,19 9.32,18.21 8.06,16.94L6.64,18.36C8.27,20 10.5,21 13,21A9,9 0 0,0 22,12A9,9 0 0,0 13,3" /></svg>
		<span id="wpsg_revocation_button_text"><?php echo __('Vertrag widerrufen', 'wpsgRevocation'); ?></span>
	</div>
	
<?php } else { ?>

	<div id="wpsg_revocation_button" style="display:none;"></div>
	
<?php } ?>

<!-- Dialog -->
<div id="wpsg_revocation_dialog" class="">
	<div class="inner">
		<div class="header">
			<span><?php echo __('Widerrufsformular', 'wpsgRevocation'); ?></span>
			<a href="#" class="close">
				<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z" /></svg>
			</a>
		</div>
		<div class="content"></div>
		<div class="footer">
			<button class="button button-primary wpsg_revocation_button_submit"><?php echo __('Widerruf bestätigen', 'wpsgRevocation'); ?></button>
		</div>
		<div class="loading">
			<img src="<?php echo WPSG_URL; ?>views/gfx/ajax-loader.gif" alt="<?php echo __('Bitte warten ...', 'wpsgRevocation'); ?>" />
		</div>
	</div>
</div>

<script>

	console.log('ok');
	 
	const wpsg_revocation_nonce = '<?php echo wp_create_nonce('wpsg_mod_revocation_nonce'); ?>';
	const el_dialog = document.getElementById('wpsg_revocation_dialog');
	const el_dialog_content = document.getElementById('wpsg_revocation_dialog').querySelector('.inner > .content');

	const formDataToObject = (formData) => {

		const obj = {};

		for (const [key, value] of formData.entries()) {

			const keys = key.split(/\[|\]/).filter(Boolean);
			let current = obj;

			keys.forEach((k, i) => {

				const isLast = i === keys.length - 1;

				if (isLast) { current[k] = value; }
				else {

					if (!current[k]) { current[k] = isNaN(keys[i + 1]) ? {} : []; }

					current = current[k];

				}

			});

		}

		return obj;

	}

	const submit_dialog = event => {

		event.preventDefault();
		event.stopPropagation();

		el_dialog.classList.add('loading');

		// const form_data = Object.fromEntries(new FormData(el_dialog.querySelector('form#wpsg_revocation_form')).entries());
		 const form = el_dialog.querySelector('form#wpsg_revocation_form');
		 const formData = new FormData(form);
		 const form_data = formDataToObject(formData);

		fetch('<?php echo admin_url('admin-ajax.php'); ?>?action=wpsg_mod_revocation&do=ajaxDialogSubmit', {
			method: 'POST',
			headers: {
				'Content-Type': 'application/json'
			},
			body: JSON.stringify({
				nonce: wpsg_revocation_nonce,
				form_data: form_data,
			})
		}).then(res => res.json()).then(res => {

			let html = '';
  
			if ((res.success??false) === false) {

				html += '<ul id="wpsg_error" class="wpsg_error">';
				(res.arError??[]).forEach(error => { html += `<li>${error}</li>`; });
				html += '</ul>';

				el_dialog_content.querySelector('#msg_target').innerHTML = html;
	
				el_dialog.classList.remove('loading');

			 } else {
					
				alert('<?php echo __('Ihr Widerruf ist bei uns eingegangen und wird nun bearbeitet.', 'wpsgRevocation'); ?>');
				close_dialog(event);
				
				document.querySelectorAll('.wpsg_revocation_order_product_checkbox:checked').forEach(el => {
 
					el.checked = false;

				});
				
			}

		});

	}
	const close_dialog = event => {

		event.preventDefault();
		event.stopPropagation();

		el_dialog.classList.remove('visible');

	};
	const open_dialog = el => {

		el_dialog.classList.toggle('visible');
		el_dialog.classList.add('loading');

		const dialog_config = {
			arProductID: [],
			arOrderProductID: [],
			set_product_anr: ''
		}

		// Produkte auf der Seite
		document.querySelectorAll('input[name="wpsg[produkt_id]"]').forEach(el => { dialog_config.arProductID.push(parseInt(el.value)); })

		if (el !== undefined && el.classList.contains('wpsg_revocation_order_product')) {

			el.querySelector('input').checked = true;

		}

		if (el !== undefined && el.classList.contains('wpsg_revocation_product_button')) {

			dialog_config.set_product_anr = el.dataset.product_anr;

		}

		// Bestellung / Produkte
		document.querySelectorAll('.wpsg_revocation_order_product_checkbox:checked').forEach(el => {

			const order_id = parseInt(el.dataset.order_id);
			const order_product_id = parseInt(el.dataset.order_product_id);
			const product_id = parseInt(el.dataset.product_id);
			const amount = parseInt(el.dataset.amount);

			dialog_config.arOrderProductID.push({
				order_id: order_id,
				order_product_id: order_product_id,
				amount: amount
			});

			dialog_config.arProductID.push(product_id);

		});

		fetch('<?php echo admin_url('admin-ajax.php'); ?>', {
			method: 'POST',
			body: new URLSearchParams({
				nonce: wpsg_revocation_nonce,
				action: 'wpsg_mod_revocation',
				do: 'ajaxDialogContent',
				dialog_config: JSON.stringify(dialog_config),
			})
		}).then(res => res.text()).then(html => {

			el_dialog_content.innerHTML = html;
			el_dialog.classList.remove('loading');

		});

        setTimeout(() => {document.querySelector('[name="wpsg_revocation_reason[row][0][product_nr]"]')?.focus();}, 1000)

    };

	 // Button am Produkt
	document.querySelectorAll('.wpsg_revocation_product_button').forEach(el => {

		el.addEventListener('click', event => {

			open_dialog(el);

		});

	});

	// Button Listener FlyOut
	document.getElementById('wpsg_revocation_button').addEventListener('click', event => { open_dialog(); });

	// Shortcode-Link Listener [wpshopgermany_widerruf]
	document.addEventListener('click', event => {

		const link = event.target.closest('a.wpsg_revocation_link');

		if (link === null) return;

		event.preventDefault();
        open_dialog();

	});


	// GET Parameter verarbeiten
	const urlParams = new URLSearchParams(window.location.search);
	
	const get_order_nr = urlParams.get('wpsg_revo_order');
	const get_product_nr = urlParams.get('wpsg_revo_product');
	const get_amount = urlParams.get('wpsg_revo_amount');
	const get_contact = urlParams.get('wpsg_revo_contact');

	if (get_order_nr !== null) {

		document.addEventListener('DOMContentLoaded', () => {

			open_dialog();

			const checkAndFillForm = () => {

				const form = el_dialog.querySelector('form#wpsg_revocation_form');

				if (!form) {

					setTimeout(checkAndFillForm, 100);
					return;

				}

				if (get_product_nr) {

					const productNrInput = form.querySelector('input[name*="[product_nr]"]');

					if (productNrInput) productNrInput.value = get_product_nr;

				}

				if (get_order_nr) {

					const orderNrInput = form.querySelector('input[name*="[order_nr]"]');

					if (orderNrInput) orderNrInput.value = get_order_nr;

				}

				if (get_amount) {

					const amountInput = form.querySelector('input[name*="[amount]"]');

					if (amountInput) amountInput.value = get_amount;

				}

				if (get_contact) {

					const contactInput = form.querySelector('input[name="wpsg_revocation_reason[contact]"]');

					if (contactInput) contactInput.value = get_contact;

				}

			};

			checkAndFillForm();

		});

	}

	// Layer Listener
	el_dialog.addEventListener('click', event => {

		if (!event.target.closest('.inner')) close_dialog(event);

	});

	// Produkte in Bestellung auf der Seite Listener
	document.querySelectorAll('.wpsg_revocation_order_product')?.forEach(el => {

		el.addEventListener('click', event => {

			if (!event.target.closest('input[type="checkbox"]')) {

				open_dialog(el);

			}

		});

	});

	// Close Button Listener
	el_dialog.querySelectorAll('.close')?.forEach(el => {

		el.addEventListener('click', event => { close_dialog(event); })

	})

	// Submit Button Listener
	el_dialog.querySelectorAll('.wpsg_revocation_button_submit')?.forEach(el => {

		el.addEventListener('click', event => { submit_dialog(event); });

	})

	// ESC Taste lauschen
	document.addEventListener('keydown', function (e) {

		if (e.key === 'Escape') {

			el_dialog.classList.remove('visible');

		}

	});
	
</script>