<?php
/** 
 * @package     VikRestaurants
 * @subpackage  core
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://vikwp.com
 */

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

$params = $this->params;

/**
 * Trigger event to display custom HTML.
 * In case it is needed to include any additional fields,
 * it is possible to create a plugin and attach it to an event
 * called "onDisplayViewConfigappCronjobsSettings". The event method
 * receives the view instance as argument.
 *
 * @since 1.10
 */
$forms = $this->onDisplayView('CronjobsSettings');

?>

<div class="config-fieldset">

    <div class="config-fieldset-head">
        <h3><?php echo JText::translate('VRMENUTITLEHEADER4'); ?></h3>
    </div>

    <div class="config-fieldset-body">

        <!-- SECURE KEY - Text -->

        <?php
        echo $this->formFactory->createField()
            ->type('text')
            ->name('cron_secure_key')
            ->value($params['cron_secure_key'])
            ->label(JText::translate('VRMANAGECONFIG100'));
        ?>

        <!-- LOGGING MODE - Select -->

        <?php
        echo $this->formFactory->createField()
            ->type('select')
            ->name('cron_log_mode')
            ->value($params['cron_log_mode'])
            ->class('medium')
            ->label(JText::translate('VRMANAGECONFIG72'))
            ->description(JText::translate('VRMANAGECONFIG72_DESC'))
            ->options([
                JHtml::fetch('select.option', 0, JText::translate('VRCONFIGAPIREGLOGOPT0')),
                JHtml::fetch('select.option', 1, JText::translate('VRCONFIGAPIREGLOGOPT1')),
                JHtml::fetch('select.option', 2, JText::translate('VRCONFIGAPIREGLOGOPT2')),
            ]);
        ?>

        <!-- FLUSH LOGS - Select -->

        <?php
        echo $this->formFactory->createField()
            ->type('select')
            ->name('cron_log_flush')
            ->value($params['cron_log_flush'])
            ->class('medium')
            ->label(JText::translate('VRMANAGECONFIG73'))
            ->description(JText::translate('VRMANAGECONFIG73_DESC'))
            ->options([
                JHtml::fetch('select.option',  1, JText::translate('VRCONFIGAPIFLUSHLOGOPT1')),
                JHtml::fetch('select.option',  7, JText::translate('VRCONFIGAPIFLUSHLOGOPT2')),
                JHtml::fetch('select.option', 30, JText::translate('VRCONFIGAPIFLUSHLOGOPT3')),
                JHtml::fetch('select.option',  0, JText::translate('VRCONFIGAPIFLUSHLOGOPT0')),
            ]);
        ?>

        <!-- Define role to detect the supported hook -->
        <!-- {"rule":"customizer","event":"onDisplayViewConfigappCronjobsSettings","key":"basic","type":"field"} -->

        <?php   
        /**
         * Look for any additional fields to be pushed within
         * the Cron Jobs > Settings > Details fieldset.
         *
         * @since 1.10
         */
        if (isset($forms['basic']))
        {
            echo $forms['basic'];

            // unset details form to avoid displaying it twice
            unset($forms['basic']);
        }
        ?>

    </div>

</div>

<!-- PLUGINS -->

<div class="config-fieldset">

    <div class="config-fieldset-head">
        <h3><?php echo JText::translate('VRCONFIG_API_PLUGINS'); ?></h3>
    </div>

    <div class="config-fieldset-body">

        <?php echo VREApplication::getInstance()->alert(JText::translate('VRMANAGECONFIG101_HELP'), 'info'); ?>

        <!-- SEE LIST - Button -->

        <?php
        echo $this->formFactory->createField()
            ->type('link')
            ->href('index.php?option=com_vikrestaurants&view=cronjobs')
            ->text(JText::translate('VRMANAGECONFIG101'))
            ->id('vr-cron-see-btn')
            ->hiddenLabel(true);
        ?>

        <!-- Define role to detect the supported hook -->
        <!-- {"rule":"customizer","event":"onDisplayViewConfigappCronjobsSettings","key":"plugins","type":"field"} -->

        <?php   
        /**
         * Look for any additional fields to be pushed within
         * the Cron Jobs > Settings > Plugins fieldset.
         *
         * @since 1.10
         */
        if (isset($forms['plugins']))
        {
            echo $forms['plugins'];

            // unset details form to avoid displaying it twice
            unset($forms['plugins']);
        }
        ?>

    </div>

</div>

<!-- Define role to detect the supported hook -->
<!-- {"rule":"customizer","event":"onDisplayViewConfigappCronjobsSettings","type":"fieldset"} -->

<?php
/**
 * Iterate remaining forms to be displayed as new fieldsets
 * within the Cron Jobs > Settings tab.
 *
 * @since 1.10
 */
foreach ($forms as $formTitle => $formHtml)
{
    ?>
    <div class="config-fieldset">
        
        <div class="config-fieldset-head">
            <h3><?php echo JText::translate($formTitle); ?></h3>
        </div>

        <div class="config-fieldset-body">
            <?php echo $formHtml; ?>
        </div>
        
    </div>
    <?php
}
?>

<script>
    (function($, w) {
        'use strict';

        $(function() {
            $('#vr-cron-see-btn').on('click', function(event) {
                if (!w.configObserver.isChanged()) {
                    // nothing has changed, go ahead
                    return true;
                }

                // ask for a confirmation
                if (!confirm(Joomla.JText._('VRE_CONFIRM_MESSAGE_UNSAVE'))) {
                    // do not leave the page
                    event.preventDefault();
                    event.stopPropagation();
                    return false;
                }
            });
        });
    })(jQuery, window);
</script>