$(function () {
	$("input#date_of_birth")
		.datepicker({
			format: "dd/mm/yyyy",
			todayHighlight: true,
			autoclose: true,
			endDate: "0",
		})
		.on("changeDate", function (ev) {});

	$(".confirm_and_submit").click(function (event) {
		event.preventDefault();

		$("#errorPrompt").hide();
		$("#errorPrompt").html("");

		// validate and process form here

		$(".confirm_and_submit").addClass("disabled");

		$(".legal_name_feedback").hide();
		var legal_name = $("input#legal_name").val();
		if (legal_name == "") {
			$(".legal_name_feedback").html("Full legal name cannot be empty");
			$(".legal_name_feedback").show();
			$("input#legal_name").focus();

			$(".confirm_and_submit").removeClass("disabled");
			return false;
		}

		$(".nationality_feedback").hide();
		var nationality = $("input#nationality").val();
		if (nationality == "") {
			$(".nationality_feedback").html("Nationality cannot be empty");
			$(".nationality_feedback").show();
			$("input#nationality").focus();

			$(".confirm_and_submit").removeClass("disabled");
			return false;
		}

		$(".date_of_birth_feedback").hide();
		var date_of_birth = $("input#date_of_birth").val();
		if (date_of_birth == "") {
			$(".date_of_birth_feedback").html("Date of birth cannot be empty");
			$(".date_of_birth_feedback").show();
			$("input#date_of_birth").focus();

			$(".confirm_and_submit").removeClass("disabled");
			return false;
		}

		$(".address_feedback").hide();
		var address = $("input#address").val();
		if (address == "") {
			$(".address_feedback").html("Address cannot be empty");
			$(".address_feedback").show();
			$("input#address").focus();

			$(".confirm_and_submit").removeClass("disabled");
			return false;
		}

		$(".ssn_feedback").hide();
		var ssn = "default";
		if ($("input#ssn").val()) {
			ssn = $("input#ssn").val();
			if (ssn == "") {
				$(".ssn_feedback").html("SSN cannot be empty");
				$(".ssn_feedback").show();
				$("input#ssn").focus();

				$(".confirm_and_submit").removeClass("disabled");
				return false;
			}
		}

		$(".mobile_number_feedback").hide();
		var mobile_number = $("input#mobile_number").val();
		var mobile_pattern = /^((\+[1-9]{1,4}[ \-]*)|(\([0-9]{2,3}\)[ \-]*)|([0-9]{2,4})[ \-]*)*?[0-9]{3,4}?[ \-]*[0-9]{3,4}?$/;

		if (mobile_number == "" || !mobile_number.startsWith("+") || !mobile_number.match(mobile_pattern)) {
			$(".mobile_number_feedback").html("Enter a valid mobile number.");
			$(".mobile_number_feedback").show();
			$("input#mobile_number").focus();

			$(".confirm_and_submit").removeClass("disabled");
			return false;
		}

		$(".email_feedback").hide();
		var email_pattern = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
		var email = $("input#email").val();

		if (email == "" || !email.match(email_pattern)) {
			$(".email_feedback").html("Enter a valid email address.");
			$(".email_feedback").show();
			$("input#email").focus();

			$(".confirm_and_submit").removeClass("disabled");
			return false;
		}

		var validImageTypes = ["image/gif", "image/jpeg", "image/jpg", "image/png"];
		let request_data = new FormData();

		var identity_file = $("#identity_file")[0].files[0];

		$(".identity_file_feedback").hide();
		if (identity_file.length === 0) {
			$(".identity_file_feedback").html("Please upload identity document with photo.");
			$(".identity_file_feedback").show();
			$("input#identity_file").focus();

			$(".confirm_and_submit").removeClass("disabled");
			return false;
		} else {
			request_data.append("identity_file", identity_file);
		}

		var backside_file = $("#backside_file")[0].files[0];

		$(".backside_file_feedback").hide();
		if (backside_file.length === 0) {
			$(".backside_file_feedback").html("Please upload backside of identity document.");
			$(".backside_file_feedback").show();
			$("input#backside_file").focus();

			$(".confirm_and_submit").removeClass("disabled");
			return false;
		} else {
			request_data.append("backside_file", backside_file);
		}

		$("input#legal_name").prop("disabled", true);
		$("input#nationality").prop("disabled", true);
		$("input#date_of_birth").prop("disabled", true);
		$("input#address").prop("disabled", true);
		$("input#mobile_number").prop("disabled", true);
		$("input#email").prop("disabled", true);
		$("input#ssn").prop("disabled", true);
		$("input#identity_file").prop("disabled", true);
		$("input#backside_file").prop("disabled", true);

		$(".button_spinner").show();

		request_data.append("legal_name", legal_name);
		request_data.append("nationality", nationality);
		request_data.append("date_of_birth", date_of_birth);
		request_data.append("address", address);
		request_data.append("mobile_number", mobile_number);
		request_data.append("email", email);
		request_data.append("ssn", ssn);

		$.ajax({
			url: "../helpers/identity_verification",
			type: "POST",
			data: request_data,
			processData: false,
			contentType: false,
		}).done(function (response) {
			if (response == "success") {
				$("#errorPrompt").hide();
				$(".confirm_and_submit").removeClass("disabled");
				$(".button_spinner").hide();

				$("#page_content").html('<div class="col-md-12">' + '<h6 class="card-title">Identity Verification</h6>' + '<p class="primary-color"><i class="link-icon" data-feather="user-check"></i></p>' + '<p style="margin-top: 30px">Identity verification documents <br/> have been submitted.</p>' + "</div>");
			} else {
				$("#errorPrompt").html(response);
				$("#errorPrompt").show();
				$(".confirm_and_submit").removeClass("disabled");
				$(".button_spinner").hide();

				$("input#legal_name").prop("disabled", false);
				$("input#nationality").prop("disabled", false);
				$("input#date_of_birth").prop("disabled", false);
				$("input#address").prop("disabled", false);
				$("input#mobile_number").prop("disabled", false);
				$("input#email").prop("disabled", false);
				$("input#ssn").prop("disabled", false);
				$("input#identity_file").prop("disabled", false);
				$("input#backside_file").prop("disabled", false);

				$("html, body").animate(
					{
						scrollTop: $("#top_area").offset().top,
					},
					500
				);
			}
		});

		//cancel the submit button default behaviours
		return false;
	});
});
