var Crm = function () {};

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

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

    Crm.InitTabConfig();
    Crm.InitTabProspect();

    Event.AddById("btnAddPlugin", "click", Crm.AddPlugin);
    Event.AddByClass("removePlugin", "click" , Crm.RemovePlugin);
};

/***
 * Ajout d'un plugin
 */
Crm.AddPlugin = function(e){
  
    e.preventDefault();
    
     Dialog.open('', {"title": Dashboard.GetCode("Crm.AddPlugin"),
     "app": "EeApp",
     "class": "DialogEeApp",
     "method": "AddPluginApp",
     "params": "Crm",
     });
 };
 
 /***
  * Remove plugin to the shop 
  * @returns {undefined}
  */
 Crm.RemovePlugin = function(e){
     
     Animation.Confirm(Language.GetCode("Crm.ConfirmRemovePlugin"), 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);
         });
     });
 };
 
/**
 * Init config tab
 */
Crm.InitTabConfig = function(){
    
    //Prospect type
    Event.AddById("btnAddProspectType", "click", () => {
        Crm.ShowAddProspectType("");
    });
    EntityGrid.Initialise('gdProspectType');

    Event.AddById("btnAddBusinessType", "click", () => {
        Crm.ShowAddBusinessType("");
    });
    EntityGrid.Initialise('gdBusinessType');

    Event.AddById("btnAddBusinessStatut", "click", () => {
        Crm.ShowAddBusinessStatut("");
    });
    EntityGrid.Initialise('gdBusinessStatut');
};


/**
 * Tab des catgéogrie
 * @returns {undefined}
 */
Crm.InitTabProspect = function () {
    //Prospect
    Event.AddById("btnAddProspect", "click", () => {
        Crm.ShowAddProspect("")
    });
    EntityGrid.Initialise('gdProspect');
};

/**
 * Edit Prospect
 * @param {type} ProspectId
 * @returns {undefined}
 */
Crm.EditProspect = function (ProspectId) {
    Crm.ShowAddProspect(ProspectId);
};

/***
 * Add Propect Type
 */
Crm.ShowAddProspectType= function (ProspectId) {

    Animation.Ask("Label", function(name){

        if(name != ""){
            let Request = new Http("Crm", "SaveProspectType");
                Request.Add("Name", name);

                Request.Post(function(data){
                    data = JSON.parse(data); 
                    Crm.ReloadConfig(data);
                });
        }    
    });
};

/***
 * Suppprimer un type de prospect
 */
Crm.DeleteProspectType = function(typeId){

    Animation.Confirm(Language.GetCode("Crm.ConfirmDelete"), function(){
        let Request = new Http("Crm", "DeleteProspectType");
        Request.Add("TypeId", typeId);

        Request.Post(function(data){
            data = JSON.parse(data); 
            Crm.ReloadConfig(data);
        });
      }
    );
};

/***
 * Save business type
 */
Crm.ShowAddBusinessType= function (ProspectId) {

    Animation.Ask("Label", function(name){

        if(name != ""){
            let Request = new Http("Crm", "SaveBusinessType");
                Request.Add("Name", name);

                Request.Post(function(data){
                    data = JSON.parse(data); 
                    Crm.ReloadConfig(data);
                });
        }    
    });
};

/***
 * Suppprimer un type de businness
 */
Crm.DeleteBusinessType = function(typeId){

    Animation.Confirm(Language.GetCode("Crm.ConfirmDelete"), function(){
        let Request = new Http("Crm", "DeleteBusinessType");
        Request.Add("TypeId", typeId);

        Request.Post(function(data){
            data = JSON.parse(data); 
            Crm.ReloadConfig(data);
        });
      }
    );
};

/***
 * Save business status
 */
Crm.ShowAddBusinessStatut= function (ProspectId) {

    Animation.Ask("Label", function(name){

        if(name != ""){
            let Request = new Http("Crm", "SaveBusinessStatut");
                Request.Add("Name", name);

                Request.Post(function(data){
                    data = JSON.parse(data); 
                    Crm.ReloadConfig(data);
                });
        }    
    });
};

/***
 * Suppprimer un type de businness
 */
Crm.DeleteBusinessStatut = function(typeId){

    Animation.Confirm(Language.GetCode("Crm.ConfirmDelete"), function(){
        let Request = new Http("Crm", "DeleteBusinessStatut");
        Request.Add("TypeId", typeId);

        Request.Post(function(data){
            data = JSON.parse(data); 
            Crm.ReloadConfig(data);
        });
      }
    );
};

/***
 * Rafraichit la tab de configuration
 * @returns {undefined}
 */
Crm.ReloadConfig = function (data) {
    let gdProspect = Dom.GetById("gdProspectType");
    gdProspectType.parentNode.parentNode.parentNode.innerHTML = data.data;
    Crm.InitTabConfig();
};

/**
 * Dialogue d'ajout de prospect
 */
Crm.ShowAddProspect = function (ProspectId) {

    Dialog.open('', {"title": Dashboard.GetCode("Crm.AddProspect"),
        "app": "Crm",
        "class": "DialogAdminCrm",
        "method": "AddProspect",
        "params": ProspectId
    });
};

/***
 * Rafraichit la tab des catégorie
 * @returns {undefined}
 */
Crm.ReloadProspect = function (data) {
    let gdProspect = Dom.GetById("gdProspect");
    gdProspect.parentNode.innerHTML = data.data;
    Crm.InitTabProspect();
};

/***
 * Supprime une catégorie
 * @param {type} ProspectId
 * @returns {undefined}
 */
Crm.DeleteProspect = function (ProspectId) {

    Animation.Confirm(Language.GetCode("Crm.ConfirmRemoveProspect"), () => {

        let data = "Class=Crm&Methode=DeleteProspect&App=Crm";
        data += "&ProspectId=" + ProspectId;

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

            data = JSON.parse(data);

            Crm.ReloadProspect(data);
        });
    });
};


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

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

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

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

};