<?php
ob_start();
/**
 * 2026企业CMS - 后台操作日志
 */
require_once __DIR__ . '/header.php';
check_permission('admin');
$pageTitle = '操作日志';

// 处理清空日志
if (isset($_GET['clear']) && $_GET['clear'] == 'all') {
    check_permission('superadmin');
    db_exec("DELETE FROM `logs` WHERE `id` > 0");
    add_log('clear', 'logs', '清空所有日志');
    header('Location: /admin/logs.php?msg=' . urlencode('日志已清空！'));
    exit;
}

// 筛选
$where = '1=1';
$params = [];
if (!empty($_GET['admin_id'])) {
    $where .= ' AND l.`admin_id`=?';
    $params[] = intval($_GET['admin_id']);
}
if (!empty($_GET['action'])) {
    $where .= ' AND l.`action` LIKE ?';
    $params[] = '%' . $_GET['action'] . '%';
}
if (!empty($_GET['module'])) {
    $where .= ' AND l.`module`=?';
    $params[] = $_GET['module'];
}

$page = max(1, intval($_GET['page'] ?? 1));
$pageSize = 30;
$total = db_count('logs l', $where, $params);
$offset = ($page - 1) * $pageSize;
$logs = db_query("SELECT l.*,a.`username` FROM `logs` l LEFT JOIN `admins` a ON l.`admin_id`=a.`id` WHERE {$where} ORDER BY l.`id` DESC LIMIT {$offset},{$pageSize}", $params);
$admins = db_query("SELECT `id`,`username` FROM `admins` ORDER BY `id` ASC");
?>
<div class="page-header">
    <h2>操作日志</h2>
    <?php if ($_SESSION['admin_role'] === 'superadmin'): ?>
    <a href="javascript:void(0)" onclick="if(confirm('确定要清空所有日志吗？此操作不可恢复！')){location.href='/admin/logs.php?clear=all'}" class="btn btn-danger">清空日志</a>
    <?php endif; ?>
</div>

<!-- 筛选 -->
<form method="get" class="search-bar" style="margin-bottom:20px;">
    <select name="admin_id" style="padding:8px;border:1px solid #ddd;border-radius:4px;">
        <option value="">全部管理员</option>
        <?php foreach ($admins as $ad): ?>
        <option value="<?php echo $ad['id']; ?>" <?php echo ($_GET['admin_id'] ?? '')==$ad['id']?'selected':''; ?>><?php echo xss_clean($ad['username']); ?></option>
        <?php endforeach; ?>
    </select>
    <input type="text" name="action" placeholder="操作动作关键词" value="<?php echo xss_clean($_GET['action'] ?? ''); ?>">
    <input type="text" name="module" placeholder="模块名称" value="<?php echo xss_clean($_GET['module'] ?? ''); ?>">
    <button type="submit">筛选</button>
    <a href="/admin/logs.php" class="btn" style="padding:8px 15px;">重置</a>
</form>

<div class="table-wrapper">
    <table>
        <thead><tr><th>ID</th><th>管理员</th><th>操作</th><th>模块</th><th>说明</th><th>IP</th><th>时间</th></tr></thead>
        <tbody>
            <?php foreach ($logs as $log): ?>
            <tr>
                <td><?php echo $log['id']; ?></td>
                <td><?php echo xss_clean($log['username'] ?? '系统'); ?></td>
                <td><span style="color:#2c82e0;font-weight:bold;"><?php echo xss_clean($log['action']); ?></span></td>
                <td><?php echo xss_clean($log['module']); ?></td>
                <td style="max-width:250px;overflow:hidden;text-overflow:<?php echo xss_clean(str_cut($log['description'] ?? '', 40)); ?>"><?php echo xss_clean($log['description'] ?? ''); ?></td>
                <td style="font-size:12px;color:#999;"><?php echo xss_clean($log['ip']); ?></td>
                <td><?php echo date('m-d H:i', strtotime($log['created_at'])); ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

<?php if ($total > $pageSize): $totalPage = ceil($total/$pageSize); ?>
<div class="pagination">
    <?php for ($i=1; $i<=$totalPage; $i++): ?><a href="/admin/logs.php?page=<?php echo $i; ?>" <?php echo $i==$page?'class="current"':''; ?>><?php echo $i; ?></a><?php endfor; ?>
</div>
<?php endif; ?>

<div style="margin-top:15px;color:#999;font-size:12px;">
    共 <?php echo $total; ?> 条日志记录 | 仅保留最近 1000 条记录
</div>
<?php require_once __DIR__ . '/footer.php'; ?>
