<?php
session_start();
require_once '../inc/admin_auth.php';
requireAdmin();
require_once '../inc/functions.php';

$userId = isset($_GET['user_id']) ? (int)$_GET['user_id'] : 0;
$page = (int)($_GET['page'] ?? 1);
if ($page < 1) $page = 1;
$perPage = 50;
$offset = ($page - 1) * $perPage;

$sql = "SELECT SQL_CALC_FOUND_ROWS l.*, u.username 
        FROM user_action_logs l 
        LEFT JOIN users u ON l.user_id = u.id";
$params = [];
if ($userId) {
    $sql .= " WHERE l.user_id = ?";
    $params[] = $userId;
}
$sql .= " ORDER BY l.created_at DESC LIMIT ? OFFSET ?";
$params[] = $perPage;
$params[] = $offset;

$stmt = $pdo->prepare($sql);
$i = 1;
foreach ($params as $param) {
    $stmt->bindValue($i++, $param, is_int($param) ? PDO::PARAM_INT : PDO::PARAM_STR);
}
$stmt->execute();
$logs = $stmt->fetchAll();

$total = $pdo->query("SELECT FOUND_ROWS()")->fetchColumn();

require_once '../inc/header.php';
?>

<style>
/* === Modern Dark Theme for Action Logs === */
.action-logs-dark {
    font-family: 'Inter', 'Segoe UI', Roboto, system-ui, sans-serif;
    background: #0b0f17;
    color: #e2e8f0;
    padding: 2rem;
    border-radius: 2rem;
    box-shadow: inset 0 0 0 1px rgba(255,255,255,0.05), 0 20px 30px -10px black;
    margin: 1rem 0;
}

