<?php

/*
 * PuzzleApp
 * Webemyos
 * Jérôme Oliva
 * GNU Licence
 */

namespace Apps\Asso\Module\Admin;

use Core\Control\Button\Button;
use Core\Controller\AdministratorController;
use Core\View\View;
use Core\View\ElementView;
use Core\Control\TabStrip\TabStrip;
use Core\Control\EntityGrid\EntityGrid;
use Core\Control\EntityGrid\EntityColumn;
use Core\Control\EntityGrid\EntityIconColumn;
use Core\Control\EntityGrid\EntityFunctionColumn;
use Core\Utility\Date\Date;

use Apps\Asso\Widget\AssoMemberForm\AssoMemberForm;
use Apps\Asso\Entity\AssoMember;
use Apps\Asso\Entity\AssoMemberShip;
use Apps\Asso\Widget\AssoMemberShipForm\AssoMemberShipForm; 

use Apps\Asso\Entity\AssoReceipt;
use Apps\Asso\Widget\AssoBudgetReceiptForm\AssoBudgetReceiptForm; 

use Apps\Asso\Entity\AssoExpense;
use Apps\Asso\Widget\AssoBudgetExpenseForm\AssoBudgetExpenseForm; 

use Apps\Asso\Entity\AssoEquipment;
use Apps\Asso\Widget\AssoEquipmentForm\AssoEquipmentForm; 


use Apps\Asso\Helper\AssoHelper;

