<?php
session_start();
require_once 'includes/config.php';
require_once 'includes/cai-dat-helper.php';
$_anh_bia_tbsk = layAnhBiaTrang($pdo, 'thong_bao_su_kien');

$su_kien_mo = $pdo->query("SELECT * FROM su_kien WHERE trang_thai IN ('dang_mo', 'da_chot') ORDER BY ngay_dien_ra ASC")->fetchAll();
?>
<!DOCTYPE html>
<html lang="vi">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Thông báo sự kiện</title>
    <link rel="icon" type="image/png" href="images/icon.png">
    <link rel="apple-touch-icon" href="images/icon.png">
    <style>
        body { font-family: Arial, sans-serif; margin:0; background:#f5f7fb; color:#243447; }
        .hero-trang { background: #0056b3; color: white; padding: 50px 20px; text-align: center; background-size: cover; background-position: center; background-repeat: no-repeat; position: relative; min-height: 220px; display: flex; flex-direction: column; align-items: center; justify-content: center; }
        .hero-trang::before { content:''; position:absolute; inset:0; background:rgba(0,60,140,0.68); }
        .hero-trang > * { position: relative; z-index: 1; }
        .hero-trang h1 { margin: 10px 0 8px; font-size: 32px; }
        .hero-trang p { margin: 0; font-size: 16px; opacity: 0.9; }
        .nut-tro-ve-hero { display:inline-block; padding:8px 20px; background:rgba(255,255,255,0.2); color:white; text-decoration:none; border-radius:20px; font-size:14px; margin-bottom:20px; }
        .nut-tro-ve-hero:hover { background:rgba(255,255,255,0.3); }
        .wrap { max-width: 900px; margin: 0 auto; padding: 24px; }
        .card { background:#fff; border-radius:14px; padding:20px; box-shadow:0 3px 12px rgba(0,0,0,.06); margin-bottom:16px; }
        .item { border:1px solid #e8edf4; border-radius:12px; padding:12px; margin-bottom:10px; }
        .btn { display:inline-block; background:#0056b3; color:#fff; text-decoration:none; padding:8px 12px; border-radius:8px; margin-top:6px; }
        .muted { color:#6c757d; font-size:13px; }
    </style>
</head>
<body>
    <div class="hero-trang"<?php if($_anh_bia_tbsk): ?> style="background-image:url('<?php echo htmlspecialchars($_anh_bia_tbsk); ?>')"<?php endif; ?>>
        <a href="index.php" class="nut-tro-ve-hero">⬅ Trang chủ</a>
        <h1>📢 Thông báo Sự kiện</h1>
        <p>Danh sách các cơ hội sự kiện đang mở cho cộng đồng và thành viên</p>
    </div>
    <div class="wrap">
        <div class="card">
            <h2 style="margin-top:0;">📢 Thông báo sự kiện</h2>
            <p class="muted">Danh sách các cơ hội sự kiện đang mở cho cộng đồng và thành viên.</p>
            <?php if (count($su_kien_mo) > 0): ?>
                <?php foreach ($su_kien_mo as $sk): ?>
                    <div class="item">
                        <strong><?php echo htmlspecialchars($sk['ten_su_kien']); ?></strong>
                        <?php
                        $thoi_gian_text = '';
                        if (!empty($sk['ngay_bat_dau']) || !empty($sk['ngay_ket_thuc'])) {
                            $bat_dau = !empty($sk['ngay_bat_dau']) ? date('d/m/Y H:i', strtotime($sk['ngay_bat_dau'])) : '';
                            $ket_thuc = !empty($sk['ngay_ket_thuc']) ? date('d/m/Y H:i', strtotime($sk['ngay_ket_thuc'])) : '';
                            if ($bat_dau && $ket_thuc) {
                                $thoi_gian_text = '🗓 Từ ' . $bat_dau . ' đến ' . $ket_thuc;
                            } elseif ($bat_dau) {
                                $thoi_gian_text = '🗓 Bắt đầu ' . $bat_dau;
                            } elseif ($ket_thuc) {
                                $thoi_gian_text = '🗓 Kết thúc ' . $ket_thuc;
                            }
                        } elseif (!empty($sk['ngay_dien_ra'])) {
                            $thoi_gian_text = '🗓 ' . date('d/m/Y H:i', strtotime($sk['ngay_dien_ra']));
                        }
                        ?>
                        <?php if ($thoi_gian_text !== ''): ?><div class="muted" style="margin:4px 0;"><?php echo htmlspecialchars($thoi_gian_text); ?></div><?php endif; ?>
                        <div class="muted">💰 Tổng ngân sách: <?php echo number_format($sk['thu_lao_du_kien'], 0, ',', '.'); ?> ₫</div>
                        <?php $total_slots = isset($sk['so_luong_tong']) && $sk['so_luong_tong'] !== null ? (int)$sk['so_luong_tong'] : (int)$sk['so_luong_nhan_su']; ?>
                        <?php
                            $stmt_chot = $pdo->prepare('SELECT COUNT(*) AS cnt FROM ung_tuyen_su_kien WHERE su_kien_id = ? AND trang_thai = "chinh_thuc"');
                            $stmt_chot->execute([$sk['id']]);
                            $row_chot = $stmt_chot->fetch();
                            $chot = $row_chot ? (int)$row_chot['cnt'] : 0;
                            $remaining = max(0, $total_slots - $chot);
                        ?>
                        <div class="muted">👥 Đã chốt: <?php echo $chot; ?> / Tổng: <?php echo $total_slots; ?></div>
                        <?php $thue_lao_moi_nguoi = $total_slots > 0 ? (float)$sk['thu_lao_du_kien'] / $total_slots : 0; ?>
                        <div class="muted">💵 Ước tính mỗi người nhận: <?php echo number_format($thue_lao_moi_nguoi, 0, ',', '.'); ?> ₫</div>
                        <?php if (!empty($sk['mo_ta'])): ?><div class="muted">📝 <?php echo htmlspecialchars($sk['mo_ta']); ?></div><?php endif; ?>
                        <?php if ((int)$sk['so_luong_nhan_su'] <= 0): ?>
                            <span class="muted" style="display:inline-block;padding:8px 10px;background:#f8d7da;color:#721c24;border-radius:8px;">Đã đủ người</span>
                        <?php else: ?>
                            <?php if (isset($_SESSION['nguoi_dung_id'])): ?>
                                <a href="ung-tuyen-su-kien.php" class="btn">Ứng tuyển ngay</a>
                            <?php else: ?>
                                <a href="dang-nhap.php" class="btn">Đăng nhập để ứng tuyển</a>
                            <?php endif; ?>
                        <?php endif; ?>
                    </div>
                <?php endforeach; ?>
            <?php else: ?>
                <div class="item">Hiện chưa có sự kiện mở.</div>
            <?php endif; ?>
            <div style="margin-top:12px;"><a href="index.php" class="btn" style="background:#6c757d;">← Quay lại trang chủ</a></div>
        </div>
    </div>

    <?php require_once 'includes/menu-dien-thoai.php'; ?>
</body>
</html>
