<?php

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

namespace Apps\Reward\Module\Admin;

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\Control\Button\Button;
use Core\Utility\Format\Format;

use Apps\Reward\Entity\RewardReward;
use Apps\Reward\Entity\RewardUser;
use Apps\Reward\Entity\RewardGifts;

use Apps\Reward\Widget\RewardForm\RewardForm;
use Apps\Reward\Widget\RewardGiftForm\RewardGiftForm;

/*
 * 
 */
 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);
        
       $tabReward = new TabStrip("tabReward", "Reward"); 
       $tabReward->AddTab($this->Core->GetCode("Reward.Rewards"), $this->GetTabReward());
       $tabReward->AddTab($this->Core->GetCode("Reward.Gifts"), $this->GetTabGifts());
       $tabReward->AddTab($this->Core->GetCode("Reward.RewardsUser"), $this->GetTabRewardUser(), true);
       $tabReward->AddTab($this->Core->GetCode("Reward.RewardsUserClaim"), $this->GetTabRewardUserClaim(), true);
      
       $view->AddElement(new ElementView("tabReward", $tabReward->Render()));

       return $view->Render();
   }

   /***
     * Save a reward
    */
    function SaveReward($data){

        $rewardForm = new RewardForm($this->Core, $data);

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

            $reward = new RewardReward($this->Core);

            if ($data["Id"] != "") {
                $reward->GetById($data["Id"]);
            } 

            $reward->Code->Value = Format::ReplaceForUrl($data["Name"]);

            $rewardForm->Populate($reward);
            $reward->Save();

            return true;

        } else {
            return $rewardForm->Error();
        }
    }

   /****
    *  Reward
    */
   function GetTabReward(){

    $gdReward = new EntityGrid("gdReward", $this->Core);
    $gdReward->Entity = "Apps\Reward\Entity\RewardReward";
    $gdReward->App = "Reward";
    $gdReward->Action = "GetTabReward";

    $btnAdd = new Button(BUTTON, "btnAddReward");
    $btnAdd->Value = $this->Core->GetCode("Reward.AddReward");

    $gdReward->AddButton($btnAdd);
    $gdReward->AddColumn(new EntityColumn("Name", "Name"));
    $gdReward->AddColumn(new EntityColumn("Action", "Action"));
    $gdReward->AddColumn(new EntityColumn("Points", "Points"));
    
    $gdReward->AddColumn(new EntityIconColumn("Action", 
                                              array(array("EditIcone", "Reward.EditReward", "Reward.EditReward"),
                                             // array("LangIcone", "Reward.TranslateReward", "Reward.TranslateReward"),
                                              array("DeleteIcone", "Reward.DeleteReward", "Reward.DeleteReward"),
                                              )    
                      ));

    return $gdReward->Render();
   }
   
    /****
    *  Gifts
    */
   function GetTabGifts(){

    $gdReward = new EntityGrid("gdGifts", $this->Core);
    $gdReward->Entity = "Apps\Reward\Entity\RewardGifts";
    $gdReward->App = "Reward";
    $gdReward->Action = "GetTabGifts";

    $btnAdd = new Button(BUTTON, "btnAddGift");
    $btnAdd->Value = $this->Core->GetCode("Reward.AddGift");

    $gdReward->AddButton($btnAdd);
    $gdReward->AddColumn(new EntityColumn("Name", "Name"));
    $gdReward->AddColumn(new EntityColumn("Description", "Description"));
    $gdReward->AddColumn(new EntityColumn("Points", "Points"));
    
    $gdReward->AddColumn(new EntityIconColumn("Action", 
                                              array(array("EditIcone", "Reward.EditGift", "Reward.EditGift"),
                                              //array("LangIcone", "Reward.TranslateReward", "Reward.TranslateReward"),
                                              array("DeleteIcone", "Reward.DeleteGift", "Reward.DeleteGift"),
                                              )    
                      ));

    return $gdReward->Render();
   }
   
   /***
     * Save a reward
    */
    function SaveGift($data){

        $rewardGiftForm = new RewardGiftForm($this->Core, $data);

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

            $reward = new RewardGifts($this->Core);

            if ($data["Id"] != "") {
                $reward->GetById($data["Id"]);
            } 

            $rewardGiftForm->Populate($reward);
            $reward->Save();

            return true;

        } else {
            return $rewardGiftForm->Error();
        }
    }
   
   
   /****
    *  Reward User
    */
   function GetTabRewardUser(){

    $gdReward = new EntityGrid("gdRewardUser", $this->Core);
    $gdReward->Entity = "Apps\Reward\Entity\RewardUser";
    $gdReward->App = "Reward";
    $gdReward->Action = "GetTabRewardUser";
    $gdReward->AddOrder("UserId, Id desc");

    $gdReward->Select("RewardReward.Name", "RewardName");
    $gdReward->Join("RewardReward", "RewardReward", "left",  "RewardReward.Id = RewardUser.RewardId");

    $gdReward->Select("user.Email", "UserEmail");
    $gdReward->Join("ee_user", "user", "left",  "user.Id = RewardUser.UserId");

    $gdReward->AddColumn(new EntityColumn("UserEmail", "UserEmail"));
    $gdReward->AddColumn(new EntityColumn("RewardName", "RewardName"));
    
    $gdReward->AddColumn(new EntityColumn("Response", "Response"));
    $gdReward->AddColumn(new EntityColumn("Status", "Status"));

    $gdReward->AddColumn(new EntityIconColumn("Action", 
                                              array(array("ShareIcone", "Reward.ValidResponseUser", "Reward.ValidResponseUser")
                                              )    
                      ));

    return $gdReward->Render();


   }

   /***
    * 
    */
   function GetTabRewardUserClaim(){
        $gdReward = new EntityGrid("gdRewardUser", $this->Core);
    $gdReward->Entity = "Apps\Reward\Entity\RewardUserClaim";
    $gdReward->App = "Reward";
    $gdReward->Action = "GetTabRewardUserClaim";
    $gdReward->AddOrder("UserId, Id desc");

    $gdReward->Select("RewardGifts.Name", "RewardName");
    $gdReward->Join("RewardGifts", "RewardGifts", "left",  "RewardGifts.Id = RewardUserClaim.RewardId");

    $gdReward->Select("user.Email", "UserEmail");
    $gdReward->Join("ee_user", "user", "left",  "user.Id = RewardUserClaim.UserId");

    $gdReward->AddColumn(new EntityColumn("UserEmail", "UserEmail"));
    $gdReward->AddColumn(new EntityColumn("RewardName", "RewardName"));
    
    $gdReward->AddColumn(new EntityColumn("DateClaim", "DateClaim"));
    $gdReward->AddColumn(new EntityColumn("Status", "Status"));

    $gdReward->AddColumn(new EntityIconColumn("Action", 
                                              array(array("ShareIcone", "Reward.ValidResponseUser", "Reward.ValidResponseUser")
                                              )    
                      ));

    return $gdReward->Render();
   }   
   /***
    * Valide response USer
    */
   function ValidResponseUser($responseUserId){
        $rewardUser = new RewardUser($this->Core);
        $rewardUser->GetById($responseUserId);
        $rewardUser->Status->Value = 2;
        $rewardUser->Save();

        return true;
   } 
          
          /*action*/
 }?>