/*
 * 
 */
 class AdminController extends AdministratorController
 {
    /**
     * Constructeur
     */
    function _construct($core="")
    {
          $this->Core = $core;
    }

    /**
     * Create
     */
    function Create()
    {
    }

    /**
     * Init
     */
    function Init()
    {
    }

    /**
     * Render
     */
    function Show($all=true)
    {
         return $this->Index();
    }
    
    /*
    * Get the home page
    */
   function Index()
   {
       $view = new View(__DIR__."/View/index.tpl", $this->Core);

       $tabAsso = new TabStrip("tabAsso", "Asso"); 
       $tabAsso->AddTab($this->Core->GetCode("Asso.Configuration"), $this->GetTabConfig());
   
       $tabAsso->AddTab($this->Core->GetCode("Asso.Membres"), $this->GetTabMember());
       
       //$tabAsso->AddTab($this->Core->GetCode("Asso.Documents"), $this->GetTabMember());
       $documentplugin = \Apps\EeApp\Helper\AppHelper::GetPluginType($this->Core, "Asso" , "Document");
        
       if($documentplugin != null){
           $tabAsso->AddTab($this->Core->GetCode("Asso.Document"), $documentplugin->RenderAdmin("Asso"));
       }

       $tabAsso->AddTab($this->Core->GetCode("Asso.MemberShip"), $this->GetTabMemberShip());
       //Entrée et sortie chaque nouvelle asgésio crée une entrée
       $tabAsso->AddTab($this->Core->GetCode("Asso.Budget"), $this->GetTabBudget());
       //Le material avec la gestion de a=la localisation et des prêts
       // 
       $tabAsso->AddTab($this->Core->GetCode("Asso.Equipement"), $this->GetTabEquipment());
  
//expense receipt => Depenses et recette
       //   $tabAsso->AddTab($this->Core->GetCode("Asso.Reunion"), $this->GetTabMember());
    //   $tabAsso->AddTab($this->Core->GetCode("Asso.Event"), $this->GetTabMember());
    //   $tabAsso->AddTab($this->Core->GetCode("Asso.Cours"), $this->GetTabMember());
    //   $tabAsso->AddTab($this->Core->GetCode("Asso.Matchs"), $this->GetTabMember());
    //   $tabAsso->AddTab($this->Core->GetCode("Asso.Annonces"), $this->GetTabMember());
   
       $view->AddElement(new ElementView("tabAsso", $tabAsso->Render()));

       return $view->Render();
   }

   /*
    * Configuration
    */
    function GetTabConfig() {
    
        $view = new View(__DIR__ . "/View/config.tpl", $this->Core);

        $plugins = \Apps\EeApp\Helper\AppHelper::GetPluginApp($this->Core, "Asso");
        
        $view->AddElement(new ElementView("Plugins", $plugins));

        return $view->Render();
    }

    /***
     * Member 
     */
    function GetTabMember(){
      $gdMember = new EntityGrid("gdMember", $this->Core);
      $gdMember->Entity = "Apps\Asso\Entity\AssoMember";
      $gdMember->App = "Asso";
      $gdMember->Action = "GetTabMember";
      
      $gdMember->AddOrder("DateCreated desc");

      $btnAdd = new Button(BUTTON, "btnAddMember");
      $btnAdd->Value = $this->Core->GetCode("Asso.AddMember");

      $gdMember->AddButton($btnAdd);
      $gdMember->AddColumn(new EntityColumn("Name", "Name"));
      $gdMember->AddColumn(new EntityColumn("FirstName", "FirstName"));
      $gdMember->AddColumn(new EntityColumn("DateCreated", "DateCreated"));
      $gdMember->AddColumn(new EntityColumn("Phone", "Phone"));
      
      
      $gdMember->AddColumn(new EntityIconColumn("Action", 
                                                array(array("EditIcone", "Asso.EditMember", "Asso.EditMember"),
                                                array("DeleteIcone", "Asso.DeleteMember", "Asso.DeleteMember")
                                                )    
                        ));

      return $gdMember->Render();
   }

   /**
     * Save a member
     */
    function SaveMember($core, $data) {

      $memberForm = new AssoMemberForm($this->Core, $data);

      if ($memberForm->Validate($data)) {

          $member = new AssoMember($this->Core);

          if ($data["Id"]) {
              $member->GetById($data["Id"]);
          } else {
            $member->DateCreated->Value = Date::Now();
            $member->Status->Value = 1;
         }

          $memberForm->Populate($member);
          $member->Save();
          
          if($data["Id"] == ""){
              $member->IdEntite = $this->Core->Db->GetInsertedId();;
          }
      
          return true;
      } else {
          return $memberForm->errors;
      }
  }

  /***
   * Delete a member
   */
  function DeleteMember($memberId){

    $member = new AssoMember($this->Core);
    $member->GetById($memberId);
    $member->Delete();

    return true;
  }


    /***
     * Member Ship 
     */
    function GetTabMemberShip(){
        $gdMemberShip = new EntityGrid("gdMemberShip", $this->Core);
        $gdMemberShip->Entity = "Apps\Asso\Entity\AssoMemberShip";
        $gdMemberShip->App = "Asso";
        $gdMemberShip->Action = "GetTabMemberShip";
        
        $gdMemberShip->AddOrder("DateCreated desc");
  
        $btnAdd = new Button(BUTTON, "btnAddMemberShip");
        $btnAdd->Value = $this->Core->GetCode("Asso.AddMemberShip");
  
        $gdMemberShip->AddButton($btnAdd);
        $gdMemberShip->AddColumn(new EntityColumn("MemberId", "MemberId"));
        $gdMemberShip->AddColumn(new EntityColumn("DateCreated", "DateCreated"));
        $gdMemberShip->AddColumn(new EntityColumn("Mount", "Mount"));
        
        
        $gdMemberShip->AddColumn(new EntityIconColumn("Action", 
                                                  array(array("EditIcone", "Asso.EditMemberShip", "Asso.EditMemberShip"),
                                                  array("DeleteIcone", "Asso.DeleteMemberShip", "Asso.DeleteMemberShip")
                                                  )    
                          ));
  
        return $gdMemberShip->Render();
     }
 
     /**
     * Save a member Ship
     */
    function SaveMemberShip($core, $data) {

        $memberShipForm = new AssoMemberShipForm($this->Core, $data);
  
        if ($memberShipForm->Validate($data)) {
  
            $memberShip = new AssoMemberShip($this->Core);
  
            if ($data["Id"]) {
                $memberShip->GetById($data["Id"]);
            } else {
              $memberShip->DateCreated->Value = Date::Now();
            }
            
            $memberShipForm->Populate($memberShip);
            $memberShip->Save();
          
            return true;
        } else {
            return $memberForm->errors;
        }
    }

    /***
     * Delete a memberShip
     */
    function DeleteMemberShip($memberShipId){

        $memberShip = new AssoMemberShip($this->Core);
        $memberShip->GetById($memberId);
        $memberShip->Delete();

        return true;
    }

    /****
     * Budget Expenase and Receipt
     */
    function GetTabBudget(){

        //Recette 
        $gdReceipt = new EntityGrid("gdReceipt", $this->Core);
        $gdReceipt->Entity = "Apps\Asso\Entity\AssoReceipt";
        $gdReceipt->App = "Asso";
        $gdReceipt->Action = "GetTabBudget";
        
        $gdReceipt->AddOrder("DateCreated desc");
  
        $btnAdd = new Button(BUTTON, "btnAddReceipt");
        $btnAdd->Value = $this->Core->GetCode("Asso.AddReceipt");
  
        $gdReceipt->AddButton($btnAdd);
        $gdReceipt->AddColumn(new EntityColumn("Libelle", "Libelle"));
        $gdReceipt->AddColumn(new EntityColumn("DateCreated", "DateCreated"));
        $gdReceipt->AddColumn(new EntityColumn("Mount", "Mount"));
        
        
        $gdReceipt->AddColumn(new EntityIconColumn("Action", 
                                                  array(array("EditIcone", "Asso.EditReceipt", "Asso.EditReceipt"),
                                                  array("DeleteIcone", "Asso.DeleteReceipt", "Asso.DeleteReceipt")
                                                  )    
                          ));
  

        //Depense
        $gdExpense = new EntityGrid("gdExpense", $this->Core);
        $gdExpense->Entity = "Apps\Asso\Entity\AssoExpense";
        $gdExpense->App = "Asso";
        $gdExpense->Action = "GetTabBudget";

     //   $gdExpense->AddOrder("DateCreated desc");

        $btnAdd = new Button(BUTTON, "btnAddExpense");
        $btnAdd->Value = $this->Core->GetCode("Asso.AddExpense");

        $gdExpense->AddButton($btnAdd);
        $gdExpense->AddColumn(new EntityColumn("Libelle", "Libelle"));
        $gdExpense->AddColumn(new EntityColumn("DateCreated", "DateCreated"));
        $gdExpense->AddColumn(new EntityColumn("Mount", "Mount"));


        $gdExpense->AddColumn(new EntityIconColumn("Action", 
                                                array(array("EditIcone", "Asso.EditExpense", "Asso.EditExpense"),
                                                array("DeleteIcone", "Asso.DeleteExpense", "Asso.DeleteExpense")
                                                )    
                        ));

        $view .= "<div class='row'>";
        
        $view .= "<div class='col-md-12'>";
        $view .= "<h2>Montant disponible : ".AssoHelper::GetMountAvailable($this->Core)."&euro;</h2>" ;               
        $view .= "</div>";
        
        $view .= "<div class='col-md-6'>";                    
        $view .=  $gdReceipt->Render();
        $view .= "</div>";

        $view .= "<div class='col-md-6'>";                    
        $view .=  $gdExpense->Render();
        $view .= "</div>";
        $view .= "</div>";
        
        return $view;
    }

    /**
     * Save a receipt
     */
    function SaveReceipt($core, $data) {

        $budgetReceiptForm = new AssoBudgetReceiptForm($this->Core, $data);
  
        if ($budgetReceiptForm->Validate($data)) {
  
            $budgetReceipt = new AssoReceipt($this->Core);
  
            if ($data["Id"]) {
                $budgetReceipt->GetById($data["Id"]);
            } else {
              $budgetReceipt->DateCreated->Value = Date::Now();
           }
  
            $budgetReceiptForm->Populate($budgetReceipt);
            $budgetReceipt->Save();
          
            return true;
        } else {
            return $memberForm->errors;
        }
    }

   /***
     * Delete a Receipt
     */
    function DeleteReceipt($receiptId){

        $receipt = new AssReceipt($this->Core);
        $receipt->GetById($receiptId);
        $receipt->Delete();

        return true;
    }

    /**
     * Save a expense
     */
    function SaveExpense($core, $data) {

        $budgetExpenseForm = new AssoBudgetExpenseForm($this->Core, $data);
  
        if ($budgetExpenseForm->Validate($data)) {
  
            $budgetExpense = new AssoExpense($this->Core);
  
            if ($data["Id"]) {
                $budgetExpense->GetById($data["Id"]);
            } else {
              $budgetExpense->DateCreated->Value = Date::Now();
           }
  
            $budgetExpenseForm->Populate($budgetExpense);
            $budgetExpense->Save();
          
            return true;
        } else {
            return $memberForm->errors;
        }
    }

   /***
     * Delete a Expense
     */
    function DeleteExpense($ExpenseId){

        $Expense = new AssExpense($this->Core);
        $Expense->GetById($ExpenseId);
        $Expense->Delete();

        return true;
    }


    /***
     * Equipment 
     */
    function GetTabEquipment(){
        $gdEquipment = new EntityGrid("gdEquipment", $this->Core);
        $gdEquipment->Entity = "Apps\Asso\Entity\AssoEquipment";
        $gdEquipment->App = "Asso";
        $gdEquipment->Action = "GetTabEquipment";
        
        $gdEquipment->AddOrder("DateCreated desc");
  
        $btnAdd = new Button(BUTTON, "btnAddEquipment");
        $btnAdd->Value = $this->Core->GetCode("Asso.AddEquipment");
  
        $gdEquipment->AddButton($btnAdd);
        $gdEquipment->AddColumn(new EntityColumn("Libelle", "Libelle"));
        $gdEquipment->AddColumn(new EntityColumn("Description", "Description"));
        $gdEquipment->AddColumn(new EntityColumn("Price", "Price"));
        $gdEquipment->AddColumn(new EntityColumn("Quantity", "Quantity"));
        $gdEquipment->AddColumn(new EntityColumn("DateCreated", "DateCreated"));
        
        
        $gdEquipment->AddColumn(new EntityIconColumn("Action", 
                                                  array(array("EditIcone", "Asso.EditEquipment", "Asso.EditEquipment"),
                                                  array("DeleteIcone", "Asso.DeleteEquipment", "Asso.DeleteEquipment")
                                                  )    
                          ));
  
        return $gdEquipment->Render();
     }

    /**
     * Save a equipment
     */
    function SaveEquipment($core, $data) {

        $EquipmentForm = new AssoEquipmentForm($this->Core, $data);
  
        if ($EquipmentForm->Validate($data)) {
  
            $Equipment = new AssoEquipment($this->Core);
  
            if ($data["Id"]) {
                $Equipment->GetById($data["Id"]);
            } else {
              $Equipment->DateCreated->Value = Date::Now();
           }
  
            $EquipmentForm->Populate($Equipment);
            $Equipment->Save();
          
            return true;
        } else {
            return $memberForm->errors;
        }
    }

    /***
     * Delete a Expense
     */
    function DeleteEquipment($EquipmentId){

        $Equipment = new AssoEquipment($this->Core);
        $Equipment->GetById($EquipmentId);
        $Equipment->Delete();

        return true;
    }

   /*action*/
 }?>