var classes = $("#profile").prop("class");
$("#profile").prop("class", classes + " active");

$(document).ready(function () {
	$("#phone_number").intlTelInput({
		formatOnDisplay: true,
		nationalMode: true,
		separateDialCode: true,
		autoHideDialCode: false,
		autoPlaceholder: "polite",
		formatOnDisplay: true,
	});

	$("#save_details_btn").click(function (event) {
		event.preventDefault();

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

		$("#save_details_btn").attr("disabled", "");

		$(".fullname_feedback").hide();
		var fullname = $("input#fullname").val();
		if (fullname == "") {
			$(".fullname_feedback").html("First name cannot be empty.");
			$(".fullname_feedback").show();
			$("input#fullname").focus();

			$("#save_details_btn").removeAttr("disabled");
			return false;
		}

		var countryData = $("#phone_number").intlTelInput(
			"getSelectedCountryData"
		);
		var dialCode = countryData.dialCode;
		var phone_number = $("input#phone_number").val();

		var usr_number = "";
		if (phone_number == "") {
			usr_number = "default";
		} else {
			usr_number = "+" + dialCode + phone_number;
		}

		$(".save_profile_spinner").show();

		$("input#fullname").prop("disabled", true);
		$("input#phone_number").prop("disabled", true);
		$("input#address_1").prop("disabled", true);
		$("input#address_2").prop("disabled", true);
		$("input#city").prop("disabled", true);
		$("input#region").prop("disabled", true);
		$("input#postal_code").prop("disabled", true);
		$("input#website").prop("disabled", true);

		var form_data =
			"fullname=" +
			fullname +
			"&phone_number=" +
			encodeURIComponent(usr_number)+
			"&address_1=" +
			encodeURIComponent($("input#address_1").val())+
			"&address_2=" +
			encodeURIComponent($("input#address_2").val())+
			"&city=" +
			encodeURIComponent($("input#city").val())+
			"&region=" +
			encodeURIComponent($("input#region").val())+
			"&postal_code=" +
			encodeURIComponent($("input#postal_code").val())+
			"&website=" +
			encodeURIComponent($("input#website").val());

		$.ajax({
			url: "../helpers/profile-settings",
			type: "POST",
			data: form_data,
		}).done(function (response) {
			if (response == "success") {
				$("#feedbackPrompt").hide();

				$("#feedbackPrompt").html(
					'<div class="alert alert-success" role="alert">Changes saved!</div>'
				);
				$("#feedbackPrompt").show();
			} else {
				$("#feedbackPrompt").html(
					'<div class="alert alert-danger" role="alert">' +
						response +
						"</div>"
				);
				$("#feedbackPrompt").show();
			}

			$(".save_profile_spinner").hide();

			$("#save_details_btn").removeAttr("disabled");

			$("input#fullname").prop("disabled", false);
			$("input#phone_number").prop("disabled", false);
			$("input#address_1").prop("disabled", false);
			$("input#address_2").prop("disabled", false);
			$("input#city").prop("disabled", false);
			$("input#region").prop("disabled", false);
			$("input#postal_code").prop("disabled", false);
			$("input#website").prop("disabled", false);
		});

		return false;
	});
});
