<?php
require_once __DIR__ . '/../MPHX/common.php';

if (!is_admin_login()) {
    header('Location: /admin/login.php');
    exit;
}

// 连接到官网数据库
require_once __DIR__ . '/../dre/includes/Database.php';
$db = new Database();

// 分页参数
$page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$perPage = 20;
$offset = ($page - 1) * $perPage;

// 获取总数
$countSql = "SELECT COUNT(*) as total FROM dre_feedback WHERE source = 'project_admin'";
$countResult = $db->query($countSql);
$totalCount = $db->fetch($countResult)['total'];
$totalPages = ceil($totalCount / $perPage);

// 获取分页数据
$sql = "SELECT * FROM dre_feedback WHERE source = 'project_admin' ORDER BY created_at DESC LIMIT $perPage OFFSET $offset";
$result = $db->query($sql);
$feedbacks = array();
while ($row = $db->fetch($result)) {
    $feedbacks[] = $row;
}

// 统计数量
$sql = "SELECT COUNT(*) as count FROM dre_feedback WHERE source = 'project_admin' AND status = 'pending'";
$result = $db->query($sql);
$pendingCount = $db->fetch($result)['count'];

$sql = "SELECT COUNT(*) as count FROM dre_feedback WHERE source = 'project_admin' AND status = 'replied'";
$result = $db->query($sql);
$repliedCount = $db->fetch($result)['count'];
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>反馈历史 - MLEOE 宝塔系统</title>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/css/all.min.css">
    <link rel="stylesheet" href="css/style.css?v=2026040501">
    <style>
        .stats-row {
            display: flex;
            gap: 15px;
            margin-bottom: 20px;
        }
        .stat-card {
            flex: 1;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            display: flex;
            align-items: center;
            gap: 15px;
        }
        .stat-icon {
            width: 50px;
            height: 50px;
            border-radius: 8px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            flex-shrink: 0;
        }
        .stat-icon.pending {
            background: #fef3c7;
            color: #d97706;
        }
        .stat-icon.replied {
            background: #d1fae5;
            color: #059669;
        }
        .stat-icon.total {
            background: #dbeafe;
            color: #2563eb;
        }
        .stat-info h4 {
            font-size: 28px;
            font-weight: 600;
            margin: 0 0 5px 0;
            color: #1f2937;
        }
        .stat-info p {
            font-size: 14px;
            color: #6b7280;
            margin: 0;
        }
        .feedback-item {
            background: white;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 15px;
            border-left: 4px solid #e5e7eb;
        }
        .feedback-item.status-pending {
            border-left-color: #f59e0b;
        }
        .feedback-item.status-replied {
            border-left-color: #10b981;
        }
        .feedback-item.status-read {
            border-left-color: #3b82f6;
        }
        .feedback-header {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            margin-bottom: 15px;
        }
        .feedback-info h4 {
            font-size: 16px;
            font-weight: 600;
            margin: 0 0 8px 0;
            color: #1f2937;
        }
        .feedback-meta {
            font-size: 13px;
            color: #6b7280;
            display: flex;
            gap: 15px;
            flex-wrap: wrap;
        }
        .feedback-meta span {
            display: flex;
            align-items: center;
            gap: 5px;
        }
        .status-badge {
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 500;
        }
        .status-pending {
            background: #fef3c7;
            color: #d97706;
        }
        .status-read {
            background: #dbeafe;
            color: #2563eb;
        }
        .status-replied {
            background: #d1fae5;
            color: #059669;
        }
        .feedback-message {
            background: #f9fafb;
            padding: 15px;
            border-radius: 6px;
            margin-bottom: 15px;
            color: #374151;
            line-height: 1.6;
        }
        .feedback-reply {
            background: #ecfdf5;
            border: 1px solid #a7f3d0;
            padding: 15px;
            border-radius: 6px;
            margin-top: 15px;
        }
        .feedback-reply-title {
            font-size: 14px;
            font-weight: 600;
            color: #059669;
            margin-bottom: 10px;
            display: flex;
            align-items: center;
            gap: 6px;
        }
        .feedback-reply p {
            color: #064e3b;
            line-height: 1.6;
            margin: 0;
        }
        .feedback-reply-time {
            font-size: 12px;
            color: #6b7280;
            margin-top: 10px;
            text-align: right;
        }
        .pagination {
            display: flex;
            justify-content: center;
            gap: 8px;
            margin-top: 20px;
        }
        .page-item {
            padding: 8px 16px;
            border: 1px solid #e5e7eb;
            border-radius: 6px;
            cursor: pointer;
            background: white;
            transition: all 0.2s;
        }
        .page-item:hover {
            background: #f3f4f6;
            border-color: #3b82f6;
            color: #3b82f6;
        }
        .page-item.active {
            background: #3b82f6;
            color: white;
            border-color: #3b82f6;
        }
        .empty-state {
            text-align: center;
            padding: 60px 20px;
            color: #9ca3af;
        }
        .empty-state i {
            font-size: 48px;
            margin-bottom: 15px;
            opacity: 0.3;
        }
    </style>
