<?php

/*
 * PuzzleApp
 * Webemyos
 * Jérôme Oliva
 * GNU Licence
 */

namespace Apps\Crm\Module\Admin;

use Core\Control\Button\Button;
use Core\Controller\Controller;
use Core\View\View;
use Core\View\ElementView;
use Core\Control\TabStrip\TabStrip;
use Core\Control\EntityGrid\EntityGrid;
use Core\Control\EntityGrid\EntityColumn;
use Core\Control\EntityGrid\EntityIconColumn;
use Core\Control\EntityGrid\EntityFunctionColumn;
use Apps\Crm\Entity\CrmProspectType;
use Apps\Crm\Entity\CrmProspect;
use Apps\Crm\Entity\CrmBusinessType;
use Apps\Crm\Entity\CrmBusinessStatut;
use Apps\Crm\Widget\CrmProspectForm\CrmProspectForm;

/*
 * 
 */

class AdminController extends Controller {

    /**
     * Constructeur
     */
    function _construct($core = "") {
        $this->Core = $core;
    }

    /**
     * Create
     */
    function Create() {
        
    }

    /**
     * Init
     */
    function Init() {
        
    }

    /**
     * Render
     */
    function Show($all = true) {
        return $this->Index();
    }

    /*
     * Get the home page
     */

    function Index() {
        $view = new View(__DIR__ . "/View/index.tpl", $this->Core);

        $tabCrm = new TabStrip("tabCrm", "Crm");
        $tabCrm->AddTab($this->Core->GetCode("Crm.Configuration"), $this->GetTabConfig());
        $tabCrm->AddTab($this->Core->GetCode("Crm.Prospect"), $this->GetTabProspect(), true);
       
        $view->AddElement(new ElementView("tabCrm", $tabCrm->Render()));

        return $view->Render();
    }

    /*     * **
     * Configuration
     */

    function GetTabConfig() {

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

        //ProsoectType
        $gdProspectType = new EntityGrid("gdProspectType", $this->Core);
        $gdProspectType->Entity = "Apps\Crm\Entity\CrmProspectType";
        $gdProspectType->App = "Crm";
        $gdProspectType->Action = "GetTabProspect";

        $btnAdd = new Button(BUTTON, "btnAddProspectType");
        $btnAdd->Value = $this->Core->GetCode("Crm.AddProspectType");

        $gdProspectType->AddButton($btnAdd);
        $gdProspectType->AddColumn(new EntityColumn("Name", "Name"));
        $gdProspectType->AddColumn(new EntityIconColumn("Action",
                        array(array("EditIcone", "Crm.EditProspectType", "Crm.EditProspectType"),
                    array("DeleteIcone", "Crm.DeleteProspectType", "Crm.DeleteProspectType")
        )));

        $view->AddElement(new ElementView("gdProspectType", $gdProspectType->Render()));

        //BusinessType
        $gdBusinessType = new EntityGrid("gdBusinessType", $this->Core);
        $gdBusinessType->Entity = "Apps\Crm\Entity\CrmBusinessType";
        $gdBusinessType->App = "Crm";
        $gdBusinessType->Action = "GetTabProspect";

        $btnAdd = new Button(BUTTON, "btnAddBusinessType");
        $btnAdd->Value = $this->Core->GetCode("Crm.AddBusinessType");

        $gdBusinessType->AddButton($btnAdd);
        $gdBusinessType->AddColumn(new EntityColumn("Name", "Name"));
        $gdBusinessType->AddColumn(new EntityIconColumn("Action",
                        array(array("EditIcone", "Crm.EditBusinessType", "Crm.EditBusinessType"),
                    array("DeleteIcone", "Crm.DeleteBusinessType", "Crm.DeleteBusinessType")
        )));

        $view->AddElement(new ElementView("gdBusinessType", $gdBusinessType->Render()));

        //BusinessStatus
        $gdBusinessStatut = new EntityGrid("gdBusinessStatut", $this->Core);
        $gdBusinessStatut->Entity = "Apps\Crm\Entity\CrmBusinessStatut";
        $gdBusinessStatut->App = "Crm";
        $gdBusinessStatut->Action = "GetTabProspect";

        $btnAdd = new Button(BUTTON, "btnAddBusinessStatut");
        $btnAdd->Value = $this->Core->GetCode("Crm.AddBusinessStatut");

        $gdBusinessStatut->AddButton($btnAdd);
        $gdBusinessStatut->AddColumn(new EntityColumn("Name", "Name"));
        $gdBusinessStatut->AddColumn(new EntityIconColumn("Action",
                        array(array("EditIcone", "Crm.EditBusinessStatut", "Crm.EditBusinessStatut"),
                    array("DeleteIcone", "Crm.DeleteBusinessStatut", "Crm.DeleteBusinessStatut")
        )));

        $view->AddElement(new ElementView("gdBusinessStatut", $gdBusinessStatut->Render()));
        
        $plugins = \Apps\EeApp\Helper\AppHelper::GetPluginApp($this->Core, "Crm");
        
        $view->AddElement(new ElementView("Plugins", $plugins));
        

        return $view->Render();
    }

