<?php
/**
 * @extension       Marlev Template
 * @author          Lev Milicenco<support@marlev.it>
 * @link            http://www.marlev.it
 * @copyright       Copyright 2021 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\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Marlev\Component\Tmarlevv2\Administrator\Helper\ml_layout;
//for_use
HtmlHelper::_('bootstrap.tooltip');

defined('_JEXEC') or die;
$input = Factory::getApplication()->input;
$id = $input->getInt("id");
$render = new ml_layout($this->layout, $this->load->template);
$getlayout = $render->return_layout();
?>

<div id="loadlayout" class="template-layout">

    <div id="devices-view" data-active="view-desktop">
        <span id="view-desktop" class="active-view adapt-viewer"><span title="<?php echo Text::_("SHOW_PC_LAYOUT") ?>"
                                                                       class="hasTooltip"><i class="fa fa-desktop"
                                                                                             aria-hidden="true"></i></span></span>
        &nbsp;<span id="view-tablet" class="adapt-viewer"><span title="<?php echo Text::_("SHOW_TABLET_LAYOUT") ?>"
                                                                class="hasTooltip"><i class="fa fa-tablet"
                                                                                      aria-hidden="true"></i></span></span>
        &nbsp;<span id="view-mobile" class="adapt-viewer"> <span title="<?php echo Text::_("SHOW_MOBILE_LAYOUT") ?>"
                                                                 class="hasTooltip"><i class="fa fa-mobile"
                                                                                       aria-hidden="true"></i></span></span>
    </div>

    <?php
    $body = $render->elabora_posdata("ml_body");
    echo '<div class="content-pos-name"><div class="section-name-body"><strong>Body</strong></div>' . $render->in_popup('ml_body', 'ml_body', '', $body, 'Body') .
        ' <input type="hidden"  value="ml_body" name="layout[sections][]">' .
        '</div>';
    foreach ($getlayout->sections as $key => $getpos):
    $gkey = $key . '_section'; //_section e' fissaione defiitita in template/admin/layout.php
    ?>
    <?php
    $get_section = $render->elabora_posdata($gkey);
    $show_hide_check = $render->assign_suspend($gkey);
    $show_hide_section = '<span class="form-check form-switch checkbox-section suspend-position-block hasTooltip" title="' . Text::_("COM_TMARLEV_SHOW_HIDE_SECTION") . '">';
    $show_hide_section .= '<input  id="checkbox-' . $gkey . '" class="suspend-section form-check-input" role="switch" name="layout[suspended][' . $gkey . ']" data-id="#' . $gkey . '"  ' . $show_hide_check->checked . '   type="checkbox" value="1" >';
    $show_hide_section .= '<label class="form-check-label" for="checkbox-' . $gkey . '"></label></span>';
    echo '<div class="content-pos-name"><div class="section-name-resto"><div class="d-flex justify-content-center"><strong>' . Text::_("COM_TMARLEV_SECTION") . ' ' . ucfirst(str_replace("_section", "", $gkey)) . ' </strong>&nbsp;' . $show_hide_section . ' ' . $render->in_popup($gkey, $gkey, '', $get_section, ucfirst($key) . ' Section') . '</div></div></div>';
    echo ' <input type="hidden"  value="' . $gkey . '" name="layout[sections][]">';
    echo '<div id="' . $gkey . '" class="layouts" >';
    //  echo '<div class="section-options">' . $render->in_popup($gkey, $gkey, '', $get_section, ucfirst($key) . ' Section') . '</div>';
    foreach ($getpos as $gpos => $pos):

        $pos = trim($pos);
        $posdata = $render->elabora_posdata($pos);
        $parentid = 'pos_' . $pos;
        $positions_details = $render->get_positions_details($posdata, $pos);
        $suspended = $render->assign_suspend($pos);
        $child_nopadding = $render->child_nopadding($pos);
        ?>
        <div id="<?php echo $parentid ?>"
             class="<?php echo $suspended->suspended_class; ?> position <?php echo $posdata->width ?> <?php echo $child_nopadding ?>">
            <div style="text-align: center;">
                <strong> <?php echo Text::_("COM_TMARLEV_BLOCK_OF_POSITION") . " " . $pos ?></strong></div>
            <div class="add-position-block">
                <div class="d-flex justify-content-center">
                    <span class="add-position" data-id="<?php echo $parentid ?>" data-class="subposition"
                          data-maxcols="<?php echo $positions_details->maxcols; ?>"
                          data-positions="<?php echo $positions_details->positions; ?>"><span
                                class="hasTooltip icon-plus"
                                title="<?php echo Text::_('COM_TMARLEV_ADD_POSITION_LABEL') ?>"></span></span>
                    <span class="form-check form-switch checkbox-section suspend-position-block hasTooltip"
                          title="<?php echo Text::_('COM_TMARLEV_SUSPEND_UNSUSPEND_POSITION') ?>">
                            <input <?php echo $suspended->checked; ?> class='suspend-position form-check-input'
                                                                      role="switch" data-id="<?php echo $parentid ?>"
                                                                      id="checkbox-<?php echo $parentid ?>"
                                                                      type="checkbox" value="1"
                                                                      name="layout[suspended][<?php echo $pos ?>]">
                            <label class="form-check-label" for="checkbox-<?php echo $parentid ?>"></label>
                        </span>
                    <span class="inlarge-block"><span class="hasTooltip inlarge-click icon-expand-2"
                                                      data-inputid="<?php echo $parentid ?>_input"
                                                      data-actual="<?php echo $posdata->width ?>"
                                                      data-id="<?php echo $parentid ?>"
                                                      title="<?php echo Text::_('COM_TMARLEV_POSITION_BLOCK_WIDTH') ?>"></span></span>

                    <?php
                    $section_key = $pos . '_subsection'; //_subsection e' fissaione defiitita in template/admin/layout.php
                    $get_subsection = $render->elabora_posdata($section_key);
                    echo $render->in_popup($section_key, $section_key, '', $get_subsection, ucfirst($pos) . ' Subsection');
                    ?>

                    <input type="hidden" id="<?php echo $parentid ?>_input" value="<?php echo $posdata->width ?>"
                           name="layout[width][<?php echo $pos ?>]">
                    <input type="hidden" id="<?php echo $parentid ?>-positions"
                           value="<?php echo implode(",", $posdata->defpositions) ?>"
                           name="layout[positions][<?php echo $pos ?>]">
                    <input type="hidden" value="<?php echo $section_key; ?>" name="layout[sections][]">
                </div>
            </div>
            <div class="row">
                <?php
                if ($posdata->positions != false):
                    $get_defcollayout = array();
                    $get_tabcollayout = array();
                    $get_mobcollayout = array();

                    $modules = ml_layout::get_modules($posdata->positions);
                    foreach ($posdata->positions as $n1 => $subpos):

                        if ($this->layout !== false && !isset($this->layout['positions'][$subpos])) {
                            continue;
                        }

                        $subposdata = $render->elabora_posdata($subpos);
                        $get_defcollayout[] = $subposdata->defcollayout[0];
                        $get_tabcollayout[] = $subposdata->tabcollayout[0];
                        $get_mobcollayout[] = $subposdata->mobcollayout[0];
                        ?>
                        <div data-id="pos_<?php echo $subpos ?>"
                             class="subposition pos_<?php echo $subpos ?> col-md-<?php echo $subposdata->defcollayout[0] ?> col-lg-<?php echo $subposdata->defcollayout[0] ?> col-12 col-sm-12"
                             data-mobile="<?php echo $subposdata->mobcollayout[0] ?>"
                             data-tablet="<?php echo $subposdata->tabcollayout[0] ?>"
                             data-pc="<?php echo $subposdata->defcollayout[0] ?>" data-position="<?php echo $subpos ?>">
                            <div style="padding: 2px">
                                <div class="show-subposition">
                                    <?php
                                    echo $render->inners_positions($subposdata, 'innertop');

                                    $selected = new \stdClass;
                                    $selected->pc = $subposdata->defcollayout[0];
                                    $selected->tablet = $subposdata->tabcollayout[0];
                                    $selected->mobile = $subposdata->mobcollayout[0];
                                    ?>

                                    <?php
                                    if ($subpos == "content") {
                                        echo '<div class="content-pos-name"><div class="section-name-resto"><strong>' . ucfirst($subpos) . '</strong></div><div class="small">' . Text::_('COM_TMARLEV_CONTENT_BLOCK_DESK') . '</div>' . $render->in_popup($parentid, $subpos, $selected, $subposdata, "Content") . '</div>';
                                        echo '<input type="hidden"  value="' . $subpos . '" name="layout[sections][]">';
                                    } else {
                                        echo '<div>' . $subpos . '</div>';
                                        echo $render->in_popup($parentid, $subpos, $selected, $subposdata, $subpos);
                                        echo ml_layout::show_modules($subpos, $modules);
                                        echo '&nbsp;&nbsp;<span class="colcancel"><span class="hasTooltip icon-cancel" data-original-title="' . Text::_('COM_TMARLEV_DELETE_THIS_COL') . '"></span></span>';
                                    }
                                    echo $render->inners_positions($subposdata, 'innerbottom');
                                    ?>
                                </div>
                                <div class="clearfix"></div>
                            </div>
                            <div class="w-100 end-position"></div>
                        </div>

                    <?php endforeach; ?>
                    <input type="hidden" id="<?php echo $parentid ?>-mobcollayout"
                           value="<?php echo implode(",", $get_mobcollayout) ?>"
                           name="layout[mobcollayout][<?php echo $pos ?>]">
                    <input type="hidden" id="<?php echo $parentid ?>-tabcollayout"
                           value="<?php echo implode(",", $get_tabcollayout) ?>"
                           name="layout[tabcollayout][<?php echo $pos ?>]">
                    <input type="hidden" id="<?php echo $parentid ?>-defcollayout"
                           value="<?php echo implode(",", $get_defcollayout) ?>"
                           name="layout[defcollayout][<?php echo $pos ?>]">
                <?php else: ?>
                    <input type="hidden" id="<?php echo $parentid ?>-mobcollayout"
                           value="<?php echo implode(",", $posdata->mobcollayout) ?>"
                           name="layout[mobcollayout][<?php echo $pos ?>]">
                    <input type="hidden" id="<?php echo $parentid ?>-tabcollayout"
                           value="<?php echo implode(",", $posdata->tabcollayout) ?>"
                           name="layout[tabcollayout][<?php echo $pos ?>]">
                    <input type="hidden" id="<?php echo $parentid ?>-defcollayout"
                           value="<?php echo implode(",", $posdata->defcollayout) ?>"
                           name="layout[defcollayout][<?php echo $pos ?>]">
                    <?php
                    echo $render->show_subbpositions($posdata, $pos, 'subposition', $posdata->defpositions);

                endif;

                ?>

                <div class="w-100  end-position"></div>
            </div>
        </div>
    <?php endforeach; ?>

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


<?php endforeach; ?>
<div class="hidden">

    <span id="cols-warning" data-message="<?php echo Text::_("COM_TMARLEV_MAX12_COLS") ?>"></span>
    <span id="all-positions_added" data-message="<?php echo Text::_("COM_TMARLEV_ALL_POSITIONS_ADDED") ?>"></span>

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


<input type="hidden" name="params[layoutid]" value="<?php echo $this->layoutid; ?>"/>
<div style="display:none" class="avvaiable-positions-content">
    <div id="avvaiable-positions-block">
        <?php echo $render->get_multiselect_positions(); ?>
    </div>

</div>
<div style="display: none;" class="duplicates_template">
    <div id="duplicates_template">
        <?php echo $render->get_duplicates_template(); ?>
    </div>

</div>
<div style="display:none">
    <div id="layout-lift-editposition">
        <?php echo $render->get_editable_positions(); ?>
    </div>

</div>
</div>




