<?php
include_once __DIR__ . "/../inc/config.php";

$mysqli = get_mysqli_connection();
$sql = "SELECT id, report_date, created_at, title, subject_type, subject_code
        FROM mac_report WHERE is_active = 1 ORDER BY report_date DESC, id DESC";
$result = $mysqli->query($sql);
$reports = $result ? $result->fetch_all(MYSQLI_ASSOC) : [];
$mysqli->close();

// Group by subject_type for organization
$groups = [];
foreach ($reports as $r) {
    $type = $r['subject_type'] ?: '其他';
    $groups[$type][] = $r;
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>宏观研究</title>
    <script src="/lib/js/tailwindcss-3.4.17.js"></script>
    <style type="text/tailwindcss">
        @layer utilities {
            .card-hover { @apply transition-all duration-300 hover:shadow-lg hover:-translate-y-1; }
        }
    </style>
</head>
<body class="bg-gray-50 min-h-screen font-sans text-gray-900">
    <header class="bg-white shadow-sm sticky top-0 z-50 border-b border-gray-200">
        <div class="max-w-4xl mx-auto px-4 py-4 flex justify-between items-center">
            <div class="flex items-center space-x-2">
                <div class="w-8 h-8 bg-indigo-600 rounded-lg flex items-center justify-center">
                    <svg class="w-5 h-5 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 19v-6a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2a2 2 0 002-2zm0 0V9a2 2 0 012-2h2a2 2 0 012 2v10m-6 0a2 2 0 002 2h2a2 2 0 002-2m0 0V5a2 2 0 012-2h2a2 2 0 012 2v14a2 2 0 01-2 2h-2a2 2 0 01-2-2z"/>
                    </svg>
                </div>
                <h1 class="text-xl font-bold">宏观研究</h1>
            </div>
            <a href="/index-2.php" class="text-sm text-gray-500 hover:text-indigo-600 transition-colors">首页</a>
        </div>
    </header>

    <main class="max-w-4xl mx-auto px-4 py-8">
        <?php if (empty($reports)): ?>
        <div class="text-center py-16 text-gray-400">
            <svg class="w-16 h-16 mx-auto mb-4 opacity-30" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
            </svg>
            <p class="text-lg">暂无报告数据</p>
        </div>
        <?php else: ?>
        <?php foreach ($groups as $type => $items): ?>
        <section class="mb-8">
            <div class="flex items-center space-x-2 mb-4">
                <span class="px-2.5 py-0.5 rounded text-xs font-medium bg-indigo-100 text-indigo-800 uppercase tracking-wide"><?= htmlspecialchars($type) ?></span>
                <span class="text-xs text-gray-400"><?= count($items) ?> 篇</span>
            </div>
            <div class="space-y-2">
            <?php foreach ($items as $r):
                $date = $r['report_date'];
                $created = $r['created_at'] ? date('Y-m-d H:i', strtotime($r['created_at'])) : '';
            ?>
                <a href="report.php?id=<?= $r['id'] ?>"
                   class="card-hover block bg-white rounded-lg border border-gray-200 p-4 hover:border-indigo-300">
                    <div class="flex justify-between items-start">
                        <div class="flex-1 min-w-0">
                            <h3 class="text-base font-semibold text-gray-900 truncate"><?= htmlspecialchars($r['title']) ?></h3>
                            <?php if ($r['subject_code']): ?>
                            <p class="text-xs text-gray-400 mt-1"><?= htmlspecialchars($r['subject_code']) ?></p>
                            <?php endif; ?>
                        </div>
                        <div class="flex-shrink-0 text-right ml-4">
                            <div class="text-sm text-gray-500"><?= $date ?></div>
                            <?php if ($created): ?>
                            <div class="text-xs text-gray-400 mt-0.5"><?= $created ?></div>
                            <?php endif; ?>
                        </div>
                    </div>
                </a>
            <?php endforeach; ?>
            </div>
        </section>
        <?php endforeach; ?>
        <?php endif; ?>
    </main>

    <footer class="border-t border-gray-200 py-8 text-center text-sm text-gray-400">
        <p>宏观研究 &copy; <?= date('Y') ?></p>
    </footer>
</body>
</html>
