<?php

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

class MY_FrontendController extends CI_Controller
{
    public $_data = [];
    public $data = [];

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
    }

    private function buildNav()
    {
        $defaultController = str_replace('/frontpage', "", $this->router->default_controller);
        $this->load->model('Landingmain_Model');
        $this->load->model('Landingsubmenu_Model');
        $landingmain = $this->Landingmain_Model->get();
        $menuHTML = "";
        $mobileHTML = "";
        $z_index_indicator = 1000;
        $zz_index_indicator = 10000;
        $odd_indicator = true;
        $even_indicator = false;
        $z_odd_indicator = true;
        $z_even_indicator = false;

        foreach ($landingmain as $mainItem) {
            $main = (array) $mainItem;
            $submenu = $this->Landingsubmenu_Model->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'];
                $menuHTML .= sprintf('<li class="nav-item"><a class="nav-link text-right" id="%s" href="%s">%s</a>', $main['uri'] ,$uri, $item);
                if ($odd_indicator == true) {
                    $mobileHTML .= sprintf('<li class="nav-item %s" style="z-index:%s"><a class="nav-link" href="%s">%s</a></li>', "odd", $z_index_indicator, $uri, $item);
                    $z_index_indicator--;
                    $even_indicator = true;
                    $odd_indicator = false;
                } else {
                    $mobileHTML .= sprintf('<li class="nav-item %s" style="z-index:%s"><a class="nav-link" href="%s">%s</a></li>', "even", $z_index_indicator, $uri, $item);
                    $z_index_indicator--;
                    $odd_indicator = true;
                    $even_indicator = false;
                }
            } 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'];
                    if ($z_odd_indicator == true) {
                        $submenuHTML .= sprintf('<li class="nav-item %s" style="z-index:%s"><a  href="%s">%s</a></li>', "odd", $zz_index_indicator, $uri, $item);
                        $z_index_indicator--;
                        $zz_index_indicator--;
                        $z_even_indicator = true;
                        $z_odd_indicator = false;
                    } else {
                        $submenuHTML .= sprintf('<li class="nav-item %s" style="z-index:%s"><a  href="%s">%s</a></li>', "even", $zz_index_indicator, $uri, $item);
                        $zz_index_indicator--;
                        $z_odd_indicator = true;
                        $z_even_indicator = false;
                    }
                }
                //$active = in_array($this->data['page'], $all) ? ' active' : '';
                $menuHTML .= '<li class="nav-item dropdown">' .
                    '<a class="dropdown-toggle text-right' .  '" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">' .
                    '<span class="nav-label">' . $main['title'] . '</span> <span class="caret"></span>' .
                    '</a>' .
                    '<ul class="dropdown-menu">' . $submenuHTML . '</ul>' .
                    '</li>';
                if ($odd_indicator == true) {
                    $mobileHTML .= '<li class="nav-item dropdown odd" style="z-index:' . $z_index_indicator . '">' .
                        '<a class="dropdown-toggle' .  '" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">' .
                        '<span class="nav-label">' . $main['title'] . '</span> <span class="caret"></span>' .
                        '</a>' .
                        '<ul class="dropdown-menu">' . $submenuHTML . '</ul>' .
                        '</li>';
                    $z_index_indicator--;
                    $even_indicator = true;
                    $odd_indicator = false;
                } else {
                    $mobileHTML .= '<li class="nav-item dropdown even" style="z-index:' . $z_index_indicator . '">' .
                        '<a class="dropdown-toggle' .  '" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">' .
                        '<span class="nav-label">' . $main['title'] . '</span> <span class="caret"></span>' .
                        '</a>' .
                        '<ul class="dropdown-menu">' . $submenuHTML . '</ul>' .
                        '</li>';
                    $z_index_indicator--;
                    $even_indicator = false;
                    $odd_indicator = true;
                }
            }
        }
        return [
            'desktop' => $menuHTML,
            'mobile' => $mobileHTML,
        ];
    }

    private function buildFooter()
    {
        $this->load->model("Footer_management_Model");
        $D_hyperlink = "";
        $M_hyperlink = "";
        $D_Social = "";
        $M_Social = "";
        $footer = $this->Footer_management_Model->get_front();
        foreach ($footer['text'] as $text) {
            // var_dump($text);
            $D_hyperlink .= '<a class="footer_col" href="'.$text['link'].'">' . $text['title'] . '</a>';
            $M_hyperlink .= '<a class="footer_col" href="'.$text['link'].'">' . $text['title'] . '</a>';
        }
        foreach ($footer['image'] as $image) {
            //  var_dump($image);
            $D_Social .= '<a href="'.$image['link'].'"><img src="' . base_url($image['path']) . '" alt=""></a>';
            $M_Social .= '<a href="'.$image['link'].'"><img src="' . base_url($image['path']) . '" alt=""></a>';
        }

        return [
            "D_hyperlink" => $D_hyperlink,
            "M_hyperlink" => $M_hyperlink,
            "D_Social" => $D_Social,
            "M_Social" => $M_Social
        ];
    }

    protected function view($template, $data, $toString = false)
    {
        $this->_data['_controller'] = $this->router->fetch_class();
        $this->_data['_view'] = $template;
        $this->_data['_nav'] = $this->buildNav();
        $this->_data['_footer'] = $this->buildFooter();
        
        return $this->load->view('front/layouts/main', array_merge($this->_data, $data), $toString);
    }
}
