<?php

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

namespace Apps\Booking\Module\Admin;

use Core\Control\Button\Button;
use Core\Controller\Controller;
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 Apps\Booking\Entity\BookingCaracteristique;
use Apps\Booking\Entity\BookingCaracteristiqueItem;
use Apps\Booking\Entity\BookingOpeningTime;

use Apps\Booking\Widget\BookingCaracteristiqueForm\BookingCaracteristiqueForm;

/*
 * 
 */
 class AdminController extends Controller
 {
    /**
     * 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);
     
       $tabBooking = new TabStrip("tabBooking", "Booking"); 
       $tabBooking->AddTab($this->Core->GetCode("Booking.Reservation"), $this->GetTabReservation());
       //$tabBooking->AddTab($this->Core->GetCode("Booking.Booking"), $this->GetTabBooking());
       $tabBooking->AddTab($this->Core->GetCode("Booking.OpeningTime"), $this->GetTabOpeningTime());
       $tabBooking->AddTab($this->Core->GetCode("Booking.Caracteristiques"), $this->GetTabCaracteristique());
       
       $view->AddElement(new ElementView("tabBooking", $tabBooking->Render()));
 

       return $view->Render();
   }

   /***
    * Get te booking Property
    */
    function GetTabBooking(){

        return "TODO PLUGIN "; 
        $marketForm = new MarketForm($this->Core);
        $marketForm->Load();
        
        return $marketForm->Render(); 
   }

   /***
    * Horaires d'ouverture
    */
   function GetTabOpeningTime(){

    $view = new View(__DIR__."/View/openingTime.tpl", $this->Core);
    
    $days = [];
    
    for($i =0; $i <= 7; $i++)
    {
        $day = new BookingOpeningTime($this->Core);

        $timeExist = $day->Find("Day=".$i); 

        if(count($timeExist) > 0){
            $timeDay = $timeExist[0];
            $days[] = $timeDay;
        } 
        else{
            $day->Day->Value = $i;
            $days[] = $day;
        }
    }

    $view->AddElement(new ElementView("Days", $days)); 

    return $view->Render();
   }

   /**
    * Sauavegare des horaires d'ouverture
    */
   function SaveOpeningTime($core, $data){

        $times = json_decode($data["time"]);

        foreach($times as $time){
            
            $timeDay = new BookingOpeningTime($core);
            $timeExist = $timeDay->Find("Day=".$time->day); 

            if(count($timeExist) > 0){
                $timeDay = $timeExist[0];
            } 

            $timeDay->Day->Value = $time->day;
            $timeDay->HourStartAm->Value = $time->hourStartAm;
            $timeDay->HourEndAm->Value = $time->hourEndAm;
            $timeDay->HourStartPm->Value = $time->hourStartPm;
            $timeDay->HourEndPm->Value = $time->hourEndPm;
            $timeDay->Save();
        }

        return true;
   }

    /***
    * Caracteristiques des produits
    */
    function GetTabCaracteristique(){
       
        $gdCaracteristique = new EntityGrid("gdCaracteristique", $this->Core);
        $gdCaracteristique->Entity = "Apps\Booking\Entity\BookingCaracteristique";
        $gdCaracteristique->App = "Booking";
        $gdCaracteristique->Action = "GetTabCaracteristique";
  
        $btnAdd = new Button(BUTTON, "btnAddCaracteristique");
        $btnAdd->Value = $this->Core->GetCode("Booking.AddCaracteristique");
  
        $gdCaracteristique->AddButton($btnAdd);
  
        $gdCaracteristique->AddColumn(new EntityColumn("Name", "Name"));
        
        $gdCaracteristique->AddColumn(new EntityIconColumn("Action", 
                                                  array(array("EditIcone", "Booking.EditCaracteristique", "Booking.EditCaracteristique"),
                                                        array("DeleteIcone", "Booking.DeleteCaracteristique", "Booking.DeleteCaracteristique"),
                                                  )    
                          ));
  
        return $gdCaracteristique->Render();
     }
   
         /**
       * Sauvegarde une caractériqtiques et ses valeurs
       */
      public static function SaveCaracteristique($core, $data) {
          
          $caracteristiqueForm = new BookingCaracteristiqueForm($core, $data);
  
          if ($caracteristiqueForm->Validate($data)) {
  
              $caracteristique = new BookingCaracteristique($core);
  
              if ($data["Id"] != "") {
                  $caracteristique->GetById($data["Id"]);
              } 
  
              $caracteristiqueForm->Populate($caracteristique);
              $caracteristique->Save();
  
              if ($data["Id"] == "") {
                   $data["Id"] = $core->Db->GetInsertedId();
              }
              
              //Sauvegarde des items
              foreach (json_decode($data["items"]) as $caracteristiqueItem) {
  
                  $item = new BookingCaracteristiqueItem($core);
                  if ($caracteristiqueItem->id != 0) {
                      $item->GetById($caracteristiqueItem->id);
                  }
                  $item->CaracteristiqueId->Value = $data["Id"];
                  $item->Label->Value = $caracteristiqueItem->label;
                  $item->Save();
              }
  
              return true;
          }
  
          return false;
      }
  
      /*     * *
       * Remove a caractristic item
       */
  
      public static function RemoveCaracteristiqueItem($core, $itemId) {
  
          $item = new BookingCaracteristiqueItem($core);
          $item->GetById($itemId);
          $item->Delete();
      }
  
      /*     * *
       * Get the items of a caracteristique
       */
  
      public static function GetItemByCaracteristique($core, $caracteristiqueId) {
          $item = new BookingCaracteristiqueItem($core);
          $items = $item->Find("CaracteristiqueId=" . $caracteristiqueId);
  
          return $item->ToAllArray($items);
      }
  
      /*     * *
       * Delete a caracteristiques 
       */
  
      public static function DeleteCaracteristique($core, $caracteristiqueId) {
  
          $items = new BookingCaracteristiqueItem($core);
          $items->AddArgument(new Argument("Apps\Shop\Entity\ShopCaracteristiqueItem", "CaracteristiqueId", EQUAL, $caracteristiqueId));
          $items->DeleteByArg();
  
          $caracteristique = new BookingCaracteristique($core);
  
          $caracteristique->GetById($caracteristiqueId);
          $caracteristique->Delete();
          
          return true;
      } 

      /***
       * Reservations 
       */
      function GetTabReservation(){
        $gdReservation = new EntityGrid("gdReservation", $this->Core);
        $gdReservation->Entity = "Apps\Booking\Entity\BookingReservation";
        $gdReservation->App = "Booking";
        $gdReservation->Action = "GetTabReservation";
        $gdReservation->AddOrder("Id desc");
        $gdReservation->AddColumn(new EntityColumn("DateStart", "DateStart"));
        $gdReservation->AddColumn(new EntityColumn("Name", "Name"));
        $gdReservation->AddColumn(new EntityColumn("FirstName", "FirstName"));
        
        $gdReservation->AddColumn(new EntityIconColumn("Action", 
                                                  array(array("EditIcone", "Booking.EditReservation", "Booking.EditReservation"),
                                                        array("DeleteIcone", "Booking.ArchiveReservation", "Booking.ArchiveReservation"),
                                                  )    
                          ));
  
        return $gdReservation->Render();

      }
          
          /*action*/
 }?>