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

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

{% set active_menu = 'system' %}

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

{% block content %}
    {% set cron = admin.cron_info %}
    {% set params = admin.extension_config_get({ 'ext': 'mod_cron' }) %}
    <div class="card">
        <div class="card-header">
            <div>
                <h3 class="card-title">{{ 'Cron Settings'|trans }}</h3>
                <p class="card-subtitle">{{ 'Review cron status, confirm your schedule, and control whether the guest cron endpoint is available.'|trans }}</p>
            </div>
        </div>
        <div class="card-body">
            <div class="alert alert-info" role="alert">
                <strong class="me-1">{{ 'Recommended Schedule:'|trans }}</strong><code>*/5 * * * * php {{ cron.cron_path }}</code>
            </div>

            <div class="row row-cards">
                <div class="col-xl-3 col-md-6">
                    <section class="card card-sm h-100">
                        <div class="card-body">
                            <div class="text-secondary small mb-1">{{ 'Timezone'|trans }}</div>
                            <div class="fw-semibold">{{ 'now'|format_date(pattern='zzz') }}</div>
                        </div>
                    </section>
                </div>
                <div class="col-xl-3 col-md-6">
                    <section class="card card-sm h-100">
                        <div class="card-body">
                            <div class="text-secondary small mb-1">{{ 'Time on Server'|trans }}</div>
                            <div class="fw-semibold">{{ 'now'|format_datetime }}</div>
                        </div>
                    </section>
                </div>
                <div class="col-xl-6">
                    <section class="card card-sm h-100">
                        <div class="card-body">
                            <div class="text-secondary small mb-1">{{ 'Last Scheduled Execution'|trans }}</div>
                            {% if cron.last_cron_exec %}
                                <div class="fw-semibold">{{ cron.last_cron_exec|format_datetime }}</div>
                                <div class="text-secondary">{{ cron.last_cron_exec|timeago }} {{ 'ago'|trans }}</div>
                            {% else %}
                                <div class="fw-semibold">{{ 'Scheduled tasks were never executed'|trans }}</div>
                            {% endif %}
                        </div>
                    </section>
                </div>
            </div>
        </div>
        <div class="card-body border-top">
            <form id="guest-cron-form" method="post" action="{{ 'cron/save_settings'|api_url }}" {{ fb_api_form({message: 'Configuration Updated'|trans, reload: true}) }}>
                <input type="hidden" name="ext" value="mod_cron">
                <div class="d-flex flex-column flex-lg-row align-items-lg-start justify-content-between gap-3 mb-3">
                    <div>
                        <h4 class="card-title mb-2">{{ 'Guest Cron Endpoint'|trans }}</h4>
                        <p class="text-secondary mb-0">{{ 'Toggle to enable or disable the guest cron URL. A security hash is generated automatically when enabled.'|trans }}</p>
                    </div>
                    <label class="form-check form-switch form-switch-2 m-0">
                        <input class="form-check-input" name="guest_cron" type="checkbox" {% if params.guest_cron %}checked="checked"{% endif %} onchange="document.getElementById('guest-cron-form').requestSubmit()">
                        <span class="form-check-label">{{ 'Enable Guest Access'|trans }}</span>
                    </label>
                </div>
                {% if params.guest_cron and params.cron_hash %}
                    <label class="form-label" for="guest-cron-url">{{ 'Guest Cron URL'|trans }}</label>
                    <input id="guest-cron-url" class="form-control font-monospace mb-2" type="text" value="{{ 'cron/run'|api_url(role: 'guest', query: {hash: params.cron_hash}) }}" readonly>
                    <div class="d-flex justify-content-end mb-3">
                        <a class="btn btn-outline-warning" href="{{ 'cron/regenerate_cron_hash'|api_url }}" {{ fb_api_link({message: 'Hash Regenerated'|trans, reload: true}) }}>
                            {{ 'Regenerate Hash'|trans }}
                        </a>
                    </div>
                {% endif %}
            </form>
        </div>
        <div class="card-footer d-flex justify-content-end">
            <a class="btn btn-primary" href="{{ 'cron/run'|api_url }}" target="_blank" {{ fb_api_link({message: 'Cron Executed'|trans}) }}>
                <svg class="icon icon-tabler">
                    <use xlink:href="#play"/>
                </svg>
                <span>{{ 'Execute Now'|trans }}</span>
            </a>
        </div>
    </div>
{% endblock %}
