{extends file="layout.tpl"}

{block name="content"}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h4 class="mb-1">Harmonogram zadań</h4>
            <p class="text-muted mb-0">Zarządzaj automatycznymi zadaniami systemu</p>
        </div>
        <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#jobModal" onclick="resetJobForm()">
            <i class="bi bi-plus-lg me-1"></i>Dodaj zadanie
        </button>
    </div>

    <!-- Status crona -->
    <div class="card mb-4">
        <div class="card-body">
            <div class="row align-items-center">
                <div class="col-md-8">
                    <h6 class="mb-2">Status systemowego crona</h6>
                    <div class="d-flex gap-4">
                        <span>
                            {if $cronStatus.installed}
                            <i class="bi bi-check-circle text-success me-1"></i>Zainstalowany
                            {else}
                            <i class="bi bi-x-circle text-danger me-1"></i>Niezainstalowany
                            {/if}
                        </span>
                        <span>
                            {if $cronStatus.running}
                            <i class="bi bi-check-circle text-success me-1"></i>Uruchomiony
                            {else}
                            <i class="bi bi-x-circle text-danger me-1"></i>Zatrzymany
                            {/if}
                        </span>
                        <span>
                            {if $cronStatus.entry_exists}
                            <i class="bi bi-check-circle text-success me-1"></i>Wpis w crontab
                            {else}
                            <i class="bi bi-x-circle text-warning me-1"></i>Brak wpisu w crontab
                            {/if}
                        </span>
                    </div>
                </div>
                <div class="col-md-4 text-end">
                    {if !$cronStatus.entry_exists}
                    <div class="alert alert-warning mb-0 text-start small">
                        <strong>Dodaj do crontab:</strong><br>
                        <code>* * * * * php {$smarty.const.BASE_PATH|default:'/var/www/html'}/cron.php</code>
                    </div>
                    {/if}
                </div>
            </div>
        </div>
    </div>

    <!-- Lista zadań -->
    <div class="card">
        <div class="card-header">
            <h5 class="card-title mb-0"><i class="bi bi-list-check me-2"></i>Zadania</h5>
        </div>
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead>
                        <tr>
                            <th>Nazwa</th>
                            <th>Typ</th>
                            <th>Harmonogram</th>
                            <th>Ostatnie uruchomienie</th>
                            <th>Status</th>
                            <th class="text-end">Akcje</th>
                        </tr>
                    </thead>
                    <tbody>
                        {foreach $jobs as $job}
                        <tr>
                            <td><strong>{$job.name}</strong></td>
                            <td>
                                <span class="badge bg-secondary">
                                    {$availableJobs[$job.type].name|default:$job.type}
                                </span>
                            </td>
                            <td><code>{$job.schedule}</code></td>
                            <td>
                                {if $job.last_run}
                                {$job.last_run|date_format:"%d.%m.%Y %H:%M"}
                                {else}
                                <span class="text-muted">Nigdy</span>
                                {/if}
                            </td>
                            <td>
                                {if $job.enabled}
                                <span class="badge bg-success">Aktywne</span>
                                {else}
                                <span class="badge bg-secondary">Wyłączone</span>
                                {/if}
                            </td>
                            <td class="text-end">
                                <form method="post" action="{$baseUrl}/admin/cron/run" class="d-inline">
                                    <input type="hidden" name="csrf_token" value="{$csrfToken}">
                                    <input type="hidden" name="job_id" value="{$job.id}">
                                    <button type="submit" class="btn btn-sm btn-outline-success" title="Uruchom teraz">
                                        <i class="bi bi-play"></i>
                                    </button>
                                </form>
                                <button type="button" class="btn btn-sm btn-outline-primary" title="Edytuj" 
                                        onclick="editJob({$job|json_encode|escape:'html'})">
                                    <i class="bi bi-pencil"></i>
                                </button>
                                <form method="post" action="{$baseUrl}/admin/cron/delete" class="d-inline">
                                    <input type="hidden" name="csrf_token" value="{$csrfToken}">
                                    <input type="hidden" name="job_id" value="{$job.id}">
                                    <button type="submit" class="btn btn-sm btn-outline-danger" title="Usuń" 
                                            onclick="return confirm('Usunąć to zadanie?')">
                                        <i class="bi bi-trash"></i>
                                    </button>
                                </form>
                            </td>
                        </tr>
                        {foreachelse}
                        <tr>
                            <td colspan="6" class="text-center py-5 text-muted">
                                <i class="bi bi-calendar-x fs-1 d-block mb-3"></i>
                                Brak zaplanowanych zadań
                            </td>
                        </tr>
                        {/foreach}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Dostępne typy zadań -->
    <div class="card mt-4">
        <div class="card-header">
            <h5 class="card-title mb-0"><i class="bi bi-puzzle me-2"></i>Dostępne typy zadań</h5>
        </div>
        <div class="card-body">
            <div class="row">
                {foreach $availableJobs as $key => $jobType}
                <div class="col-md-4 mb-3">
                    <div class="border rounded p-3 h-100">
                        <h6 class="mb-1">{$jobType.name}</h6>
                        <small class="text-muted">{$jobType.description}</small>
                        <div class="mt-2">
                            <code class="small">{$key}</code>
                        </div>
                    </div>
                </div>
                {/foreach}
            </div>
        </div>
    </div>
