// SETTINGS
var paniersConf = {
    type                          : "panier",
    baseURL                       : "paniers.php",
    // LIST
    list_selector                 : "#paniersList",
    // SEARCH / FILTRES
    search_selector               : "#paniersSearch .searchGrp",
    filtres_selector              : "#paniersSearch select",
    // VIEW
    view_modalSelector            : "#modalViewPanier",
    view_btnSelector              : ".btnViewPanier",
    // ADD
    add_modalSelector             : "#modalAddPanier",
    add_btnSelector               : "#btnAddPanier",
    // EDIT
    edit_modalSelector            : "#modalEditPanier",
    edit_btnSelector              : ".btnEditPanier",
    // DELETE
    delete_modalSelector          : "#modalDeletePanier",
    delete_btnSelector            : ".btnDeletePanier",
}

// MODALS
var modalViewPanier    = false;
var modalAddPanier     = false;
var modalEditPanier    = false;
var modalDeletePanier  = false;

$(document).ready( function() {
    // LIST LOADER
    initListProgressLoad($(paniersConf.list_selector), paniersConf.baseURL, paniersConf.type+" list");
    
    // SEARCH / FILTRES
    $(paniersConf.search_selector).initSearchGroup();
    $(paniersConf.filtres_selector).initFiltreSelect();
    
    // VIEW
    modalViewPanier = $(paniersConf.view_modalSelector);
    if(modalViewPanier.length>0) {
        initViewPanier();
        // VIEW REQUEST
        var id = getUrlParameter("ref");
        if(id!==false && parseInt(id)>0 && current_page == "paniers") {
            modalViewPanier_clear(modalViewPanier);
            modalViewPanier_loadDatas(modalViewPanier, id);
        }
    }
    
    // ADD
    modalAddPanier = $(paniersConf.add_modalSelector);
    if(modalAddPanier.length>0) initAddPanier();

    // EDIT
    modalEditPanier = $(paniersConf.edit_modalSelector);
    if(modalEditPanier.length>0) initEditPanier();

    // DELETE
    modalDeletePanier = $(paniersConf.delete_modalSelector);
    if(modalDeletePanier.length>0) initDeletePanier();
});

/***** MODAL VIEW *****/
function initViewPanier() {
    // INIT VIEW BTNs
    var initBtnFct = function() { modalForm_initBtnView(
        $(paniersConf.view_btnSelector),        // BTNs
        modalViewPanier,                        // MODAL
        modalViewPanier_clear,                  // CLEAR FUNCTION
        modalViewPanier_loadDatas               // LOAD DATAS FUNCTION
    )};
    registerListProgressLoadCbkFct( $("tbody.progressLoadList"), initBtnFct );
    initBtnFct();

    // CANCEL
    modalViewPanier.on('hidden.bs.modal', function() { modalViewPanier_clear(modalViewPanier); });
}

function modalViewPanier_loadDatas(modal, id) {
    $.post(paniersConf.baseURL, { 'ref' : id, 'action' : 'getDatas' }, function(jsonTxt) {
        var datas = JSON.parse(jsonTxt);

        modal.find("small.db_ref > span").html(datas.ref);
        
        modal.find("td.nom").html(datas.nom);
        modal.find("td.groupe").html(datas.groupe_nom);
        modal.find("td.valeur").html(number_format(datas.valeur,2)+" €");
        
        modal.modal('show');
    }).fail(function() { alert("("+paniersConf.type+" - load modal view)"); });
}

function modalViewPanier_clear(modal) {
    modal.removeAttr("view_id");
    
    modal.find("small.db_ref > span").html("");
    
    modal.find("td.nom").html("");
    modal.find("td.groupe").html("");
    modal.find("td.valeur").html("");
    
    modal.find("table.modal-body tfoot").addClass("hide").find("tr:not(.header)").remove();
}

/***** MODAL FORM ADD/EDIT *****/
function modalFormPanier_init(modal) {
    modal.find("form").on("submit", function(e) { e.preventDefault(); });
    
    // NOM
    initFirstUpperCaseInput(modal.find("input[name=nom]"), null, function() { $(this).blur(); });
    modal.find("input[name=nom]").keydown(function(e) {
        if(e.keyCode==13) { e.preventDefault(); $(this).blur(); }
    });
    
    // GROUPE
    inlineSelectManager_init(
        modal,                                          // MODAL
        "panier",                                       // MODAL FORM NAME
        modal.find("select[name=groupe]").parent(),     // SELECT GROUPE
        "le groupe de panier",                          // VALUE NAME
        paniersConf.baseURL,                            // URL
        "groupe_ref",                                   // REF ATTR
        "add_groupe",                                   // ADD ACTION
        "edit_groupe",                                  // EDIT ACTION
        "delete_groupe",                                // DELETE ACTION
        "groupes_select_list"                           // REFRESH LIST ACTION
    );
    
    // VALEUR
    initFloatInput(modal.find("input[name=valeur]"));
    modal.find("input[name=valeur]").keydown(function(e) {
        if(e.keyCode==13) { e.preventDefault(); $(this).blur(); }
    });
}

