document.addEventListener('DOMContentLoaded', async function () { const numParcours = getNumParcoursFromURL(); // Initialisation des modals Materialize const modals = document.querySelectorAll('.modal'); M.Modal.init(modals); // Récupération des informations du parcours (async () => { try { const data = await fetchWithJson(`/parcours/read/${numParcours}`, 'GET'); if (data.valid) { const contrat = data.parcours["@expand"].contrat; if (contrat) { document.getElementById('idContrat').value = isNullOrUndefined(contrat.id) ? '' : contrat.id; document.getElementById('numContrat').value = isNullOrUndefined(contrat.numContrat) ? '' : contrat.numContrat; document.getElementById('numSaisine').value = isNullOrUndefined(contrat.numSaisine) ? '' : contrat.numSaisine; if (contrat.produit) { document.getElementById('typeProjet').value = contrat.produit; } if (contrat.type) { document.getElementById('typeProduit').value = contrat.type; } } checkNumberProductProject() } } catch (error) { console.error("Erreur lors de la récupération des informations du parcours:", error); } })(); // Vérification des informations contrats dans la base document.getElementById('checkNumsBtn').addEventListener('click', function (event) { // Empêche le comportement par défaut du formulaire event.preventDefault(); const numContratElement = document.getElementById('numContrat'); const numSaisineElement = document.getElementById('numSaisine'); const numContratValue = numContratElement ? numContratElement.value.trim() : ""; const numSaisineValue = numSaisineElement ? numSaisineElement.value.trim() : ""; if (numContratValue) { fetchContractDetails(`/contrat/read/numContrat/${numContratValue}`, numContratValue); } else { fetchContractDetails(`/contrat/read/numSaisine/${numSaisineValue}`, numSaisineValue); } }); // Ajouts des informations contrats au parcours document.getElementById('btnYes').addEventListener('click', function (event) { const idContrat = document.getElementById('idContrat').value; // Update Parcours avec ID contrat fetch(`/parcours/update/${numParcours}/${idContrat}`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, }) .then(response => response.json()) .then(data => { if (data.valid) { window.location.reload(); } else { console.log("Echec lors de la mise à jour de la relation id contrat - id parcours :", data); } }); }); // Ecoute de l'événement 'input' au champ numParcours document.getElementById('numContrat').addEventListener('input', function () { validateField('numContrat', true); }); // Ecoute de l'événement 'input' au champ numParcours document.getElementById('numSaisine').addEventListener('input', function () { validateField('numSaisine', true); }); // Ecoute de l'événement 'input' sur l'ensemble des champs du formulaire document.getElementById('projetForm').addEventListener('input', function () { checkNumberProductProject() }); //Submit Event "Enregistrer et continuer" document.getElementById('projetForm').addEventListener('submit', async function (event) { // Empêche le comportement par défaut du formulaire event.preventDefault(); // Supression mémoire tampon Client/Inter sessionStorage.removeItem('tmp'); // Extraction des valeurs du formulaire const numContrat = document.getElementById('numContrat').value; const numSaisine = document.getElementById('numSaisine').value; const typeProjet = document.getElementById('typeProjet').value; const typeProduit = document.getElementById('typeProduit').value; let idContrat = ""; if (numSaisine != "") { const response = await fetch(`/contrat/read/numSaisine/${numSaisine}`, { method: 'GET', headers: { 'Content-Type': 'application/json', }, }); const data = await response.json(); if (data.valid) { idContrat = data.contrat.id; } } if (numContrat != "" && idContrat == "") { const response = await fetch(`/contrat/read/numContrat/${numContrat}`, { method: 'GET', headers: { 'Content-Type': 'application/json', }, }); const data = await response.json(); if (data.valid) { idContrat = data.contrat.id; } } if (idContrat == "") { const response = await fetch(`/contrat/create`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, }); const data = await response.json(); if (data.valid) { idContrat = data.contrat.id; } } const response = await fetch(`/contrat/update/${idContrat}`, { method: 'POST', body: JSON.stringify({ "numContrat": numContrat, "numSaisine": numSaisine, "produit": typeProjet, "type": typeProduit }), headers: { 'Content-Type': 'application/json', }, }); const data = await response.json(); if (data.valid) { fetch(`/parcours/update/${numParcours}/${idContrat}`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, }) .then(response => response.json()) .then(data => { if (data.valid) { window.location.href = `/navParcours?numParcours=${getNumParcoursFromURL()}&submenu=client`; } else { console.log('Echec lors de la mise à jour de la relation id contrat - id parcours :', data); } }); } }); }); //Fonction fetch des informations contrat function fetchContractDetails(endpoint, value) { fetch(endpoint, { method: 'GET', headers: { 'Content-Type': 'application/json', }, }) .then(response => response.json()) .then(data => { if (data.valid) { // Affiche un modal const elem = document.getElementById('matchSaisineContrat'); const instance = M.Modal.getInstance(elem); const contrat = data.contrat; const client = contrat && contrat["@expand"] ? contrat["@expand"].client : null; const intermediaire = contrat && contrat["@expand"] ? contrat["@expand"].intermediaire : null; document.getElementById('idContrat').value = contrat.id ? contrat.id : null; document.getElementById('displayedNumContrat').innerText = contrat.numContrat ? contrat.numContrat : null; document.getElementById('displayedNumSaisine').innerText = contrat.numSaisine ? contrat.numSaisine : null; document.getElementById('dateCreaContrat').innerText = contrat.created ? new Date(contrat.created).toLocaleDateString('fr-FR', { day: 'numeric', month: 'long', year: 'numeric' }) : null; document.getElementById('dateModiContrat').innerText = contrat.updated ? new Date(contrat.updated).toLocaleDateString('fr-FR', { day: 'numeric', month: 'long', year: 'numeric' }) : null; document.getElementById('displayedTypeContrat').innerText = contrat.type ? contrat.type : null; document.getElementById('displayedProduitContrat').innerText = contrat.produit ? contrat.produit : null; document.getElementById('nomClientContrat').innerText = client && client.nom ? client.nom : null; document.getElementById('nomInterContrat').innerText = intermediaire && intermediaire.nom ? intermediaire.nom : null; instance.open(); } else { // Affiche un modal const elem = document.getElementById('noMatchSaisineContrat'); const instance = M.Modal.getInstance(elem); instance.open(); } }); } //Vérifie si le numero de contrat OU le numero de saisine, en plus du choix du produit et projet, ont été selectionnés //Active le bouton submit en fonction function checkNumberProductProject() { const isNumContratValid = (document.getElementById('numContrat').value !== "" && validateField('numContrat', true)) const isNumSaisineValid = (document.getElementById('numSaisine').value !== "" && validateField('numSaisine', true)) if ((isNumContratValid || isNumSaisineValid) && (document.getElementById('typeProjet').value !== "") && (document.getElementById('typeProduit').value !== "")) { document.getElementById('infoClientBtn').disabled = false } else { document.getElementById('infoClientBtn').disabled = true } }