<?php

namespace Apps\Shop\Widget\CardWidget;

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

use Apps\Shop\Helper\CartHelper;
use Apps\Shop\Helper\ProductHelper;
use Apps\Shop\Entity\ShopProduct;

class CardWidget extends Form {

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

        $this->Init($data);
    }

    /**
     * Initialisation 
     */
    function Init($data) {
        $this->form = new Form("CardWidgetForm", $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" => "Button",
            "Id" => "btnSave",
            "Value" => "Save",
            "CssClass" => "btn btn-primary"
        ));
    }

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

    function 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));
    }


    /***
     * Add To Card Button
     */
    function RenderAddToCard($data){

        $product = new ShopProduct($this->Core);
        $product->GetById($data);

        /***
         * Produit en téléchargement gratuit
         */
        if($product->Type->Value == 2 && $product->Price->Value == 0) {
            
            $view = new View(__DIR__ . "/View/downloadLink.tpl", $this->Core);
            $view->AddElement(new ElementView("productId", base64_encode($product->IdEntite)));
            return $view->Render(); 
        }

        switch($product->Type->Value){
            //Produit Simpe
            case 1:
            case 2:
          
                $form = new Form("CardWidgetForm", $data);

                $form->SetView(__DIR__ . "/View/addToCard.tpl", $this->Core);
                $form->SetAction("/View/addToCard.tpl");
        
                $form->Add(array("Type" => "Hidden",
                    "Id" => "Id",
                    "Value" => $data
                ));
                
                $form->Add(array("Type" => "TextBox",
                    "Id" => "Name",
                    "Validators" => ["Required"]
                ));
                
                $form->Add(array("Type" => "Button",
                    "Id" => "btnAddToCart",
                    "Value" => $this->Core->GetCode("Shop.AddToCard"),
                    "CssClass" => "btn btn-primary"
                ));
        
                return $form->Render();
            break;
            case 3:
                $form = new Form("CardWidgetForm", $data);

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

                $childsProduct = ProductHelper::GetChilds($this->Core, $product->IdEntite);
        
                $form->AddElementView(new ElementView("Childs" , $childsProduct));
               
                $form->Add(array("Type" => "Hidden",
                    "Id" => "Id",
                    "Value" => $data
                ));
                
                $form->Add(array("Type" => "TextBox",
                    "Id" => "Name",
                    "Validators" => ["Required"]
                ));
                
                $form->Add(array("Type" => "Button",
                    "Id" => "btnAddToCart",
                    "Value" => $this->Core->GetCode("Shop.AddToCard"),
                    "CssClass" => "btn btn-primary"
                ));
        
                return $form->Render();
                break;

        }

       
    }

    /**
     * Icon du panier
     */
    function RenderCardIcon(){

        if($this->Core->IsConnected()){
        
        $view = new View(__DIR__ . "/View/cardIcon.tpl", $this->Core);

        $view->AddElement(new ElementView("nbCart", CartHelper::GetNumberElement()));

        return $view->Render();
        }
        
        return "";
    }
}
