jQuery(function ($) {
	if (typeof formulatePricingEntries === 'undefined') {
		return;
	}

	var nonce = formulatePricingEntries.nonce;
	var i18n  = formulatePricingEntries.strings;

	$('#bf-check-all-entries').on('change', function () {
		$('.bf-entry-check').prop('checked', this.checked);
	});

	$(document).on('click', '.bf-view-entry', function (e) {
		e.preventDefault();
		var entryId = $(this).data('entry-id');
		$('#bf-entry-modal-body').html(
			'<div class="bf-loading"><svg width="18" height="18" viewBox="0 0 18 18" fill="none" style="animation:bf-spin .7s linear infinite"><circle cx="9" cy="9" r="7" stroke="#d1d5db" stroke-width="2"/><path d="M9 2a7 7 0 017 7" stroke="#6366f1" stroke-width="2" stroke-linecap="round"/></svg> ' +
			i18n.loading +
			'</div>'
		);
		$('#bf-entry-modal').show();
		$('.bf-modal-delete-entry').data('entry-id', entryId);
		$.post(ajaxurl, { action: 'formulate_pricing_get_entry', entry_id: entryId, nonce: nonce }, function (res) {
			if (!res.success) {
				return;
			}
			var entry = res.data;
			var html  = '<div class="bf-entry-meta-grid">';
			html += '<span><strong>' + i18n.id + ':</strong> #' + entry.id + '</span>';
			html += '<span><strong>' + i18n.date + ':</strong> ' + entry.created_at + '</span>';
			html += '<span><strong>' + i18n.status + ':</strong> ' + entry.status + '</span>';
			if (entry.payment_status_label) {
				html +=
					'<span><strong>' +
					i18n.payment +
					':</strong> <span class="bf-payment-badge" style="background:' +
					(entry.payment_status_color || '#9ca3af') +
					'20;color:' +
					(entry.payment_status_color || '#9ca3af') +
					'">' +
					$('<div>').text(entry.payment_status_label).html() +
					'</span></span>';
			}
			if (entry.wc_order_url) {
				var orderLabel = entry.wc_order_number ? '#' + entry.wc_order_number : '#' + entry.wc_order_id;
				html += '<span><strong>' + i18n.order + ':</strong> <a href="' + entry.wc_order_url + '" target="_blank" rel="noopener">' + orderLabel + '</a></span>';
			}
			html += '</div>';
			html += '<table class="bf-entry-detail-table"><tbody>';
			var rows = entry.display_rows && entry.display_rows.length ? entry.display_rows : [];
			if (rows.length) {
				rows.forEach(function (row) {
					html += '<tr><th>' + $('<div>').text(row.key).html() + '</th><td>' + $('<div>').text(String(row.value)).html() + '</td></tr>';
				});
			} else if (entry.entry_data) {
				var data = entry.entry_data;
				for (var key in data) {
					if (key.indexOf('_label_') === 0 || key.charAt(0) === '_') {
						continue;
					}
					var labelKey = '_label_' + key;
					var label    = data[labelKey] || i18n.fieldLabel;
					var val      = data[key];
					if (val === '' || val === null) {
						continue;
					}
					if (Array.isArray(val)) {
						val = val.join(', ');
					}
					html += '<tr><th>' + $('<div>').text(label).html() + '</th><td>' + $('<div>').text(String(val)).html() + '</td></tr>';
				}
			}
			html += '</tbody></table>';
			$('#bf-entry-modal-body').html(html);
			$('.bf-entry-row[data-entry-id="' + entryId + '"]').removeClass('bf-entry-unread').find('.bf-unread-dot').remove();
		});
	});

	$(document).on('click', '.bf-modal-delete-entry', function () {
		var entryId = $(this).data('entry-id');
		if (!confirm(i18n.confirmDelete)) {
			return;
		}
		$.post(ajaxurl, { action: 'formulate_pricing_delete_entry', entry_id: entryId, nonce: nonce }, function () {
			$('.bf-entry-row[data-entry-id="' + entryId + '"]').fadeOut(300, function () {
				$(this).remove();
			});
			$('#bf-entry-modal').hide();
		});
	});

	$(document).on('click', '.bf-delete-entry', function (e) {
		e.preventDefault();
		var entryId = $(this).data('entry-id');
		if (!confirm(i18n.confirmDelete)) {
			return;
		}
		$.post(ajaxurl, { action: 'formulate_pricing_delete_entry', entry_id: entryId, nonce: nonce }, function () {
			$('.bf-entry-row[data-entry-id="' + entryId + '"]').fadeOut(300, function () {
				$(this).remove();
			});
		});
	});

	$(document).on('click', '.bf-modal-close, .bf-modal-backdrop', function () {
		$('#bf-entry-modal').hide();
	});

	$('#bf-bulk-apply').on('click', function () {
		var action = $('#bf-bulk-action').val();
		if (!action) {
			alert(i18n.selectAction);
			return;
		}
		var ids = $('.bf-entry-check:checked')
			.map(function () {
				return $(this).val();
			})
			.get();
		if (!ids.length) {
			alert(i18n.selectEntries);
			return;
		}
		if (action === 'delete' && !confirm(i18n.confirmBulkDelete)) {
			return;
		}
		$.post(ajaxurl, { action: 'formulate_pricing_bulk_entry_action', bulk_action: action, ids: ids, nonce: nonce }, function (res) {
			if (!res.success) {
				return;
			}
			if (action === 'delete') {
				ids.forEach(function (id) {
					$('.bf-entry-row[data-entry-id="' + id + '"]').fadeOut(300, function () {
						$(this).remove();
					});
				});
			}
			$('.bf-bulk-msg').text(res.data.message);
			setTimeout(function () {
				$('.bf-bulk-msg').text('');
			}, 3000);
		});
	});
});
