<?php
/**
 * 芋头CMS
 * ============================================================================
 * 版权所有 2016-2028 沅陵县乌托邦网络服务有限公司，并保留所有权利。
 * 网站地址: http://www.yutoucms.cn
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 芋头 <mxgsf@qq.com>
 * Date: 2026-5-20
 */


$page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$pageSize = 15;
$keyword = isset($_GET['keyword']) ? trim($_GET['keyword']) : '';
$actionType = isset($_GET['action_type']) ? trim($_GET['action_type']) : '';
$dateFrom = isset($_GET['date_from']) ? trim($_GET['date_from']) : '';
$dateTo = isset($_GET['date_to']) ? trim($_GET['date_to']) : '';

$where = '1=1';
$params = [];
if (!empty($keyword)) {
    $where .= ' AND (l.action LIKE ? OR l.content LIKE ?)';
    $params[] = '%' . $keyword . '%';
    $params[] = '%' . $keyword . '%';
}
if (!empty($actionType)) {
    $where .= ' AND l.action = ?';
    $params[] = $actionType;
}
if (!empty($dateFrom)) {
    $where .= ' AND l.create_time >= ?';
    $params[] = strtotime($dateFrom);
}
if (!empty($dateTo)) {
    $where .= ' AND l.create_time <= ?';
    $params[] = strtotime($dateTo . ' 23:59:59');
}

$offset = ($page - 1) * $pageSize;
$logs = $db->fetchAll("SELECT l.*, a.username FROM {$db->table('log')} l LEFT JOIN {$db->table('admin')} a ON l.user_id = a.id WHERE {$where} ORDER BY l.id DESC LIMIT {$offset}, {$pageSize}", $params);
$total = $db->fetch("SELECT COUNT(*) as cnt FROM {$db->table('log')} l WHERE {$where}", $params)['cnt'];
$totalPages = ceil($total / $pageSize);

