<?php 
include 'includes/config.php';
include 'includes/header.php';

// Tambahkan fungsi ini di bagian atas file setelah include config.php
function indonesian_date($timestamp) {
    $bulan = array (
        1 => 'Januari',
        'Februari',
        'Maret',
        'April',
        'Mei',
        'Juni',
        'Juli',
        'Agustus',
        'September',
        'Oktober',
        'November',
        'Desember'
    );
    
    $hari = array (
        'Minggu',
        'Senin',
        'Selasa',
        'Rabu',
        'Kamis',
        'Jumat',
        'Sabtu'
    );
    
    $date = date('w d m Y H:i', strtotime($timestamp));
    $split = explode(' ', $date);
    
    $num_day = $split[0];
    $day = $split[1];
    $month = $split[2];
    $year = $split[3];
    $time = $split[4];
    
    // Jika ingin menampilkan nama hari
    // return $hari[(int)$num_day] . ', ' . $day . ' ' . $bulan[(int)$month] . ' ' . $year . ' ' . $time;
    
    // Tanpa nama hari
    return $day . ' ' . $bulan[(int)$month] . ' ' . $year . ' ' . $time;
}

// Ambil data event aktif dari database
$active_events = [];
$query = "SELECT * FROM dafdir_event WHERE status = 'aktif' ORDER BY tanggal_event DESC LIMIT 1"; // Hanya ambil 1 event aktif terbaru
$result = mysqli_query($conn, $query);
if ($result && mysqli_num_rows($result) > 0) {
    $active_event = mysqli_fetch_assoc($result);
    
    // Ambil data absensi untuk event aktif ini
    $query_absensi = "SELECT * FROM dafdir_absensi WHERE id_event = ? ORDER BY waktu_isi DESC";
    $stmt = mysqli_prepare($conn, $query_absensi);
    mysqli_stmt_bind_param($stmt, "i", $active_event['id_event']);
    mysqli_stmt_execute($stmt);
    $result_absensi = mysqli_stmt_get_result($stmt);
    $absensi_data = [];
    if ($result_absensi && mysqli_num_rows($result_absensi) > 0) {
        while ($row = mysqli_fetch_assoc($result_absensi)) {
            $absensi_data[] = $row;
        }
    }
}

// Di bagian atas index.php, setelah include config.php
if (isset($_GET['logout']) && $_GET['logout'] === 'success') {
    echo '<script>
    document.addEventListener("DOMContentLoaded", function() {
        Swal.fire({
            icon: "success",
            title: "Logout Berhasil",
            text: "Anda telah keluar dari sistem admin",
            timer: 3000
        });
    });
    </script>';
}

?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SMANSAB - Absensi Digital</title>
    <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@300;400;600;700&display=swap" rel="stylesheet">
    <link href="assets/css/sweetalert2.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="assets/css/style.css" rel="stylesheet">
    <style>
        .signature-preview {
            max-width: 150px;
            max-height: 50px;
        }
        .badge-status {
            font-size: 0.8rem;
            padding: 5px 10px;
        }
        .full-width-card {
        width: 100vw;
        margin-left: calc(-50vw + 50%); /* agar menyesuaikan posisi tengah */
    }
    </style>
