var Booking = function () {};

/*
 * Add Event when the App loaded
 */
Booking.Load = function (parameter)
{
    this.LoadEvent();
};

/*
 * Add event
 */
Booking.LoadEvent = function ()
{
    Dashboard.AddEventAppMenu(Booking.Execute, "", "Booking");
    Dashboard.AddEventWindowsTool("Booking");

    Booking.InitTabCaracteristique();
    Booking.InitTabReservation();
    Booking.InitTabOpeningTime();
};

/***
 * Start the Event for Member 
 */
Booking.LoadMember = function () {
};

/*
 * Execute a function
 */
Booking.Execute = function (e)
{
    //Call the function
    Dashboard.Execute(this, e, "Booking");
    return false;
};

/***
 * Init the app front page
 */
Booking.Init = function (app, method) {
    if (app == "Booking") {
        switch (method) {
            default :
                break;
        }
    }
};

/***
 * Init the Admin Widget
 */
Booking.InitAdminWidget = function () {
    Event.AddByClass("editReservation","click", function(e){
        Booking.EditReservation(e.srcElement.id);
    });
};

/**
 * Tab des caracteristique
 * @returns {undefined}
 */
Booking.InitTabCaracteristique = function () {
    //Catégory
    Event.AddById("btnAddCaracteristique", "click", () => {
        Booking.ShowAddCaracteristique("")
    });
    EntityGrid.Initialise('gdCaracteristique');
};

/**
 * Dialogue d'ajout de caractéristique
 */
Booking.ShowAddCaracteristique = function (caracteristiqueId) {

    Dialog.open('', {"title": Language.GetCode("Booking.AddCaracteristique"),
        "app": "Booking",
        "class": "DialogAdminBooking",
        "method": "AddCaracteristique",
        "type": "left",
        "params": caracteristiqueId
    });
};

/****
 * Edite une caracteristique
 * @param {type} caracteristiqueId
 * @returns {undefined}
 */
Booking.EditCaracteristique = function (caracteristiqueId) {
    Booking.ShowAddCaracteristique(caracteristiqueId);
};


/***
 * Supprime une caractéristique
 * @param {type} categoryId
 * @returns {undefined}
 */
Booking.DeleteCaracteristique = function (caracteristiqueId) {

    Animation.Confirm(Language.GetCode("Booking.ConfirmRemoveCaracteristique"), () => {

        let data = "Class=Booking&Methode=DeleteCaracteristique&App=Booking";
        data += "&caracteristiqueId=" + caracteristiqueId;

        Request.Post("Ajax.php", data).then(data => {

            data = JSON.parse(data);

            Booking.ReloadCaracteristique(data);
        });
    });
};


/***
 * Rafraichit la tab des caractéristiques
 * @returns {undefined}
 */
Booking.ReloadCaracteristique = function (data) {
    let gdCaracteristique = Dom.GetById("gdCaracteristique");
    gdCaracteristique.parentNode.innerHTML = data.data;
    Booking.InitTabCaracteristique();
};

/**
 * Init the reservation tab
 */
Booking.InitTabReservation = function(){
    EntityGrid.Initialise('gdReservation');
};

/***
 * Edite une reservation 
 */
Booking.EditReservation = function(id){

    Dialog.open('', {"title": Language.GetCode("Booking.EditReservation"),
    "app": "Booking",
    "class": "DialogAdminBooking",
    "method": "EditReservation",
    "type": "left",
    "params": id
    });
};


Booking.InitTabOpeningTime = function(){

    Event.AddById("btnSaveOpeningTime", "click", function(){

        let lines = document.querySelectorAll("#gdOpeningTime tr");
        let openDays = [];

        for(let i = 1; i < lines.length; i++){

            let data = lines[i].getElementsByTagName("input");

            openDays.push({day : data[0].value,   
                hourStartAm : data[1].value,   
                hourEndAm : data[2].value,   
                hourStartPm : data[3].value,   
                hourEndPm : data[4].value,   
            });
        }

        console.log(openDays);
        console.log("Save Opening Time");

        let request = new Http("Booking", "SaveOpeningTime");
        request.Add("time" , JSON.stringify(openDays));

        request.Post((data)=>{
            Animation.Notify("Booking.OpeningTimeSaved");
        });
    });
};
