<?php

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

namespace Apps\Reward\Module\Member;

use Core\Control\Button\Button;
use Core\Controller\Controller;
use Core\View\View;
use Core\View\ElementView;

use Apps\Reward\Widget\RewardUserForm\RewardUserForm;
use Apps\Reward\Entity\RewardUser;
use Apps\Reward\Entity\RewardReward;
use Apps\Reward\Entity\RewardUserClaim;


use Apps\Reward\Helper\RewardHelper;
use Core\Utility\Date\Date;



/*
 * 
 */
 class MemberController 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);

       $reward = new RewardReward($this->Core);
       $view->AddElement(new ElementView("Rewards", $reward->GetAll()));

       $view->AddElement(new ElementView("UserStatus", RewardHelper::GetStatusByUser($this->Core, $this->Core->User->IdEntite)));

       return $view->Render();
   }

   /****
    * Save User Respons
    */
   function SaveUserResponse($data){

    $rewardUserForm = new RewardUserForm($this->Core, $data);

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

        $rewardUser = new RewardUser($this->Core);
        $rewardUser->UserId->Value = $this->Core->User->IdEntite;
        $rewardUser->Status->Value = 1;

        $rewardUserForm->Populate($rewardUser);

        $rewardUser->Save();

        return true;
    } else {
        return $rewardUserForm->Error();
    }

   }
         
   /***
    * Demande de récompenses
    */
   function ClaimThis($rewardId){
       
       $rewardUserClaim = new RewardUserClaim($this->Core);
       $rewardUserClaim->UserId->Value = $this->Core->User->IdEntite;
       $rewardUserClaim->RewardId->Value = $rewardId;
       $rewardUserClaim->Status->Value = 1;
       $rewardUserClaim->DateClaim->Value = Date::Now();
       $rewardUserClaim->Save();
       
   }
   
   
          /*action*/
 }?>