<?php
// sekretaris/dashboard.php
$pageTitle = 'Dashboard Sekretaris';
require_once '../includes/config.php';
require_once '../includes/database.php';
require_once '../includes/functions.php';
require_once '../includes/auth.php';
// Definisi fungsi jika tidak tersedia
if (!function_exists('getKampanyeStatusBadge')) {
    function getKampanyeStatusBadge($status) {
        switch($status) {
            case 'aktif':
                return '<span class="badge bg-success">Aktif</span>';
            case 'selesai':
                return '<span class="badge bg-primary">Selesai</span>';
            case 'dibatalkan':
                return '<span class="badge bg-danger">Dibatalkan</span>';
            case 'menunggu_persetujuan':
                return '<span class="badge bg-warning text-dark">Menunggu Persetujuan</span>';
            default:
                return '<span class="badge bg-secondary">' . ucfirst($status) . '</span>';
        }
    }
}

if (!function_exists('getDonasiStatusBadge')) {
    function getDonasiStatusBadge($status) {
        switch($status) {
            case 'menunggu':
                return '<span class="badge bg-warning text-dark">Menunggu</span>';
            case 'diverifikasi':
                return '<span class="badge bg-success">Diverifikasi</span>';
            case 'ditolak':
                return '<span class="badge bg-danger">Ditolak</span>';
            default:
                return '<span class="badge bg-secondary">' . ucfirst($status) . '</span>';
        }
    }
}
// Mulai session jika belum dimulai
if (session_status() == PHP_SESSION_NONE) {
    session_start();
}

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

// Ambil data untuk dashboard
$db = dbConnect();

// Jumlah kampanye menunggu persetujuan
$db->query("SELECT COUNT(*) as count FROM kampanye WHERE status = 'menunggu_persetujuan'");
$kampanye_menunggu = $db->single();
$kampanye_menunggu_value = $kampanye_menunggu->count ?? 0;

// Jumlah kampanye aktif
$db->query("SELECT COUNT(*) as count FROM kampanye WHERE status = 'aktif'");
$kampanye_aktif = $db->single();
$kampanye_aktif_value = $kampanye_aktif->count ?? 0;

// Jumlah kampanye selesai
$db->query("SELECT COUNT(*) as count FROM kampanye WHERE status = 'selesai'");
$kampanye_selesai = $db->single();
$kampanye_selesai_value = $kampanye_selesai->count ?? 0;

// Jumlah berita
$db->query("SELECT COUNT(*) as count FROM berita");
$jumlah_berita = $db->single();
$jumlah_berita_value = $jumlah_berita->count ?? 0;