</head>
<body>
    <div class="content-area">
        <!-- 统计卡片 -->
        <div class="stats-row">
            <div class="stat-card">
                <div class="stat-icon pending">
                    <i class="fas fa-clock"></i>
                </div>
                <div class="stat-info">
                    <h4><?php echo $pendingCount; ?></h4>
                    <p>待处理</p>
                </div>
            </div>
            <div class="stat-card">
                <div class="stat-icon replied">
                    <i class="fas fa-check-circle"></i>
                </div>
                <div class="stat-info">
                    <h4><?php echo $repliedCount; ?></h4>
                    <p>已回复</p>
                </div>
            </div>
            <div class="stat-card">
                <div class="stat-icon total">
                    <i class="fas fa-inbox"></i>
                </div>
                <div class="stat-info">
                    <h4><?php echo $totalCount; ?></h4>
                    <p>总计</p>
                </div>
            </div>
        </div>
        
        <!-- 反馈列表 -->
        <div class="card">
            <h3><i class="fas fa-history"></i> 反馈历史</h3>
            
            <?php if (empty($feedbacks)): ?>
            <div class="empty-state">
                <i class="fas fa-inbox"></i>
                <p>暂无反馈记录</p>
            </div>
            <?php else: ?>
                <?php foreach ($feedbacks as $fb): ?>
                <div class="feedback-item status-<?php echo $fb['status']; ?>">
                    <div class="feedback-header">
                        <div class="feedback-info">
                            <h4><?php echo htmlspecialchars($fb['name']); ?> 
                                <span class="status-badge status-<?php echo $fb['status']; ?>">
                                    <?php
                                    $statusText = array(
                                        'pending' => '待处理',
                                        'read' => '已读',
                                        'replied' => '已回复'
                                    );
                                    echo $statusText[$fb['status']];
                                    ?>
                                </span>
                            </h4>
                            <div class="feedback-meta">
                                <span><i class="fas fa-envelope"></i> <?php echo htmlspecialchars($fb['email']); ?></span>
                                <?php if ($fb['phone']): ?>
                                <span><i class="fas fa-phone"></i> <?php echo htmlspecialchars($fb['phone']); ?></span>
                                <?php endif; ?>
                                <span><i class="fas fa-clock"></i> <?php echo $fb['created_at']; ?></span>
                            </div>
                        </div>
                    </div>
                    
                    <?php if ($fb['subject']): ?>
                    <div style="margin-bottom: 10px;">
                        <strong style="color: #6b7280; font-size: 13px;">主题：</strong>
                        <span style="color: #374151; font-size: 14px;"><?php echo htmlspecialchars($fb['subject']); ?></span>
                    </div>
                    <?php endif; ?>
                    
                    <div class="feedback-message">
                        <?php echo nl2br(htmlspecialchars($fb['message'])); ?>
                    </div>
                    
                    <?php if ($fb['reply_content']): ?>
                    <div class="feedback-reply">
                        <div class="feedback-reply-title">
                            <i class="fas fa-reply"></i> 官网回复
                        </div>
                        <p><?php echo nl2br(htmlspecialchars($fb['reply_content'])); ?></p>
                        <?php if ($fb['replied_at']): ?>
                        <div class="feedback-reply-time">
                            <i class="fas fa-clock"></i> 回复时间：<?php echo $fb['replied_at']; ?>
                        </div>
                        <?php endif; ?>
                    </div>
                    <?php endif; ?>
                </div>
                <?php endforeach; ?>
            <?php endif; ?>
            
            <!-- 分页 -->
            <?php if ($totalPages > 1): ?>
            <div class="pagination">
                <?php if ($page > 1): ?>
                <div class="page-item" onclick="location.href='?page=<?php echo $page - 1; ?>'">
                    <i class="fas fa-chevron-left"></i> 上一页
                </div>
                <?php endif; ?>
                
                <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                <div class="page-item <?php echo $i === $page ? 'active' : ''; ?>" onclick="location.href='?page=<?php echo $i; ?>'">
                    <?php echo $i; ?>
                </div>
                <?php endfor; ?>
                
                <?php if ($page < $totalPages): ?>
                <div class="page-item" onclick="location.href='?page=<?php echo $page + 1; ?>'">
                    下一页 <i class="fas fa-chevron-right"></i>
                </div>
                <?php endif; ?>
            </div>
            <?php endif; ?>
        </div>
    </div>
    
    <script src="/static/js/jquery.min.js"></script>
    <script>
    $(document).ready(function() {
        $('.page-item').on('click', function() {
            if (!$(this).hasClass('active')) {
                var page = $(this).attr('onclick').match(/page=(\d+)/)[1];
                location.href = '?page=' + page;
            }
        });
    });
    </script>
</body>
</html>
