<?php

namespace Apps\Crm\Widget\CrmProspectForm;

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

class CrmProspectForm extends Form {

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

        $this->Init($data);
    }

    /**
     * Initialisation 
     */
    function Init($data) {
        $this->form = new Form("CrmProspectFormForm", $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" => "TextBox",
            "Id" => "FirstName",
            "Validators" => ["Required"]
        ));
        
        $this->form->Add(array("Type" => "TextBox",
            "Id" => "LastName",
            "Validators" => ["Required"]
        ));

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

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

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

        $this->form->Add(array("Type" => "TextBox",
            "Id" => "Phone",
        ));
       
        $this->form->Add(array("Type" => "TextArea",
            "Id" => "Adress",
        ));
        $this->form->Add(array("Type" => "DateBox",
            "Id" => "DateMeet",
        ));
       
        $this->form->Add(array("Type" => "TextArea",
            "Id" => "PlaceMeet",
        ));

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

        $this->form->Add(array("Type" => "TextArea",
            "Id" => "Description",
        ));
       
        $this->form->Add(array("Type" => "Button",
            "Id" => "btnSaveProspect",
            "Value" => $this->Core->GetCode("Crm.SaveProspect"),
            "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));
    }

}
