<?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!');

?>

<!-- INSTRUCTIONS -->

<div class="cron-instructions">

    <p>To properly configure a cron job you have to follow the instructions below:</p>
        
    <ol>
        <li>Create at least a cron job by clicking the <b>See Cron Jobs List</b> button under the settings tab.</li>
        <li>Pick the right cron job from the apposite dropdown and click the download button.</li>
        <li>Rename the downloaded file with a unique name.</li>
        <li>Create a new cron job from the control panel of your server and assign the downloaded file as script.</li>
    </ol>

    <p>If you have no idea how to create a new cron job from your control panel, you should contact your hosting provider.</p>

</div>

<!-- CRON JOB FILE - Select -->

<?php 
$jobs = JHtml::fetch('vrehtml.admin.cronjobs', $blank = '', $group = true);

// make sure the list contains more than 1 element because the placeholder is always included
if (count($jobs) > 1)
{
    $cronSelectField = $this->formFactory->createField()
        ->type('groupedlist')
        ->hidden(true)
        ->id('vre-cron-select')
        ->options($jobs);

    $cronDownloadField = $this->formFactory->createField()
        ->type('link')
        ->href(VREFactory::getPlatform()->getUri()->addCSRF('index.php?option=com_vikrestaurants&task=configapp.downloadcron', $xhtml = true))
        ->target('_blank')
        ->id('cron-download-link')
        ->text('<i class="fas fa-download big"></i>')
        ->class('disabled')
        ->hidden(true);

    echo $this->formFactory->createField()
        ->label('Download Runnable File')
        ->render(function($data, $input) use ($cronSelectField, $cronDownloadField) {
            ?>
            <div class="multi-field first-flex">
                <?php echo $cronSelectField; ?>
                <?php echo $cronDownloadField; ?>
            </div>
            <?php
        });
}
else
{
    echo $this->formFactory->createField()
        ->type('alert')
        ->style('warning')
        ->hiddenLabel(true)
        ->text(JText::translate('VRECONFIGCRONMISSING'));
}
?>

<script>
    (function($, w) {
        $(function() {
            $('#vre-cron-select').select2({
                placeholder: '--',
                allowClear: false,
                width: 300,
            }).on('change', function() {
                const cronId = $('#vre-cron-select').val();

                if (cronId) {
                    $('#cron-download-link').removeClass('disabled');
                } else {
                    $('#cron-download-link').addClass('disabled');
                }
            });

            // exclude download select from observer
            w.configObserver.exclude('#vre-cron-select');

            $('#cron-download-link').on('click', function(event) {
                const cronId = $('#vre-cron-select').val();

                if (!cronId) {
                    event.preventDefault();
                    event.stopPropagation();
                    return false;
                }

                $(this).attr('href', $(this).attr('href') + '&id_cron=' + cronId);
                return true;
            });
        });
    })(jQuery, window);
</script>