<?php

namespace Apps\Forum\Widget\ReponseForm;

use Core\Model\Model;
use Core\View\View;
use Core\View\ElementView;
use Core\Utility\Date\Date;
use Core\Control\Form\Form;

/**
 * Classe ReponseForm
 * Cette classe combine les fonctionnalités d'un widget et d'un modèle
 * pour remplacer le modèle ReponseModel
 * 
 * Note: Cette classe est utilisée par ViewModelManager pour générer automatiquement
 * un formulaire à partir de l'entité ForumReponse.
 */
class ReponseForm extends Model {
    
    private $sujetId;
    private $form;
    private $excludes = ["UserId", "DateCreated"];
    private $state = "Init";

    /**
     * Constructeur
     */
    public function __construct($core = "", $data = "") {
        parent::__construct($core);
        $this->Core = $core;
        $this->Data = $data;
        
        $entityName = "Apps\Forum\Entity\ForumReponse";
        $this->Entity = new $entityName($core);
        
        $this->InitForm($data);
    }

    /**
     * Initialisation du formulaire
     */
    private function InitForm($data) {
        $this->form = new Form("ReponseFormForm", $data);

        $this->form->SetView(__DIR__ . "/View/index.tpl", $this->Core);
        $this->form->SetAction("/View/index.tpl");

        $this->form->Add(array("Type" => "Hidden",
            "Id" => "Id",
        ));
        
        $this->form->Add(array("Type" => "Hidden",
            "Id" => "MessageId",
        ));

        $this->form->Add(array("Type" => "TextArea",
            "Id" => "Message",
            "Validators" => ["Required"],
            "CssClass" => "form-control",
            "Rows" => 5
        ));
        
        $this->form->Add(array("Type" => "Button",
            "Id" => "btnSendReponse",
            "Value" => $this->Core->GetCode("Forum.SendReponse"),
            "CssClass" => "btn btn-primary"
        ));
    }
    
    /**
     * Render the html form
     */
    public function Render() {
        return $this->form->Render();
    }

    /**
     * Validate the data
     */
    public function Validate($data = "") {
        if (empty($data)) {
            $data = $this->Data;
        }
        return $this->form->Validate($data);
    }

    /**
     * Load control with the entity
     */
    public function Load($entity) {
        $this->form->Load($entity);
    }

    /**
     * Populate the entity with the form Data
     */
    public function Populate($entity) {
        if (isset($_POST['Message'])) {
            $entity->Message->Value = $_POST['Message'];
        }
        
        $entity->MessageId->Value = $this->sujetId;
        $entity->UserId->Value = $this->Core->User->IdEntite;
        $entity->DateCreated->Value = Date::Now();
    }

    /**
     * Error message
     */
    public function Error() {
        if (isset($this->form->errors)) {
            $errors[] = $this->form->errors;
            return json_encode(array("statut" => "Error", "message" => $errors));
        }
        return json_encode(array("statut" => "Error", "message" => ["Une erreur est survenue"]));
    }
}
