<?php
// ketua/kegiatan/detail.php
$pageTitle = 'Detail Kegiatan';
require_once '../../includes/config.php';
require_once '../../includes/database.php';
require_once '../../includes/functions.php';
require_once '../../includes/auth.php';

// Mulai session jika belum dimulai
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

// Cek apakah user sudah login dan role-nya ketua
if (!isset($_SESSION['user_id']) || !isset($_SESSION['role']) || $_SESSION['role'] != 'ketua') {
    // Redirect ke halaman login jika belum login atau bukan ketua
    setFlash('login_required', 'Anda harus login sebagai ketua yayasan untuk mengakses halaman ini', 'danger');
    header('Location: ' . SITE_URL . '/auth/login.php');
    exit;
}

// Cek apakah ada ID kegiatan
if (!isset($_GET['id']) || empty($_GET['id'])) {
    setFlash('kegiatan', 'ID kegiatan tidak valid', 'danger');
    header('Location: ' . SITE_URL . '/ketua/kegiatan/index.php');
    exit;
}

$id = $_GET['id'];
$db = dbConnect();

// Ambil detail kegiatan
$db->query("SELECT b.*, u.nama_lengkap as created_by_name, u.email as created_by_email 
           FROM berita b
           LEFT JOIN users u ON b.created_by = u.id
           WHERE b.id = :id");
$db->bind(':id', $id);
$berita = $db->single();

if (!$berita) {
    setFlash('kegiatan', 'Kegiatan tidak ditemukan', 'danger');
    header('Location: ' . SITE_URL . '/ketua/kegiatan/index.php');
    exit;
}

// Include header
require_once '../../includes/header.php';
?>

<div class="container-fluid">
    <div class="row">
        <!-- Sidebar -->
        <nav id="sidebar" class="col-md-3 col-lg-2 d-md-block bg-light sidebar collapse">
            <div class="position-sticky pt-3">
                <ul class="nav flex-column">
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/ketua/dashboard.php">
                            <i class="fas fa-tachometer-alt me-2"></i>
                            Dashboard
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/ketua/kampanye/index.php">
                            <i class="fas fa-hand-holding-heart me-2"></i>
                            Persetujuan Kampanye
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/ketua/laporan/index.php">
                            <i class="fas fa-chart-line me-2"></i>
                            Laporan Keuangan
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="<?= SITE_URL ?>/ketua/kegiatan/index.php">
                            <i class="fas fa-newspaper me-2"></i>
                            Kegiatan Yayasan
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/ketua/staff/index.php">
                            <i class="fas fa-users-cog me-2"></i>
                            Manajemen Staff
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/ketua/profil/index.php">
                            <i class="fas fa-user-circle me-2"></i>
                            Profil Saya
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>/auth/logout.php">
                            <i class="fas fa-sign-out-alt me-2"></i>
                            Logout
                        </a>
                    </li>
                </ul>
                
                <h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
                    <span>Pintasan</span>
                </h6>
                <ul class="nav flex-column mb-2">
                    <li class="nav-item">
                        <a class="nav-link" href="<?= SITE_URL ?>" target="_blank">
                            <i class="fas fa-external-link-alt me-2"></i>
                            Lihat Website
                        </a>
                    </li>
                </ul>
            </div>
        </nav>

        <!-- Content Area -->
        <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
            <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                <h1 class="h2">Detail Kegiatan</h1>
                <div class="btn-toolbar mb-2 mb-md-0">
                    <a href="<?= SITE_URL ?>/ketua/kegiatan/index.php" class="btn btn-sm btn-outline-secondary me-2">
                        <i class="fas fa-arrow-left me-1"></i> Kembali
                    </a>
                    <a href="<?= SITE_URL ?>/pages/berita-detail.php?id=<?= $berita->id ?>" target="_blank" class="btn btn-sm btn-outline-primary">
                        <i class="fas fa-external-link-alt me-1"></i> Lihat di Website
                    </a>
                </div>
            </div>

            <?php flash(); ?>

            <!-- Detail Kegiatan -->
            <div class="row">
                <div class="col-md-8">
                    <div class="card mb-4">
                        <div class="card-body">
                            <h3 class="card-title"><?= $berita->judul ?></h3>
                            <div class="mb-3 text-muted">
                                <i class="far fa-calendar-alt me-1"></i> <?= date('d F Y H:i', strtotime($berita->created_at)) ?>
                            </div>

                            <div class="text-center mb-4">
                                <img src="<?= SITE_URL ?>/assets/uploads/berita/<?= $berita->gambar ?? 'default.jpg' ?>" 
                                     class="img-fluid rounded" alt="<?= $berita->judul ?>" 
                                     style="max-height: 400px;">
                            </div>
                            
                            <div class="berita-content">
                                <?= nl2br($berita->konten) ?>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="col-md-4">
                    <!-- Informasi Kegiatan -->
                    <div class="card mb-4">
                        <div class="card-header bg-light">
                            <h5 class="mb-0">Informasi Kegiatan</h5>
                        </div>
                        <div class="card-body">
                            <table class="table table-borderless">
                                <tr>
                                    <th width="140">Dibuat Oleh</th>
                                    <td><?= $berita->created_by_name ?></td>
                                </tr>
                                <tr>
                                    <th>Email Pembuat</th>
                                    <td><?= $berita->created_by_email ?></td>
                                </tr>
                                <tr>
                                    <th>Tanggal Dibuat</th>
                                    <td><?= date('d/m/Y H:i', strtotime($berita->created_at)) ?></td>
                                </tr>
                                <tr>
                                    <th>Tanggal Update</th>
                                    <td><?= date('d/m/Y H:i', strtotime($berita->updated_at)) ?></td>
                                </tr>
                            </table>
                        </div>
                    </div>
                    
                    <!-- Aktivitas Kegiatan -->
                    <div class="card">
                        <div class="card-header bg-light">
                            <h5 class="mb-0">Riwayat Aktivitas</h5>
                        </div>
                        <div class="card-body">
                            <?php
                            // Ambil riwayat aktivitas berita
                            $db->query("SELECT l.*, u.nama_lengkap, u.role 
                                      FROM logs l 
                                      JOIN users u ON l.user_id = u.id 
                                      WHERE l.entity_type = 'berita' AND l.entity_id = :id
                                      ORDER BY l.created_at DESC 
                                      LIMIT 5");
                            $db->bind(':id', $id);
                            $logs = $db->resultSet();
                            
                            if (empty($logs)):
                            ?>
                                <p class="text-muted">Belum ada aktivitas untuk kegiatan ini.</p>
                            <?php else: ?>
                                <div class="list-group small">
                                    <?php foreach ($logs as $log): ?>
                                        <div class="list-group-item list-group-item-action">
                                            <div class="d-flex w-100 justify-content-between">
                                                <strong><?= $log->activity ?></strong>
                                                <small><?= date('d/m/Y H:i', strtotime($log->created_at)) ?></small>
                                            </div>
                                            <p class="mb-1"><?= $log->description ?></p>
                                            <small>Oleh: <?= $log->nama_lengkap ?> (<?= getRoleName($log->role) ?>)</small>
                                        </div>
                                    <?php endforeach; ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
        </main>
    </div>
</div>

<style>
.berita-content {
    line-height: 1.8;
}
</style>

<?php require_once '../../includes/footer.php'; ?>