var Asso = function () {};

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

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

    Asso.InitTabMember();

    let DocumentPluginContainer = document.getElementById("DocumentPlugin");

    if(DocumentPluginContainer){
           eval(DocumentPluginContainer.innerHTML); 
    }

    Asso.InitTabMemberShip();
    Asso.InitTabBudget();
    Asso.InitTabEquipment();
};

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

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

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

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


/***
 * Ajout d'un plugin
 */
Asso.AddPlugin = function(e){
  
    e.preventDefault();
    
     Dialog.open('', {"title": Dashboard.GetCode("Asso.AddPlugin"),
     "app": "EeApp",
     "class": "DialogEeApp",
     "method": "AddPluginApp",
     "params": "Asso",
     "type" : "right"
     });
 };
 
 /***
  * Remove plugin to the shop 
  * @returns {undefined}
  */
 Asso.RemovePlugin = function(e){
     
     Animation.Confirm(Language.GetCode("Asso.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}
 */
 Asso.RefreshPlugin = function(){
    Dashboard.StartApp("", "Asso", "");
};

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

};

/*
 * Init the Event for tabMember 
 */
Asso.InitTabMember = function(){
    //Member
   Event.AddById("btnAddMember", "click", () => {
       Asso.ShowAddMember("");
   });
   EntityGrid.Initialise('gdMember');
   
};

/**
* Edit Member
* @param {type} MemberId
* @returns {undefined}
*/
Asso.EditMember = function (MemberId) {
   Asso.ShowAddMember(MemberId);
};

/**
* Dialogue add a categorie
*/
Asso.ShowAddMember = function (MemberId) {

   Dialog.open('', {"title": Dashboard.GetCode("Asso.AddMember"),
       "app": "Asso",
       "class": "DialogAdminAsso",
       "method": "AddMember",
       "params": MemberId
   });
};

//Reload the Member grid
Asso.ReloadMember = function(html){
   let MemberContainer = Dom.GetById("gdMember").parentNode;
   MemberContainer.innerHTML = html;
   Asso.InitTabMember();
};

//Delete a Member
Asso.DeleteMember = function(MemberId){
     Animation.Confirm(Language.GetCode("Asso.ConfirmRemoveMember"), () => {

       let data = "Class=Asso&Methode=DeleteMember&App=Asso";
       data += "&MemberId=" + MemberId;

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

           data = JSON.parse(data);

           Asso.ReloadMember(data.data);
       });
   });
};

/*
 * Init the Event for tabMember 
 */
Asso.InitTabMemberShip = function(){
    //MemberShip
   Event.AddById("btnAddMemberShip", "click", () => {
       Asso.ShowAddMemberShip("");
   });
   EntityGrid.Initialise('gdMemberShip');
   
};

/**
* Edit MemberShip
* @param {type} MemberShipId
* @returns {undefined}
*/
Asso.EditMemberShip = function (MemberShipId) {
   Asso.ShowAddMemberShip(MemberShipId);
};

/**
* Dialogue add a categorie
*/
Asso.ShowAddMemberShip = function (MemberShipId) {

   Dialog.open('', {"title": Dashboard.GetCode("Asso.AddMemberShip"),
       "app": "Asso",
       "class": "DialogAdminAsso",
       "method": "AddMemberShip",
       "params": MemberShipId
   });
};

//Reload the MemberShip grid
Asso.ReloadMemberShip = function(html){
   let MemberShipContainer = Dom.GetById("gdMemberShip").parentNode;
   MemberShipContainer.innerHTML = html;
   Asso.InitTabMemberShip();
};

//Delete a MemberShip
Asso.DeleteMemberShip = function(MemberShipId){
     Animation.Confirm(Language.GetCode("Asso.ConfirmRemoveMemberShip"), () => {

       let data = "Class=Asso&Methode=DeleteMemberShip&App=Asso";
       data += "&MemberShipId=" + MemberShipId;

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

           data = JSON.parse(data);

           Asso.ReloadMemberShip(data.data);
       });
   });
};


Asso.InitTabBudget = function(){

    //Receipt
    Event.AddById("btnAddReceipt", "click", () => {
        Asso.ShowAddBudgetReceipt("");
    });
    
    EntityGrid.Initialise('gdReceipt');

    //Expense
    Event.AddById("btnAddExpense", "click", () => {
        Asso.ShowAddBudgetExpense("");
    });
    
    EntityGrid.Initialise('gdExpense');
};

