<?php
/**
 * @version     $Id$
 * @author      JoomlaUX
 * @package     Joomla.Site
 * @subpackage  mod_jux_easy_megamenu
 * @copyright   Copyright (C) 2008 - 2018 JoomlaUX. All rights reserved.
 * @license     License GNU General Public License version 2 or later; see LICENSE.txt, see LICENSE.php
 */

defined('_JEXEC') or die('Restricted access');

use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri; 
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Form\FormField; 
use Joomla\Registry\Registry;
use Joomla\CMS\Language\Text;

class JFormFieldMegamenu extends FormField {

    protected $type = 'Megamenu';

    protected function getInput() {
        require_once dirname(dirname(__FILE__)) . '/includes/menu/menu.php';

        $jdoc   = Factory::getDocument();
        $jinput = Factory::getApplication()->input;

        // Add StyleSheet
        $jdoc->addStyleSheet(Uri::root(true) . '/modules/mod_jux_easy_megamenu/elements/assets/css/admin-j30.css');
        // $jdoc->addStyleSheet(Uri::root(true) . '/media/plg_editors_codemirror/css/codemirror.min.css');
        $jdoc->addStyleSheet(Uri::root(true) . '/modules/mod_jux_easy_megamenu/assets/minicolors/css/jquery.minicolors.css');
        $jdoc->addStyleSheet(Uri::root(true) . '/modules/mod_jux_easy_megamenu/elements/assets/css/megamenu.css');
        $jdoc->addStyleSheet(Uri::root(true) . '/modules/mod_jux_easy_megamenu/elements/assets/css/jquery-ui.min.css');
        $jdoc->addStyleSheet(Uri::root(true) . '/modules/mod_jux_easy_megamenu/assets/font-awesome/css/font-awesome.css');
        $jdoc->addStyleSheet(Uri::root(true) . '/modules/mod_jux_easy_megamenu/assets/css/jux-rtl.css');

        // Add script
        HTMLHelper::_('jquery.framework');
        $jdoc->addScript(Uri::root(true) . '/modules/mod_jux_easy_megamenu/assets/bootstrap/js/bootstrap.js');
        // $jdoc->addScript(Uri::root(true) . '/media/plg_editors_codemirror/js/joomla-editor-codemirror.js');
        $jdoc->addScript(Uri::root(true) . '/modules/mod_jux_easy_megamenu/elements/assets/js/jquery-ui.min.js');
        $jdoc->addScript(Uri::root(true) . '/modules/mod_jux_easy_megamenu/elements/assets/js/codemirror.min.js');
        $jdoc->addScript(Uri::root(true) . '/modules/mod_jux_easy_megamenu/elements/assets/js/megamenu.js');
        $jdoc->addScript(Uri::root(true) . '/modules/mod_jux_easy_megamenu/elements/assets/js/updatemenu.js');
        $jdoc->addScript(Uri::root(true) . '/modules/mod_jux_easy_megamenu/assets/minicolors/js/jquery.minicolors.min.js');
        
        $jdoc->addScriptDeclaration('
			var ROOT_URL = \'' . Uri::root() . '\';
			var BASE_URL = \'' . Uri::root(true) . '/\';
			var MOD_ACTION =  \'' . Uri::root() . 'modules/mod_jux_easy_megamenu/includes/action.php\';
		');

        $params = new Registry();
        $params->loadObject($this->form->getValue('params'));

        $joomlaMenu = new JoomlaMenu($params['mod_menu'], $params);
        $options    = (array)json_decode($params->get('mega_options', ''), true);
        $menu_nav_class = '';
        $rtlClass = '';
        $mobileLayoutClass = '';
        if (isset($options[$params['mod_menu']])) {
            $option = $options[$params['mod_menu']];

            $rtlClass = $option['rtl'] == 1 ? ' rtl' : '';
            $menu_nav_class .= ' jux-' . $option['menu_orientation'];
            if ($option['menu_orientation'] == 'horizontal') {
              $menu_nav_class .= '-' . $option['sm_horizontal_drop'];
              $menu_align = isset($option['menubar_align']) ? $option['menubar_align'] : 'left';
              $menu_nav_align_class = 'jux-align-' . $menu_align;
            } else {
              $menu_nav_align_class = '';
            }

            $mobileLayout = isset($option['mobile_layout']) ? $option['mobile_layout'] : 1;
            $mobileLayoutClass = $mobileLayout == 2 ? 'jux-toggle-without-btn' : '';
        } else {
            $menu_nav_align_class = 'jux-align-left';
        }

        list($menu, $option) = $joomlaMenu->render();

        require_once dirname(dirname(__FILE__)).'/includes/preset.php';
        global $preset;

        $db = Factory::getDbo();
        $query = $db->getQuery(true);
        $query->select('id, title, module, position');
        $query->from('#__modules');
        $query->where('published = 1');
        $query->where('client_id = 0');
        $query->order('title');
        $db->setQuery($query);
        $modules = $db->loadObjectList();
        $current_mod_id = $jinput->get('id', '', 'INT');
        $html = '<div id="jux-admin-megamenu" class="jux-admin-megamenu jux-ez-menu-item-options">
      <div class="admin-inline-toolbox clearfix">
        <div class="jux-admin-mm-row clearfix">

          <div id="jux-admin-mm-intro" class="pull-left">
            <h3>' . Text::_('JUX_NAVIGATION_MM_TOOLBOX') . '</h3>
            <p>' . Text::_('JUX_NAVIGATION_MM_TOOLBOX_DESC') . '</p>
          </div>

          <div id="jux-admin-mm-tb">
            <div id="jux-admin-mm-toolitem" class="admin-toolbox">
              <h3>' . Text::_('JUX_NAVIGATION_MM_ITEM_CONF') . '</h3>
                <ul>
                    <li>
                        <label class="hasTip" title="' . Text::_('JUX_NAVIGATION_MM_DISPLAY') . '::' . Text::_('JUX_NAVIGATION_MM_DISPLAY_DESC') . '">' . Text::_('JUX_NAVIGATION_MM_DISPLAY') . '</label>

                        <div class="btn-group btn-group-yesno radio toolitem-display">
                            <input class="btn-check toolbox-toggle" type="radio" id="toggleDisplay1" data-action="toggleDisplay" name="toggleDisplay" value="1">
                            <label for="toggleDisplay1" class="btn btn-outline-success">' . Text::_('JYES') . ' </label>
                            <input class="btn-check toolbox-toggle" type="radio" id="toggleDisplay0" data-action="toggleDisplay" name="toggleDisplay" value="0">
                            <label for="toggleDisplay0" class="btn btn-outline-danger">
                            ' . Text::_('JNO') . ' </label>
                        </div>
                    </li>
                </ul>

                <ul>
                    <li>
                      <label class="hasTip" title="' . Text::_('JUX_NAVIGATION_MM_TITLE') . '::' . Text::_('JUX_NAVIGATION_MM_TITLE_DESC') . '">' . Text::_('JUX_NAVIGATION_MM_TITLE') . '</label>
                        <input type="text" class="input-medium toolitem-title toolbox-input form-control" name="toolitem-title" data-name="title" value="" />
                    </li>
                </ul>

                <ul>
                    <li>
                        <label class="hasTip" title="' . Text::_('JUX_NAVIGATION_MM_SUBMENU') . '::' . Text::_('JUX_NAVIGATION_MM_SUBMENU_DESC') . '">' . Text::_('JUX_NAVIGATION_MM_SUBMENU') . '</label>
                        <div class="btn-group btn-group-yesno radio toolitem-sub">
                            <input class="btn-check toolbox-toggle" type="radio" id="toggleSub0" data-action="toggleSub" name="toggleSub" value="0">
                            <label for="toggleSub0" class="btn btn-outline-danger">
                            ' . Text::_('JNO') . ' </label>
                            <input class="btn-check toolbox-toggle" type="radio" id="toggleSub1" data-action="toggleSub" name="toggleSub" value="1">
                            <label for="toggleSub1" class="btn btn-outline-success">' . Text::_('JYES') . ' </label>
                        </div>
                    </li>
                </ul>

                <ul>
                    <li>
                        <label class="hasTip" title="' . Text::_('JUX_NAVIGATION_MM_GROUP') . '::' . Text::_('JUX_NAVIGATION_MM_GROUP_DESC') . '">' . Text::_('JUX_NAVIGATION_MM_GROUP') . '</label>
                        <div class="btn-group btn-group-yesno radio toolitem-group">
                            <input class="btn-check toolbox-toggle" type="radio" id="toggleGroup1" data-action="toggleGroup" name="toggleGroup" value="1">
                            <label for="toggleGroup1" class="btn btn-outline-success">' . Text::_('JYES') . ' </label>
                            <input class="btn-check toolbox-toggle" type="radio" id="toggleGroup0" data-action="toggleGroup" name="toggleGroup" value="0">
                            <label for="toggleGroup0" class="btn btn-outline-danger">
                            ' . Text::_('JNO') . ' </label>
                        </div>
                    </li>
                </ul>

                <ul>
                    <li>
                        <label class="hasTip" title="' . Text::_('JUX_NAVIGATION_MM_POSITIONS') . '::' . Text::_('JUX_NAVIGATION_MM_POSITIONS_DESC') . '">' . Text::_('JUX_NAVIGATION_MM_POSITIONS') . '</label>
                        <fieldset class="btn-group">
                            <a href="" class="btn toolitem-moveleft toolbox-action" data-action="moveItemsLeft" title="' . Text::_('JUX_NAVIGATION_MM_MOVE_LEFT') . '"><i class="icon-arrow-left"></i></a>
                            <a href="" class="btn toolitem-moveright toolbox-action" data-action="moveItemsRight" title="' . Text::_('JUX_NAVIGATION_MM_MOVE_RIGHT') . '"><i class="icon-arrow-right"></i></a>
                        </fieldset>
                    </li>
                </ul>

                <ul>
                    <li>
                    <label class="hasTip" title="' . Text::_('JUX_NAVIGATION_MM_EX_CLASS') . '::' . Text::_('JUX_NAVIGATION_MM_EX_CLASS_DESC') . '">' . Text::_('JUX_NAVIGATION_MM_EX_CLASS') . '</label>
                    <input type="text" class="input-medium toolitem-exclass toolbox-input form-control" name="toolitem-exclass" data-name="class" value="" />
                    </li>
                </ul>

                <ul>
                    <li>
                        <label class="hasTip" title="' . Text::_('JUX_NAVIGATION_MM_CAPTION') . '::' . Text::_('JUX_NAVIGATION_MM_CAPTION_DESC') . '">
                        ' . Text::_('JUX_NAVIGATION_MM_CAPTION') . '
                        </label>
                        <input type="text" class="input-medium toolitem-caption toolbox-input form-control" name="toolitem-caption" data-name="caption" value="" />
                    </li>
                </ul>
                <!-- Switch Image and Icon -->
                <ul>
                    <li>
                        <label class="hasTip" title="' . Text::_('JUX_NAVIGATION_MM_ICON_SWITCH') . '::' . Text::_('JUX_NAVIGATION_MM_ICON_SWITCH_DESC') . '">' . Text::_('JUX_NAVIGATION_MM_ICON_SWITCH') . '</label>
                        <div class="btn-group btn-group-yesno radio toolitem-iconswitch">
                            <input class="btn-check toolbox-toggle" type="radio" id="toggleIconSwitch1" data-action="toggleIconSwitch" name="toggleIconSwitch" value="1" data-action="toggleIconSwitch">
                            <label for="toggleIconSwitch1" class="btn btn-outline-success">' . Text::_('JUX_NAVIGATION_MM_ICON') . ' </label>
                            <input class="btn-check toolbox-toggle" type="radio" id="toggleIconSwitch2" data-action="toggleIconSwitch" name="toggleIconSwitch" value="2" data-action="toggleIconSwitch">
                            <label for="toggleIconSwitch2" class="btn btn-outline-danger">
                            ' . Text::_('JUX_NAVIGATION_MM_IMAGE') . ' </label>
                        </div>
                    </li>
                </ul>
                <!-- Menu icon -->
                <ul id="juxMenuItemIcon">
                    <li>
                        <label class="hasTip" title="' . Text::_('JUX_NAVIGATION_MM_ICON') . '::' . Text::_('JUX_NAVIGATION_MM_ICON_DESC') . '">
                        ' . Text::_('JUX_NAVIGATION_MM_ICON') . '
                        </label>
                        <input type="text" class="input-medium toolitem-xicon toolbox-input form-control" name="toolitem-xicon" data-name="xicon" value="" />
                        <a style="display: block;" href="https://fontawesome.com/v4.7.0/icons/" target="_blank">' . Text::_('JUX_NAVIGATION_MM_ICON') . '</a>
                    </li>
                </ul>
                <ul id="juxMenuItemImage">
                    <li>
                        <label class="hasTip" title="' . Text::_('JUX_NAVIGATION_MM_IMAGE_URL') . '">
                        ' . Text::_('JUX_NAVIGATION_MM_IMAGE_URL') . '
                        </label>
                        <input type="text" class="input-medium toolitem-imgurl toolbox-input form-control" name="toolitem-imgurl" data-name="imgurl" value="" />
                    </li>
                </ul>
            </div>

            <div id="jux-admin-mm-toolsub" class="admin-toolbox">
              <h3>' . Text::_('JUX_NAVIGATION_MM_SUBMNEU_CONF') . '</h3>
                <ul>
                    <li>
                        <label class="hasTip" title="' . Text::_('JUX_NAVIGATION_MM_SUBMNEU_GRID') . '::' . Text::_('JUX_NAVIGATION_MM_SUBMNEU_GRID_DESC') . '">' . Text::_('JUX_NAVIGATION_MM_SUBMNEU_GRID') . '</label>
                        <div class="btn-group">
                            <a href="" class="btn toolsub-addrow toolbox-action" data-action="addRow"><i class="icon-plus"></i></a>
                        </div>
                    </li>
                </ul>
                <ul>
                    <li>
                        <label class="hasTip" title="' . Text::_('JUX_NAVIGATION_MM_HIDE_COLLAPSE') . '::' . Text::_('JUX_NAVIGATION_MM_HIDE_COLLAPSE_DESC') . '">' . Text::_('JUX_NAVIGATION_MM_HIDE_COLLAPSE') . '</label>
                        <div class="btn-group btn-group-yesno radio toolsub-hidewhencollapse">
                            <input class="btn-check toolbox-toggle" type="radio" id="togglesubHideWhenCollapse1" data-action="hideWhenCollapse" name="hideWhenCollapse" value="1">
                            <label for="togglesubHideWhenCollapse1" class="btn btn-outline-success">' . Text::_('JYES') . ' </label>
                            <input class="btn-check toolbox-toggle" type="radio" id="togglesubHideWhenCollapse0" data-action="hideWhenCollapse" name="hideWhenCollapse" value="0">
                            <label for="togglesubHideWhenCollapse0" class="btn btn-outline-danger">
                            ' . Text::_('JNO') . ' </label>
                        </div>
                    </li>
                </ul>
                <ul>
                    <li>
                        <label class="hasTip" title="' . Text::_('JUX_NAVIGATION_MM_SUBMNEU_WIDTH_PX') . '::' . Text::_('JUX_NAVIGATION_MM_SUBMNEU_WIDTH_PX_DESC') . '">' . Text::_('JUX_NAVIGATION_MM_SUBMNEU_WIDTH_PX') . '</label>
                        <input type="number" min="1" step="1" class="toolsub-width toolbox-input input-small form-control" name="toolsub-width" data-name="width" value="" />
                    </li>
                </ul>
                <ul>
                    <li>
                        <label class="hasTip" title="' . Text::_('JUX_NAVIGATION_MM_ALIGN') . '::' . Text::_('JUX_NAVIGATION_MM_ALIGN_DESC') . '">' . Text::_('JUX_NAVIGATION_MM_ALIGN') . '</label>
                        <div class="btn-group toolsub-alignment">
                            <a class="btn toolsub-align-left toolbox-action" href="#" data-action="alignment" data-align="left" title="' . Text::_('JUX_NAVIGATION_MM_ALIGN_LEFT') . '"><i class="icon-align-left"></i></a>
                            <a class="btn toolsub-align-right toolbox-action" href="#" data-action="alignment" data-align="right" title="' . Text::_('JUX_NAVIGATION_MM_ALIGN_RIGHT') . '"><i class="icon-align-right"></i></a>
                            <a class="btn toolsub-align-center toolbox-action" href="#" data-action="alignment" data-align="center" title="' . Text::_('JUX_NAVIGATION_MM_ALIGN_CENTER') . '"><i class="icon-align-center"></i></a>
                            <a class="btn toolsub-align-justify toolbox-action" href="#" data-action="alignment" data-align="justify" title="' . Text::_('JUX_NAVIGATION_MM_ALIGN_JUSTIFY') . '"><i class="icon-align-justify"></i></a>
                        </div>
                    </li>
                </ul>
                <ul>
                    <li>
                        <label class="hasTip" title="' . Text::_('JUX_NAVIGATION_MM_EX_CLASS') . '::' . Text::_('JUX_NAVIGATION_MM_EX_CLASS_DESC') . '">' . Text::_('JUX_NAVIGATION_MM_EX_CLASS') . '</label>
                        <input type="text" class="toolsub-exclass toolbox-input input-medium form-control" name="toolsub-exclass" data-name="class" value="" />
                    </li>
                </ul>
            </div>

            <div id="jux-admin-mm-toolcol" class="admin-toolbox">
              <h3>' . Text::_('JUX_NAVIGATION_MM_COLUMN_CONF') . '</h3>
                <ul>
                    <li>
                        <label class="hasTip" title="' . Text::_('JUX_NAVIGATION_MM_ADD_REMOVE_COLUMN') . '::' . Text::_('JUX_NAVIGATION_MM_ADD_REMOVE_COLUMN_DESC') . '">' . Text::_('JUX_NAVIGATION_MM_ADD_REMOVE_COLUMN') . '</label>
                        <div class="btn-group">
                            <a href="" class="btn toolcol-addcol toolbox-action" data-action="addColumn"><i class="icon-plus"></i></a>
                            <a href="" class="btn toolcol-removecol toolbox-action" data-action="removeColumn"><i class="icon-minus"></i></a>
                        </div>
                    </li>
                </ul>
                <ul>
                    <li>
                        <label class="hasTip" title="' . Text::_('JUX_NAVIGATION_MM_HIDE_COLLAPSE') . '::' . Text::_('JUX_NAVIGATION_MM_HIDE_COLLAPSE_DESC') . '">' . Text::_('JUX_NAVIGATION_MM_HIDE_COLLAPSE') . '</label>
                        <div class="btn-group btn-group-yesno radio toolcol-hidewhencollapse">
                            <input class="btn-check toolbox-toggle" type="radio" id="toggleHideWhenCollapse1" data-action="hideWhenCollapse" name="toggleHideWhenCollapse" value="1">
                            <label for="toggleHideWhenCollapse1" class="btn btn-outline-success">' . Text::_('JYES') . ' </label>
                            <input class="btn-check toolbox-toggle" type="radio" id="toggleHideWhenCollapse0" data-action="hideWhenCollapse" name="toggleHideWhenCollapse" value="0">
                            <label for="toggleHideWhenCollapse0" class="btn btn-outline-danger">
                            ' . Text::_('JNO') . ' </label>
                        </div>
                    </li>
                </ul>
                <ul>
                    <li>
                        <label class="hasTip" title="' . Text::_('JUX_NAVIGATION_MM_WIDTH_SPAN') . '::' . Text::_('JUX_NAVIGATION_MM_WIDTH_SPAN_DESC') . '">' . Text::_('JUX_NAVIGATION_MM_WIDTH_SPAN') . '</label>
                        <select class="toolcol-width toolbox-input toolbox-select input-mini form-select" name="toolcol-width" data-name="width">
                          <option value="1">1</option>
                          <option value="2">2</option>
                          <option value="3">3</option>
                          <option value="4">4</option>
                          <option value="5">5</option>
                          <option value="6">6</option>
                          <option value="7">7</option>
                          <option value="8">8</option>
                          <option value="9">9</option>
                          <option value="10">10</option>
                          <option value="11">11</option>
                          <option value="12">12</option>
                        </select>
                    </li>
                </ul>

                <ul>
                    <li>
                        <label class="hasTip" title="' . Text::_('JUX_NAVIGATION_MM_MODULE') . '::' . Text::_('JUX_NAVIGATION_MM_MODULE_DESC') . '">' . Text::_('JUX_NAVIGATION_MM_MODULE') . '</label>
                        <select id="toolcol-position" class="toolcol-position toolbox-input toolbox-select input-medium form-select" name="toolcol-position" data-name="position" data-placeholder="' . Text::_('JUX_NAVIGATION_MM_SELECT_MODULE') . '">
                            <option value=""></option>
                                  '; ?>
                            <?php
                            foreach ($modules as $module) {
                                $html .= "<option value=\"{$module->id}\">{$module->title}</option>\n";
                            } ?>
                            <?php
                            $html .= '
                        </select>
                    </li>
                </ul>
                <ul>
                    <li>
                        <label class="hasTip" title="' . Text::_('JUX_NAVIGATION_MM_EX_CLASS') . '::' . Text::_('JUX_NAVIGATION_MM_EX_CLASS_DESC') . '">' . Text::_('JUX_NAVIGATION_MM_EX_CLASS') . '</label>
                        <input type="text" class="input-medium toolcol-exclass toolbox-input form-control" name="toolcol-exclass" data-name="class" value="" />
                    </li>
                </ul>
            </div>
          </div>

          <div class="toolbox-actions-group">
          <div class="preset-style-list">
            <button class="btn btn-primary" type="button" style="height: 100%">' . Text::_('JUX_MEGAMENU_PRESET_STYLES') . '</button>
            <ul>
                <li><a id="jux-menu-save-preset-style-btn" class="btn btn-small" href="#">' . Text::_('JUX_MEGAMENU_SAVE_AS_PRESET_STYLE') . '</a></li>
                <li id="jux-menu-preset">
                    <ul id="jux-preset-style-list">';
            foreach($preset as $k => $v) {
                $html .= '<li>
                            <a class="jux-menu-apply-preset-style" href="#" data-id="'.$k.'">'.$v['name'].'</a>
                            <span class="jux-menu-del-preset-style" data-id="'.$k.'" data-toggle="tooltip" title="Delete preset"></span>
                        </li>';
            }
        $html .= '</ul>
                </li>
            </ul>
          </div>
          <div class="btn-group hidden" style="display: none">
          <button class="btn active" id="jux_ez_menu_item_options">' . Text::_('JUX_MEGAMENU_MENU_ITEM_OPTIONS') . '</button>
          <button class="btn" id="jux_ez_menu_style_options">' . Text::_('JUX_MEGAMENU_MENU_STYLE_OPTIONS') . '</button>
          </div>
          <div class="jux-prev-switch">
          <span class="off active" data-trigger="jux_ez_menu_item_options">' . Text::_('JUX_MEGAMENU_STRUCTURE') . '</span>
          <span class="on" data-trigger="jux_ez_menu_style_options">' . Text::_('JUX_MEGAMENU_PREVIEW') . '</span>
          </div>
            <button class="jux-admin-tog-fullscreen toolbox-action toolbox-togglescreen" data-action="toggleScreen" data-iconfull="icon-resize-full" data-iconsmall="icon-resize-small"><i class="icon-resize-full"></i></button>
    		<button class="juxm-admin-clear-config" id="JUXMClearConfig" type="button"><i class="icon-remove"></i></button>
            <button class="btn btn-success toolbox-action toolbox-saveConfig hide" data-action="saveConfig"><i class="icon-save"></i>' . Text::_('JUX_NAVIGATION_MM_SAVE') . '</button>
          </div>

        </div>
      </div>

      <div id="jux-admin-mm-container" class="navbar'.$rtlClass.' clearfix">
      <div id="jux-easy-megamenu-' . $current_mod_id . '" class="'.$mobileLayoutClass.' juxnav menu_' . $params['mod_menu'] . ' ' . $menu_nav_align_class . ' jux-easy-megamenu-' . $current_mod_id . ' jux-easy-megamenu clearfix ' . $menu_nav_class . '">' . $menu . '
      </div>
      </div>
    </div>';
        $html .= '<div id="jux-ez-mega-menu-generator" class="metabox-holder clearfix">';
        $html .= $option;
        $html .= '</div>';
        return $html;
    }
}
