{% extends request.ajax ? 'layout_blank.html.twig' : 'layout_default.html.twig' %}

{% import 'macro_functions.html.twig' as mf %}

{% block meta_title %}{{ 'Cron settings'|trans }}{% endblock %}

{% set active_menu = 'system' %}

{% block breadcrumbs %}
    <ol class="breadcrumb">
        <li class="breadcrumb-item">
            <a href="{{ '/'|alink }}">
                <svg class="icon">
                    <use xlink:href="#home" />
                </svg>
            </a>
        </li>
        <li class="breadcrumb-item">
            <a href="{{ 'system'|alink }}">{{ 'Settings'|trans }}</a>
        </li>
        <li class="breadcrumb-item active" aria-current="page">{{ 'Cron settings'|trans }}</li>
    </ol>
{% endblock %}

{% block content %}
    <div class="card">
        <div class="card-header">
            <div>
                <h3 class="card-title">{{ 'Cron settings'|trans }}</h3>
            </div>
        </div>
        <div class="card-body border-bottom">
            <span class="card-subtitle">{{ 'Scheduled tasks must be enabled immediately after installation. Cron is responsible for sending your clients reminder about payments, domain renewal notices and many more.'|trans }}</span>
        </div>
        {% set cron = admin.cron_info %}
        <div class="table-responsive">
            <table class="table card-table table-vcenter table-striped text-nowrap">
                <tbody>
                <tr>
                    <td class="w-50 text-end">{{ 'Timezone'|trans }}:</td>
                    <td>{{ "now"|format_date(pattern='zzz') }}</td>
                </tr>
                <tr>
                    <td class="text-end">{{ 'Time on server'|trans }}:</td>
                    <td>{{ "now"|format_datetime }}</td>
                </tr>
                <tr>
                    <td class="text-end">{{ 'Last time scheduled tasks were executed'|trans }}:</td>
                    <td>
                        {% if cron.last_cron_exec %}
                            {{ cron.last_cron_exec|format_datetime }}
                            ({{ cron.last_cron_exec|timeago }} ago)
                        {% else %}
                            {{ 'Scheduled tasks were never executed'|trans }}
                        {% endif %}
                    </td>
                </tr>
                <tr>
                    <td class="text-end">{{ 'Unix crontab setup'|trans }}:</td>
                    <td>*/5 * * * * php {{ cron.cron_path }}</td>
                </tr>
                </tbody>
            </table>
        </div>
        <div class="card-body container">
            {% set params = admin.extension_config_get({ 'ext': 'mod_cron' }) %}
            <form method="post" action="{{ 'api/admin/extension/config_save'|link }}" class="api-form" data-api-msg="{{ 'Configuration updated'|trans }}">
                <input type="hidden" name="CSRFToken" value="{{ CSRFToken }}">
                <input type="hidden" name="ext" value="mod_cron">
                <div class="row">
                    <div class="col-3">
                        <label>{{ 'Enable the cron API endpoint for guests'|trans }}</label>
                    </div>
                    <div class="col-1">
                        <label class="form-check form-switch">
                            <input class="form-check-input" name="guest_cron" type="checkbox" {% if params.guest_cron %} checked="checked"{% endif %}>
                        </label>
                    </div>

                    <p class="text-muted">{{ 'If enabled, this URL can be used to execute the cronjob:'|trans }} {{ 'api/guest/cron/run'|link }}</p>
                    <input class="btn btn-primary w-25" type="submit" value="{{ 'Update'|trans }}">
                </div>
            </form>
        </div>
        <div class="card-footer text-center">
            <a href="{{'api/admin/cron/run'|link({ 'CSRFToken': CSRFToken }) }}" class="btn btn-primary api-link" target="_blank" data-api-msg="{{ 'Cron executed'|trans }}">
                <svg class="icon icon-tabler">
                    <use xlink:href="#play"/>
                </svg>
                <span>{{ 'Execute now'|trans }}</span>
            </a>
        </div>
    </div>
{% endblock %}
