<?php

namespace Apps\Immo\Widget\ImmoProjectForm;

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

use Apps\Immo\Entity\ImmoProjectUser;
use Apps\Immo\Entity\ImmoProjectAnnonce;

class ImmoProjectForm extends Form {

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

        $this->Init($data);
    }

    /**
     * Initialisation 
     */
    function Init($data) {
        $this->form = new Form("ImmoProjectFormForm", $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" => "UserIds",
        ));
        
        $this->form->Add(array("Type" => "TextBox",
            "Id" => "Name",
            "Validators" => ["Required"]
        ));
       
        $this->form->Add(array("Type" => "TextArea",
        "Id" => "Description",
        "Validators" => ["Required"]
         ));
   
        $this->form->Add(array("Type" => "Button",
            "Id" => "btnSaveProject",
            "Value" => $this->Core->GetCode("Base.Save"),
            "CssClass" => "btn btn-primary"
        ));

        if(is_object($data)){
            $this->form->AddElementView(new ElementView("ShowOtherData", true));

            $tbContact = new AutoCompleteBox("tbContact", $this->Core);
            $tbContact->PlaceHolder = $this->Core->GetCode("PuzzProject.SearchMember");
            $tbContact->Entity = "Core/Entity/User/User";
            $tbContact->Methode = "SearchUser";
            $tbContact->Parameter = "AddAction=ImmoProjectForm.SelectUser()";
            $this->form->AddElementView($tbContact);

            $projetUser = new ImmoProjectUser($this->Core);
            $projetUser->Select("user.Email", "UserEmail");
            $projetUser->Join("ee_user", "user","LEFT",  "user.Id = ImmoProjectUser.UserId");

            $users = $projetUser->Find("ProjectId=" .$data->IdEntite);

            $view = "";
            foreach($users as $user){
                $view .= "<div class='chips'>".$user->UserEmail->Value;
                $view .= "<i class='fa fa-trash removeUser'  id='".$user->IdEntite."'></i>";
                $view .="</div>";
            }

            $this->form->AddElementView(new ElementView("users", $view));
            
            //Annonce lié
            $lstAnnonce = new \Core\Control\EntityListBox\EntityListBox("lstAnnonce", $this->Core);
            $lstAnnonce->Entity = "Apps\Annonce\Entity\AnnonceAnnonce";
            $lstAnnonce->AddField("Title");
            $lstAnnonce->ListBox->Add("","");
            
            $this->form->AddElementView($lstAnnonce);
            

            $Annonce = new ImmoProjectAnnonce($this->Core);
            $Annonce->Select("AnnonceAnnonce.Title", "AnnonceTitle");

            $Annonce->Join("AnnonceAnnonce", "AnnonceAnnonce", "Left", "AnnonceAnnonce.Id = ImmoProjectAnnonce.AnnonceId");
            $Annonces = $Annonce->Find("ProjectId = " . $data->IdEntite);

            $view ="";
            foreach($Annonces as $annonce){
                $view .= "<div class='chips'>".$annonce->AnnonceTitle->Value;
                $view .= "<i class='fa fa-trash removeAnnonce'  id='".$annonce->IdEntite."'></i>";
                $view .="</div>";
           }

           $this->form->AddElementView(new ElementView("annonces", $view));
             
     
        } else{
            $this->form->AddElementView(new ElementView("ShowOtherData", false));
        }
    }

    /*     * *
     * 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));
    }

}
