/* --- Surcharge objets --- */

String.prototype.capitalize = function() {
    return this[0].toUpperCase() + this.slice(1);
}

/* --- Fonctions utilitaires --- */

function getCookie(key) {
    const split = document.cookie.split(';');
    let cookies = {};
    split.forEach(elt => {
        const val = elt.trim().split('=');
        cookies[val[0]] = val[1];
    });
    if(key !== undefined) {
        return cookies[key];
    }
    return cookies;
}

function pagination(id, pageNum, perPage, total, tabElt, paginationElt, dataPage, tableClass) {
    pageNum = parseInt(pageNum);
    perPage = parseInt(perPage);
    total = parseInt(total);
    tableClass = tableClass || 'striped';
    // Création du tableau
    let tab = `<table class="${tableClass}">`;
    let first = true;
    let keys = [];
    dataPage.forEach(elt => {
        if(first) {
            tab += '<thead><tr>';
            for (const key in elt) {
                keys.push(key);
                tab += `<th>${key.capitalize()}</th>`;
            }
            tab += '</tr></thead><tbody>';
            first = false;
        }
        tab += '<tr>';
        keys.forEach(key => {
            tab += `<td style="width: ${Math.round((1/keys.length)*100)}%;">${elt[key]}</td>`;
        });
        tab += '</tr>';
    });
    tab += '</tbody></table>';
    // Creation de la pagination
    const nbPage = Math.ceil(total / perPage);
    let pagination = `<ul class="pagination" data-active="${pageNum}">`;
    if(pageNum == 1) {
        pagination += '<li class="disabled"><span><i class="material-icons">chevron_left</i></span></li>';
    } else {
        pagination += `<li class="waves-effect"><span class="pagination-${id}-prev" data-page="${pageNum - 1}"><i class="material-icons">chevron_left</i></span></li>`;
    }
    for(let i = 1; i <= nbPage; i++) {
        if(pageNum === i) {
            pagination += `<li class="active"><span>${i}</span></li>`;
        } else {
            pagination += `<li class="waves-effect"><span class="pagination-${id}-number" data-page="${i}">${i}</span></li>`;
        }
    }
    if(pageNum == nbPage) {
        pagination += '<li class="disabled"><span><i class="material-icons">chevron_right</i></span></li>';
    } else {
        pagination += `<li class="waves-effect"><span class="pagination-${id}-next" data-page="${pageNum + 1}"><i class="material-icons">chevron_right</i></span></li>`;
    }
    pagination += '</ul>';
    // Affichage
    tabElt.html(tab);
    paginationElt.html(pagination);
}

/* --- Initialisation modules --- */

$(document).ready(function(){
    
    // Chargement menu mobile
    $('.sidenav').sidenav();
    // Select
    $('select').formSelect();
    // Modal
    M.Modal.init($('.popup'), {
        dismissible: false,
        startingTop: '20%',
        endingTop: '25%',
        onOpenEnd: (modal) => {
            setTimeout(() => { 
                M.Modal.getInstance(modal).close();
            }, 3000);
        }
    });
    // Date picker
    M.Datepicker.init($('.datepicker'), {
        format: 'dd/mm/yyyy',
        firstDay: 1,
        yearRange: [
            new Date(Date.now()).getFullYear() - 100,
            new Date(Date.now()).getFullYear()
        ],
        i18n: {
            cancel: 'Annuler',
            clear: 'Supprimer',
            months: [
                'Janvier',
                'Fevrier',
                'Mars',
                'Avril',
                'Mai',
                'Juin',
                'Juillet',
                'Aout',
                'Septembre',
                'Octobre',
                'Novembre',
                'Decembre'
            ],
            monthsShort: [
                'Janv',
                'Fevr',
                'Mars',
                'Avr',
                'Mai',
                'Juin',
                'Juil',
                'Aout',
                'Sept',
                'Oct',
                'Nov',
                'Dec'
            ],
            weekdays: [
                'Dimanche',
                'Lundi',
                'Mardi',
                'Mercredi',
                'Jeudi',
                'Vendredi',
                'Samedi'       
            ],
            weekdaysShort: [
                'Dim',
                'Lun',
                'Mar',
                'Mer',
                'Jeu',
                'Ven',
                'Sam'
            ],
            weekdaysAbbrev:	['D','L','M','M','J','V','S']
        }
    });

});