// SEARCH
var legumesSearch = false;
// VIEW
var modalViewLegume = false;
var btnViewLegume = false;
var currentViewLegume = 0;
var legumesBaseURL = "legumes.php";
// ADD
var modalAddLegume = false;
var formAddLegume = false;
var btnAddLegume = false;
// EDIT
var modalEditLegume = false;
var formEditLegume = false;
var btnEditLegume = false;
var currentEditLegume = 0;
// DELETE
var modalDeleteLegume = false;
var btnDeleteLegume = false;
var currentDeleteLegume = 0;
$(document).ready( function() {
// INIT SEARCH
legumesSearch = $("#legumesSearch");
if(legumesSearch.length>0) initSearchLegumes();
// INIT VIEW LEGUME
modalViewLegume = $("#modalViewLegume");
btnViewLegume = $(".btnViewLegume");
if(modalViewLegume.length>0 && btnViewLegume.length>0) {
initViewLegume();
// VIEW REQUEST
var id = getUrlParameter("id");
if(id!==false && parseInt(id)>0) loadDatasInViewLegumeModal(id);
}
// INIT ADD LEGUME
modalAddLegume = $("#modalAddLegume");
formAddLegume = $("#formAddLegume");
btnAddLegume = $("#btnAddLegume");
if(modalAddLegume.length>0 && formAddLegume.length>0 && btnAddLegume.length>0) initAddLegume();
// INIT EDIT LEGUME
modalEditLegume = $("#modalEditLegume");
formEditLegume = $("#formEditLegume");
btnEditLegume = $(".btnEditLegume");
if(modalEditLegume.length>0 && formEditLegume.length>0 && btnEditLegume.length>0) initEditLegume();
// INIT DELETE LEGUME
modalDeleteLegume = $("#modalDeleteLegume");
btnDeleteLegume = $(".btnDeleteLegume");
if(modalDeleteLegume.length>0 && btnDeleteLegume.length>0) initDeleteLegume();
});
/***** SEARCH LEGUMES *****/
function initSearchLegumes() {
legumesSearch.find("input[name=search]").unbind('keypress').keypress( function(event) {
if(event.keyCode==13) searchLegume(false);
});
legumesSearch.find(".btnClearSearch").unbind('click').click( function(event) {
searchLegume(true);
});
}
function searchLegume(clear) {
legumesSearch.find("input[name=search]").blur();
legumesSearch.find(".btnClearSearch").blur();
if(clear) document.location = "?clearSearch";
else document.location = "?search="+legumesSearch.find("input[name=search]").val();
}
/***** VIEW LEGUME *****/
function initViewLegume() {
// BTN VIEW
btnViewLegume.unbind('click').click( function(event) {
event.preventDefault();
id = parseInt($(this).attr('ref'));
if(!id>0) return;
clearViewLegumeModal();
currentViewLegume = id;
loadDatasInViewLegumeModal(id);
});
// CANCEL
modalViewLegume.on('hidden.bs.modal', function (e) {
clearViewLegumeModal();
currentViewLegume = 0;
});
}
function loadDatasInViewLegumeModal(id) {
datas = {
'ref' : id,
'action' : 'getDatas'
};
$.post( legumesBaseURL, datas, function( result ) {
var datas = JSON.parse(result);
modalViewLegume.find("small.db_ref > span").html(datas.ref);
modalViewLegume.find("td.nom").html(datas.nom);
modalViewLegume.find("td.tarif").html(datas.tarif_print+" (depuis le "+datas.tarif_date_print+")");
if(datas.historique_tarif.length>0) {
modalViewLegume.find("table.modal-body tfoot").removeClass("hide");
$.each(datas.historique_tarif, function(n,e) { addTarifLegumeHist(e); });
}
modalViewLegume.modal('show');
}).fail(function() { alert(srvErrorMsg+" (load modal view legume)"); });
}
function addTarifLegumeHist(datas) {
var tr = $("
").attr("ref", datas.ref);
tr.append($(" | ").html(datas.tarif_print));
tr.append($(" | ").html(datas.tarif_periode));
btnDel = $('').attr('ref', datas.ref).attr('tarif', datas.tarif_print).attr('periode', datas.tarif_periode);
tr.append($(" | ").append(btnDel));
modalViewLegume.find("table.modal-body tfoot").append(tr);
btnDel.click(function(e) {
tr = $(this).parent().parent();
e.preventDefault();
$(this).blur();
id = parseInt( $(this).attr("ref") );
if(!id>0) return;
msg = "Êtes vous sûr de vouloir supprimer le tarif de "+$(this).attr("tarif")+" en application "+$(this).attr("periode")+" ?";
if(confirm(msg)) {
datas = {
'action' : 'deleteTarif',
'tarif' : id
};
$.post( legumesBaseURL, datas, function( result ) {
if(parseInt(result)>0) {
tr.remove();
if(modalViewLegume.find("table.modal-body tfoot tr:not(.header)").length == 0) modalViewLegume.find("table.modal-body tfoot").addClass("hide");
}
else {
console.error(result);
alert(result);
}
}).fail(function() { alert(srvErrorMsg+" (form legume - delete tarif)"); });
}
})
}
function clearViewLegumeModal() {
modalViewLegume.find("small.db_ref > span").html("");
modalViewLegume.find("td.nom").html("");
modalViewLegume.find("td.tarif").html("");
modalViewLegume.find("table.modal-body tfoot").addClass("hide").find("tr:not(.header)").remove();
}
/***** FORM LEGUME *****/
function initFormLegume(form) {
form.on("submit", function(e) { e.preventDefault(); });
form.find("button.btnEditTarif").click(function(e) {
e.preventDefault();
$(this).blur().addClass('hide');
form.find("input[name=prix]").attr('ref', 'new').prop('disabled', false);
form.find("select[name=unite]").prop('disabled', false);
form.find("input[name=date]").prop('disabled', false).val(getTodayDate("yyyy-mm-dd"));
});
initFloatInput(form.find("input[name=prix]"));
}
function loadDatasInFormLegumeDatas(modal,form,id) {
datas = {
'ref' : id,
'action' : 'getDatas'
};
$.post( legumesBaseURL, datas, function( result ) {
var datas = JSON.parse(result);
form.find("input[name=nom]").val(datas.nom);
form.find("input[name=prix]").val(datas.tarif_prix).attr('ref', datas.tarif_ref);
form.find("select[name=unite]").val(datas.tarif_unite);
form.find("input[name=date]").val(datas.tarif_date);
if(modal) modal.modal('show');
}).fail(function() { alert(srvErrorMsg+" (form legume - load datas)"); });
}
function clearFormLegumeDatas(form) {
add = (form.attr('id') == "formAddLegume");
form.find("input[name=nom]").val("");
if(add) form.find("button.btnEditTarif").addClass('hide');
else form.find("button.btnEditTarif").removeClass('hide');
form.find("input[name=prix]").val("").attr('ref', "new").prop('disabled', !add);
form.find("select[name=unite]").val("kg").prop('disabled', !add);
form.find("input[name=date]").val(getTodayDate("yyyy-mm-dd")).prop('disabled', !add);
// MODALOADER
form.find("div.modaLoader").removeClass("show");
}
// GET FORM DATAS
function getLegumeFormDatas(form) {
var datas = {
'nom' : form.find("input[name=nom]").val(),
'tarif_ref' : (form.find("input[name=prix]").attr('ref')=="new") ? "new" : parseInt(form.find("input[name=prix]").attr('ref')),
'tarif_prix' : parseFloat( form.find("input[name=prix]").val() ),
'tarif_unite' : form.find("select[name=unite]").val(),
'tarif_date' : form.find("input[name=date]").val()
};
return datas;
}
/***** ADD LEGUME *****/
function initAddLegume() {
// BTN ADD
btnAddLegume.unbind('click').click( function(event) {
event.preventDefault();
clearFormLegumeDatas(formAddLegume);
modalAddLegume.modal('show');
});
// INIT FORM ADD
initFormLegume(formAddLegume);
// SAVE LEGUME
$(".btnSaveAddLegume").unbind('click').click( function(event) {
event.preventDefault();
var datas = getLegumeFormDatas(formAddLegume);
if(datas.nom=="") {
alert("ERREUR : au minimum un nom doit être renseigné !");
return;
}
datas.action = 'add';
formAddLegume.find("div.modaLoader").addClass("show");
$(".btnSaveAddLegume").prop("disabled", true);
$.post( legumesBaseURL, datas, function( result ) {
if(parseInt(result)>0) {
modalAddLegume.modal('hide');
setTimeout(function() {
document.location.reload();
},200);
}
else {
console.error(result);
alert(result);
formAddLegume.find("div.modaLoader").removeClass("show");
$(".btnSaveAddLegume").prop("disabled", false);
}
}).fail(function() {
alert(srvErrorMsg+" (add legume)");
formAddLegume.find("div.modaLoader").removeClass("show");
$(".btnSaveAddLegume").prop("disabled", false);
});
});
// CANCEL
modalAddLegume.on('hidden.bs.modal', function (e) { clearFormLegumeDatas(formAddLegume); });
}
/***** EDIT LEGUME *****/
function initEditLegume() {
// BTN EDIT
btnEditLegume.unbind('click').click( function(event) {
event.preventDefault();
id = parseInt($(this).attr('ref'));
if(!id>0) return;
currentEditLegume = id;
clearFormLegumeDatas(formEditLegume);
loadDatasInFormLegumeDatas(modalEditLegume,formEditLegume,id);
});
// INIT FORM EDIT
initFormLegume(formEditLegume);
// SAVE LEGUME
$(".btnSaveEditLegume").unbind('click').click( function(event) {
event.preventDefault();
if(!currentEditLegume>0) return;
var datas = getLegumeFormDatas(formEditLegume);
if(datas.nom=="") {
alert("ERREUR : au minimum un nom doit être renseigné !");
return;
}
datas.action = 'edit';
datas.ref = currentEditLegume;
formEditLegume.find("div.modaLoader").addClass("show");
$(".btnSaveEditLegume").prop("disabled", true);
$.post( legumesBaseURL, datas, function( result ) {
if(parseInt(result)>0) {
modalEditLegume.modal('hide');
setTimeout(function() {
document.location.reload();
},200);
}
else {
console.error(result);
alert(result);
formEditLegume.find("div.modaLoader").removeClass("show");
$(".btnSaveEditLegume").prop("disabled", false);
}
}).fail(function() {
alert(srvErrorMsg+" (edit legume)");
formEditLegume.find("div.modaLoader").removeClass("show");
$(".btnSaveEditLegume").prop("disabled", false);
});
});
// CANCEL
modalEditLegume.on('hidden.bs.modal', function (e) {
clearFormLegumeDatas(formEditLegume);
currentEditLegume = 0;
});
}
/***** DELETE LEGUME *****/
function initDeleteLegume() {
// BTN DELETE
btnDeleteLegume.unbind('click').click( function(event) {
event.preventDefault();
id = parseInt($(this).attr('ref'));
if(!id>0) return;
currentDeleteLegume = id;
modalDeleteLegume.find('b.name').html( $(this).attr('nom') );
modalDeleteLegume.modal('show');
});
// DELETE
$("#btnDeleteLegume").unbind('click').click( function(event) {
event.preventDefault();
$(this).blur();
if(!currentDeleteLegume>0) return;
var datas = {
action : 'delete',
ref : currentDeleteLegume
};
$.post( legumesBaseURL, datas, function( result ) {
if(parseInt(result)>0) {
modalEditLegume.modal('hide');
setTimeout(function() {
document.location.reload();
},200);
}
else {
console.error(result);
alert(result);
}
}).fail(function() { alert(srvErrorMsg+" (delete legume)"); });
});
// CANCEL
modalDeleteLegume.on('hidden.bs.modal', function (e) {
modalDeleteLegume.find('b.name').html("");
currentDeleteLegume = 0;
});
}