    /*     * *
     * Save un type de prospect
     */

    function SaveProspectType($data) {

        $prospectType = new CrmProspectType($this->Core);
        $prospectType->Name->Value = $data["Name"];
        $prospectType->Save();

        return true;
    }

    /*     * *
     * Delete un type de prospect
     */

    function DeleteProspectType($prospectTypeId) {
        $prospectType = new CrmProspectType($this->Core);
        $prospectType->GetById($prospectTypeId);
        $prospectType->Delete();

        return true;
    }

    /*     * *
     * Save un type de businness
     */

    function SaveBusinessType($data) {

        $prospectType = new CrmBusinessType($this->Core);
        $prospectType->Name->Value = $data["Name"];
        $prospectType->Save();

        return true;
    }

    /*     * *
     * Delete un type de business
     */

    function DeleteBusinessType($businessTypeId) {
        $businessType = new CrmBusinessType($this->Core);
        $businessType->GetById($businessTypeId);
        $businessType->Delete();

        return true;
    }

    /*     * *
     * Delete un type de business
     */

    function DeleteBusinessStatut($businessStatutId) {
        $businessStatut = new CrmBusinessStatut($this->Core);
        $businessStatut->GetById($businessStatutId);
        $businessStatut->Delete();

        return true;
    }

    /*     * *
     * Save un status de businness
     */

    function SaveBusinessStatut($data) {

        $prospectType = new CrmBusinessStatut($this->Core);
        $prospectType->Name->Value = $data["Name"];
        $prospectType->Save();

        return true;
    }

    /*     * *
     * Prospect of the Crm
     */

    function GetTabProspect() {

        $gdProspect = new EntityGrid("gdProspect", $this->Core);
        $gdProspect->Entity = "Apps\Crm\Entity\CrmProspect";
        $gdProspect->AddOrder("Id Desc");
        $gdProspect->App = "Crm";
        $gdProspect->Action = "GetTabProspect";

        $btnAdd = new Button(BUTTON, "btnAddProspect");
        $btnAdd->Value = $this->Core->GetCode("Crm.AddProspect");

        $gdProspect->AddButton($btnAdd);
        $gdProspect->AddColumn(new EntityColumn("FirstName", "FirstName"));
        $gdProspect->AddColumn(new EntityColumn("LastName", "LastName"));
        $gdProspect->AddColumn(new EntityColumn("Email", "Email"));
        $gdProspect->AddColumn(new EntityColumn("Phone", "Phone"));
        $gdProspect->AddColumn(new EntityColumn("Type", "Type"));
        $gdProspect->AddColumn(new EntityColumn("Description", "Description"));
        $gdProspect->AddColumn(new EntityFunctionColumn("Demande de rappel", "GetCalledBack"));

        
        $gdProspect->AddColumn(new EntityIconColumn("Action",
                        array(array("EditIcone", "Crm.EditProspect", "Crm.EditProspect"),
                    array("DeleteIcone", "Crm.DeleteProspect", "Crm.DeleteProspect"),
                        )
        ));

        return $gdProspect->Render();
    }

    /*     * *
     * Save A prospect
     */

    function SaveProspect($data) {

        $prospectForm = new CrmProspectForm($this->Core, $data);

        if ($prospectForm->Validate($data)) {

            $prospect = new CrmProspect($this->Core);

            if ($data["Id"] != "") {
                $prospect->GetById($data["Id"]);
            }

            $prospectForm->Populate($prospect);
            $prospect->Save();

            return true;
        } else {
            return $prospectForm->Error();
        }
    }

    /*     * *
     * Delete a prospect
     */

    function DeleteProspect($prospectId) {
        $prospect = new CrmProspect($this->Core);

        $prospect->GetById($data["Id"]);
        $prospect->Delete();

        return true;
    }

    /* action */
}

?>