<?php
include __DIR__ . '/auth.php';
admin_check();
include __DIR__ . '/layout.php';

$log = __DIR__ . '/../cache/oplog/yanscms.log';
$message = '';
$error = '';

function oplog_read_lines($log){
    return is_file($log) ? file($log, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) : array();
}
function oplog_write_lines($log, $lines){
    $dir = dirname($log);
    if(!is_dir($dir)) @mkdir($dir, 0777, true);
    $content = $lines ? implode("\n", $lines) . "\n" : '';
    return file_put_contents($log, $content, LOCK_EX) !== false;
}

if($_SERVER['REQUEST_METHOD'] === 'POST'){
    $action = $_POST['action'] ?? '';
    if($action === 'clear'){
        if(oplog_write_lines($log, array())){
            admin_oplog('clear_oplog', '清空操作日志');
            $message = '操作日志已清空。';
        }else{
            $error = '清空失败，请检查 cache/oplog 目录权限。';
        }
    }elseif($action === 'delete'){
        $selected = $_POST['selected'] ?? array();
        if(!is_array($selected)) $selected = array();
        $selected = array_flip(array_map('strval', $selected));
        $lines = oplog_read_lines($log);
        $kept = array();
        $deleted = 0;
        foreach($lines as $idx=>$line){
            $key = md5($idx . '|' . $line);
            if(isset($selected[$key])){
                $deleted++;
                continue;
            }
            $kept[] = $line;
        }
        if($deleted <= 0){
            $error = '请先勾选需要删除的日志。';
        }elseif(oplog_write_lines($log, $kept)){
            admin_oplog('delete_oplog', '删除操作日志 ' . $deleted . ' 条');
            $message = '已删除 ' . $deleted . ' 条操作日志。';
        }else{
            $error = '删除失败，请检查 cache/oplog 目录权限。';
        }
    }
}

$allLines = oplog_read_lines($log);
$recent = array_slice($allLines, -300, null, true);
$lines = array_reverse($recent, true);
admin_header('操作日志');
?>
<?php if($message): ?><div class="success"><?php echo admin_h($message); ?></div><?php endif; ?>
<?php if($error): ?><div class="error"><?php echo admin_h($error); ?></div><?php endif; ?>

<div class="card">
    <h2>最近操作日志</h2>
    <form method="post" id="oplogForm">
        <div class="toolbar line-actions">
            <button type="button" class="btn small ghost" onclick="document.querySelectorAll('.oplog-check').forEach(function(c){c.checked=true;});">全选</button>
            <button type="button" class="btn small ghost" onclick="document.querySelectorAll('.oplog-check').forEach(function(c){c.checked=false;});">取消</button>
            <button class="btn small danger" name="action" value="delete">删除勾选</button>
            <button class="btn small danger ghost" name="action" value="clear" data-confirm="确定清空全部操作日志吗？此操作不可恢复。">清空全部</button>
        </div>
        <table>
            <tr><th style="width:46px">选择</th><th>序号</th><th class="oplog-time">时间</th><th>IP</th><th>操作</th><th>详情</th></tr>
            <?php if(!$lines): ?>
            <tr><td colspan="6" class="muted">暂无操作日志。</td></tr>
            <?php endif; ?>
            <?php $i=1; foreach($lines as $idx=>$line): $p=explode("\t", $line); $key=md5($idx . '|' . $line); ?>
            <tr>
                <td><input class="oplog-check" type="checkbox" name="selected[]" value="<?php echo admin_h($key); ?>"></td>
                <td><?php echo $i++; ?></td>
                <td class="oplog-time"><?php echo admin_h($p[0]??''); ?></td>
                <td><?php echo admin_h($p[1]??''); ?></td>
                <td><?php echo admin_h($p[2]??''); ?></td>
                <td><?php echo admin_h($p[3]??''); ?></td>
            </tr>
            <?php endforeach; ?>
        </table>
    </form>
</div>
<?php admin_footer(); ?>