// Kampanye terbaru
$db->query("SELECT k.*, u.nama_lengkap as created_by_name 
           FROM kampanye k 
           JOIN users u ON k.created_by = u.id 
           ORDER BY k.created_at DESC 
           LIMIT 5");
$kampanye_terbaru = $db->resultSet();

// Berita terbaru
$db->query("SELECT b.*, u.nama_lengkap as created_by_name 
           FROM berita b 
           JOIN users u ON b.created_by = u.id 
           ORDER BY b.created_at DESC 
           LIMIT 5");
$berita_terbaru = $db->resultSet();

// Pesan kontak terbaru
$db->query("SELECT * FROM kontak ORDER BY created_at DESC LIMIT 5");
$kontak_terbaru = $db->resultSet();

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

<!-- Sidebar & Content -->
<div class="container-fluid">
    <div class="row">
        <!-- Sidebar Admin -->
        <?php require_once '../includes/sidebar-sekretaris.php'; ?>

        <!-- 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">Dashboard Sekretaris</h1>
                <div class="btn-toolbar mb-2 mb-md-0">
                    <a href="<?= SITE_URL ?>/sekretaris/kampanye/tambah.php" class="btn btn-sm btn-success">
                        <i class="fas fa-plus me-1"></i> Tambah Kampanye
                    </a>
                </div>
            </div>

            <?php flash(); ?>

            <!-- Statistik Overview -->
            <div class="row dashboard-stats">
                <div class="col-md-3 mb-4">
                    <div class="card h-100">
                        <div class="card-body">
                            <div class="d-flex align-items-center">
                                <div class="card-icon rounded-circle d-flex align-items-center justify-content-center me-3">
                                    <i class="fas fa-clock"></i>
                                </div>
                                <div>
                                    <h5 class="card-title">Menunggu Persetujuan</h5>
                                    <h3 class="mb-0"><?= $kampanye_menunggu_value ?></h3>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-3 mb-4">
                    <div class="card h-100">
                        <div class="card-body">
                            <div class="d-flex align-items-center">
                                <div class="card-icon rounded-circle d-flex align-items-center justify-content-center me-3">
                                    <i class="fas fa-project-diagram"></i>
                                </div>
                                <div>
                                    <h5 class="card-title">Kampanye Aktif</h5>
                                    <h3 class="mb-0"><?= $kampanye_aktif_value ?></h3>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-3 mb-4">
                    <div class="card h-100">
                        <div class="card-body">
                            <div class="d-flex align-items-center">
                                <div class="card-icon rounded-circle d-flex align-items-center justify-content-center me-3">
                                    <i class="fas fa-check-circle"></i>
                                </div>
                                <div>
                                    <h5 class="card-title">Kampanye Selesai</h5>
                                    <h3 class="mb-0"><?= $kampanye_selesai_value ?></h3>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-3 mb-4">
                    <div class="card h-100">
                        <div class="card-body">
                            <div class="d-flex align-items-center">
                                <div class="card-icon rounded-circle d-flex align-items-center justify-content-center me-3">
                                    <i class="fas fa-newspaper"></i>
                                </div>
                                <div>
                                    <h5 class="card-title">Jumlah Berita</h5>
                                    <h3 class="mb-0"><?= $jumlah_berita_value ?></h3>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Kampanye & Berita -->
            <div class="row">
                <!-- Kampanye Terbaru -->
                <div class="col-md-6 mb-4">
                    <div class="card">
                        <div class="card-body">
                            <div class="d-flex justify-content-between align-items-center mb-3">
                                <h5 class="card-title mb-0">Kampanye Terbaru</h5>
                                <a href="<?= SITE_URL ?>/sekretaris/kampanye/index.php" class="btn btn-sm btn-outline-primary">Lihat Semua</a>
                            </div>
                            <?php if (empty($kampanye_terbaru)): ?>
                                <p class="text-center py-3">Belum ada data kampanye</p>
                            <?php else: ?>
                                <div class="list-group">
                                    <?php foreach ($kampanye_terbaru as $kampanye): ?>
                                        <a href="<?= SITE_URL ?>/sekretaris/kampanye/detail.php?id=<?= $kampanye->id ?>" class="list-group-item list-group-item-action">
                                            <div class="d-flex w-100 justify-content-between">
                                                <h6 class="mb-1"><?= $kampanye->judul ?></h6>
                                                <?= getKampanyeStatusBadge($kampanye->status) ?>
                                            </div>
                                            <p class="mb-1 text-muted small"><?= potongTeks($kampanye->deskripsi, 80) ?></p>
                                            <div class="d-flex justify-content-between align-items-center mt-1">
                                                <small>Target: <?= rupiah($kampanye->target_donasi) ?></small>
                                                <small>
                                                    <i class="far fa-calendar-alt me-1"></i>
                                                    <?= date('d/m/Y', strtotime($kampanye->created_at)) ?>
                                                </small>
                                            </div>
                                        </a>
                                    <?php endforeach; ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>

                <!-- Berita Terbaru -->
                <div class="col-md-6 mb-4">
                    <div class="card">
                        <div class="card-body">
                            <div class="d-flex justify-content-between align-items-center mb-3">
                                <h5 class="card-title mb-0">Berita Terbaru</h5>
                                <a href="<?= SITE_URL ?>/sekretaris/berita/index.php" class="btn btn-sm btn-outline-primary">Lihat Semua</a>
                            </div>
                            <?php if (empty($berita_terbaru)): ?>
                                <p class="text-center py-3">Belum ada data berita</p>
                            <?php else: ?>
                                <div class="list-group">
                                    <?php foreach ($berita_terbaru as $berita): ?>
                                        <a href="<?= SITE_URL ?>/sekretaris/berita/edit.php?id=<?= $berita->id ?>" class="list-group-item list-group-item-action">
                                            <div class="d-flex w-100 justify-content-between">
                                                <h6 class="mb-1"><?= $berita->judul ?></h6>
                                                <small>
                                                    <i class="far fa-calendar-alt me-1"></i>
                                                    <?= date('d/m/Y', strtotime($berita->created_at)) ?>
                                                </small>
                                            </div>
                                            <p class="mb-1 text-muted small"><?= potongTeks($berita->konten, 80) ?></p>
                                        </a>
                                    <?php endforeach; ?>
                                </div>
                            <?php endif; ?>
                            <div class="text-center mt-3">
                                <a href="<?= SITE_URL ?>/sekretaris/berita/tambah.php" class="btn btn-sm btn-success">
                                    <i class="fas fa-plus me-1"></i> Tambah Berita
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Pesan Kontak -->
            <div class="row">
                <div class="col-12 mb-4">
                    <div class="card">
                        <div class="card-body">
                            <div class="d-flex justify-content-between align-items-center mb-3">
                                <h5 class="card-title mb-0">Pesan Kontak Terbaru</h5>
                                <a href="<?= SITE_URL ?>/sekretaris/kontak/index.php" class="btn btn-sm btn-outline-primary">Lihat Semua</a>
                            </div>
                            <?php if (empty($kontak_terbaru)): ?>
                                <p class="text-center py-3">Belum ada pesan kontak</p>
                            <?php else: ?>
                                <div class="table-responsive">
                                    <table class="table table-hover align-middle">
                                        <thead>
                                            <tr>
                                                <th>Nama</th>
                                                <th>Email</th>
                                                <th>Subjek</th>
                                                <th>Tanggal</th>
                                                <th>Status</th>
                                                <th>Aksi</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php foreach ($kontak_terbaru as $kontak): ?>
                                                <tr>
                                                    <td><?= $kontak->nama ?></td>
                                                    <td><?= $kontak->email ?></td>
                                                    <td><?= $kontak->subjek ?></td>
                                                    <td><?= date('d/m/Y H:i', strtotime($kontak->created_at)) ?></td>
                                                    <td>
                                                        <?php if ($kontak->is_read): ?>
                                                            <span class="badge bg-success">Sudah Dibaca</span>
                                                        <?php else: ?>
                                                            <span class="badge bg-warning text-dark">Belum Dibaca</span>
                                                        <?php endif; ?>
                                                    </td>
                                                    <td>
                                                        <a href="<?= SITE_URL ?>/sekretaris/kontak/detail.php?id=<?= $kontak->id ?>" class="btn btn-sm btn-info text-white">
                                                            <i class="fas fa-eye"></i> Lihat
                                                        </a>
                                                    </td>
                                                </tr>
                                            <?php endforeach; ?>
                                        </tbody>
                                    </table>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Notifikasi & Aktivitas Terbaru -->
            <div class="row">
                <!-- Notifikasi -->
                <div class="col-md-6 mb-4">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">Notifikasi</h5>
                            <?php
                            $notifikasi = [];
$db->query("SHOW TABLES LIKE 'notifications'");
$tableExists = $db->rowCount() > 0;
if ($tableExists) {
    $db->query("SELECT * FROM notifications WHERE user_id = :user_id AND is_read = 0 ORDER BY created_at DESC");
    $db->bind(':user_id', $_SESSION['user_id']);
    $notifikasi = $db->resultSet();
}
                            
                            if (empty($notifikasi)):
                            ?>
                                <p class="text-center py-3">Tidak ada notifikasi baru</p>
                            <?php else: ?>
                                <div class="list-group">
                                    <?php foreach ($notifikasi as $notif): ?>
                                        <div class="list-group-item list-group-item-action">
                                            <div class="d-flex w-100 justify-content-between">
                                                <h6 class="mb-1"><?= $notif->title ?></h6>
                                                <small><?= date('d/m/Y H:i', strtotime($notif->created_at)) ?></small>
                                            </div>
                                            <p class="mb-1"><?= $notif->message ?></p>
                                            <?php if ($notif->link): ?>
                                                <a href="<?= $notif->link ?>" class="btn btn-sm btn-outline-primary mt-2">Lihat Detail</a>
                                            <?php endif; ?>
                                            <a href="<?= SITE_URL ?>/sekretaris/notifikasi/mark-read.php?id=<?= $notif->id ?>" class="btn btn-sm btn-outline-secondary mt-2">Tandai Sudah Dibaca</a>
                                        </div>
                                    <?php endforeach; ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>

                <!-- Aktivitas Terbaru -->
                <div class="col-md-6 mb-4">
                    <div class="card">
                        <div class="card-body">
                            <h5 class="card-title">Aktivitas Terbaru</h5>
                            <?php
                            // Ambil aktivitas terbaru
                            $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 IN ('kampanye', 'berita')
                                    ORDER BY l.created_at DESC 
                                    LIMIT 5");
                            $aktivitas = $db->resultSet();
                            
                            if (empty($aktivitas)):
                            ?>
                                <p class="text-center py-3">Belum ada aktivitas</p>
                            <?php else: ?>
                                <div class="list-group">
                                    <?php foreach ($aktivitas as $log): ?>
                                        <div class="list-group-item list-group-item-action">
                                            <div class="d-flex w-100 justify-content-between">
                                                <h6 class="mb-1"><?= $log->activity ?></h6>
                                                <small><?= date('d/m/Y H:i', strtotime($log->created_at)) ?></small>
                                            </div>
                                            <p class="mb-1 text-muted small"><?= $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>

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