</head>
<body>
    <!-- <nav class="navbar navbar-expand-lg navbar-dark bg-primary shadow-sm">
        <div class="container">
            <a class="navbar-brand fw-bold" href="#">
                <i class="fas fa-user-check fa-lg text-white"></i>
                <span class="ms-2">SMANSAB Absensi Digital</span>
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto">
                    <li class="nav-item">
                        <a class="nav-link active" href="index.php">Beranda</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="attendance.php">Isi Absensi</a>
                    </li>
                </ul>
                <ul class="navbar-nav">
                    <li class="nav-item">
                        <a class="nav-link" href="admin/login.php">
                            <i class="fas fa-sign-in-alt me-1"></i> Admin Login
                        </a>
                    </li>
                </ul>
            </div>
        </div>
    </nav> -->

    <div class="container my-1">
        <div class="row justify-content-center">
            <div class="col-lg-10">
               <div class="card shadow-sm border-0 text-white mb-5 full-width-card" style="background-color:rgb(3, 34, 80);">
                    <div class="card-body text-center py-5">
                        <h1 class="display-4 fw-bold mb-3" style="color: #ffffff;">
                            SISTEM ABSENSI DIGITAL
                        </h1>
                        <p class="lead">
                            <span style="font-weight: 900; color: #ffffff;">SMAN 1 BANTARAN <br>
                            Solusi Absensi Modern dan Efisien</span>
                        </p>
                        <a href="attendance.php" class="btn btn-light btn-lg px-4 py-2 mt-3 shadow-sm">
                            <i class="fas fa-pen-fancy me-2"></i> Isi Absensi Sekarang
                        </a>
                    </div>
                </div>


                <?php if (isset($active_event)): ?>
                <div class="card shadow-sm border-0 mb-4">
                    <div class="card-header bg-primary text-white py-3">
                        <div class="d-flex justify-content-between align-items-center">
                            <h5 class="card-title mb-0">
                                <i class="fas fa-calendar-alt me-2"></i> 
                                <?= htmlspecialchars($active_event['nama_event']) ?>
                            </h5>
                            <span class="badge bg-white text-primary">
                                <?= date('d F Y', strtotime($active_event['tanggal_event'])) ?>
                            </span>
                        </div>
                    </div>
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center mb-4">
                            <div>
                                <span class="text-muted">
                                    <i class="fas fa-user-check me-1"></i> Pengesah: 
                                    <?= htmlspecialchars($active_event['pengesah'] ?? 'Belum ditentukan') ?>
                                </span>
                            </div>
                            <div>
                                <span class="badge bg-danger bg-opacity-100 text-white">
                                    Total Peserta: <?= count($absensi_data) ?>
                                </span>
                            </div>
                        </div>

                        <?php if (!empty($absensi_data)): ?>
                        <div class="table-responsive">
                            <table class="table table-hover align-middle">
                                <thead class="table-light">
                                    <tr>
                                        <th width="5%">No</th>
                                        <th width="25%">Nama Lengkap</th>
                                        <th width="30%">Jabatan</th>
                                        <th width="20%">Tanda Tangan</th>
                                        <th width="20%">Waktu Absensi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($absensi_data as $index => $absensi): ?>
                                    <tr>
                                        <td><?= $index + 1 ?></td>
                                        <td class="fw-semibold"><?= htmlspecialchars($absensi['nama_lengkap']) ?></td>
                                        <td><?= htmlspecialchars($absensi['jabatan']) ?></td>
                                        <td>
                                            <?php if (!empty($absensi['tanda_tangan'])): ?>
                                            <img src="<?= htmlspecialchars($absensi['tanda_tangan']) ?>" 
                                                 alt="Tanda Tangan" class="signature-preview">
                                            <?php else: ?>
                                            <span class="text-muted">Tidak ada</span>
                                            <?php endif; ?>
                                        </td>
                                        <td>
                                            <?= indonesian_date($absensi['waktu_isi']) ?>
                                        </td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                        <?php else: ?>
                        <div class="alert alert-info text-center py-4">
                            <i class="fas fa-info-circle fa-2x mb-3 text-primary"></i>
                            <h5 class="fw-semibold">Belum ada data absensi</h5>
                            <p class="mb-0">Silakan isi absensi terlebih dahulu</p>
                        </div>
                        <?php endif; ?>
                    </div>
                </div>
                <?php else: ?>
                <div class="alert alert-info text-center py-4">
                    <i class="fas fa-info-circle fa-2x mb-3 text-primary"></i>
                    <h5 class="fw-semibold">Tidak ada kegiatan aktif saat ini</h5>
                    <p class="mb-0">Silakan hubungi administrator untuk informasi lebih lanjut</p>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="assets/js/sweetalert2.min.js"></script>
    <script src="assets/js/main.js"></script>
</body>
</html>