<?php

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

namespace Apps\estimation\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 Apps\estimation\Entity\estimationCity;
use Apps\estimation\Widget\EstimationCityForm\EstimationCityForm;

/*
 * 
 */

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

        $tabEstimation = new TabStrip("tabEstimation", "Estimation");
        $tabEstimation->AddTab($this->Core->GetCode("Estimation.City"), $this->GetTabCity());
        $view->AddElement(new ElementView("tabEstimation", $tabEstimation->Render()));

        return $view->Render();

        return $view->Render();
    }

    /*     * **
     * Ville a estimer 
     */

    function GetTabCity() {

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

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

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

        return $gdCity->Render();
    }

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

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

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

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

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

?>