<?php

defined('BASEPATH') or exit('No direct script access allowed');

define('CLIENTS_AREA', true);

class FrontendController extends ClientsController
{
    public $use_banner = true;
    public $use_sidebar = false;
    public $use_breadcrumb = false;
    public $page_color = '#084c94';
    public $banner_desktop = '';
    public $banner_mobile = '';
    public $background_image = '';
    public $currentController;
    public $main_id = 0;
    public $submenu_id = 0;
    public $breadcrumb = [];
    public $css = [];
    public $js = [];

    public function __construct()
    {
        parent::__construct();
        $this->breadcrumb['home'] = [
            'name' => '主頁',
            'url' => base_url('/'),
        ];
    }

    private function _buildNatvigationMenu()
    {
        $defaultController = str_replace('/index', '', $this->router->default_controller);
        $lang = get_contact_language();
        $this->load->model('at-admin/Landingmain_Model', 'landingmain');
        $this->load->model('at-admin/Landingsubmenu_Model', 'landingsubmenu');
        $landingmain = $this->landingmain->get();
        $menuHTML = "";
        $mobileHTML = "";
        $lg = "";
        switch ($lang) {
            case "english":
                $lg = "_en";
                break;
            default:
                break;
        }
        foreach ($landingmain as $mainItem) {
            $main = (array) $mainItem;
            $submenu = $this->landingsubmenu->getbymainid($main['id']);
            if (count($submenu) == 0) {
                if ($main['uri'] === "") {
                    $main['uri'] = $defaultController;
                }
                $active = ($this->data['page'] == $main['uri']) ? ' active' : '';
                $uri = base_url($main['uri']);
                $item = $main['title' . $lg];
                $menuHTML .= sprintf('<li class="nav-item"><a class="nav-link text-right%s" href="%s">%s</a></li>', $active, $uri, $item);
                $mobileHTML .= sprintf('<li class="nav-item"><a class="nav-link%s" href="%s">%s</a></li>', $active, $uri, $item);
            }
            else {
                $submenuHTML = "";
                $all = [];
                foreach ($submenu as $subItem) {
                    $sub = (array) $subItem;
                    $all[] = $sub['uri'];
                    $active = ($this->data['page'] == $sub['uri']) ? ' active' : '';
                    $uri = base_url($main['uri'] . '/' . $sub['uri']);
                    $item = $sub['title' . $lg];
                    $submenuHTML .= sprintf('<li class="nav-item"><a%s href="%s">%s</a></li>', $active, $uri, $item);
                }
                $active = in_array($this->data['page'], $all) ? ' active' : '';
                $menuHTML .= '<li class="nav-item dropdown">' .
                    '<a class="dropdown-toggle text-right' . $active . '" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">' .
                    '<span class="nav-label">' . $main['title' . $lg] . '</span> <span class="caret"></span>' .
                    '</a>' .
                    '<ul class="dropdown-menu">' . $submenuHTML . '</ul>' .
                    '</li>';
                $mobileHTML .= '<li class="nav-item dropdown">' .
                    '<a class="dropdown-toggle' . $active . '" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">' .
                    '<span class="nav-label">' . $main['title' . $lg] . '</span> <span class="caret"></span>' .
                    '</a>' .
                    '<ul class="dropdown-menu">' . $submenuHTML . '</ul>' .
                    '</li>';
            }
        }
        return [
            'desktop' => $menuHTML,
            'mobile' => $mobileHTML,
        ];
    }

    private function _buildSidebar()
    {
        $this->load->model('at-admin/Landingmain_Model', 'landingmain');
        $this->load->model('at-admin/Landingsubmenu_Model', 'landingsubmenu');
        if ($this->main_id == 0) {
            $defaultController = str_replace('/index', '', $this->router->default_controller);
            $lang = get_contact_language();
            $landingmain = $this->landingmain->search_by_uri($this->currentController);
            $this->main_id = $landingmain['id'];
        }
        else {
            $landingmain = (array) $this->landingmain->get_id($this->main_id)[0];
        }
        $submenu = $this->landingsubmenu->getbymainid($this->main_id);
        if (count($submenu) == 0) {
//            return '<div class="sidebar-title active">' . $landingmain['title'] . '</div>' .
//                '<div class="leftmenuitems"></div>';
            return "";
        }
        $sidebarHTML = "";
        foreach ($submenu as $sub) {
            if ($this->submenu_id == $sub->id) {
                $sidebarHTML .= '<div class="sidebar-items active">' . $sub->title . '</div>';
            }
            else {
                $sidebarHTML .= '<div class="sidebar-items hover-enabled"><a href="' . base_url($landingmain['uri'] . '/' . $sub->uri) . '">' . $sub->title . '</a></div>';
            }
        }
        return $sidebarHTML;
    }

