/**
* Function to get a URL parameter by name
* @param {String} name - The name of the URL parameter to retrieve
* @param {String} url - The URL to search (optional, defaults to window.location.href)
* @returns {String|null} - The value of the URL parameter or null if not found
*/
function getUrlParameter(name, url = window.location.href) {
name = name.replace(/[\[\]]/g, '\\$&');
const regex = new RegExp('[?&]' + name + '(=([^]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}
/**
* Function to make an HTTP GET request with a URL parameter
* @param {String} paramName - The name of the URL parameter to use in the request
* @param {String} requestUrl - The base URL for the HTTP request
*/
async function requestContractInfo() {
// const requestUrl = 'http://hq.geolink.pt:23000/api/kiosk_stand/retrieve_contract'; // DEV
const requestUrl = 'https://taxi.geolink.pt:23000/api/kiosk_stand/retrieve_contract'; // PROD
const contractId = getUrlParameter('id');
if (!contractId) {
console.error('Contract ID not found in the URL');
window.location = 'https://taxi-link.com';
return;
}
const formData = new FormData();
formData.append('contract_id', contractId);
const requestOption = {
method: 'POST',
body: formData,
}
try {
const response = await fetch(requestUrl, requestOption)
.then(response => response.json())
const content = response.content;
fillFormatDateTime(new Date(content.date_creation));
fillClientInfo(content.kiosk_stand_user);
fillTaxiInfo(content);
fillServiceAmount(content);
fillDestinationAddress(content)
renderCanceledSpan(content)
} catch (error) {
console.error('Fetch error:', error);
window.location = 'https://taxi-link.com'
}
}
function renderCanceledSpan(content) {
if (!content) {
console.error('Service data not found');
return;
}
if (content.canceled === 'True') {
document.getElementById("canceled-span").innerHTML = "CANCELADO";
}
}
/**
* fill destination address
*/
function fillDestinationAddress(content) {
const destinationAddress = document.getElementById("destination-address");
const destinationAddress2 = document.getElementById("destination-address-2");
if (!content) {
console.error('Service data not found');
return;
}
if (content.end_address) {
const end_address = content.end_address;
destinationAddress.innerHTML = end_address;
destinationAddress2.innerHTML = end_address;
}
}
/**
* fill destination address
*/
function fillStartAddress(serviceData) {
const destinationAddress = document.getElementById("start-address");
const destinationAddress2 = document.getElementById("start-address-2");
if (!serviceData) {
console.error('Service data not found');
return;
}
// start_address
// end_address
if (serviceData.start_address) {
const start_address = serviceData.start_address;
destinationAddress.innerHTML = start_address;
destinationAddress2.innerHTML = start_address;
}
}
/**
*
* @param {*} serviceData
*/
function fillServiceAmount(content) {
const serviceAmount = document.getElementById("service-amount");
if (!content) {
console.error('Service data not found');
return;
}
if (typeof content.amount === 'number') {
serviceAmount.innerHTML = content.amount.toFixed(2) + '€';
}
if (typeof content.amount === 'string') {
serviceAmount.innerHTML = content.amount + '€';
}
}
/**
*
* @param {name, dev_id} taxiData
*/
function fillTaxiInfo(taxiData) {
const taxiIdentify = document.getElementById("taxi-identify");
if (!taxiData) {
console.error('Taxi data not found');
return;
}
if (taxiData.name) {
taxiIdentify.innerHTML += 'Condutor: ' + taxiData.name + '
';
}
if (taxiData.dev_id) {
taxiIdentify.innerHTML += 'Licença: ' + taxiData.dev_id;
}
}
/**
* fill client info
* @param {name, phone, email} - The client data
*/
function fillClientInfo(clientData) {
const clientIdentify = document.getElementById("client-identify");
if (!clientData) {
console.error('Client data not found');
return;
}
if (clientData.name) {
clientIdentify.innerHTML = 'Nome: ' + clientData.name + '
';
}
if (clientData.phone) {
clientIdentify.innerHTML += 'Telefone: ' + clientData.phone + '
';
}
if (clientData.email) {
clientIdentify.innerHTML += 'Email: ' + clientData.email;
}
}
/**
* get the current date and time in pt
*/
function fillFormatDateTime(date) {
const dateTime = document.getElementById("date-time");
// var dateStr = date.toLocaleString('pt-PT', { timeZone: 'europe/lisbon' });
var dateStr = date.toLocaleDateString('pt-PT', { timeZone: 'europe/lisbon' });
dateTime.innerHTML = dateStr;
}
/**
* function
*/
requestContractInfo();