/**
* Dialogue add a receipt
*/
Asso.ShowAddBudgetReceipt = function (budgetReceiptId) {

    Dialog.open('', {"title": Dashboard.GetCode("Asso.AddBudgetReceipt"),
        "app": "Asso",
        "class": "DialogAdminAsso",
        "method": "AddBudgetReceipt",
        "params": budgetReceiptId
    });
 };

 /**
* Edit Receipt
* @param {type} ReceiptId
* @returns {undefined}
*/
Asso.EditReceipt = function (ReceiptId) {
    Asso.ShowAddBudgetReceipt(ReceiptId);
 };
 
 //Reload the Receipt grid
Asso.ReloadReceipt = function(html){
    let ReceiptContainer = Dom.GetById("gdReceipt").parentNode.parentNode;
    ReceiptContainer.innerHTML = html;
    Asso.InitTabBudget();
 };
 
 //Delete a Receipt
 Asso.DeleteReceipt = function(ReceiptId){
      Animation.Confirm(Language.GetCode("Asso.ConfirmRemoveReceipt"), () => {
 
        let data = "Class=Asso&Methode=DeleteReceipt&App=Asso";
        data += "&ReceiptId=" + ReceiptId;
 
        Request.Post("Ajax.php", data).then(data => {
 
            data = JSON.parse(data);
 
            Asso.ReloadReceipt(data.data);
        });
    });
 };
 
/**
* Dialogue add a receipt
*/
Asso.ShowAddBudgetExpense = function (budgetExpenseId) {

    Dialog.open('', {"title": Dashboard.GetCode("Asso.AddBudgetExpense"),
        "app": "Asso",
        "class": "DialogAdminAsso",
        "method": "AddBudgetExpense",
        "params": budgetExpenseId
    });
 };

 /**
* Edit Expense
* @param {type} ExpenseId
* @returns {undefined}
*/
Asso.EditExpense = function (ExpenseId) {
    Asso.ShowAddBudgetExpense(ExpenseId);
 };
 
 //Reload the Expense grid
Asso.ReloadExpense = function(html){
    let ExpenseContainer = Dom.GetById("gdExpense").parentNode.parentNode;
    ExpenseContainer.innerHTML = html;
    Asso.InitTabBudget();
 };
 
 //Delete a Expense
 Asso.DeleteExpense = function(ExpenseId){
      Animation.Confirm(Language.GetCode("Asso.ConfirmRemoveExpense"), () => {
 
        let data = "Class=Asso&Methode=DeleteExpense&App=Asso";
        data += "&ExpenseId=" + ExpenseId;
 
        Request.Post("Ajax.php", data).then(data => {
 
            data = JSON.parse(data);
 
            Asso.ReloadExpense(data.data);
        });
    });
 };

 /*
 * Init the Event for tabEquipment 
 */
Asso.InitTabEquipment = function(){
    //Equipment
   Event.AddById("btnAddEquipment", "click", () => {
       Asso.ShowAddEquipment("");
   });
   EntityGrid.Initialise('gdEquipment');
   
};

/**
* Edit Equipment
* @param {type} EquipmentId
* @returns {undefined}
*/
Asso.EditEquipment = function (EquipmentId) {
   Asso.ShowAddEquipment(EquipmentId);
};

/**
* Dialogue add a categorie
*/
Asso.ShowAddEquipment = function (EquipmentId) {

   Dialog.open('', {"title": Dashboard.GetCode("Asso.AddEquipment"),
       "app": "Asso",
       "class": "DialogAdminAsso",
       "method": "AddEquipment",
       "params": EquipmentId
   });
};

//Reload the Equipment grid
Asso.ReloadEquipment = function(html){
   let EquipmentContainer = Dom.GetById("gdEquipment").parentNode;
   EquipmentContainer.innerHTML = html;
   Asso.InitTabEquipment();
};

//Delete a Equipment
Asso.DeleteEquipment = function(EquipmentId){
     Animation.Confirm(Language.GetCode("Asso.ConfirmRemoveEquipment"), () => {

       let data = "Class=Asso&Methode=DeleteEquipment&App=Asso";
       data += "&EquipmentId=" + EquipmentId;

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

           data = JSON.parse(data);

           Asso.ReloadEquipment(data.data);
       });
   });
};