/**
 * EasySite Pro – Admin JavaScript
 */
(function ($) {
	'use strict';

	$(document).on('click', '.easysite-apply-branding', function (e) {
		e.preventDefault();
		var $btn = $(this);
		$btn.prop('disabled', true).text(easysiteAdmin.i18n.saving);

		$.post(easysiteAdmin.ajaxUrl, {
			action: 'easysite_apply_branding',
			nonce: easysiteAdmin.nonce
		}).done(function (res) {
			$btn.text(res.success ? easysiteAdmin.i18n.saved : easysiteAdmin.i18n.error);
		}).fail(function () {
			$btn.text(easysiteAdmin.i18n.error);
		}).always(function () {
			setTimeout(function () { $btn.prop('disabled', false).text('Apply branding'); }, 2000);
		});
	});

	$('#gradient_preset').on('change', function () {
		var val = $(this).val();
		$('.easysite-gradient-swatch').removeClass('is-active');
		$('.easysite-gradient-swatch[data-gradient="' + val + '"]').addClass('is-active');
	});

	$(document).on('click', '.easysite-gradient-swatch', function () {
		var val = $(this).data('gradient');
		var $select = $('#gradient_preset');
		if ($select.length) {
			$select.val(val).trigger('change');
		}
	});
})(jQuery);
