<?php

if (!defined('_PS_VERSION_')) {

    exit;

}



class CustomColors extends Module

{
    protected $_html = '';

    public function __construct()

    {

        $this->name = 'customcolors';

        $this->tab = 'administration';

        $this->version = '1.0.0';

        $this->author = 'gabiirese';
        $this->need_instance = 0;



        parent::__construct();



        $this->displayName = $this->l('Gabiirese Custom Backoffice Colors');
        $this->description = $this->l('Modulo gratuito de gabiirese para personalizar los colores del Backoffice de PrestaShop.');


        $this->ps_versions_compliancy = array('min' => '1.7.8.0', 'max' => '9.1.99');
    }



    public function install()

    {

        return parent::install() && $this->registerHook('displayBackOfficeHeader') && $this->installModuleSettings();

    }



    public function uninstall()

    {

        return parent::uninstall() && $this->deleteModuleSettings();
    }



    /**

     * Instalar valores predeterminados de configuración

     */

    private function installModuleSettings()

    {

        // Establecer valores predeterminados para los colores

        Configuration::updateValue('CUSTOMCOLOR_BACKGROUND', '#0e0f11');

        Configuration::updateValue('CUSTOMCOLOR_BORDER', '#dbe6e9');

        Configuration::updateValue('CUSTOMCOLOR_TEXT', '#f0f8ff');
        Configuration::updateValue('CUSTOMCOLOR_NAV', '#363a41');

        Configuration::updateValue('CUSTOMCOLOR_NAVTEXT', '#d0ff71');

        Configuration::updateValue('CUSTOMCOLOR_HEAD', '#131a29');

        Configuration::updateValue('CUSTOMCOLOR_FOOTER', '#131a29');

        return true;

    }



    /**
     * Eliminar valores de configuracion al desinstalar.
     */
    private function deleteModuleSettings()
    {
        Configuration::deleteByName('CUSTOMCOLOR_BACKGROUND');
        Configuration::deleteByName('CUSTOMCOLOR_BORDER');
        Configuration::deleteByName('CUSTOMCOLOR_TEXT');
        Configuration::deleteByName('CUSTOMCOLOR_NAV');
        Configuration::deleteByName('CUSTOMCOLOR_NAVTEXT');
        Configuration::deleteByName('CUSTOMCOLOR_HEAD');
        Configuration::deleteByName('CUSTOMCOLOR_FOOTER');
        return true;
    }



    public function getContent()
    {

        $this->_html = '<div style="display:flex;align-items:center;gap:16px;margin:0 0 24px;">
            <img src="' . $this->_path . 'assets/gabiirese_v5.png" alt="gabiirese" style="width:76px;height:auto;border-radius:8px;" />
            <div>
                <h2 style="margin:0;">' . $this->displayName . '</h2>
                <p style="margin:4px 0 0;color:#6c868e;">infinit8.es - modulo creado por gabiirese</p>
            </div>
        </div>';

        if (Tools::isSubmit('submit_customcolors')) {
            // Guardar los valores del formulario

            $background = Tools::getValue('CUSTOMCOLOR_BACKGROUND');

            $border = Tools::getValue('CUSTOMCOLOR_BORDER');

            $text = Tools::getValue('CUSTOMCOLOR_TEXT');

            $nav = Tools::getValue('CUSTOMCOLOR_NAV');

            $navtext = Tools::getValue('CUSTOMCOLOR_NAVTEXT');

            $head = Tools::getValue('CUSTOMCOLOR_HEAD');

            $footer = Tools::getValue('CUSTOMCOLOR_FOOTER');



            // Guardar configuración en la base de datos

            Configuration::updateValue('CUSTOMCOLOR_BACKGROUND', $background);

            Configuration::updateValue('CUSTOMCOLOR_BORDER', $border);

            Configuration::updateValue('CUSTOMCOLOR_TEXT', $text);

            Configuration::updateValue('CUSTOMCOLOR_NAV', $nav);

            Configuration::updateValue('CUSTOMCOLOR_NAVTEXT', $navtext);

            Configuration::updateValue('CUSTOMCOLOR_HEAD', $head);

            Configuration::updateValue('CUSTOMCOLOR_FOOTER', $footer);



            // Mostrar un mensaje de éxito

            $this->_html .= $this->displayConfirmation($this->l('Colores guardados correctamente.'));

        }



        // Mostrar el formulario de configuración

        $this->_html .= $this->renderForm();

        return $this->_html;

    }



