<?php
/**
 * (c) ININOUT & PYXIS. All Rights Reserved.
 * Wszelkie modyfikacje tego pliku wymagaja zgloszenia oraz zgody autorow.
 * Any modifications to this file require notification and consent of the authors.
 */
/**
 * i-BOK 5 - Przeglądanie logów (admin)
 */

// Obsługa usuwania pliku logu
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_log'])) {
    if (csrf_validate()) {
        $file_to_delete = basename($_POST['logfile'] ?? '');
        $path = IBOK_LOGS_DIR . '/' . $file_to_delete;
        if (file_exists($path) && strpos(realpath($path), realpath(IBOK_LOGS_DIR)) === 0) {
            @unlink($path);
            $del_msg = __('admin_logs.deleted');
        }
    }
}

// Obsługa podglądu
$view_file = '';
$view_content = '';
if (isset($_GET['view'])) {
    $view_file = basename($_GET['view']);
    $path = IBOK_LOGS_DIR . '/' . $view_file;
    if (file_exists($path) && strpos(realpath($path), realpath(IBOK_LOGS_DIR)) === 0) {
        $view_content = file_get_contents($path);
    }
}

// Lista plików logów
$log_files = [];
$files = glob(IBOK_LOGS_DIR . '/*.log');
if ($files) {
    foreach ($files as $f) {
        $name = basename($f);
        if ($name === '.htaccess') continue;
        $log_files[] = [
            'name' => $name,
            'size' => filesize($f),
            'mtime' => filemtime($f),
        ];
    }
    usort($log_files, function($a, $b) { return $b['mtime'] - $a['mtime']; });
}
?>

<div class="fade-in">
    <h4 class="mb-4">
        <i class="bi bi-journal-text me-2"></i> <?= __('admin_logs.title') ?>
    </h4>

    <?= ibok_help_section('admin_logi', __('help.show_help'), __('help_admin.logi_section_content')) ?>

    <?php if (!empty($del_msg)): ?>
        <div class="alert alert-success alert-dismissible fade show">
            <?= e($del_msg) ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>

    <?php if (!empty($view_content)): ?>
    <!-- Podgląd pliku -->
    <div class="card mb-4">
        <div class="card-header d-flex justify-content-between">
            <span><i class="bi bi-file-text me-1"></i> <?= e($view_file) ?></span>
            <a href="admin.php?page=logi" class="btn btn-sm btn-outline-secondary">
                <i class="bi bi-arrow-left me-1"></i> <?= __('common.back') ?>
            </a>
        </div>
        <div class="card-body p-0">
            <pre class="p-3 mb-0" style="max-height: 500px; overflow: auto; font-size: 0.8rem; background: #f8f9fa;"><?= e($view_content) ?></pre>
        </div>
    </div>
    <?php endif; ?>

    <!-- Lista plików -->
    <div class="card">
        <div class="card-body">
            <?php if (empty($log_files)): ?>
                <p class="text-muted text-center py-4"><?= __('admin_logs.no_logs') ?></p>
            <?php else: ?>
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th><?= __('admin_logs.filename') ?></th>
                                <th><?= __('admin_logs.size') ?></th>
                                <th><?= __('admin_logs.modified') ?></th>
                                <th class="text-center"><?= __('common.actions') ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($log_files as $lf): ?>
                            <tr>
                                <td>
                                    <i class="bi bi-file-text me-1"></i>
                                    <a href="admin.php?page=logi&view=<?= urlencode($lf['name']) ?>"><?= e($lf['name']) ?></a>
                                </td>
                                <td><?= number_format($lf['size'] / 1024, 1) ?> KB</td>
                                <td><?= date('d.m.Y H:i:s', $lf['mtime']) ?></td>
                                <td class="text-center">
                                    <a href="admin.php?page=logi&view=<?= urlencode($lf['name']) ?>"
                                       class="btn btn-sm btn-outline-primary me-1" title="<?= __('admin_logs.view') ?>">
                                        <i class="bi bi-eye"></i>
                                    </a>
                                    <form method="post" action="admin.php?page=logi" class="d-inline form-confirm-delete"
                                          data-confirm="<?= e(__('admin_logs.confirm_delete')) ?>">
                                        <?= csrf_field() ?>
                                        <input type="hidden" name="delete_log" value="1">
                                        <input type="hidden" name="logfile" value="<?= e($lf['name']) ?>">
                                        <button type="submit" class="btn btn-sm btn-outline-danger" title="<?= __('admin_logs.delete') ?>">
                                            <i class="bi bi-trash"></i>
                                        </button>
                                    </form>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>
<script nonce="<?= csp_nonce() ?>">
document.querySelectorAll('.form-confirm-delete').forEach(function(form) {
    form.addEventListener('submit', function(e) {
        if (!confirm(this.getAttribute('data-confirm'))) e.preventDefault();
    });
});
</script>
