/**
 * Registration type templates on Settings → Registration. All CRUD via AJAX.
 */
(function($) {
	'use strict';

	var L10n = window.evgeRegistrationTypeTemplatesSettings || {};
	var ajaxUrl = L10n.ajaxUrl || '';
	var nonce = L10n.ajaxNonce || '';
	var i18n = L10n.i18n || {};
	var editIcon = L10n.editIcon || '';
	var deleteIcon = L10n.deleteIcon || '';

	function loadTemplates() {
		var $list = $('#evge-registration-type-templates-list');
		$list.html('<p class="evge-templates-loading evge-inline-list-empty-state">' + (i18n.loading || 'Loading…') + '</p>');
		$.post(ajaxUrl, {
			action: 'evge_get_registration_type_templates',
			nonce: nonce
		}).done(function(res) {
			if (res.success && res.data && res.data.templates) {
				renderList(res.data.templates);
			} else {
				$list.html('<p class="evge-templates-error">' + (i18n.error || 'An error occurred.') + '</p>');
			}
		}).fail(function() {
			$list.html('<p class="evge-templates-error">' + (i18n.error || 'An error occurred.') + '</p>');
		});
	}

	function renderList(templates) {
		var $list = $('#evge-registration-type-templates-list');
		if (!templates.length) {
			$list.html('<p class="evge-inline-list-empty-state">' + (i18n.noTemplates || 'No templates yet.') + '</p>');
			return;
		}
		var html = '<table class="wp-list-table widefat fixed striped"><thead><tr><th>' + (i18n.name || 'Name') + '</th><th>' + (i18n.description || 'Description') + '</th><th style="width:100px">' + (i18n.actions || 'Actions') + '</th></tr></thead><tbody>';
		$.each(templates, function(i, t) {
			var name = (t.name || '').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/&/g, '&amp;');
			var desc = (t.description || '').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/&/g, '&amp;');
			var editLabel = (i18n.edit || 'Edit');
			var deleteLabel = (i18n.delete || 'Delete');
			var editAttr = editLabel.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
			var deleteAttr = deleteLabel.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
			var editHtml = editLabel.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
			var deleteHtml = deleteLabel.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
			html += '<tr data-id="' + t.id + '">';
			html += '<td class="evge-template-name">' + name + '</td>';
			html += '<td class="evge-template-description">' + desc + '</td>';
			html += '<td class="evge-template-actions">';
			html += '<div class="evge-tooltip-wrap evge-button-tooltip"><button type="button" class="evge-admin-secondary-button button evge-flex-center evge-template-edit" data-id="' + t.id + '" aria-label="' + editAttr + '" title="' + editAttr + '"><span class="evge-icon-text">' + editIcon + '</span></button><div class="evge-tooltip"><p>' + editHtml + '</p></div></div> ';
			html += '<div class="evge-tooltip-wrap evge-button-tooltip"><button type="button" class="evge-admin-secondary-button button evge-flex-center evge-template-delete" data-id="' + t.id + '" aria-label="' + deleteAttr + '" title="' + deleteAttr + '"><span class="evge-icon-text">' + deleteIcon + '</span></button><div class="evge-tooltip"><p>' + deleteHtml + '</p></div></div>';
			html += '</td></tr>';
		});
		html += '</tbody></table>';
		$list.html(html);
	}

	function setFormEditMode(id, name, description) {
		$('#evge-template-edit-id').val(id || '');
		$('#evge-template-name').val(name || '');
		$('#evge-template-description').val(description || '');
		$('#evge-template-save').text(i18n.saveTemplate || 'Save Template');
		$('.evge-template-form-title').text(id ? (i18n.editTemplate || 'Edit template') : (i18n.addTemplate || 'Add template'));
		$('#evge-template-cancel').toggle(!!id);
	}

	function clearForm() {
		setFormEditMode(null, '', '');
		$('#evge-registration-type-template-form-wrap').hide();
	}

	function showForm() {
		$('#evge-registration-type-template-form-wrap').show();
	}

	function saveTemplate() {
		var id = $('#evge-template-edit-id').val();
		var name = $('#evge-template-name').val().trim();
		var description = $('#evge-template-description').val().trim();
		if (!name) return;
		var data = {
			action: 'evge_save_registration_type_template',
			nonce: nonce,
			name: name,
			description: description
		};
		if (id) data.id = id;
		$('#evge-template-save').prop('disabled', true);
		$.post(ajaxUrl, data).done(function(res) {
			if (res.success) {
				loadTemplates();
				clearForm();
			} else {
				alert(res.data && res.data.message ? res.data.message : (i18n.error || 'An error occurred.'));
			}
		}).fail(function() {
			alert(i18n.error || 'An error occurred. Please try again.');
		}).always(function() {
			$('#evge-template-save').prop('disabled', false);
		});
	}

	function deleteTemplate(id) {
		if (!confirm(i18n.confirmDelete || 'Delete this template?')) return;
		$.post(ajaxUrl, {
			action: 'evge_delete_registration_type_template',
			nonce: nonce,
			id: id
		}).done(function(res) {
			if (res.success) {
				loadTemplates();
				clearForm();
			} else {
				alert(res.data && res.data.message ? res.data.message : (i18n.error || 'An error occurred.'));
			}
		}).fail(function() {
			alert(i18n.error || 'An error occurred. Please try again.');
		});
	}

	$(function() {
		if (!$('#evge-registration-type-templates-list').length) return;
		loadTemplates();

		$(document).on('click', '#evge-add-registration-type-template', function(e) {
			e.preventDefault();
			clearForm();
			showForm();
			$('#evge-template-name').focus();
		});
		$(document).on('click', '#evge-template-save', function() {
			saveTemplate();
		});
		$(document).on('click', '#evge-template-cancel', function() {
			clearForm();
		});
		$(document).on('click', '.evge-template-edit', function() {
			var $row = $(this).closest('tr');
			var id = $row.data('id');
			var name = $row.find('.evge-template-name').text();
			var description = $row.find('.evge-template-description').text();
			setFormEditMode(id, name, description);
			showForm();
		});
		$(document).on('click', '.evge-template-delete', function() {
			var id = $(this).data('id');
			if (id) deleteTemplate(id);
		});
	});
})(jQuery);
