function getURLForm(provider) {
	let url = "";
	switch (provider) {
		case "Konecta":
			url =
				"/02sicb/formulario/KonectaAjaxController/enviarFormularioFactura.ajax";
			break;
	}
	return url;
}

function generateDataForm(options) {
	const data = {
		id: "",
		provider: options.provider,
		promotion: options.promotion,
		segment: options.segment,
		url: getURLForm(options.provider),
	};
	setTagForm(options, data.id);
	return data;
}

function renderResponseForm(response) {
	initModalRes();
	const el = getElModalRes();
	const serverResponse = {
		mainResponse: response.entidad.mensaje,
		error: response.entidad.errores,
	};

	switch (serverResponse.error) {
		case 0:
			$(el.title).text("Obrigado!");
			$(el.text).text(serverResponse.mainResponse);
			$(el.img.tag).attr("src", el.img.ok);
			if ($(el.btnContainer).hasClass("d-none")) {
				$(el.btnContainer).removeClass("d-none");
			}
			break;
		default:
			$(el.title).text("Erro!");
			$(el.text).text(serverResponse.mainResponse);
			$(el.img.tag).attr("src", el.img.ko);
			if (!$(el.btnContainer).hasClass("d-none")) {
				$(el.btnContainer).addClass("d-none");
			}
			break;
	}

	showModalRes();
}

function setTagForm(options, id) {
	const form = options.form;
	$(form).attr("data-generic-tag", "cmb");
	$(form).data("generic-tag", "cmb");
	$(form).attr("data-cmb-proveedor", options.provider);
	$(form).data("cmb-proveedor", options.provider);
	$(form).attr("data-cmb-campania", options.promotion);
	$(form).data("cmb-campania", options.promotion);
	$(form).attr("data-cmb-campania", options.promotion);
	$(form).data("cmb-campania", options.promotion);
	$(form).attr("data-cmb-idsolicitud", id);
	$(form).data("cmb-idsolicitud", id);
}

function getElModalRes() {
	return {
		modal: $("#form-modal-response"),
		img: {
			tag: $("#form-modal-response .img-okko"),
			ok:
				"/02sicb/gc/prod/adminwcm/img/estaticos/oferta-personalizada/obrigado.svg",
			ko:
				"/02sicb/gc/prod/adminwcm/img/estaticos/generic-chat/ico-pago-ko.svg",
		},
		title: $("#form-modal-response .title-okko"),
		text: $("#form-modal-response .text-okko"),
		btnContainer: $("#form-modal-response #ok-buttons"),
	};
}

function getElModalErr() {
	return {
		modal: $("#form-modal-error"),
		img: {
			tag: $("#form-modal-error .img-okko"),
			ok:
				"/02sicb/gc/prod/adminwcm/img/estaticos/generic-chat/ico-pago-ok.svg",
			ko:
				"/02sicb/gc/prod/adminwcm/img/estaticos/generic-chat/ico-pago-ko.svg",
		},
		title: $("#form-modal-error .title-okko"),
		text: $("#form-modal-error .text-okko"),
		btnContainer: $("#form-modal-error #ko-buttons"),
	};
}

function showModalRes() {
	$("#form-modal-response").modal("show");
}

function showModalError(errors) {
	initModalError();
	let error = errors[0].defaultMessage;
	const el = getElModalErr();
	$(el.modal).modal("show");
	$(el.img.tag).attr("src", el.img.ko);
	$(el.title).text("Erro!");
	$(el.text).text(error);
}

function initModalRes() {
	if ($("#form-modal-response").length === 0) {
		$("<div>")
			.attr("id", "form-modal-response")
			.attr("class", "modal fade")
			.append(
				$("<div>")
					.attr("class", "modal-dialog")
					.append(
						$("<div>")
							.attr("class", "modal-content")
							.append(
								$("<div>")
									.attr("class", "modal-body")
									.append([
										$("<span>")
											.attr("class", "btn-close")
											.attr("data-dismiss", "modal")
											.html("&times;"),
										$("<img>").attr("class", "img-okko"),
										$("<p>").attr("class", "title-okko"),
										$("<p>").attr("class", "text-okko"),
										$("<div>")
											.attr("id", "ok-buttons")
											.attr("class", "d-none")
											.append(
												$("<div>")
													.attr(
														"class",
														"content-buttons",
													)
													.append([
														$("<a>")
															.attr(
																"id",
																"btn-area-cliente",
															)
															.attr(
																"class",
																"btn wcom-button btn wcom-button button-green",
															)
															.attr(
																"href",
																"/login",
															)
															.append(
																$("<span>")
																	.attr(
																		"class",
																		"button-text",
																	)
																	.text(
																		"Área do Cliente",
																	),
															),
														$("<a>")
															.attr(
																"id",
																"btn-homepage",
															)
															.attr(
																"class",
																"btn wcom-button btn wcom-button button-green",
															)
															.attr("href", "/")
															.append(
																$("<span>")
																	.attr(
																		"class",
																		"button-text",
																	)
																	.text(
																		"Homepage Iberdrola",
																	),
															),
													]),
											),
									]),
							),
					),
			)
			.appendTo("body");
	}
}

function initModalError() {
	if ($("#form-modal-error").length === 0) {
		$("<div>")
			.attr("id", "form-modal-error")
			.attr("class", "modal fade")
			.append(
				$("<div>")
					.attr("class", "modal-dialog")
					.append(
						$("<div>")
							.attr("class", "modal-content")
							.append(
								$("<div>")
									.attr("class", "modal-body")
									.append([
										$("<span>")
											.attr("class", "btn-close")
											.attr("data-dismiss", "modal")
											.html("&times;"),
										$("<img>").attr("class", "img-okko"),
										$("<p>").attr("class", "title-okko"),
										$("<p>").attr("class", "text-okko"),
									]),
							),
					),
			)
			.appendTo("body");
	}
}
