<?php
/**
 *
 * @extension       Marlev - Template Manager
 * @author          Lev Milicenco<support@marlev.it>
 * @link            http://www.marlev.it
 * @copyright       Copyright 2014 marlev.it All Rights Reserved
 * @license         http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 */
require_once JPATH_ROOT . '/administrator/components/com_tmarlevv2/src/Helper/loader.php';
use Joomla\CMS\Language\Text;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
//for_use



defined('_JEXEC') or die;

$input = Factory::getApplication()->input;
$updatetime = $input->getInt("updatetime", 0);
if($updatetime === 1){
    echo '<span id="update-time" style="color:green;">'.Factory::getDate()->format('Y-m-d H:i:s').'</span>';
    exit();
}
//HTMLHelper::_('bootstrap.tooltip', '.hasPopover');
HtmlHelper::_('behavior.keepalive');
HtmlHelper::_('formbehavior.chosen', 'select');
HtmlHelper::_('bootstrap.tooltip');
HtmlHelper::_('bootstrap.framework');
//\Joomla\CMS\HTML\HTMLHelper::_('bootstrap.tabs', '.selector', []);


$id = $input->getInt("id");
$session = Factory::getApplication()->getSession();
$activemenu = $session->get('activemenu', "tm-home");
$session->clear('activemenu');
//$document = Factory::getApplication()->getDocument();
$prepare_rand = rand(0, 99999) . time();
$rand = substr(md5($prepare_rand), 0, 25);

$wa = Factory::getApplication()->getDocument()->getWebAssetManager();
$wacss = ["tmstyle"=>'style.css',"tmbootstrap"=>'bootstrap.min.css',"tmslides"=>'slides.css'];
foreach($wacss as $kccs=>$vcss){
    $wa->registerAndUseStyle($kccs, 'administrator/components/com_tmarlevv2/css/'.$vcss);
}
$wa->useScript('jquery');
foreach(['tmjqui'=>'jquery-ui.min.js',"tmfunc"=>'functions.js',"tmadmin"=>'admin.js'] as $kjs=>$vjs){
    $wa->registerAndUseScript($kjs, 'administrator/components/com_tmarlevv2/js/'.$vjs, [], ['defer' => true], ['jquery']);
}


//$document->addStyleSheet('https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css');
//$document->addStyleSheet(Uri::base() . 'components/com_tmarlevv2/helpers/bootstrap.min.css?' . $rand);
//$document->addStyleSheet(Uri::base() . 'components/com_tmarlevv2/helpers/style.css?' . $rand);
//$document->addStyleSheet(Uri::base() . 'components/com_tmarlevv2/helpers/slides.css?' . $rand);
$getlink = str_replace("administrator/", "", Uri::base()) . 'templates/' . $this->load->template . "/";
//$document->addScript(Uri::base() . 'components/com_tmarlevv2/helpers/jquery-ui.min.js?' . $rand);
//$document->addScript(Uri::base() . 'components/com_tmarlevv2/helpers/functions.js?' . $rand);
//$document->addScript(Uri::base() . 'components/com_tmarlevv2/helpers/admin.js?' . $rand);
if($this->layout_loaded) {
    $wajs = ["tmllife"=>'layout-life.js',"tmlslider"=>'layout-slider.js',"tmlfunc"=>'layout-functions.js',
        'tmlayout'=>'layout.js'
        ];
    foreach($wajs as $kjs=>$vjs){
        $wa->registerAndUseScript($kjs, 'administrator/components/com_tmarlevv2/js/'.$vjs, [], ['defer' => true], ['jquery']);
    }
//    $document->addScript(Uri::base() . 'components/com_tmarlevv2/helpers/layout-life.js?' . $rand);
//    $document->addScript(Uri::base() . 'components/com_tmarlevv2/helpers/layout-slider.js?' . $rand);
//    $document->addScript(Uri::base() . 'components/com_tmarlevv2/helpers/layout-functions.js?' . $rand);
//    $document->addScript(Uri::base() . 'components/com_tmarlevv2/helpers/layout.js?' . $rand);
    if ($this->installtype === "install" || $this->installtype === "installpreset") {
        $wa->addInlineScript('jQuery(document).ready(function () { jQuery.loading_wait("'.Text::_("COM_TMARLEV_INSTALLING").'","'.Text::_("COM_TMARLEV_INSTALLING_DESK").'");jQuery.install_ml_template();});');
    }
    else{
        $wa->addInlineScript('jQuery(document).ready(function () {jQuery.template_layout();});');
    }
}
?>
<?php //start close jomla divs          ?>
</div>
</div>
<?php //end close jomla divs     ?>
<div id="ml-save-fixed" class="btns-closed" style="display: none;">
    <div class="ml-apply-fixed ml-submit ml-apply" data-lang="<?php echo Text::_('COM_TMARLEV_SAVE_ALL') ?>" data-task="template.apply" >
        <?php echo Text::_('COM_TMARLEV_SAVE_ALL') ?>
    </div>

    <div class="ml-cancell-fixed ml-submit" data-lang="<?php echo Text::_('COM_TMARLEV_CANCELL') ?>" data-task="template.cancel">
        <?php echo Text::_('COM_TMARLEV_CANCELL') ?>
    </div>
