<?php

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

namespace Apps\Immo\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\Immo\Entity\ImmoCity;
use Apps\Immo\Widget\ImmoCityForm\ImmoCityForm;

use Apps\Immo\Entity\ImmoProject;
use Apps\Immo\Widget\ImmoProjectForm\ImmoProjectForm;
use Apps\Immo\Entity\ImmoProjectUser;

use Apps\Immo\Helper\ProjectHelper;
use Apps\Immo\Entity\ImmoProjectEvent;
use Apps\Immo\Widget\ImmoProjectEventForm\ImmoProjectEventForm;

use Apps\Immo\Entity\ImmoProjectAnnonce;


/*
 * 
 */

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);

        $tabImmo = new TabStrip("tabImmo", "Immo");
        $tabImmo->AddTab($this->Core->GetCode("Immo.City"), $this->GetTabCity());
        $tabImmo->AddTab($this->Core->GetCode("Immo.Project"), $this->GetTabProject(), true);
        
        $view->AddElement(new ElementView("tabImmo", $tabImmo->Render()));

        return $view->Render();

        return $view->Render();
    }

    /*     * **
     * Ville a estimer 
     */

    function GetTabCity() {

        $gdCity = new EntityGrid("gdCity", $this->Core);
        $gdCity->Entity = "Apps\\Immo\Entity\\ImmoCity";
        $gdCity->App = "Immo";
        $gdCity->Action = "GetTabCity";

        $btnAdd = new Button(BUTTON, "btnAddCity");
        $btnAdd->Value = $this->Core->GetCode("Immo.AddCity");

        $gdCity->AddButton($btnAdd);
       
        $gdCity->AddColumn(new EntityColumn($this->Core->GetCode("Immo.Name"), "Name"));
        $gdCity->AddColumn(new EntityColumn($this->Core->GetCode("Immo.Code"), "Code"));
       
        $gdCity->AddColumn(new EntityIconColumn("Action",
                        array(array("EditIcone", "Immo.EditCity", "Immo.EditCity"),
                    array("DeleteIcone", "Immo.DeleteCity", "Immo.DeleteCity"),
                        )
        ));

        return $gdCity->Render();
    }

    /*
     * Save a city
     */
    function SaveCity($core, $data) {

        $cityForm = new ImmoCityForm($this->Core, $data);

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

            $city = new ImmoCity($this->Core);

            if ($data["Id"]) {
                $city->GetById($data["Id"]);
            }
            $cityForm->Populate($city);
            $city->Save();
            
    
            return true;
        } else {
            return $cityForm->errors;
        }
    }

    /***
     * Project Immo
     */
    function GetTabProject(){

        $gdProject = new EntityGrid("gdProject", $this->Core);
        $gdProject->Entity = "Apps\\Immo\Entity\\ImmoProject";
        $gdProject->App = "Immo";
        $gdProject->Action = "GetTabProject";

        $gdProject->AddOrder("Id desc");
        
        $btnAdd = new Button(BUTTON, "btnAddProject");
        $btnAdd->Value = $this->Core->GetCode("Immo.AddProject");

        $gdProject->AddButton($btnAdd);
       
        $gdProject->AddColumn(new EntityColumn($this->Core->GetCode("Immo.Name"), "Name"));
        $gdProject->AddColumn(new EntityColumn($this->Core->GetCode("Immo.DateCreated"), "DateCreated"));
        $gdProject->AddColumn(new EntityColumn($this->Core->GetCode("Immo.Description"), "Description"));
        $gdProject->AddColumn(new EntityFunctionColumn($this->Core->GetCode("Immo.Events"), "GetEvents"));
       
        $gdProject->AddColumn(new EntityIconColumn("Action",
                        array(array("EditIcone", "Immo.EditProject", "Immo.EditProject"),
                              array("AddIcone", "Immo.AddEvent", "Immo.AddEvent")
                        )
        ));

        return $gdProject->Render();
    }
  
    /*
     * Save a project
     */
    function SaveProject($data) {

        $projectForm = new ImmoProjectForm($this->Core, $data);

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

            $project = new ImmoProject($this->Core);

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

                if($data["UserIds"] != "") {
                    ProjectHelper::AddUers($this->Core, $data["Id"], $data["UserIds"]);
                }

            } else {
                $project->DateCreated->Value = Date::Now();
            }

            $projectForm->Populate($project);
            $project->Save();
            
    
            return true;
        } else {
            return $projectForm->errors;
        }
    }

    /***
     * Remove a user tyo a project
     */
    function RemoveUserProjet($userProjetId){

        $userProject  = new ImmoProjectUser($this->Core);
        $userProject->GetById($userProjetId);
        $userProject->Delete();
        return true;
    }

    /***
     * Sauvegarde l'event du projet
     */
    function SaveEventProject($data){

        $eventForm = new ImmoProjectEventForm($this->Core, $data);

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

            $event = new ImmoProjectEvent($this->Core);

            if ($data["Id"]) {
                $event->GetById($data["Id"]);
            }
            $eventForm->Populate($event);
            $event->Save();
            
    
            return true;
        } else {
            return $eventForm->errors;
        }
      }

    /***
     * Remove a event to a project
     *  */  
    function RemoveEventProject($eventId){
        $event = new ImmoProjectEvent($this->Core);
        $event->GetById($eventId);
        $event->Delete();

        return true;
    }
    
    

    /***
     * Lie une annonce a u nprojet
     */
    function AddAnnonceToProjet($data){
        
        $annonceProjet = new ImmoProjectAnnonce($this->Core);
        $annonceProjet->ProjectId->Value = $data["projetId"];
        $annonceProjet->AnnonceId->Value = $data["annonceId"];
        $annonceProjet->Save();
        
        
        return true;
    }
    
    /* action */
}

?>