{include file="sections/header.tpl"}

<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title">
                        <i class="fa fa-clock-o"></i> {Lang::T('Cron Job Logs')}
                    </h5>
                    <div class="btn-group pull-right">
                        <a href="{$_url}logs/cron" class="btn btn-primary btn-sm">
                            <i class="fa fa-refresh"></i> Refresh
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    
                    <!-- Statistics Overview -->
                    <div class="row mb-4">
                        <div class="col-md-3">
                            <div class="card bg-primary text-white">
                                <div class="card-body">
                                    <h4>{$stats.total_runs}</h4>
                                    <p>Total Runs (7 days)</p>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="card bg-success text-white">
                                <div class="card-body">
                                    <h4>{$stats.successful_runs}</h4>
                                    <p>Successful Runs</p>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="card bg-danger text-white">
                                <div class="card-body">
                                    <h4>{$stats.failed_runs}</h4>
                                    <p>Failed Runs</p>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="card bg-info text-white">
                                <div class="card-body">
                                    <h4>{$stats.avg_execution_time}s</h4>
                                    <p>Avg Execution Time</p>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Last Run Information -->
                    <div class="alert alert-info">
                        <h6><i class="fa fa-info-circle"></i> Last Run Information</h6>
                        <p><strong>Last Run:</strong> {if $stats.last_run}{$stats.last_run}{else}Never{/if}</p>
                        <p><strong>Last Success:</strong> {if $stats.last_success}{$stats.last_success}{else}Never{/if}</p>
                        <p><strong>Last Failure:</strong> {if $stats.last_failure}{$stats.last_failure}{else}Never{/if}</p>
                    </div>

                    <!-- Filters -->
                    <div class="row mb-3">
                        <div class="col-md-12">
                            <form method="post" action="{$_url}logs/cron" class="form-inline">
                                <div class="form-group mr-2">
                                    <input type="text" name="q" class="form-control" placeholder="Search error messages..." value="{$q}">
                                </div>
                                <div class="form-group mr-2">
                                    <select name="cron_type" class="form-control">
                                        <option value="">All Types</option>
                                        <option value="main" {if $cron_type eq 'main'}selected{/if}>Main Cron</option>
                                        <option value="reminder" {if $cron_type eq 'reminder'}selected{/if}>Reminder Cron</option>
                                    </select>
                                </div>
                                <div class="form-group mr-2">
                                    <select name="status" class="form-control">
                                        <option value="">All Status</option>
                                        <option value="completed" {if $status eq 'completed'}selected{/if}>Completed</option>
                                        <option value="failed" {if $status eq 'failed'}selected{/if}>Failed</option>
                                        <option value="running" {if $status eq 'running'}selected{/if}>Running</option>
                                    </select>
                                </div>
                                <button type="submit" class="btn btn-primary">
                                    <i class="fa fa-search"></i> Filter
                                </button>
                                <a href="{$_url}logs/cron" class="btn btn-default">
                                    <i class="fa fa-times"></i> Clear
                                </a>
                            </form>
                        </div>
                    </div>

                    <!-- Logs Table -->
                    <div class="table-responsive">
                        <table class="table table-striped table-hover">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Type</th>
                                    <th>Started</th>
                                    <th>Finished</th>
                                    <th>Status</th>
                                    <th>Duration</th>
                                    <th>Expired Users</th>
                                    <th>Notifications</th>
                                    <th>Auto Renewals</th>
                                    <th>Memory</th>
                                    <th>Error</th>
                                </tr>
                            </thead>
                            <tbody>
                                {foreach from=$d item=log}
                                <tr class="{if $log.status == 'completed'}success{elseif $log.status == 'failed'}danger{else}warning{/if}">
                                    <td>{$log.id}</td>
                                    <td>
                                        <span class="label label-{if $log.cron_type == 'main'}primary{else}info{/if}">
                                            {$log.cron_type|upper}
                                        </span>
                                    </td>
                                    <td>{$log.started_at}</td>
                                    <td>{if $log.finished_at}{$log.finished_at}{else}-{/if}</td>
                                    <td>
                                        <span class="label label-{if $log.status == 'completed'}success{elseif $log.status == 'failed'}danger{else}warning{/if}">
                                            {$log.status|upper}
                                        </span>
                                    </td>
                                    <td>
                                        {if $log.execution_time}
                                            {$log.execution_time}s
                                        {else}
                                            -
                                        {/if}
                                    </td>
                                    <td>
                                        <span class="badge badge-info">{$log.expired_users_processed}</span>
                                        {if $log.expired_users_found != $log.expired_users_processed}
                                            <small class="text-muted">({$log.expired_users_found} found)</small>
                                        {/if}
                                    </td>
                                    <td>
                                        <span class="badge badge-success">{$log.notifications_sent}</span>
                                    </td>
                                    <td>
                                        {if $log.auto_renewals_attempted > 0}
                                            <span class="badge badge-warning">{$log.auto_renewals_successful}/{$log.auto_renewals_attempted}</span>
                                        {else}
                                            -
                                        {/if}
                                    </td>
                                    <td>
                                        {if $log.memory_usage}
                                            {$log.memory_usage}
                                        {else}
                                            -
                                        {/if}
                                    </td>
                                    <td>
                                        {if $log.error_message}
                                            <span class="text-danger" title="{$log.error_message}">
                                                <i class="fa fa-exclamation-triangle"></i>
                                            </span>
                                        {else}
                                            -
                                        {/if}
                                    </td>
                                </tr>
                                {foreachelse}
                                <tr>
                                    <td colspan="11" class="text-center text-muted">
                                        <i class="fa fa-info-circle"></i> No cron logs found
                                    </td>
                                </tr>
                                {/foreach}
                            </tbody>
                        </table>
                    </div>

                    <!-- Pagination -->
                    {include file="pagination.tpl"}

                    <!-- Legend -->
                    <div class="mt-3">
                        <h6>Legend:</h6>
                        <ul class="list-inline">
                            <li><span class="label label-success">COMPLETED</span> - Cron job finished successfully</li>
                            <li><span class="label label-danger">FAILED</span> - Cron job encountered an error</li>
                            <li><span class="label label-warning">RUNNING</span> - Cron job is currently running</li>
                        </ul>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>

{include file="sections/footer.tpl"}
