<?php

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

namespace Apps\Crm;

use Core\Core\Core;
use Apps\Base\Base;
use Core\Core\Request;
use Core\Core\Response;
use Apps\Crm\Module\Admin\AdminController;

class Crm extends Base {

    /**
     * Author and version
     * */
    public $Author = 'Webemyos';
    public $Version = '1.0.0';

    /**
     * Constructeur
     * */
    function __construct() {
        $this->Core = Core::getInstance();
        parent::__construct($this->Core, "Crm");
    }

    /**
     * Set public route
     */
    function GetRoute($routes = "") {
        $this->Route->SetPublic(array());

        return $this->Route;
    }

    /*     * *
     * Execute action after install
     */

    function PostInstall() {
        \Apps\EeApp\Helper\AppHelper::UpdateAppParams($this->Core,
                "Crm",
                $this->Version,
                "", // Add Description of the app
                0, // Set 1 if the App have a AdminWidgetDashBoard
                0  // Set 1 if the App have a Member Module
        );
    }

    /**
     * Start Admin
     */
    function Run() {
        echo parent::RunApp($this->Core, "Crm", "Crm");
    }

    /*     * *
     * Save A prospect Type
     */

    function SaveProspectType() {
        $adminController = new AdminController($this->Core);
        $result = $adminController->SaveProspectType(Request::GetPosts());

        if ($result === true) {
            $adminController = new AdminController($this->Core);
            return Response::Success($adminController->GetTabConfig());
        } else {
            return Response::Error();
        }
    }

    /*     * *
     * Delete A prospect Type
     */

    function DeleteProspectType() {
        $adminController = new AdminController($this->Core);
        $result = $adminController->DeleteProspectType(Request::GetPost("TypeId"));

        if ($result === true) {
            $adminController = new AdminController($this->Core);
            return Response::Success($adminController->GetTabConfig());
        } else {
            return Response::Error();
        }
    }

    /*     * *
     * Save A businness Type
     */

    function SaveBusinessType() {
        $adminController = new AdminController($this->Core);
        $result = $adminController->SaveBusinessType(Request::GetPosts());

        if ($result === true) {
            $adminController = new AdminController($this->Core);
            return Response::Success($adminController->GetTabConfig());
        } else {
            return Response::Error();
        }
    }

    /*     * *
     * Delete A business Type
     */

    function DeleteBusinessType() {
        $adminController = new AdminController($this->Core);
        $result = $adminController->DeleteBusinessType(Request::GetPost("TypeId"));

        if ($result === true) {
            $adminController = new AdminController($this->Core);
            return Response::Success($adminController->GetTabConfig());
        } else {
            return Response::Error();
        }
    }

    /*     * *
     * Save A businness Statut
     */

    function SaveBusinessStatut() {
        $adminController = new AdminController($this->Core);
        $result = $adminController->SaveBusinessStatut(Request::GetPosts());

        if ($result === true) {
            $adminController = new AdminController($this->Core);
            return Response::Success($adminController->GetTabConfig());
        } else {
            return Response::Error();
        }
    }

    /*     * *
     * Delete A business Status
     */

    function DeleteBusinessStatut() {
        $adminController = new AdminController($this->Core);
        $result = $adminController->DeleteBusinessStatut(Request::GetPost("TypeId"));

        if ($result === true) {
            $adminController = new AdminController($this->Core);
            return Response::Success($adminController->GetTabConfig());
        } else {
            return Response::Error();
        }
    }

    /*     * *
     * Add/Update a prospect
     */

    function SaveProspect() {

        $adminController = new AdminController($this->Core);
        $result = $adminController->SaveProspect(Request::GetPosts());

        if ($result === true) {
            $adminController = new AdminController($this->Core);
            return Response::Success($adminController->GetTabProspect());
        } else {
            return Response::Error();
        }
    }

    /*     * *
     * Supprime un prospect
     */

    function DeleteProspect() {
        $adminController = new AdminController($this->Core);
        $result = $adminController->DeleteProspect(Request::GetPost("ProspectId"));

        if ($result === true) {
            $adminController = new AdminController($this->Core);
            return Response::Success($adminController->GetTabProspect());
        } else {
            return Response::Error();
        }
    }

    /*     * *
     * Run member Controller
     * 
     * /
      /*function RunMember(){
      $memberController = new MemberController($this->Core);
      return $memberController->Index();
      } */

    /**
     * Get The siteMap 
     */
    /* public function GetSiteMap(){
      return SitemapHelper::GetSiteMap($this->Core);
      } */

    /*     * *
     * Get the widget
     */
    public function GetWidget($type = "", $params = "") {

        //TODO VERIFIER QUE le PLUGIN EXISTE
        switch ($type) {
            case "LeadForm" :
                $widget = new \Plugins\ImmoLeadPlugin\ImmoLeadPlugin($this->Core);
                break;
        }

        return $widget->Render($params);
    }

    /*     * *
     * Get Forum Addable widget
     */

    public function GetListWidget() {
        
        $plugins = \Apps\EeApp\Helper\AppHelper::GetPluginApp($this->Core, "Crm");
    
        $cmsPlugin = []; 
        
        foreach($plugins as $crmPlugin){
            
            $pluginPath = "\\Plugins\\" .$crmPlugin->PluginName->Value . "\\".$crmPlugin->PluginName->Value;
            $plugin = new $pluginPath($this->Core);
            
            $cmsPlugin[] = $plugin->GetConfigCms();
        }
        
        return $cmsPlugin;
    }
}