    /**

     * Mostrar el formulario de configuración en el Backoffice

     */

    private function renderForm()

    {

        $fields_form = array(

            'form' => array(

                'legend' => array(

                    'title' => $this->l('Configuración de colores'),

                    'icon' => 'icon-paint-brush',

                    'style' => 'margin-bottom: 50px;'

                ),

                'input' => array(

                    array(

                        'type' => 'color',

                        'label' => $this->l('Fondo de la tarjeta'),

                        'name' => 'CUSTOMCOLOR_BACKGROUND',

                        'value' => Configuration::get('CUSTOMCOLOR_BACKGROUND'),

                        'desc' => $this->l('El color de fondo de las tarjetas en el Backoffice.')

                    ),

                    array(

                        'type' => 'color',

                        'label' => $this->l('Color del borde'),

                        'name' => 'CUSTOMCOLOR_BORDER',

                        'value' => Configuration::get('CUSTOMCOLOR_BORDER'),

                        'desc' => $this->l('El color del borde de las tarjetas y elementos.')

                    ),

                    array(

                        'type' => 'color',

                        'label' => $this->l('Color del texto'),

                        'name' => 'CUSTOMCOLOR_TEXT',

                        'value' => Configuration::get('CUSTOMCOLOR_TEXT'),

                        'desc' => $this->l('El color del texto en el Backoffice.')

                    ),

                    array(

                        'type' => 'color',

                        'label' => $this->l('Color de la barra de navegación'),

                        'name' => 'CUSTOMCOLOR_NAV',

                        'value' => Configuration::get('CUSTOMCOLOR_NAV'),

                        'desc' => $this->l('El color de la barra de navegación del Backoffice.')

                    ),

                    array(

                        'type' => 'color',

                        'label' => $this->l('Color del texto de navegación'),

                        'name' => 'CUSTOMCOLOR_NAVTEXT',

                        'value' => Configuration::get('CUSTOMCOLOR_NAVTEXT'),

                        'desc' => $this->l('El color del texto de navegación del Backoffice.')

                    ),

                    array(

                        'type' => 'color',

                        'label' => $this->l('Color de la tarjeta cabecero'),

                        'name' => 'CUSTOMCOLOR_HEAD',

                        'value' => Configuration::get('CUSTOMCOLOR_HEAD'),

                        'desc' => $this->l('El color del head de la tarjeta.')

                    ),

                    array(

                        'type' => 'color',

                        'label' => $this->l('Color de la tarjeta footer'),

                        'name' => 'CUSTOMCOLOR_FOOTER',

                        'value' => Configuration::get('CUSTOMCOLOR_FOOTER'),

                        'desc' => $this->l('El color del footer de la tarjeta.')

                    ),

                ),

                'submit' => array(

                    'title' => $this->l('Guardar'),

                    'name' => 'submit_customcolors',

                ),

            ),

        );



        $helper = new HelperForm();

        $helper->show_toolbar = false;

        $helper->table = $this->table;

        $helper->identifier = $this->identifier;

        $helper->submit_action = 'submit_customcolors';

        $helper->fields_value['CUSTOMCOLOR_BACKGROUND'] = Configuration::get('CUSTOMCOLOR_BACKGROUND');

        $helper->fields_value['CUSTOMCOLOR_BORDER'] = Configuration::get('CUSTOMCOLOR_BORDER');

        $helper->fields_value['CUSTOMCOLOR_TEXT'] = Configuration::get('CUSTOMCOLOR_TEXT');

        $helper->fields_value['CUSTOMCOLOR_NAV'] = Configuration::get('CUSTOMCOLOR_NAV');

        $helper->fields_value['CUSTOMCOLOR_NAVTEXT'] = Configuration::get('CUSTOMCOLOR_NAVTEXT');

        $helper->fields_value['CUSTOMCOLOR_HEAD'] = Configuration::get('CUSTOMCOLOR_HEAD');

        $helper->fields_value['CUSTOMCOLOR_FOOTER'] = Configuration::get('CUSTOMCOLOR_FOOTER');



        return $helper->generateForm(array($fields_form));

    }



    public function hookDisplayBackOfficeHeader()

