var Courses = function () {};

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

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

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

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

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

/***
 * Init the Admin Widget
 */
Courses.InitAdminWidget = function () {

};



Courses.InitTabConfig = function(){
    Event.AddById("btnAddPlugin", "click",     Courses.AddPlugin);
    Event.AddByClass("removePlugin", "click" , Courses.RemovePlugin);

    Courses.RegisterPlugin();
};

/***
 * Registert the plugin
 * @returns {undefined}
 */
Courses.RegisterPlugin = function(){
    let pluginScript = Dom.GetById("pluginScript");
    
    if(pluginScript){
        eval(pluginScript.innerHTML);
    }
};

/***
 * Ajout d'un plugin
 */
Courses.AddPlugin = function(e){

    e.preventDefault();
    
        Dialog.open('', {"title": Dashboard.GetCode("Courses.AddPlugin"),
        "app": "EeApp",
        "class": "DialogEeApp",
        "method": "AddPluginApp",
        "params": "Courses",
        "type" : "right"
        });
};

/***
  * Remove plugin to the shop 
  * @returns {undefined}
  */
Courses.RemovePlugin = function(e){
     
    Animation.Confirm(Language.GetCode("Annonce.RemovePlugin"), function(){
        let container = e.srcElement.parentNode;
        
        let  data = "Class=EeApp&Methode=RemovePluginApp&App=EeApp";
             data += "&pluginId="+e.srcElement.id;
             
        Request.Post("Ajax.php", data).then(data => {
            container.parentNode.removeChild(container);
        });
    });
};

/***
 * Refresh 
 * @returns {undefined}
 */
Courses.RefreshPlugin = function(){
    Dashboard.StartApp("", "Courses", "");
};


/**
 * Tab des catgéogrie
 * @returns {undefined}
 */
Courses.InitTabCour = function () {
    //Catégory
    Event.AddById("btnAddCour", "click", () => {
        Courses.ShowAddCour("");
    });
    EntityGrid.Initialise('gdCour');
};

/**
 * Edit Cour
 * @param {type} CourId
 * @returns {undefined}
 */
Courses.EditCour = function (CourId) {
    Courses.ShowAddCour(CourId);
};

/**
 * Dialogue d'ajout de catégorie
 */
Courses.ShowAddCour = function (CourId) {

    Dialog.open('', {"title": Dashboard.GetCode("Courses.AddCategorie"),
        "app": "Courses",
        "class": "DialogAdminCourses",
        "method": "AddCour",
        "params": CourId
    });
};

/***
 * Rafraichit la tab des catégorie
 * @returns {undefined}
 */
Courses.ReloadCour = function (data) {
    let gdCour = Dom.GetById("gdCour");
    gdCour.parentNode.innerHTML = data.data;
    Courses.InitTabCour();
};

/***
 * Supprime un cour
 * @param {type} CourId
 * @returns {undefined}
 */
Courses.DeleteCour = function (CourId) {

    Animation.Confirm(Language.GetCode("Courses.ConfirmRemoveCour"), () => {

        let data = "Class=Courses&Methode=DeleteCour&App=Courses";
        data += "&CourId=" + CourId;

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

            data = JSON.parse(data);

            Courses.ReloadCour(data);
        });
    });
};