$actionTypes = $db->fetchAll("SELECT DISTINCT action FROM {$db->table('log')} ORDER BY action");
?>
<div class="page-container fade-in">
    <div class="page-header">
        <div class="page-title">
            <div class="page-icon"><svg style="width:22px;height:22px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="8" y="2" width="8" height="4" rx="1" ry="1"/><path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2"/><line x1="8" y1="10" x2="8" y2="10"/><line x1="12" y1="10" x2="12" y2="10"/><line x1="16" y1="10" x2="16" y2="10"/><line x1="8" y1="14" x2="8" y2="14"/><line x1="12" y1="14" x2="12" y2="14"/><line x1="16" y1="14" x2="16" y2="14"/></svg></div>
            <div>
                <h1>操作日志</h1>
                <p style="font-size:12px;color:var(--gray-500);margin:0;">记录后台操作历史</p>
            </div>
        </div>
    </div>
    <div class="stats-grid">
        <div class="stat-card">
            <div class="stat-icon" style="background:var(--primary-bg);color:var(--primary);"><svg style="width:22px;height:22px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="8" y="2" width="8" height="4" rx="1" ry="1"/><path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2"/><line x1="8" y1="10" x2="8" y2="10"/><line x1="12" y1="10" x2="12" y2="10"/><line x1="16" y1="10" x2="16" y2="10"/><line x1="8" y1="14" x2="8" y2="14"/><line x1="12" y1="14" x2="12" y2="14"/><line x1="16" y1="14" x2="16" y2="14"/></svg></div>
            <div class="stat-info">
                <div class="stat-value"><?php echo $total; ?></div>
                <div class="stat-label">日志总数</div>
            </div>
        </div>
    </div>
    <div class="filter-bar">
        <form method="get" style="display:flex;gap:12px;width:100%;flex-wrap:wrap;">
            <input type="hidden" name="act" value="log">
            <input type="text" name="keyword" value="<?php echo htmlspecialchars($keyword); ?>" placeholder="搜索操作内容..." style="flex:1;min-width:150px;">
            <select name="action_type" class="form-control" style="width:140px;">
                <option value="">全部操作</option>
                <?php foreach ($actionTypes as $t): ?>
                <option value="<?php echo htmlspecialchars($t['action']); ?>" <?php echo $actionType == $t['action'] ? 'selected' : ''; ?>><?php echo htmlspecialchars($t['action']); ?></option>
                <?php endforeach; ?>
            </select>
            <input type="date" name="date_from" value="<?php echo htmlspecialchars($dateFrom); ?>" class="form-control" style="width:140px;" placeholder="开始日期">
            <input type="date" name="date_to" value="<?php echo htmlspecialchars($dateTo); ?>" class="form-control" style="width:140px;" placeholder="结束日期">
            <button type="submit" class="btn btn-primary"><svg style="width:14px;height:14px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/></svg> 搜索</button>
            <?php if (!empty($keyword) || !empty($actionType) || !empty($dateFrom) || !empty($dateTo)): ?>
            <a href="?act=log" class="btn btn-secondary"><svg style="width:22px;height:22px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="1 4 1 10 7 10"/><path d="M3.51 15a9 9 0 1 0 2.13-9.36L1 10"/></svg> 重置</a>
            <?php endif; ?>
        </form>
    </div>
    <div class="content-card">
        <?php if (empty($logs)): ?>
        <div class="empty-state">
            <div class="empty-icon"><svg style="width:22px;height:22px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="8" y="2" width="8" height="4" rx="1" ry="1"/><path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2"/><line x1="8" y1="10" x2="8" y2="10"/><line x1="12" y1="10" x2="12" y2="10"/><line x1="16" y1="10" x2="16" y2="10"/><line x1="8" y1="14" x2="8" y2="14"/><line x1="12" y1="14" x2="12" y2="14"/><line x1="16" y1="14" x2="16" y2="14"/></svg></div>
            <h3>暂无日志</h3>
            <p>操作记录会显示在这里</p>
        </div>
        <?php else: ?>
        <table class="data-table">
            <thead>
                <tr>
                    <th class="id-cell">ID</th>
                    <th style="width:100px;">操作者</th>
                    <th>操作内容</th>
                    <th style="width:80px;">IP</th>
                    <th style="width:160px;">时间</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($logs as $item): ?>
                <tr>
                    <td class="id-cell"><?php echo $item['id']; ?></td>
                    <td><span class="badge badge-gray"><?php echo htmlspecialchars($item['username'] ?? '系统'); ?></span></td>
                    <td>
                        <div style="font-weight:500;color:var(--gray-800);"><?php echo htmlspecialchars($item['action']); ?></div>
                        <?php if (!empty($item['content'])): ?>
                        <div style="font-size:11px;color:var(--gray-400);margin-top:4px;"><?php echo htmlspecialchars($item['content']); ?></div>
                        <?php endif; ?>
                    </td>
                    <td style="font-family:monospace;font-size:12px;color:var(--gray-500);"><?php echo $item['ip'] ?? '-'; ?></td>
                    <td style="font-size:12px;color:var(--gray-500);"><?php echo date('Y-m-d H:i:s', $item['create_time']); ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <?php if ($totalPages > 1): ?>
        <div class="pagination" style="padding:16px;">
            <?php if ($page > 1): ?><a href="?act=log&page=<?php echo $page-1; ?>&keyword=<?php echo urlencode($keyword); ?>&action_type=<?php echo urlencode($actionType); ?>&date_from=<?php echo urlencode($dateFrom); ?>&date_to=<?php echo urlencode($dateTo); ?>"><svg style="width:14px;height:14px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="15 18 9 12 15 6"/></svg> 上页</a><?php endif; ?>
            <?php for ($i = max(1, $page-2); $i <= min($totalPages, $page+2); $i++): ?>
            <a href="?act=log&page=<?php echo $i; ?>&keyword=<?php echo urlencode($keyword); ?>&action_type=<?php echo urlencode($actionType); ?>&date_from=<?php echo urlencode($dateFrom); ?>&date_to=<?php echo urlencode($dateTo); ?>" <?php echo $i==$page?'class="active"':''; ?>><?php echo $i; ?></a>
            <?php endfor; ?>
            <?php if ($page < $totalPages): ?><a href="?act=log&page=<?php echo $page+1; ?>&keyword=<?php echo urlencode($keyword); ?>&action_type=<?php echo urlencode($actionType); ?>&date_from=<?php echo urlencode($dateFrom); ?>&date_to=<?php echo urlencode($dateTo); ?>"><svg style="width:14px;height:14px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="9 18 15 12 9 6"/></svg> 下页</a><?php endif; ?>
        </div>
        <?php endif; ?>
        <?php endif; ?>
    </div>
</div>

