var Immo = function () {};

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

    Immo.InitTabCity();
    Immo.InitTabProject();
};

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

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

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

/*
 * Init the Event for tabCity 
 */
Immo.InitTabCity = function(){
    //Catégory
   Event.AddById("btnAddCity", "click", () => {
       Immo.ShowAddCity("");
   });
   EntityGrid.Initialise('gdCity');
   
};

/**
* Edit category
* @param {type} categoryId
* @returns {undefined}
*/
Immo.EditCity = function (categoryId) {
   Immo.ShowAddCity(categoryId);
};

/**
* Dialogue add a categorie
*/
Immo.ShowAddCity = function (categoryId) {

   Dialog.open('', {"title": Dashboard.GetCode("Immo.AddCity"),
       "app": "Immo",
       "class": "DialogAdminImmo",
       "method": "AddCity",
       "params": categoryId
   });
};

//Reload the category grid
Immo.ReloadCity = function(html){
   let categoryContainer = Dom.GetById("gdCity").parentNode;
   categoryContainer.innerHTML = html;
   Immo.InitTabCity();
};

//Delete a category
Immo.DeleteCity = function(categoryId){
     Animation.Confirm(Language.GetCode("Immo.ConfirmRemoveCategorie"), () => {

       let data = "Class=Immo&Methode=DeleteCity&App=Immo";
       data += "&categoryId=" + categoryId;

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

           data = JSON.parse(data);

           Immo.ReloadCity(data.data);
       });
   });
};

/*
 * Init the Event for tabProject 
 */
Immo.InitTabProject = function(){
    //Catégory
   Event.AddById("btnAddProject", "click", () => {
       Immo.ShowAddProject("");
   });
   EntityGrid.Initialise('gdProject');

   Event.AddByClass("editEvent", "click", Immo.EditEvent);
   Event.AddByClass("removeEvent", "click", Immo.RemoveEvent);
};

/***
 * Delete a evetn to a projet
 */
Immo.RemoveEvent = function(e){

    Animation.Confirm(Language.GetCode("Immo.ConfirmRemoveEvent"), ()=>{
        let element = e.srcElement;
        let container = e.srcElement.parentNode.parentNode;
        let eventId = element.parentNode.id;

        let request = new Http("Immo", "RemoveEventProject");
        request.Add("EventId", eventId);

        request.Post(data => {
            container.parentNode.removeChild(container);
        });
    });
};

Immo.EditEvent = function(e){

    let element = e.srcElement;
    let eventId = element.parentNode.id;

    Dialog.open('', {"title": Dashboard.GetCode("Immo.AddEventProject"),
        "app": "Immo",
        "class": "DialogAdminImmo",
        "method": "EditEventProject",
        "params": eventId
    });
};

/***
 * Ajout d'un évenement sur un projet
 */
Immo.AddEvent = function(projectId){

    Dialog.open('', {"title": Dashboard.GetCode("Immo.AddEventProject"),
        "app": "Immo",
        "class": "DialogAdminImmo",
        "method": "AddEventProject",
        "params": projectId
    });
};

/**
* Edit project
* @param {type} categoryId
* @returns {undefined}
*/
Immo.EditProject = function (projectId) {
   Immo.ShowAddProject(projectId);
};

/**
* Dialogue add a categorie
*/
Immo.ShowAddProject = function (projectId) {

   Dialog.open('', {"title": Dashboard.GetCode("Immo.AddProject"),
       "app": "Immo",
       "class": "DialogAdminImmo",
       "method": "AddProject",
       "params": projectId
   });
};

//Reload the category grid
Immo.ReloadProject = function(html){
   let categoryContainer = Dom.GetById("gdProject").parentNode;
   categoryContainer.innerHTML = html;
   Immo.InitTabProject();
};


/***
 * Init the app front page
 */
Immo.Init = function (app, method) {
    if (app == "estimation") {
        ImmoEstimateForm.Init();
    }
};

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

};