<?php

namespace Apps\Lang\Widget\TranslateForm;

use Core\Control\Form\Form;
use Core\View\View;
use Core\View\ElementView;

class TranslateForm extends Form {

    /**
     * Constructeur
     */
    function __construct($core = "", $data = "") {
        $this->Core = $core;

        $data = explode("-", $data);
        $this->App = $data[0];
        $this->EntityName = $data[1];
        $this->EntityId = $data[2];
       
        $entityPath = "Apps\\".$this->App. "\\Entity\\".$this->EntityName;

        $entity = new $entityPath($core);
        $entity->GetById($this->EntityId);
 
        $this->Init($entity, $this->App, $this->EntityName, $this->EntityId );
    }

    /**
     * Initialisation 
     */
    function Init($data, $app, $entityName, $entityId) {
        $this->form = new Form("TranslateFormForm", $data);

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

        $this->form->Add(array("Type" => "Hidden",
            "Id" => "AppName",
            "Value" => $app
        ));
        
        $this->form->Add(array("Type" => "Hidden",
            "Id" => "EntityName",
            "Value" => $entityName  
        ));

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

        $this->form->Add(array("Type" => "EntityListBox",
            "Id" => "LangId",
            "Entity" => "Core\Entity\Langs\Langs",
            "Label" => "Name",
            "Validators" => ["Required"]
        ));

        $this->form->Add(array("Type" => "TextBox",
            "Id" => "NameSource",
            "Value" => $data->Name->Value,
        ));

        $this->form->Add(array("Type" => "TextBox",
            "Id" => "Title",
        ));

        $this->form->Add(array("Type" => "TextBox",
            "Id" => "DescriptionSource",
            "Value" => $data->Description->Value,
        ));

        $this->form->Add(array("Type" => "TextBox",
            "Id" => "Description",
            "Value" => "",
        ));

        $this->form->Add(array("Type" => "TextArea",
            "Id" => "ContentSource",
            "Value" => $data->Content->Value,
            "Style" => "height:50vh", 
        ));

        $this->form->Add(array("Type" => "TextArea",
            "Id" => "Content",
            "Value" => "", 
            "Style" => "height:50vh", 
        ));
        
        $this->form->Add(array("Type" => "Button",
            "Id" => "btnSaveTranslate",
            "Value" => $this->Core->GetCode("Base.Save"),
            "CssClass" => "btn btn-primary"
        ));
    }

    /*     * *
     * Render the html form
     */

    function Render() {
        return $this->form->Render();
    }

    /*     * *
     * Validate the data
     */

    function Validate($data = "") {
        $errors = $this->form->Validate($this->Data);

        return $errors;
    }

    /*     * *
     * Load control with the entity
     */

    function Load($entity) {
        $this->form->Load($entity);
    }

    /*     * *
     * Populate the entity with the form Data
     */

    function Populate($entity) {
        
    }

    /*     * *
     * Error message
     */

    function Error() {
        $errors[] = $this->form->errors;

        return json_encode(array("statut" => "Error", "message" => $errors));
    }

}