function modalFormPanier_loadDatas(modal,id) {
    $.post(paniersConf.baseURL, { 'ref' : id, 'action' : 'getDatas' }, function(jsonTxt) {
        var datas = JSON.parse(jsonTxt);

        modal.find("input[name=nom]").val(datas.nom);
        
        inlineSelectManager_setValue(modal.find("select[name=groupe]").parent(), datas.groupe_ref);
        
        modal.find("input[name=valeur]").val(datas.valeur);

        if(modal) modal.modal('show');
    }).fail(function() { alert("("+paniersConf.type+" - load datas in modal form)"); });
}

function modalFormPanier_clear(modal) {
    modal.removeAttr("edit_id");
    modal.find("div.modaLoader").removeClass("show");
    
    modal.find("input[name=nom]").val("");
    inlineSelectManager_setValue(modal.find("select[name=groupe]").parent(), 0);
    modal.find("input[name=valeur]").val("0");
}

function modalFormPanier_getDatas(modal) {
    var datas = {
        'nom' :     modal.find("input[name=nom]").val(),
        'groupe' :  parseInt(modal.find("select[name=groupe]").val()),
        'valeur' :  parseFloat(modal.find("input[name=valeur]").val())
    };
    return datas;
}

function modalFormPanier_checkDatas(datas) { 
    if(datas.nom=="") { 
        alert("ERREUR : au minimum, un nom doit être renseigné !");
        return false;
    }
    return true;
}

/***** ADD *****/
function initAddPanier() {
    // INIT FORM
    modalFormPanier_init(modalAddPanier);

    // INIT ADD BTN
    modalForm_initBtnAdd(
        $(paniersConf.add_btnSelector),         // BTN ADD
        modalAddPanier,                         // MODAL
        modalFormPanier_clear                   // CLEAR FORM FUNCTION
    );
    
    // INIT SAVE BTN
    modalForm_initBtnSaveAdd(
        modalAddPanier.find(".btnSave"),        // BTN SAVE
        paniersConf.type,                       // ADD TYPE
        modalAddPanier,                         // MODAL
        modalFormPanier_getDatas,               // GET FORM DATAS FUNCTION
        modalFormPanier_checkDatas,             // CHECK FORM DATAS FUNCTION
        paniersConf.baseURL                     // SAVE URL
    );

    // CANCEL
    modalAddPanier.on('hidden.bs.modal', function (e) { modalFormPanier_clear(modalAddPanier); });
}

/***** EDIT *****/
function initEditPanier() {
    // INIT FORM
    modalFormPanier_init(modalEditPanier);

    // INIT EDIT BTNs
    var initBtnFct = function() { modalForm_initBtnEdit(
        $(paniersConf.edit_btnSelector),        // BTNs
        modalEditPanier,                        // MODAL
        modalFormPanier_clear,                  // CLEAR FUNCTION
        modalFormPanier_loadDatas               // LOAD DATAS FUNCTION
    )};
    registerListProgressLoadCbkFct( $("tbody.progressLoadList"), initBtnFct );
    initBtnFct();
    
    // INIT SAVE EDIT BTN
    modalForm_initBtnSaveEdit(
        modalEditPanier.find(".btnSave"),       // BTN SAVE
        paniersConf.type,                       // EDIT TYPE
        modalEditPanier,                        // MODAL
        modalFormPanier_getDatas,               // GET FORM DATAS FUNCTION
        modalFormPanier_checkDatas,             // CHECK FORM DATAS FUNCTION
        paniersConf.baseURL                     // SAVE URL
    );

    // CANCEL
    modalEditPanier.on('hidden.bs.modal', function (e) { modalFormPanier_clear(modalEditPanier); });
}

/***** DELETE *****/
function initDeletePanier() {
    // INIT DELETE BTNs
    var initBtnFct = function() { modalForm_initBtnDelete(
        $(paniersConf.delete_btnSelector),      // BTNs
        modalDeletePanier,                      // MODAL
        modalDeletePanier_clear,                // CLEAR MODAL FUNCTION
        modalDeletePanier_loadDatas             // LOAD DATAS FUNCTION
    )};
    registerListProgressLoadCbkFct( $("tbody.progressLoadList"), initBtnFct );
    initBtnFct();

    // INIT SAVE DELETE BTN
    modalForm_initBtnSaveDelete(
        modalDeletePanier.find(".btnSave"),     // BTN SAVE
        paniersConf.type,                       // DELETE TYPE
        modalDeletePanier,                      // MODAL
        false,                                  // GET FORM DATAS FUNCTION
        false,                                  // CHECK FORM DATAS FUNCTION
        paniersConf.baseURL                     // SAVE URL
    );

    // CANCEL
    modalDeletePanier.on('hidden.bs.modal', function (e) { modalDeletePanier_clear(); });
}

function modalDeletePanier_loadDatas(btn, id) {
    modalDeletePanier.find('b.name').html( btn.attr('nom') );
    modalDeletePanier.modal('show');
}

function modalDeletePanier_clear() {
    modalDeletePanier.removeAttr("delete_id");
    modalDeletePanier.find('b.name').html("");
}