<?php
/**
* 2024 Anvanto
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
*
*  @author    Anvanto <anvantoco@gmail.com>
*  @copyright 2024 Anvanto
*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*/

if (!defined('_PS_VERSION_')) {
    exit;
}

class AdminAnsizeguideSettingsController extends ModuleAdminController
{
    protected $_module = null;

    public function __construct()
    {
        $this->bootstrap = true;
        $this->display = 'view';
 
        $this->name = 'AdminAnsizeguideSettingsController';
        
        parent::__construct();
    }

    public function l($string, $class = null, $addslashes = false, $htmlentities = true)
    {
        return parent::l($string, pathinfo(__FILE__, PATHINFO_FILENAME), $addslashes, $htmlentities);
    }

    public function initToolBarTitle()
    {
        $this->toolbar_title[] = $this->l('Size Guide: Settings');
    }

    public function initHeader()
    {
        parent::initHeader();

        $id_lang = $this->context->language->id;
        $link = $this->context->link;
        $tabs = &$this->context->smarty->tpl_vars['tabs']->value;
       
        foreach ($tabs as &$tab0) {
            if ($tab0['class_name'] == 'IMPROVE') {
                foreach ($tab0['sub_tabs'] as &$tab1) {
                    if ($tab1['class_name'] == 'AdminParentModulesSf') {
                        
                        foreach ($tab1['sub_tabs'] as &$tab2) {
                            if ($tab2['class_name'] == 'AdminAnsizeguideSettings') {
                                $sub_tabs = &$tab2['sub_tabs'];

                                $tab = Tab::getTab($id_lang, Tab::getIdFromClassName('AdminAnsizeguideWidgets'));
                                $tab['current'] = false;
                                $tab['href'] = $link->getAdminLink('AdminAnsizeguideWidgets');
                                $tab['active'] = true;
                                $tab['name'] = $this->l('Size Guide');
                                $sub_tabs[] = $tab;

                                $tab = Tab::getTab($id_lang, Tab::getIdFromClassName('AdminAnsizeguideStatistics'));
                                $tab['current'] = false;
                                $tab['href'] = $link->getAdminLink('AdminAnsizeguideStatistics');
                                $tab['active'] = true;
                                $tab['name'] = $this->l('Statistics');
                                $sub_tabs[] = $tab;

                                $tab = Tab::getTab($id_lang, Tab::getIdFromClassName('AdminAnsizeguideSettings'));
                                $tab['current'] = true;
                                $tab['href'] = $link->getAdminLink('AdminAnsizeguideSettings');
                                $tab['active'] = true;
                                $sub_tabs[] = $tab;
                                
                                break;
                            }
                        }
                    //    break;
                    }
                }
              //  break;
            }
        }
    }  
    
    public function initContent()
    {
        $this->context->smarty->assign('current_tab_level', 3);
        return parent::initContent();
    } 
    /**
     * Create the structure of your form.
     */
    protected function getSettingsForm()
    {
        $form['form']['legend'] = [
            'title' => $this->l('Settings'),
        ];

        $form['form']['submit'] = [
            'name' => 'save',
            'title' => $this->l('Save'),
        ];

        $form['form']['input'][] = [
            'type' => 'switch',
            'name' => an_sizeguide::PREFIX . 'recording_statistics',
            'label' => $this->l('Recording statistics'),
            'values' => [
                [
                    'id' => 'active_on',
                    'value' => 1,
                    'label' => $this->l('Enabled')
                ],
                [
                    'id' => 'active_off',
                    'value' => 0,
                    'label' => $this->l('Disabled')
                ]
            ],
        ];

        return $form;
    }

    public function renderView()
    {
        $languages = $this->context->controller->getLanguages();
  
        $helper = new HelperForm();
        $helper->show_toolbar = false;
        $helper->name_controller = $this->name;
        $helper->submit_action = $this->name;
        $helper->currentIndex = $this->context->link->getAdminLink('AdminAnsizeguideSettings', false);
        $helper->token = Tools::getAdminTokenLite('AdminAnsizeguideSettings');
        $helper->default_form_language = $this->context->language->id;
        $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
        $helper->tpl_vars = [
            'uri' => $this->module->getPathUri(),
            'languages' => $languages,
            'id_language' => $this->context->language->id
        ];

        $form = $this->getSettingsForm();
       
        foreach ($form['form']['input'] as $input){
                $helper->tpl_vars['fields_value'][$input['name']] = Configuration::get($input['name']);
        }
            
        return $helper->generateForm([$form]). $this->module->topPromo();        
    }

    public function postProcess()
    {    
        $form = $this->getSettingsForm();
        
        if (Tools::isSubmit($form['form']['submit']['name'])) {
            
            if (_PS_MODE_DEMO_) {
                $this->errors[] = $this->module->demoModeMessage;
                return false;
            }     

            foreach ($form['form']['input'] as $input){

                $html = false;
                if (isset($input['html']) && $input['html']){
                    $html = true;
                }
                Configuration::updateValue($input['name'], Tools::getValue($input['name']), $html);
            }

            $currentIndex = $this->context->link->getAdminLink('AdminAnsizeguideSettings', false);
            $token = Tools::getAdminTokenLite('AdminAnsizeguideSettings');   

            Tools::redirectAdmin($currentIndex . '&token=' . $token . '&conf=4');
        }
        return true;
    }
}
