/**
 * WTE Business Card — Admin Repeater JS
 */
'use strict';
document.addEventListener('DOMContentLoaded', function() {
	document.querySelectorAll('.wte-repeater').forEach(function(repeater) {
		const container = repeater.querySelector('.wte-repeater__items');
		const addBtn = repeater.querySelector('.wte-repeater__add');
		const hiddenInput = repeater.querySelector('input[type="hidden"]');
		const field = repeater.dataset.field;

		function collectData() {
			const items = [];
			container.querySelectorAll('.wte-repeater__item').forEach(function(item) {
				if (field === 'wte_business_details') {
					items.push({
						title: item.querySelector('.wte-field-title')?.value || '',
						body: item.querySelector('.wte-field-body')?.value || '',
						image_id: parseInt(item.querySelector('.wte-field-image-id')?.value || '0'),
						link_url: item.querySelector('.wte-field-link-url')?.value || '',
						link_text: item.querySelector('.wte-field-link-text')?.value || '',
						order: parseInt(item.querySelector('.wte-field-order')?.value || '0'),
					});
				} else {
					items.push({
						question: item.querySelector('.wte-field-question')?.value || '',
						answer: item.querySelector('.wte-field-answer')?.value || '',
						order: parseInt(item.querySelector('.wte-field-order')?.value || '0'),
					});
				}
			});
			hiddenInput.value = JSON.stringify(items);
		}

		// Add item.
		addBtn.addEventListener('click', function() {
			const template = container.querySelector('.wte-repeater__item');
			let newItem;
			if (template) {
				newItem = template.cloneNode(true);
				newItem.querySelectorAll('input, textarea').forEach(function(el) {
					if (el.type === 'number' && el.classList.contains('wte-field-order')) {
						el.value = container.children.length;
					} else { el.value = ''; }
				});
			} else {
				newItem = document.createElement('div');
				newItem.className = 'wte-repeater__item';
				if (field === 'wte_business_details') {
					newItem.innerHTML = '<p><label>項目名</label><input type="text" class="widefat wte-field-title"></p><p><label>説明文</label><textarea class="widefat wte-field-body" rows="3"></textarea></p><p><label>リンクURL</label><input type="url" class="widefat wte-field-link-url"></p><p><label>リンクテキスト</label><input type="text" class="widefat wte-field-link-text"></p><p><label>画像ID</label><input type="number" class="wte-field-image-id" value="0"> <button type="button" class="button wte-media-select">画像を選択</button></p><p><label>表示順</label><input type="number" class="wte-field-order" value="0" min="0"></p><p><button type="button" class="button wte-repeater__remove">削除</button></p><hr>';
				} else {
					newItem.innerHTML = '<p><label>Q:</label><input type="text" class="widefat wte-field-question"></p><p><label>A:</label><textarea class="widefat wte-field-answer" rows="3"></textarea></p><p><label>表示順</label><input type="number" class="wte-field-order" value="0" min="0"></p><p><button type="button" class="button wte-repeater__remove">削除</button></p><hr>';
				}
			}
			container.appendChild(newItem);
			collectData();
		});

		// Remove item.
		container.addEventListener('click', function(e) {
			if (e.target.classList.contains('wte-repeater__remove')) {
				if (confirm('この項目を削除しますか？')) {
					e.target.closest('.wte-repeater__item').remove();
					collectData();
				}
			}
		});

		// Collect data on any input change.
		container.addEventListener('input', collectData);
	});
});