/* header */
.logs-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 1rem;
    margin-bottom: 2rem;
}
.logs-header h2 {
    margin: 0;
    font-weight: 600;
    font-size: 2rem;
    background: linear-gradient(135deg, #a5b4fc, #818cf8);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

/* filter bar */
.filter-bar {
    background: rgba(18, 25, 40, 0.8);
    backdrop-filter: blur(8px);
    border: 1px solid rgba(255,255,255,0.05);
    border-radius: 3rem;
    padding: 0.8rem 1.5rem;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 1rem;
    margin-bottom: 2rem;
    box-shadow: 0 8px 16px rgba(0,0,0,0.4);
}
.filter-group {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
}
.filter-group label {
    font-weight: 500;
    color: #cbd5e1;
    font-size: 0.95rem;
}
.filter-group input {
    background: #1e293b;
    border: 1px solid #334155;
    color: #f1f5f9;
    padding: 0.5rem 1.2rem;
    border-radius: 2rem;
    font-size: 0.95rem;
    width: 180px;
}
.btn {
    padding: 0.5rem 1.5rem;
    border-radius: 2rem;
    font-weight: 500;
    font-size: 0.95rem;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    transition: 0.2s;
    border: none;
    cursor: pointer;
    background: transparent;
    color: #e2e8f0;
}
.btn-primary {
    background: linear-gradient(145deg, #4f46e5, #6366f1);
    color: white;
    box-shadow: 0 8px 16px -5px #4f46e5;
}
.btn-primary:hover {
    background: linear-gradient(145deg, #6366f1, #818cf8);
    transform: translateY(-2px);
}
.btn-secondary {
    background: #1e293b;
    color: #e2e8f0;
    border: 1px solid #334155;
}
.btn-secondary:hover {
    background: #2d3a4f;
}

/* table */
.table-responsive {
    overflow-x: auto;
    border-radius: 1.5rem;
    background: #0f172a;
    border: 1px solid #1e293b;
    box-shadow: 0 15px 30px -12px black;
}
.modern-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
    color: #e2e8f0;
}
.modern-table th {
    background: #111c2e;
    color: #a5b4fc;
    font-weight: 600;
    padding: 1rem 0.75rem;
    text-align: left;
    white-space: nowrap;
    border-bottom: 1px solid #1e293b;
}
.modern-table td {
    padding: 1rem 0.75rem;
    border-bottom: 1px solid #1e293b;
    vertical-align: middle;
}
.modern-table tbody tr:hover {
    background: #1a2538;
}
.modern-table tbody tr:last-child td {
    border-bottom: none;
}

/* details column */
.details-cell {
    max-width: 300px;
    white-space: pre-wrap;
    word-break: break-word;
    font-family: 'JetBrains Mono', monospace;
    font-size: 0.8rem;
    background: #0b101a;
    padding: 0.5rem;
    border-radius: 0.75rem;
    border: 1px solid #1e293b;
}

/* pagination & counter */
.pagination {
    margin-top: 2rem;
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    gap: 0.4rem;
}
.page-item {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 2.5rem;
    height: 2.5rem;
    padding: 0 0.2rem;
    border-radius: 9999px;
    background: #1e293b;
    border: 1px solid #334155;
    color: #cbd5e1;
    text-decoration: none;
    font-weight: 500;
    transition: 0.2s;
}
.page-item:hover {
    background: #2d3a4f;
    border-color: #4f46e5;
    color: white;
}
.page-item.active {
    background: #4f46e5;
    border-color: #4f46e5;
    color: white;
}
.record-counter {
    margin-top: 1rem;
    font-size: 0.9rem;
    color: #94a3b8;
    background: #111c2e;
    padding: 0.5rem 1.2rem;
    border-radius: 2rem;
    display: inline-block;
    border: 1px solid #1e293b;
}
</style>

<div class="action-logs-dark">

    <!-- Header with back button -->
    <div class="logs-header">
        <h2>
            <span>📋</span> Логи действий игроков
        </h2>
        <a class="btn btn-secondary" href="index.php">
            ← В админ-панель
        </a>
    </div>

    <!-- Filter bar -->
    <div class="filter-bar">
        <form method="get" style="display: contents;">
            <div class="filter-group">
                <label for="user_id">ID пользователя:</label>
                <input type="number" name="user_id" id="user_id" value="<?= $userId ?: '' ?>" placeholder="0 = все">
            </div>
            <button type="submit" class="btn btn-primary">
                🔍 Применить
            </button>
            <?php if ($userId): ?>
                <a href="user_action_logs.php" class="btn btn-secondary">
                    ✕ Сбросить
                </a>
            <?php endif; ?>
        </form>
    </div>

    <!-- Logs table -->
    <div class="table-responsive">
        <table class="modern-table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Пользователь</th>
                    <th>Действие</th>
                    <th>Детали</th>
                    <th>IP</th>
                    <th>Время</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($logs as $log): ?>
                <tr>
                    <td><code>#<?= $log['id'] ?></code></td>
                    <td>
                        <?= htmlspecialchars($log['username'] ?? '—') ?> 
                        <small>(ID: <?= $log['user_id'] ?>)</small>
                    </td>
                    <td>
                        <span class="badge" style="background:#1e293b;">
                            <?= htmlspecialchars($log['action']) ?>
                        </span>
                    </td>
                    <td>
                        <?php if (!empty($log['details'])): ?>
                            <div class="details-cell"><?= htmlspecialchars($log['details']) ?></div>
                        <?php else: ?>
                            <span class="badge" style="background:transparent;">—</span>
                        <?php endif; ?>
                    </td>
                    <td><code><?= htmlspecialchars($log['ip']) ?></code></td>
                    <td><small><?= date('d.m.Y H:i:s', strtotime($log['created_at'])) ?></small></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>

    <!-- Record counter -->
    <div class="record-counter">
        <?php
        $from = ($page - 1) * $perPage + 1;
        $to = min($page * $perPage, $total);
        if ($total > 0) {
            echo "Показано записей: $from – $to из $total";
        } else {
            echo "Записей не найдено";
        }
        ?>
    </div>

    <!-- Pagination -->
    <?php if ($total > $perPage): ?>
        <div class="pagination">
            <?php for ($i = 1; $i <= ceil($total / $perPage); $i++): ?>
                <a class="page-item <?= ($i == $page) ? 'active' : '' ?>" 
                   href="?user_id=<?= $userId ?>&page=<?= $i ?>">
                    <?= $i ?>
                </a>
            <?php endfor; ?>
        </div>
    <?php endif; ?>

</div> <!-- .action-logs-dark -->

<?php require_once '../inc/foot.php'; ?>