    {

        // Obtener los colores desde la configuración

        $background = Configuration::get('CUSTOMCOLOR_BACKGROUND');

        $border = Configuration::get('CUSTOMCOLOR_BORDER');

        $text = Configuration::get('CUSTOMCOLOR_TEXT');

        $nav = Configuration::get('CUSTOMCOLOR_NAV');

        $navtext = Configuration::get('CUSTOMCOLOR_NAVTEXT');

        $head = Configuration::get('CUSTOMCOLOR_HEAD');

        $footer = Configuration::get('CUSTOMCOLOR_FOOTER');

    

        // Crear el contenido CSS

        $css = "

        /* Variables de color */

        :root {

            --background: $background;

            --border: $border;

            --text: $text;

            --navtext: $navtext;

            --nav: $nav;

            --head: $head;

            --footer: $footer;

            --btn-primary-bg: #d0ff71;

            --btn-primary-border: #0e0f11;

            --btn-outline-secondary-border: #0e0f11;

            --btn-outline-secondary-bg: #d0ff71;

            --menu-bg: #22252a;

            --link-color: #d0ff71;

            --nav-bar-bg: #0e0f11;

            --card-header-color: #70b580;

        }

    

        /* Estilos generales */

        .card,

        .module-addons-item-grid .module-addons-item-wrapper-grid,

        .module-item-wrapper-grid,

        .module-placeholders-wrapper .timeline-item-wrapper {

            background-color: var(--background) !important;

            border: 1px solid var(--border) !important;

            color: var(--text) !important;

        }

    

        /* Estilo para dispositivos más grandes */

        @media (min-width: 544px) {

            .form-horizontal .form-group.row > .form-control-label:first-of-type:not(.label-on-top) {

                color: var(--navtext) !important;

            }

        }

    

        /* Navegación */

        .nav-pills {

            border-top: solid 1px var(--navtext) !important;

            border-bottom: solid 1px #dfdfdf !important;

        }

    

        .nav-bar {

            background: var(--nav-bar-bg) !important;

        }

    

        .main-menu {

            padding: 0 0 10.825rem;

            margin: 0;

            background-color: var(--menu-bg) !important;

        }

    

        .menu-collapse {

            color: var(--link-color) !important;

            background-color: var(--menu-bg) !important;

        }

    

        .main-header #header-search-container .nav-bar .input-group::before,

        .nav-bar .main-header #header-search-container .input-group::before,

        .nav-bar .material-icons,

        .nav-bar .ps-tree-items .tree-name button::before,

        .ps-tree-items .tree-name .nav-bar button::before {

            color: var(--link-color) !important;

        }

    

        .main-menu .link-levelone>.link {

            color: var(--link-color) !important;

        }

    

        .main-menu .link-active.link-leveltwo>.link,

        .main-menu .link-hover.link-leveltwo>.link,

        .main-menu .link-levelone.link-active>.link,

        .main-menu .link-levelone.link-hover>.link,

        .main-menu .link-levelone.ul-open>.link,

        .main-menu .ul-open.link-leveltwo>.link {

            background: var(--menu-bg) !important;

        }

    

        /* Encabezados y pies */

        .page-head-tabs,

        body {

            background: var(--nav) !important;

        }

    

        .card-header {

            background-color: var(--head) !important;

            border-bottom: 1px solid #0e0f11;

        }

    

        .card-footer {

            background-color: var(--footer) !important;

        }

    

        /* Botones */

        .btn-primary {

            color: #0e0f11 !important;

            background-color: var(--btn-primary-bg) !important;

            border-color: var(--btn-primary-border) !important;

        }

    

        .btn-outline-secondary.disabled,

        .btn-outline-secondary:disabled {

            border-color: var(--btn-outline-secondary-border) !important;

        }

    

        .btn-outline-secondary {

            color: #172308;

            background-color: var(--btn-outline-secondary-bg);

            background-image: none;

            border-color: var(--btn-outline-secondary-border);

            border-radius: 50px;

        }

    

        /* Iconos y botones dentro de tarjetas */

        .card-header .main-header #header-search-container .input-group::before,

        .card-header .material-icons,

        .card-header .ps-tree-items .tree-name button::before,

        .main-header #header-search-container .card-header .input-group::before,

        .ps-tree-items .tree-name .card-header button::before {

            color: var(--card-header-color) !important;

        }

    ";

        return '<style id="gabiirese-customcolors-backoffice">' . $css . '</style>';
    }

    

}