</div>
<?php if($this->layout_loaded):?>
<div class="layout-lift">
    <div class="layout-lift-section">
        <?php echo $this->layout_sections; ?>
    </div>
    <div class="layout-lift-block">
        <?php echo $this->layout_positionsblock; ?>
    </div>
    <div id="layout-lift-life" style="display: none;">
        <div class="form-check form-switch">
            <input class="form-check-input" type="checkbox" id="layout-life">
<!--            <label class="form-check-label" for="layout-life">Life mode</label>-->
        </div>
    </div>
</div>
<?php endif;?>
<div id="ml-head">
    <div class="marlev-logo-container visible-lg visible-md">
        <img align="center" src="<?php echo Uri::base() ?>/components/com_tmarlevv2/images/logo.png" alt="Marlev Logo" />
    </div>
    <div class="head-btns">
        <button class="btn btn-secondary ml-submit" data-lang="<span class='icon-cancel'  ></span> <?php echo Text::_('COM_TMARLEV_CANCELL') ?>" data-task="template.cancel">
            <span class="icon-cancel"  ></span>
            <?php echo Text::_('COM_TMARLEV_CANCELL') ?></button>


        <button class="btn btn-success ml-submit" data-lang="<span class='icon-apply icon-white' ></span> <?php echo Text::_('COM_TMARLEV_SAVE_ALL') ?>" data-task="template.apply" >
            <span class="icon-apply icon-white" ></span>
            <?php echo Text::_('COM_TMARLEV_SAVE_ALL') ?></button>
        <button class="btn btn-dark ml-submit" data-lang="<span class='icon-refresh icon-white' ></span> <?php echo Text::_('JTOOLBAR_REFRESH_CACHE') ?>" data-task="template.clearcache" >
            <span class="icon-refresh icon-white" ></span>
            <?php echo Text::_('JTOOLBAR_REFRESH_CACHE') ?></button>
        <button class="btn btn-dark menu-action"  >
            <span ><i class="fas fa-bars"></i></span></button>
    </div>
</div>


<div class=" marlev_manager">
    <div class="container-fluid">


        <form action="" method="post" name="adminForm" id="adminForm" class="form-validate" >

            <div class="menu-container">

                <div class="marlev_menu" style="height:800px;">

                    <div class="header_mmenu">

                        <?php require_once $this->load->patch . "menu.php"; ?>

                    </div>
                    <div class="clearfix"></div>
                </div>
                <div class="clearfix"></div>
            </div>


            <div id="tms-container">
            <!--ml-append-messagge-->
                <div id="alert-box" data-height="0" class="">
                    <?php echo $this->message; ?>
                </div>

                <div class="tm-home">
                    <div class="container">
                <div class="row">
                    <?php

                    require_once $this->load->patch . "load_home.php";
                    ?>
                </div>
                    </div>
                </div>
                <?php if($this->layout_loaded): ?>
                <div class="tm-layout">
                    <div id="layout-life-mode" class="d-flex justify-content-center"></div>

                    <div id="tm-layout">
                        <h3><?php echo Text::_("Layout & Style"); ?></h3>
                        <?php echo $this->loadTemplate('layout'); ?>
                    </div>
                </div>
                <?php endif; ?>
                <?php require_once $this->load->patch . "marlev_index.php"; ?>
                <?php echo $this->loadTemplate('assignment'); ?>
            </div>

            <div id="layout-slider-block" data-state="close">
                <div id="load-slider-content"></div>
            </div>

            <div id="hidden-input">
                <?php if ($this->installtype === "install"): ?>
                    <input type="hidden" id='ultimateinstall' name="ultimateinstall" value="1" />
                <?php else: ?>
                    <input type="hidden"  name="ultimateinstall" value="0" />
                <?php endif; ?>
                <input type="hidden" id="layout-id" name="params[id]" value="<?php echo $id; ?>" />
                <input type="hidden" id="task" name="task" value="template.apply" />
                <input type="hidden" name="params[patch]" value="<?php echo base64_encode($this->load->patch); ?>" />
                <input type="hidden" id="layout-template" name="params[template]" value="<?php echo $this->load->template; ?>" />
                <input type="hidden" name="params[name]" value="<?php echo $this->load->template; ?>" />
                <input type="hidden" name="params[custom_version]" value="on_save" />
                <input type="hidden" name="params[active_menu]" class="tm-active-menu" value="<?php echo $activemenu; ?>" />

            </div>
            <div id="hidden-token">
                <?php echo HtmlHelper::_('form.token'); ?>
            </div>

        </form>
        <input type="hidden" id="ml-uri" value="<?php echo Uri::base(); ?>" />
        <div  class="clearfix"></div>
    </div>
</div>

<?php //open jomla divs ?>
<div>
    <div>
