diff --git a/Js/fonctions.js b/Js/fonctions.js index 709df423..22faf99b 100755 --- a/Js/fonctions.js +++ b/Js/fonctions.js @@ -46279,121 +46279,151 @@ function afficheDonneesTable(fichier){ } -function addgctypegarant(){ - $('#div_edit_typegarant').empty(); - donnees = ''; - $.ajax({ - url: $("#racineWeb").val()+"Ajaxouvrirtypegarant/", - type : 'post', - data: donnees, - error: function(errorData){ - alert("Erreur : "+errorData); - }, - success: function(data) { - //alert("Success : "+data); - $('#div_add_typegarant').html(data); - $('#div_add_typegarant').modal("show"); - $('#div_add_typegarant').on('shown.bs.modal', function(){ - stylechampsRequis(); - $('#codeTypeGarant').focus(); - }); - }, - complete: function() { - - } - }); -} +function addgctypegarant() { + const racineWeb = $("#racineWeb").val(); + const $modalAdd = $('#div_add_typegarant'); + const $modalEdit = $('#div_edit_typegarant'); + // Nettoyage de l'autre modale pour éviter les conflits d'IDs + $modalEdit.empty(); -function editgctypegarant(id){ + $.ajax({ + url: racineWeb + "Ajaxouvrirtypegarant/", + type: 'POST', + beforeSend: function() { + // Optionnel : Vous pouvez ajouter un loader global ici + }, + success: function(data) { + // Injection du contenu dans la modale + $modalAdd.html(data); + + // Affichage de la modale + $modalAdd.modal("show"); - $('#div_add_typegarant').empty(); - - donnees ='id='+id; - - $.ajax({ - url: $("#racineWeb").val()+"Ajaxmodifiertypegarant/", - type : 'post', - data: donnees, - error: function(errorData){ - alert("Erreur : "+errorData); - }, - success: function(data) { - //alert("Success : "+data); - $('#div_edit_typegarant').html(data); - $('#div_edit_typegarant').modal("show"); - $('#div_edit_typegarant').on('shown.bs.modal', function(){ - stylechampsRequis(); - $('#codeTypeGarant').focus(); - }); - - }, - complete: function() { - - } - }); + // Actions une fois que la modale est visible + $modalAdd.off('shown.bs.modal').on('shown.bs.modal', function() { + // Focus sur le premier champ pour une saisie rapide + $('#codeTypeGarant').focus(); + + // Application du style Neutral Pro aux champs requis + if (typeof stylechampsRequis === 'function') { + stylechampsRequis(); + } + }); + }, + error: function(xhr, status, error) { + console.error("Erreur Ouverture Modale:", error); + if (typeof toastr !== 'undefined') { + toastr.error("Impossible d'ouvrir le formulaire d'ajout"); + } else { + alert("Erreur lors de l'ouverture du formulaire."); + } + } + }); } -function enregistrerTypeGarant(){ - var codeTypeGarant = $('#codeTypeGarant').val(), - libelle = $('#libelle').val(), - id = $('#id').val(); - - - fichier_lister = 'Ajaxlistergctypegarant'; - - if(id == undefined){ - link = 'Ajaxouvrirtypegarant'; - }else{ - //fichier_lister = 'Ajaxlistercolleges'; - link = 'Ajaxmodifiertypegarant'; - } - +function editgctypegarant(id) { + const racineWeb = $("#racineWeb").val(); + const codeLangue = $("#codeLangue").val(); + const $modalEdit = $('#div_edit_typegarant'); + const $modalAdd = $('#div_add_typegarant'); - if(libelle <=' '){ - v_msg="Le libellé est obligatoire !"; - v_msgEng="The label is required !"; - - alert_ebene(v_msg,v_msgEng); - - $('#libelle').css('borderColor','red'); - return; - } - donnees = $('#formModal').serialize(); - - $.ajax({ - url: $("#racineWeb").val()+link+"/enregistrer/", - type : 'post', - data: donnees, - error: function(errorData){ - alert("Erreur : "+errorData); - }, - success: function(data) { - //alert("Success : "+data); - /*if(data.length > 5){ - var str = data.split('/'); - alert_ebene(str[0],str[1]); - return; - } - */ - - if(id==undefined){ - afficheDonneesTable(fichier_lister); - $("#div_add_typegarant .close").click(); - }else{ - afficheDonneesTable(fichier_lister); - $("#div_edit_typegarant .close").click(); - } - //dataTableSpeciale(); - - - }, - complete: function() { - window.location.assign($("#racineWeb" ).val()+"Gctypegarant/"); - } - }); - + // Nettoyage de la modale d'ajout pour éviter les conflits + $modalAdd.empty(); + + const msgError = (codeLangue === "en_US") + ? "Error loading data" + : "Erreur lors du chargement des données"; + + $.ajax({ + url: racineWeb + "Ajaxmodifiertypegarant/", + type: 'POST', + data: { id: id }, + success: function(data) { + $modalEdit.html(data); + $modalEdit.modal("show"); + + $modalEdit.off('shown.bs.modal').on('shown.bs.modal', function() { + // Focus sur le libellé car le code est généralement readonly en édition + $('#libelle').focus(); + + if (typeof stylechampsRequis === 'function') { + stylechampsRequis(); + } + }); + }, + error: function(xhr) { + console.error("Edit Error:", xhr.responseText); + if (typeof toastr !== 'undefined') { + toastr.error(msgError); + } else { + alert(msgError); + } + } + }); +} + + +function enregistrerTypeGarant() { + // 1. Initialisation des variables et sélecteurs + const libelle = $('#libelle').val().trim(); + const id = $('#id').val(); // Sera undefined en création + const racineWeb = $("#racineWeb").val(); + const codeLangue = $("#codeLangue").val(); + const $btn = $('#btn-enreg-typegarant'); + + // Détermination de l'URL cible (Link) + let link = (id === undefined) ? 'Ajaxouvrirtypegarant' : 'Ajaxmodifiertypegarant'; + + // 2. Gestion des traductions + const msgRequired = (codeLangue === "en_US") ? "The label is required !" : "Le libellé est obligatoire !"; + const msgSuccess = (codeLangue === "en_US") ? "Saved successfully" : "Enregistrement réussi"; + + // 3. Validation + if (libelle === '') { + if (typeof toastr !== 'undefined') { + toastr.warning(msgRequired); + } else { + alert(msgRequired); + } + $('#libelle').addClass('border-danger').focus(); + return; + } + + // 4. Préparation de l'envoi + const donnees = $('#formModal').serialize(); + $btn.prop('disabled', true).html('...'); + + $.ajax({ + url: racineWeb + link + "/enregistrer/", + type: 'post', + data: donnees, + success: function(data) { + // Fermeture des modales selon le cas (standard Bootstrap 5) + const modalId = (id === undefined) ? '#div_add_typegarant' : '#div_edit_typegarant'; + const modalInstance = bootstrap.Modal.getInstance(document.querySelector(modalId)); + + if (modalInstance) { + modalInstance.hide(); + } + + if (typeof toastr !== 'undefined') { + toastr.success(msgSuccess); + } + }, + error: function(xhr) { + console.error("Erreur Enregistrement:", xhr.responseText); + $btn.prop('disabled', false).html(' Enregistrer'); + if (typeof toastr !== 'undefined') { + toastr.error("Erreur serveur"); + } + }, + complete: function() { + // Redirection ou rafraîchissement propre + window.location.assign(racineWeb + "Gctypegarant/"); + } + }); } // Supprime une ligne du tableau diff --git a/Vue/Ajaxmodifiertypegarant/index.php b/Vue/Ajaxmodifiertypegarant/index.php index e5629f17..c88489ec 100755 --- a/Vue/Ajaxmodifiertypegarant/index.php +++ b/Vue/Ajaxmodifiertypegarant/index.php @@ -1,41 +1,69 @@ -