// SEARCH var clientsSearch = false; // VIEW var modalViewClient = false; var btnViewClient = false; var currentViewClient = 0; var clientsBaseURL = "clients.php"; // ADD var modalAddClient = false; var formAddClient = false; var btnAddClient = false; // EDIT var modalEditClient = false; var formEditClient = false; var btnEditClient = false; var currentEditClient = 0; // DELETE var modalDeleteClient = false; var btnDeleteClient = false; var currentDeleteClient = 0; $(document).ready( function() { // INIT SEARCH clientsSearch = $("#clientsSearch"); if(clientsSearch.length>0) initSearchClients(); // INIT VIEW CLIENT modalViewClient = $("#modalViewClient"); btnViewClient = $(".btnViewClient"); if(modalViewClient.length>0 && btnViewClient.length>0) { initViewClient(); // VIEW REQUEST var id = getUrlParameter("id"); if(id!==false && parseInt(id)>0) loadDatasInViewClientModal(id); } // INIT ADD CLIENT modalAddClient = $("#modalAddClient"); formAddClient = $("#formAddClient"); btnAddClient = $("#btnAddClient"); if(modalAddClient.length>0 && formAddClient.length>0 && btnAddClient.length>0) initAddClient(); // INIT EDIT CLIENT modalEditClient = $("#modalEditClient"); formEditClient = $("#formEditClient"); btnEditClient = $(".btnEditClient"); if(modalEditClient.length>0 && formEditClient.length>0 && btnEditClient.length>0) initEditClient(); // INIT DELETE CLIENT modalDeleteClient = $("#modalDeleteClient"); btnDeleteClient = $(".btnDeleteClient"); if(modalDeleteClient.length>0 && btnDeleteClient.length>0) initDeleteClient(); }); /***** SEARCH CLIENTS *****/ function initSearchClients() { clientsSearch.find("input[name=search]").unbind('keypress').keypress( function(event) { if(event.keyCode==13) searchClient(false); }); clientsSearch.find(".btnClearSearch").unbind('click').click( function(event) { searchClient(true); }); } function searchClient(clear) { clientsSearch.find("input[name=search]").blur(); clientsSearch.find(".btnClearSearch").blur(); if(clear) document.location = "?clearSearch"; else document.location = "?search="+clientsSearch.find("input[name=search]").val(); } /***** VIEW CLIENT *****/ function initViewClient() { // BTN VIEW btnViewClient.unbind('click').click( function(event) { event.preventDefault(); id = parseInt($(this).attr('ref')); if(!id>0) return; clearViewClientModal(); currentViewClient = id; loadDatasInViewClientModal(id); }); // INIT FORM VIEW var form = modalViewClient.find("div.formClient"); // TABS form.find("a.formClientTabBtn").click(function(e) { e.preventDefault(); // BTN if(!$(this).parent().hasClass("active")) { form.find("ul.formClientTabs > li").removeClass('active'); $(this).parent().addClass("active"); } // TAB var tab = form.find("div.tab."+$(this).attr("tab")); if(tab.length>0) { if(tab.hasClass("active")) return; form.find("div.formClientTabs > div.tab").removeClass("active"); tab.addClass("active"); } }); resizeFormClient(form); $( window ).on( "resize", function() { resizeFormClient(form) }); // CANCEL modalViewClient.on('hidden.bs.modal', function (e) { clearViewClientModal(); currentViewClient = 0; }); // EMPECHE LA FERMETURE SI ON EST ENTRAIN D'EDITER UNE LIGNE D'ABSENCE modalViewClient.on('hide.bs.modal', function(e) { if(modalViewClient.find("tr.editable").length>0) e.preventDefault(); }); } function resizeFormClient(form) { var wh = window.innerHeight; form.find("div.formClientTabs div.tab").css("max-height", (wh-180)+"px"); } function loadDatasInViewClientModal(id) { datas = { 'ref' : id, 'action' : 'getDatas' }; $.post( clientsBaseURL, datas, function( result ) { var datas = JSON.parse(result); currentViewClient = parseInt(datas.ref); modalViewClient.find("small.db_ref > span").html(datas.ref); modalViewClient.find("td.nom").html(datas.prenom+" "+datas.nom); modalViewClient.find("td.tel").html( (datas.tel!="" && datas.tel!=null) ? ''+datas.tel+'' : "." ); modalViewClient.find("td.email").html( (datas.email!="" && datas.email!=null) ? ''+datas.email+'' : "."); // CONTRATS $.post(clientsBaseURL, { 'ref' : id, 'action' : 'modalView_getTab', 'tab' : 'contrats' }, function(result) { modalViewClient.find("div.tabContrats").html(result); }).fail(function() { alert(srvErrorMsg+" (load modal view client - tab contrats)"); }); // ABSENCES loadModalViewTabAbsences(); modalViewClient.modal('show'); }).fail(function() { alert(srvErrorMsg+" (load modal view client)"); }); } function clearViewClientModal() { modalViewClient.find("small.db_ref > span").html(""); modalViewClient.find("ul.formClientTabs > li").removeClass('active hide'); modalViewClient.find("ul.formClientTabs > li:first-child").addClass('active'); modalViewClient.find("div.formClientTabs > div.tab").removeClass("active"); modalViewClient.find("div.formClientTabs > div.tab:first-child").addClass("active"); modalViewClient.find("td.nom").html(""); modalViewClient.find("td.tel").html(""); modalViewClient.find("td.email").html(""); modalViewClient.find("div.tabContrats").html(""); modalViewClient.find("div.tabAbsences").html(""); } function initModalViewClientTabAbsences() { // BTN ADD ABSENCES modalViewClient.find("#btnAddAbsence").click(function(e) { e.preventDefault(); $(this).blur(); var tr = $("").html( modalViewClient.find("tr.absenceNewRowTemplate").html() ); modalViewClient.find("div.tabAbsences table tbody").append(tr); nullChild = modalViewClient.find("div.tabAbsences table tbody tr.nullChild"); if(nullChild.length>0) nullChild.remove(); initModalViewClientAbsenceRow(tr); }); // INIT ROWS modalViewClient.find("div.tabAbsences table tbody tr.absence").each(function(n,e) { initModalViewClientAbsenceRow($(this)); }) } function initModalViewClientAbsenceRow(row) { var ref = row.attr("ref"); if(parseInt(ref)>0) ref = parseInt(ref); var tdDebut = row.find("td.debut"); var tdFin = row.find("td.fin"); var tdRmq = row.find("td.remarque"); var oldDebut = tdDebut.html(); var d = parseDate(oldDebut, "dd/mm/yyyy"); if(!d) d = new Date(); var debut = formatDate(d, "yyyy-mm-dd"); var oldFin = tdFin.html(); var f = parseDate(oldFin, "dd/mm/yyyy"); if(!f) f = new Date(); var fin = formatDate(f, "yyyy-mm-dd"); var oldRmq = tdRmq.html(); var rmq = `${oldRmq}`; var iptDebut = $("").val(debut).blur(function(e) { debut = $(this).val(); d = parseDate(debut, "yyyy-mm-dd"); if(f").val(fin).blur(function(e) { fin = $(this).val(); f = parseDate(fin, "yyyy-mm-dd"); if(d>f) { debut = `${fin}`; d = parseDate(debut, "yyyy-mm-dd"); iptDebut.val(debut).focus(); } }); var iptRmq = $("").val(rmq).blur(function(e) { rmq = $(this).val(); }); var btnEdit = row.find("button.btnEditAbsence"); var btnDel = row.find("button.btnDeleteAbsence"); btnEdit.click(function(e) { e.preventDefault(); $(this).blur(); if(modalViewClient.find("tr.editable").length>0 && !row.hasClass("editable")) return; // SAVE if(row.hasClass("editable")) { iptDebut.blur(); iptFin.blur(); iptRmq.blur(); datas = { 'client' : currentViewClient, 'debut' : debut, 'fin' : fin, 'remarque' : rmq }; if(ref=='new') datas.action = "add_client_absence"; else { datas.action = "edit_client_absence"; datas.absence_ref = ref; } $.post(clientsBaseURL, datas, function(result) { if(parseInt(result)>0) loadModalViewTabAbsences(); else { console.error(result); alert(result); } }).fail(function() { alert(srvErrorMsg+" (modal view client - save absence)"); }); } // EDIT else { row.addClass("editable"); btnEdit.removeClass('glyphicon-edit').addClass('glyphicon-floppy-saved'); btnDel.removeClass('glyphicon-trash').addClass('glyphicon-floppy-remove'); tdDebut.html('').append(iptDebut); tdFin.html('').append(iptFin); tdRmq.html('').append(iptRmq); iptDebut.focus(); } }); btnDel.click(function(e) { e.preventDefault(); $(this).blur(); if(modalViewClient.find("tr.editable").length>0 && !row.hasClass("editable")) return; // CANCEL if(row.hasClass("editable")) { if(ref == 'new') row.remove(); else { iptDebut.blur(); iptFin.blur(); iptRmq.blur(); tdDebut.html(oldDebut); tdFin.html(oldFin); tdRmq.html(oldRmq); row.removeClass("editable"); btnEdit.removeClass('glyphicon-floppy-saved').addClass('glyphicon-edit'); btnDel.removeClass('glyphicon-floppy-remove').addClass('glyphicon-trash'); } } // DELETE else { if(!ref>0) return; if(confirm("Êtes-vous sur de vouloir supprimer cette absence ?")) { $.post(clientsBaseURL, { 'action': "delete_client_absence", 'absence_ref': ref }, function(result) { if(parseInt(result)>0) loadModalViewTabAbsences(); else { console.error(result); alert(result); } }).fail(function() { alert(srvErrorMsg+" (modal view client - delete absence)"); }); } } }); if(ref == "new") row.find("button.btnEditAbsence").click(); } function loadModalViewTabAbsences() { $.post(clientsBaseURL, { 'ref' : currentViewClient, 'action' : 'modalView_getTab', 'tab' : 'absences' }, function(result) { modalViewClient.find("div.tabAbsences").html(result); initModalViewClientTabAbsences(); }).fail(function() { alert(srvErrorMsg+" (load modal view client - tab absences)"); }); } /***** FORM CLIENT *****/ function initFormClient(form) { form.on("submit", function(e) { e.preventDefault(); }); // NOM initUpperCaseInput(form.find("input[name=nom]"), null, function() { $(this).blur(); }); // PRENOM initFirstUpperCaseInput(form.find("input[name=prenom]"), null, function() { $(this).blur(); }); // TEL initTelInput(form.find("input[name=tel]"), true, "libs/intl-tel-input-15.0.0/build/js/utils.js"); // EMAIL initEmailInput(form.find("input[name=email]")); } function loadDatasInFormClientDatas(modal,form,id) { datas = { 'ref' : id, 'action' : 'getDatas' }; $.post( clientsBaseURL, datas, function( result ) { var datas = JSON.parse(result); form.find("input[name=nom]").val(datas.nom); form.find("input[name=prenom]").val(datas.prenom); form.find("input[name=tel]").val(datas.tel); form.find("input[name=email]").val(datas.email); if(modal) modal.modal('show'); }).fail(function() { alert(srvErrorMsg+" (form client - load datas)"); }); } function clearFormClientDatas(form) { form.find("input[name=nom]").val(""); form.find("input[name=prenom]").val(""); form.find("input[name=tel]").val(""); form.find("input[name=email]").val(""); // MODALOADER form.find("div.modaLoader").removeClass("show"); } // GET FORM DATAS function getClientFormDatas(form) { var datas = { 'nom' : form.find("input[name=nom]").val(), 'prenom' : form.find("input[name=prenom]").val(), 'tel' : form.find("input[name=tel]").intlTelInput("isValidNumber") ? form.find("input[name=tel]").val() : "", 'email' : validateEmail(form.find("input[name=email]").val()) ? form.find("input[name=email]").val() : "", }; return datas; } /***** ADD CLIENT *****/ function initAddClient() { // BTN ADD btnAddClient.unbind('click').click( function(event) { event.preventDefault(); clearFormClientDatas(formAddClient); modalAddClient.modal('show'); }); // INIT FORM ADD initFormClient(formAddClient); // SAVE CLIENT $(".btnSaveAddClient").unbind('click').click( function(event) { event.preventDefault(); var datas = getClientFormDatas(formAddClient); if(datas.nom=="" || datas.prenom=="") { alert("ERREUR : au minimum, un nom et un prénom doivent être renseigné !"); return; } datas.action = 'add'; formAddClient.find("div.modaLoader").addClass("show"); $(".btnSaveAddClient").prop("disabled", true); $.post( clientsBaseURL, datas, function( result ) { if(parseInt(result)>0) { modalAddClient.modal('hide'); setTimeout(function() { document.location.reload(); },200); } else { console.error(result); alert(result); formAddClient.find("div.modaLoader").removeClass("show"); $(".btnSaveAddClient").prop("disabled", false); } }).fail(function() { alert(srvErrorMsg+" (add client)"); formAddClient.find("div.modaLoader").removeClass("show"); $(".btnSaveAddClient").prop("disabled", false); }); }); // CANCEL modalAddClient.on('hidden.bs.modal', function (e) { clearFormClientDatas(formAddClient); }); } /***** EDIT CLIENT *****/ function initEditClient() { // BTN EDIT btnEditClient.unbind('click').click( function(event) { event.preventDefault(); id = parseInt($(this).attr('ref')); if(!id>0) return; currentEditClient = id; clearFormClientDatas(formEditClient); loadDatasInFormClientDatas(modalEditClient,formEditClient,id); }); // INIT FORM EDIT initFormClient(formEditClient); // SAVE CLIENT $(".btnSaveEditClient").unbind('click').click( function(event) { event.preventDefault(); if(!currentEditClient>0) return; var datas = getClientFormDatas(formEditClient); if(datas.nom=="" || datas.prenom=="") { alert("ERREUR : au minimum, un nom et un prénom doivent être renseigné !"); return; } datas.action = 'edit'; datas.ref = currentEditClient; formEditClient.find("div.modaLoader").addClass("show"); $(".btnSaveEditClient").prop("disabled", true); $.post( clientsBaseURL, datas, function( result ) { if(parseInt(result)>0) { modalEditClient.modal('hide'); setTimeout(function() { document.location.reload(); },200); } else { console.error(result); alert(result); formEditClient.find("div.modaLoader").removeClass("show"); $(".btnSaveEditClient").prop("disabled", false); } }).fail(function() { alert(srvErrorMsg+" (edit client)"); formEditClient.find("div.modaLoader").removeClass("show"); $(".btnSaveEditClient").prop("disabled", false); }); }); // CANCEL modalEditClient.on('hidden.bs.modal', function (e) { clearFormClientDatas(formEditClient); currentEditClient = 0; }); } /***** DELETE CLIENT *****/ function initDeleteClient() { // BTN DELETE btnDeleteClient.unbind('click').click( function(event) { event.preventDefault(); id = parseInt($(this).attr('ref')); if(!id>0) return; currentDeleteClient = id; modalDeleteClient.find('b.name').html( $(this).attr('nom') ); modalDeleteClient.modal('show'); }); // DELETE $("#btnDeleteClient").unbind('click').click( function(event) { event.preventDefault(); $(this).blur(); if(!currentDeleteClient>0) return; var datas = { action : 'delete', ref : currentDeleteClient }; $.post( clientsBaseURL, datas, function( result ) { if(parseInt(result)>0) { modalEditClient.modal('hide'); setTimeout(function() { document.location.reload(); },200); } else { console.error(result); alert(result); } }).fail(function() { alert(srvErrorMsg+" (delete client)"); }); }); // CANCEL modalDeleteClient.on('hidden.bs.modal', function (e) { modalDeleteClient.find('b.name').html(""); currentDeleteClient = 0; }); }