jQuery(function ($) {
	var tab = $(".chama-tab-switch");
	$("#chama-commission-share-buttons > .chama-copy-link").on("click", function (e) {
		e.preventDefault();
		navigator.clipboard.writeText(window.location.href);
		this.insertAdjacentHTML('afterend', '<div class="chama-copy-tooltip">Link copied</div>'); setTimeout(() => { document.querySelectorAll('.chama-copy-tooltip').forEach(el => el.remove()); }, 3000);
	});
	$(document.body).on('change', '.chama-addon-price', function () {
		let addons = [];
		var total = parseFloat($('#commission_amount').val());
		var min = $("#commission_amount").attr('min');
		var add_on_cost = parseFloat($(this).data('cost'));
		var validation_message = 'You must pay at least ';
		var symbol = $('#chama-symbol').val();
		//console.log(total);
		if (total == min) {
			if (this.checked) {
				total = total + add_on_cost;
				$('#commission_amount').val(total);
				$("#commission_amount").attr('min', total);
				validation_message = validation_message + symbol + total + '!';
				$("#commission_amount").data('msg-min', validation_message);
				$('#chama-pay-what-you-want-total').text(symbol + total);

			}
			else {
				total = total - add_on_cost;
				$('#commission_amount').val(total);
				$("#commission_amount").attr('min', total);
				validation_message = validation_message + symbol + total + '!';
				$("#commission_amount").data('msg-min', validation_message);
				$('#chama-pay-what-you-want-total').text(symbol + total);
			}
			//add to array
			$(".chama-addon-price:checked").each(function () {
				addons.push($(this).data('description') + '|' + $(this).data('cost'));
			});
			if (addons.length) {
				$('#commission_add_ons').val(addons);
			}
			else {
				$('#commission_add_ons').val('');
			}
		}

	});
	$(document.body).on('change', '#chama-shipping-price', function () {
		var total = parseFloat($('#commission_amount').val());
		var min = $("#commission_amount").attr('min');
		var validation_message = 'You must pay at least ';
		var symbol = $('#chama-symbol').val();
		var current_shipping_cost = parseFloat($(this).data('current'));
		var shipping_cost = parseFloat($(this).find(":selected").data('cost'));
		var shipping_location = $(this).find(":selected").data('location');
		var shipping_location_code = $(this).find(":selected").data('location-code');
		if (total == min) {
			total = total - current_shipping_cost;
			total = total + shipping_cost;
			$('#commission_amount').val(total);
			$("#commission_amount").attr('min', total);
			validation_message = validation_message + symbol + total + '!';
			$("#commission_amount").data('msg-min', validation_message);
			$(this).data('current', shipping_cost);
			$('#commission_shipping').val(shipping_location + '|' + shipping_cost + '|' + shipping_location_code);
			$('#chama-pay-what-you-want-total').text(symbol + total);
		}

	});
	const commission_form = document.getElementById('chama-commission-order-form');
	if (commission_form !== null) {
		commission_form.addEventListener('submit', async (e) => {
			// Avoid a full page POST request.
			e.preventDefault();
			var form = $("#chama-commission-order-form");

			if (!form.valid()) {
				return false;
			}
			var base_rest_url = $('#chama_base_rest_url').val();

			var nonce = $('#_wpnonce').val();
			var type = 'commission';
			var commission_id = $('#commission_id').val();
			var input_amount = $('#commission_amount').val();
			const form_data = {
				'_wpnonce': nonce,
				'amount': input_amount,
				'type': type,
				'commission_id': commission_id,
			};
			// Disable the form from submitting twice.
			commission_form.querySelector('.chama-commission-button').disabled = true;
			$('.chama-commission-button').addClass('chama-disabled-button');

			// server validation
			const { status, message } = await $.ajax({
				url: base_rest_url + "v1/stripe/validate",
				type: "POST",
				data: form_data,
				error: function (data) {
					var response = JSON.parse(data.responseText);

					$('#chama-payment-errors').html(response.message);
					$('#chama-payment-errors').addClass('show');
					// Re-enable the form so the customer can resubmit.
					commission_form.querySelector('.chama-commission-button').disabled = false;
					$('.chama-commission-button').removeClass('chama-disabled-button');
					return;
				}
			});
			if (status == 'passed') {
				commission_form.submit();
			}

		});
	}



});



