$(document).ready(function() {
	console.log("Download Quadrica ticket fields data");
	const quadrica_domain = "quadrica";
	const api_key = "uiRqGQxj6ERuMyA9rxOq";

	$.ajax({
		url: "https://" + quadrica_domain + ".freshdesk.com/api/v2/ticket_fields/",
		type: "GET",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		headers: {
			Authorization: "Basic " + btoa(api_key + ":x"),
		},
		success: function(data, textStatus, jqXHR) {
			$("#result").css("background-color", "limegreen");
			$("#result").text("Success");
			$("#code").text(jqXHR.status);
			$("#response").html(JSON.stringify(data, null, "<br/>"));

			// create & click a button that download the ticket fields data
			let a = document.createElement("a");
			a.href = "data:application/javascript," + encodeURIComponent("const data_ticket_form = \n" + JSON.stringify(data, null, 4));
			a.download = "ticket_fields.js";
			a.click();
		},
		error: function(jqXHR, tranStatus) {
			$("#result").css("background-color", "lightsalmon");
			$("#result").text("Error");
			$("#code").text(jqXHR.status);
			response_text = jqXHR.responseText;
			$("#response").html("Error Message : <b style='color: red'>" + response_text + "</b>.<br/> Please contact commercial@quadrica.fr for more information.");
		},
	});
});