</div>

<!-- Modal dodawania/edycji zadania -->
<div class="modal fade" id="jobModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <form method="post" action="{$baseUrl}/admin/cron/save">
                <input type="hidden" name="csrf_token" value="{$csrfToken}">
                <input type="hidden" name="job_id" id="jobId" value="">
                
                <div class="modal-header">
                    <h5 class="modal-title" id="jobModalTitle">Nowe zadanie</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Nazwa zadania</label>
                        <input type="text" name="name" id="jobName" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Typ zadania</label>
                        <select name="type" id="jobType" class="form-select" required>
                            <option value="">Wybierz...</option>
                            {foreach $availableJobs as $key => $jobType}
                            <option value="{$key}">{$jobType.name}</option>
                            {/foreach}
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Harmonogram (cron)</label>
                        <input type="text" name="schedule" id="jobSchedule" class="form-control" placeholder="*/5 * * * *" required>
                        <small class="text-muted">
                            Przykłady: <code>* * * * *</code> (co minutę), 
                            <code>0 * * * *</code> (co godzinę), 
                            <code>0 0 * * *</code> (codziennie o północy)
                        </small>
                    </div>
                    <div class="form-check">
                        <input type="checkbox" name="enabled" id="jobEnabled" class="form-check-input" value="1" checked>
                        <label class="form-check-label" for="jobEnabled">Zadanie aktywne</label>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Anuluj</button>
                    <button type="submit" class="btn btn-primary">Zapisz</button>
                </div>
            </form>
        </div>
    </div>
</div>
{/block}

{block name="scripts"}
<script>
function resetJobForm() {
    document.getElementById('jobModalTitle').textContent = 'Nowe zadanie';
    document.getElementById('jobId').value = '';
    document.getElementById('jobName').value = '';
    document.getElementById('jobType').value = '';
    document.getElementById('jobSchedule').value = '';
    document.getElementById('jobEnabled').checked = true;
}

function editJob(job) {
    document.getElementById('jobModalTitle').textContent = 'Edytuj zadanie';
    document.getElementById('jobId').value = job.id;
    document.getElementById('jobName').value = job.name;
    document.getElementById('jobType').value = job.type;
    document.getElementById('jobSchedule').value = job.schedule;
    document.getElementById('jobEnabled').checked = job.enabled;
    
    new bootstrap.Modal(document.getElementById('jobModal')).show();
}
</script>
{/block}
