// SETTINGS
var lieuxConf = {
    type                          : "lieu",
    baseURL                       : "lieux.php",
    // LIST
    list_selector                 : "#lieuxList",
    // SEARCH
    search_selector               : "#lieuxSearch .searchGrp",
    // VIEW
    view_modalSelector            : "#modalViewLieu",
    view_btnSelector              : ".btnViewLieu",
    // ADD
    add_modalSelector             : "#modalAddLieu",
    add_btnSelector               : "#btnAddLieu",
    // EDIT
    edit_modalSelector            : "#modalEditLieu",
    edit_btnSelector              : ".btnEditLieu",
    // DELETE
    delete_modalSelector          : "#modalDeleteLieu",
    delete_btnSelector            : ".btnDeleteLieu",
}

// MODALS
var modalViewLieu    = false;
var modalAddLieu     = false;
var modalEditLieu    = false;
var modalDeleteLieu  = false;

$(document).ready( function() {
    // LIST LOADER
    initListProgressLoad($(lieuxConf.list_selector), lieuxConf.baseURL, lieuxConf.type+" list");
    
    // SEARCH
    $(lieuxConf.search_selector).initSearchGroup();
    
    // VIEW
    modalViewLieu = $(lieuxConf.view_modalSelector);
    if(modalViewLieu.length>0) {
        initViewLieu();
        // VIEW REQUEST
        var id = getUrlParameter("ref");
        if(id!==false && parseInt(id)>0 && current_page == "lieux") {
            modalViewLieu_clear(modalViewLieu);
            modalViewLieu_loadDatas(modalViewLieu, id);
        }
    }
    
    // ADD
    modalAddLieu = $(lieuxConf.add_modalSelector);
    if(modalAddLieu.length>0) initAddLieu();

    // EDIT
    modalEditLieu = $(lieuxConf.edit_modalSelector);
    if(modalEditLieu.length>0) initEditLieu();

    // DELETE
    modalDeleteLieu = $(lieuxConf.delete_modalSelector);
    if(modalDeleteLieu.length>0) initDeleteLieu();
});

/***** MODAL VIEW *****/
function initViewLieu() {
    // INIT VIEW BTNs
    var initBtnFct = function() { modalForm_initBtnView(
        $(lieuxConf.view_btnSelector),        // BTNs
        modalViewLieu,                        // MODAL
        modalViewLieu_clear,                  // CLEAR FUNCTION
        modalViewLieu_loadDatas               // LOAD DATAS FUNCTION
    )};
    registerListProgressLoadCbkFct( $("tbody.progressLoadList"), initBtnFct );
    initBtnFct();

    // CANCEL
    modalViewLieu.on('hidden.bs.modal', function() { modalViewLieu_clear(modalViewLieu); });
}

function modalViewLieu_loadDatas(modal, id) {
    $.post(lieuxConf.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.modal('show');
    }).fail(function() { alert("("+lieuxConf.type+" - load modal view)"); });
}

function modalViewLieu_clear(modal) {
    modal.removeAttr("view_id");
    
    modal.find("small.db_ref > span").html("");
    
    modal.find("td.nom").html("");
}

/***** MODAL FORM ADD/EDIT *****/
function modalFormLieu_init(modal) {
    modal.find("form").preventFormValidation();
}

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

        modal.find("input[name=nom]").val(datas.nom);

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

function modalFormLieu_clear(modal) {
    modal.removeAttr("edit_id");
    modal.find("div.modaLoader").removeClass("show");
    
    modal.find("input[name=nom]").val("");
}

function modalFormLieu_getDatas(modal) {
    var datas = {
        'nom' : modal.find("input[name=nom]").val()
    };
    return datas;
}

function modalFormLieu_checkDatas(datas) { 
    if(datas.nom=="") { 
        alert("ERREUR : merci de donner un nom à ce lieu !");
        return false;
    }
    return true;
}

/***** ADD *****/
function initAddLieu() {
    // INIT FORM
    modalFormLieu_init(modalAddLieu);

    // INIT ADD BTN
    modalForm_initBtnAdd(
        $(lieuxConf.add_btnSelector),         // BTN ADD
        modalAddLieu,                         // MODAL
        modalFormLieu_clear                   // CLEAR FORM FUNCTION
    );
    
    // INIT SAVE BTN
    modalForm_initBtnSaveAdd(
        modalAddLieu.find(".btnSave"),        // BTN SAVE
        lieuxConf.type,                       // ADD TYPE
        modalAddLieu,                         // MODAL
        modalFormLieu_getDatas,               // GET FORM DATAS FUNCTION
        modalFormLieu_checkDatas,             // CHECK FORM DATAS FUNCTION
        lieuxConf.baseURL                     // SAVE URL
    );

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

/***** EDIT *****/
function initEditLieu() {
    // INIT FORM
    modalFormLieu_init(modalEditLieu);

    // INIT EDIT BTNs
    var initBtnFct = function() { modalForm_initBtnEdit(
        $(lieuxConf.edit_btnSelector),        // BTNs
        modalEditLieu,                        // MODAL
        modalFormLieu_clear,                  // CLEAR FUNCTION
        modalFormLieu_loadDatas               // LOAD DATAS FUNCTION
    )};
    registerListProgressLoadCbkFct( $("tbody.progressLoadList"), initBtnFct );
    initBtnFct();
    
    // INIT SAVE EDIT BTN
    modalForm_initBtnSaveEdit(
        modalEditLieu.find(".btnSave"),       // BTN SAVE
        lieuxConf.type,                       // EDIT TYPE
        modalEditLieu,                        // MODAL
        modalFormLieu_getDatas,               // GET FORM DATAS FUNCTION
        modalFormLieu_checkDatas,             // CHECK FORM DATAS FUNCTION
        lieuxConf.baseURL                     // SAVE URL
    );

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

/***** DELETE *****/
function initDeleteLieu() {
    // INIT DELETE BTNs
    var initBtnFct = function() { modalForm_initBtnDelete(
        $(lieuxConf.delete_btnSelector),      // BTNs
        modalDeleteLieu,                      // MODAL
        modalDeleteLieu_clear,                // CLEAR MODAL FUNCTION
        modalDeleteLieu_loadDatas             // LOAD DATAS FUNCTION
    )};
    registerListProgressLoadCbkFct( $("tbody.progressLoadList"), initBtnFct );
    initBtnFct();

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

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

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

function modalDeleteLieu_clear() {
    modalDeleteLieu.removeAttr("delete_id");
    modalDeleteLieu.find('b.name').html("");
}