<?php

namespace Apps\Shop\Widget\ProductForm;

use Core\Entity\Entity\Argument;
use Core\Control\Form\Form;
use Core\Control\Hidden\Hidden;
use Core\View\View;
use Core\View\ElementView;
use Core\Control\VTab\VTab;
use Core\Control\TabStrip\TabStrip;


use Core\Control\EntityListBox\EntityListBox;

use Apps\Shop\Helper\ProductHelper;

class ProductForm extends Form {

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

        //On le fait ici pour avoir le formuaire � l'affichage mais aussi � la validation
        $this->Init($data);
    }

    /**
     * Initialisation 
     */
    function Init($data) {
        $this->form = new Form("ProductForm", $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" => "Name",
            "Validators" => ["Required"]
        ));

        $this->form->Add(array("Type" => "TextBox",
            "Id" => "Code",
            "Validators" => ["Required"]
        ));

        $this->form->Add(array("Type" => "ListBox",
        "Id" => "Type",
        "Values" => array(
            $this->Core->GetCode("Shop.ProducTypePhysical")   => 1,
            $this->Core->GetCode("Shop.ProducTypeUpload")   => 2,
            $this->Core->GetCode("Shop.ProducTypeSubscription")   => 3,
            $this->Core->GetCode("Shop.ProducTypeRent")   => 4
            ),
            "Validators" => ["Required"]
        ));

        $this->form->Add(array("Type" => "TextArea",
            "Id" => "Description",
            "Validators" => ["Required"]
        ));

        $this->form->Add(array("Type" => "NumericBox",
            "Id" => "Price",
            "Validators" => ["Required"]
        ));
        
        $this->form->Add(array("Type" => "EntityListBox",
        "Id" => "ParentId",
        "Entity" => "Apps\Shop\Entity\ShopProduct",
        "Values"  => ["" => $this->Core->GetCode("Shop.SelectProductParent")], 
        "Field" => "Name",
        ));

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

        $this->form->Add(array("Type" => "ListBox",
        "Id" => "PriceTime",
        "Values" => array(
            $this->Core->GetCode("Shop.ProductPriceHour")   => 1,
            $this->Core->GetCode("Shop.ProductPriceWeek")   => 2,
            $this->Core->GetCode("Shop.ProductPriceMonth")   => 3,
            $this->Core->GetCode("Shop.ProductPriceYear")   => 4
            ),
            "Validators" => ["Required"]
        ));

        $this->form->Add(array("Type" => "NumericBox",
        "Id" => "Price",
        "Validators" => ["Required"]
        ));
        
        $this->form->Add(array("Type" => "Upload",
            "Id"  => "Upload",
            "App" => "Shop",
            "Method" => "SaveImageProduct",
        ));
      
        $this->form->Add(array("Type" => "Upload",
        "Id"  => "UploadDocument",
        "App" => "Shop",
        "Method" => "SaveDocumentProduct",
        "Accept" => "pdf, doc, docx"
        ));
  

        $this->form->Add(array("Type" => "Button",
            "Id" => "btnSaveProduct",
            "Value" => $this->Core->GetCode("Save"),
            "CssClass" => "btn btn-primary"
        ));
    }

    /***
     * Categorie du produit
     */
    function GetTabCategorie($product){

        $view = new View(__DIR__ . "/View/categories.tpl", $this->Core);

        $lstCategorie = new EntityListBox('lstCategorie', $this->Core);
        $lstCategorie->Entity = "Apps\Shop\Entity\ShopCategory";

       //TODO PRENDRE CEUX PAR SHOP 

        $view->AddElement($lstCategorie);

        $categories = ProductHelper::GetCategoryByProduct($this->Core, $product->IdEntite);

        $hdCategory = new Hidden("hdCategory");
        $hdCategory->Value = json_encode($categories);

        $view->AddElement($hdCategory);

        return $view;
    }

    /***
     * Categorie du produit
     */
    function GetTabCaracteristique($product){

        $view = new View(__DIR__ . "/View/caracteristiques.tpl", $this->Core);

        //TODO PRENDRE CEUX PAR SHOP 
        $lstCaracteristique = new EntityListBox('lstCaracteristique', $this->Core);
        $lstCaracteristique->Entity = "Apps\Shop\Entity\ShopCaracteristique";
        $lstCaracteristique->ListBox->Add($this->Core->GetCode("Shop.ChooseCaracteristique"), "");
      
        $view->AddElement($lstCaracteristique);

        //TODO PRENDRE CEUX PAR SHOP 

        $lstCaracteristiqueItem = new EntityListBox('lstCaracteristiqueItem', $this->Core);
        $lstCaracteristiqueItem->Entity = "Apps\Shop\Entity\ShopCaracteristiqueItem";
        $lstCaracteristiqueItem->AddField("Label");
      
        $lstCaracteristiqueItem->AddArgument(new Argument("Apps\Shop\Entity\ShopCaracteristiqueItem", "CaracteristiqueId", EQUAL, 1 ));
        $view->AddElement($lstCaracteristiqueItem);

        $caracteristiques = ProductHelper::GetCaracteristiqueByProduct($this->Core, $product->IdEntite);

        $hdCaracteristique = new Hidden("hdCaracteristique");
        $hdCaracteristique->Value = json_encode($caracteristiques);

        $view->AddElement($hdCaracteristique);

        return $view;
    }

    /*     * *
     * G�n�rer les formulaire
     */

    function Render() {

        $this->tabProduct = new VTab("tabProduct");
        $tbProd = new \Core\Control\Libelle\Libelle($this->form->Render());

        $this->tabProduct->AddTab($this->Core->GetCode("Shop.Product"), $tbProd);

        if($this->Data->IdEntite != ""){ 

            $this->tabProduct->AddTab($this->Core->GetCode("Shop.Categorie"), $this->GetTabCategorie($this->Data));
            $this->tabProduct->AddTab($this->Core->GetCode("Shop.Caracteristique"), $this->GetTabCaracteristique($this->Data));
            
           // $this->form->AddElementView(new ElementView("tabProduct" , $tabProduct->Render()));
        } else {

            $emptyLabel = new \Core\Control\Libelle\Libelle("");

            //$this->form->AddElementView(new ElementView("tabProduct" , $emptyLabel->Render() ));
        }

        return $this->tabProduct->Render();
        
        //return $this->form->Render();
    }

    /*     * *
     * Valide les donn�es selon les diff�rents formulaires
     */

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

        return $errors;
    }

    /*     * *
     * Charge les controls avec les donn�es de l'entit�
     */

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

    /*     * *
     * Charge l'entit� avec les donn�es des diff�rents formulaire
     */

    function Populate($entity) {
        
    }

    /*     * *
     * Gestion des erreurs
     */

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

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

}