    public function layout($notInThemeViewFiles = false)
    {
        /**
         * Navigation and submenu
         * @deprecated 2.3.2
         * @var boolean
         */

        $this->data['use_navigation'] = $this->use_navigation == true;
        $this->data['use_submenu']    = $this->use_submenu == true;

        /**
         * @since  2.3.2 new variables
         * @var array
         */
        $this->data['navigationEnabled'] = $this->use_navigation == true;
        $this->data['subMenuEnabled']    = $this->use_submenu == true;

        $this->data['bannerEnabled']    = $this->use_banner == true;
        $this->data['sidebarEnabled']    = $this->use_sidebar == true;
        $this->data['breadcrumbEnabled']    = $this->use_breadcrumb == true;
        $this->data['footerEnabled']    = $this->use_footer == true;

        $this->data['pageColor'] = $this->page_color;
        $this->data['bannerDesktop'] = base_url($this->banner_desktop);
        $this->data['bannerMobile'] = base_url($this->banner_mobile);
        $this->data['backgroundImage'] = base_url($this->background_image);
        $this->data['hash'] = hash('md5', uniqid('', true));

        $this->data['css'] = $this->css;

        if ($this->data['navigationEnabled']) {
            $this->data['navigationMenu'] = $this->_buildNatvigationMenu();
        }

        if ($this->data['breadcrumbEnabled']) {
            $this->data['breadcrumb'] = $this->breadcrumb;
        }

        if ($this->data['sidebarEnabled']) {
            $this->data['sidebar'] = $this->_buildSidebar();
        }
        /**
         * Theme head file
         * @var string
         */
        $this->template['head'] = $this->load->view('themes/' . active_clients_theme() . '/views/front/includes/head', $this->data, true);

        $GLOBALS['customers_head'] = $this->template['head'];

        /**
         * Load the template view
         * @var string
         */
        $module                       = CI::$APP->router->fetch_module();
        $this->data['current_module'] = $module;
        $viewPath                     = !is_null($module) || $notInThemeViewFiles ?
            $this->view :
            $this->createThemeViewPath('front/' . $this->view);

        $this->template['view']    = $this->load->view($viewPath, $this->data, true);
        $GLOBALS['customers_view'] = $this->template['view'];

        /**
         * Theme footer
         * @var string
         */
        $this->template['footer'] = $this->use_footer == true
            ? $this->load->view('themes/' . active_clients_theme() . '/views/front/includes/footer', $this->data, true)
            : '';
        $GLOBALS['customers_footer'] = $this->template['footer'];

        /**
         * @deprecated 2.3.0
         * Theme scripts.php file is no longer used since vresion 2.3.0, add app_customers_footer() in themes/[theme]/index.php
         * @var string
         */
        $this->template['scripts'] = '';
        if (file_exists(VIEWPATH . 'themes/' . active_clients_theme() . '/scripts.php')) {
            if (ENVIRONMENT != 'production') {
                trigger_error(sprintf('%1$s', 'Clients area theme file scripts.php file is no longer used since version 2.3.0, add app_customers_footer() in themes/[theme]/index.php. You can check the original theme index.php for example.'));
            }

            $this->template['scripts'] = $this->load->view('themes/' . active_clients_theme() . '/scripts', $this->data, true);
        }

        $this->template['banner'] = '';
        if ($this->use_banner) {
            $this->template['banner'] = $this->load->view('themes/' . active_clients_theme() . '/views/front/includes/' . $this->view . '_banner', $this->data, true);
        }

        $this->template['js'] = $this->js;
        /**
         * Load the theme compiled template
         */
        $this->load->view('themes/' . active_clients_theme() . '/views/front/index', $this->template);
    }
}
