<?php
/**
 * HuSNS - 敏感词管理后台页面
 */
?>
<div class="admin-content">
    <div class="admin-header">
        <h1>敏感词管理</h1>
        <button class="btn btn-primary" data-toggle="modal" data-target="#addModal">
            <i class="fa fa-plus"></i> 添加敏感词
        </button>
    </div>
    
    <!-- 搜索 -->
    <div class="admin-search">
        <form method="get" action="">
            <div class="input-group">
                <input type="text" name="keyword" class="form-control" placeholder="搜索敏感词" 
                       value="<?php echo htmlspecialchars($keyword); ?>">
                <button type="submit" class="btn btn-default">搜索</button>
            </div>
        </form>
    </div>
    
    <!-- 敏感词列表 -->
    <div class="admin-table">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>敏感词</th>
                    <th>替换词</th>
                    <th>状态</th>
                    <th>添加时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($words as $word): ?>
                <tr>
                    <td><?php echo $word['id']; ?></td>
                    <td><?php echo htmlspecialchars($word['word']); ?></td>
                    <td><?php echo htmlspecialchars($word['replacement'] ?: '-'); ?></td>
                    <td>
                        <?php if ($word['status']): ?>
                        <span class="badge badge-success">启用</span>
                        <?php else: ?>
                        <span class="badge badge-secondary">禁用</span>
                        <?php endif; ?>
                    </td>
                    <td><?php echo date('Y-m-d H:i', $word['created_at']); ?></td>
                    <td>
                        <div class="btn-group">
                            <button class="btn btn-sm btn-default" onclick="editWord(<?php echo $word['id']; ?>)">
                                <i class="fa fa-edit"></i> 编辑
                            </button>
                            <button class="btn btn-sm btn-danger" onclick="deleteWord(<?php echo $word['id']; ?>)">
                                <i class="fa fa-trash"></i> 删除
                            </button>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        
        <?php if (empty($words)): ?>
        <div class="empty-state">
            <i class="fa fa-search"></i>
            <p>暂无敏感词数据</p>
        </div>
        <?php endif; ?>
    </div>
    
    <?php if ($totalPages > 1): ?>
    <div class="pagination-wrap">
        <?php echo $this->pagination($page, $totalPages, 'admin/filter?keyword=' . urlencode($keyword)); ?>
    </div>
    <?php endif; ?>
    
    <!-- 导入区域 -->
    <div class="panel">
        <div class="panel-header">
            <h3>批量导入敏感词</h3>
        </div>
        <div class="panel-body">
            <form id="importForm" enctype="multipart/form-data">
                <input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>">
                <div class="form-group">
                    <label>上传文件</label>
                    <input type="file" name="file" class="form-control" accept=".txt">
                    <p class="help-block">支持 .txt 文件，每行一个敏感词，格式：敏感词|替换词（替换词可选）</p>
                </div>
                <button type="submit" class="btn btn-primary">导入</button>
            </form>
        </div>
    </div>
</div>

<!-- 添加/编辑弹窗 -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="modalTitle">添加敏感词</h5>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>
            <div class="modal-body">
                <form id="wordForm">
                    <input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']; ?>">
                    <input type="hidden" name="id" id="wordId">
                    
                    <div class="form-group">
                        <label>敏感词 *</label>
                        <input type="text" name="word" id="wordInput" class="form-control" required>
                    </div>
                    
                    <div class="form-group">
                        <label>替换词（可选）</label>
                        <input type="text" name="replacement" id="replacementInput" class="form-control" 
                               placeholder="留空则用*替换">
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="saveWord()">保存</button>
            </div>
        </div>
    </div>
</div>

<script>
function editWord(id) {
    $.get('<?php echo Helper::url('admin/filter/get'); ?>?id=' + id, function(res) {
        if (res.success) {
            $('#wordId').val(res.data.id);
            $('#wordInput').val(res.data.word);
            $('#replacementInput').val(res.data.replacement);
            $('#modalTitle').text('编辑敏感词');
            $('#addModal').modal('show');
        } else {
            alert(res.message);
        }
    }, 'json');
}

function saveWord() {
    $.post('<?php echo Helper::url('admin/filter/save'); ?>', $('#wordForm').serialize(), function(res) {
        if (res.success) {
            alert(res.message);
            window.location.reload();
        } else {
            alert(res.message);
        }
    }, 'json');
    
    $('#addModal').modal('hide');
}

function deleteWord(id) {
    if (confirm('确定要删除这个敏感词吗？')) {
        $.post('<?php echo Helper::url('admin/filter/delete'); ?>', {
            id: id,
            csrf_token: '<?php echo $_SESSION['csrf_token']; ?>'
        }, function(res) {
            if (res.success) {
                alert(res.message);
                window.location.reload();
            } else {
                alert(res.message);
            }
        }, 'json');
    }
}

$(function() {
    $('#importForm').submit(function(e) {
        e.preventDefault();
        
        var formData = new FormData(this);
        
        $.ajax({
            url: '<?php echo Helper::url('admin/filter/import'); ?>',
            type: 'POST',
            data: formData,
            processData: false,
            contentType: false,
            success: function(res) {
                if (res.success) {
                    alert(res.message);
                    window.location.reload();
                } else {
                    alert(res.message);
                }
            },
            error: function() {
                alert('上传失败');
            }
        });
    });
    
    $('#addModal').on('hidden.bs.modal', function() {
        $('#wordId').val('');
        $('#wordInput').val('');
        $('#replacementInput').val('');
        $('#modalTitle').text('添加敏感词');
    });
});
</script>
