<?php
session_start();
require_once '../../config/database.php';
require_once '../../config/function.php';

// Cek login
checkLogin();

$current_user = getCurrentUser();
$tahun_ajaran = getTahunAjaranAktif();

// Ambil data tahun ajaran
$query = "SELECT * FROM bkcare_tahun_ajaran ORDER BY 
          CASE WHEN status = 'aktif' THEN 0 ELSE 1 END, 
          tahun_ajaran DESC, 
          CASE semester WHEN 'ganjil' THEN 0 ELSE 1 END";
$result = mysqli_query($conn, $query);

include '../../includes/header.php';
include '../../includes/navbar.php';
?>

<!-- Main Content -->
<main class="container mx-auto px-4 py-6">
    <!-- Header -->
    <div class="flex justify-between items-center mb-6">
        <div>
            <h1 class="text-2xl font-bold text-gray-800 flex items-center">
                <i class="fas fa-calendar-alt text-purple-600 mr-3"></i>
                Data Tahun Ajaran
            </h1>
            <p class="text-gray-600 text-sm mt-1">Kelola tahun ajaran dan semester</p>
        </div>
        <a href="tambah.php" class="btn-primary">
            <i class="fas fa-plus mr-2"></i>Tambah Tahun Ajaran
        </a>
    </div>

    <!-- Table -->
    <div class="card-stat p-6">
        <div class="overflow-x-auto">
            <table class="w-full">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">No</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Tahun Ajaran</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Semester</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Status</th>
                        <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">Aksi</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    <?php if (mysqli_num_rows($result) > 0): ?>
                        <?php $no = 1; ?>
                        <?php while($row = mysqli_fetch_assoc($result)): ?>
                            <tr class="hover:bg-purple-50 transition">
                                <td class="px-6 py-4 text-sm text-gray-600"><?= $no++ ?></td>
                                <td class="px-6 py-4 text-sm font-medium text-gray-800">
                                    <i class="fas fa-calendar text-purple-400 mr-2"></i>
                                    <?= htmlspecialchars($row['tahun_ajaran']) ?>
                                </td>
                                <td class="px-6 py-4 text-sm">
                                    <span class="<?= $row['semester'] == 'ganjil' ? 'badge-info' : 'badge-success' ?> px-3 py-1 rounded-full text-xs">
                                        <i class="fas fa-<?= $row['semester'] == 'ganjil' ? 'sun' : 'moon' ?> mr-1"></i>
                                        <?= ucfirst($row['semester']) ?>
                                    </span>
                                </td>
                                <td class="px-6 py-4 text-sm">
                                    <?php if ($row['status'] == 'aktif'): ?>
                                        <span class="badge-success px-3 py-1 rounded-full text-xs">
                                            <i class="fas fa-check-circle mr-1"></i>Aktif
                                        </span>
                                    <?php else: ?>
                                        <span class="badge-warning px-3 py-1 rounded-full text-xs">
                                            <i class="fas fa-clock mr-1"></i>Nonaktif
                                        </span>
                                    <?php endif; ?>
                                </td>
                                <td class="px-6 py-4 text-sm text-right">
    <a href="edit.php?id=<?= $row['id_tahun_ajaran'] ?>" 
       class="text-blue-600 hover:text-blue-800 mx-2"
       data-bs-toggle="tooltip" 
       title="Edit Data">
        <i class="fas fa-edit"></i>
    </a>
    
    <?php if ($row['status'] == 'aktif'): ?>
        <!-- Jika aktif, tombol hapus disabled dengan tooltip khusus -->
        <span class="text-gray-400 mx-2 cursor-not-allowed" 
              data-bs-toggle="tooltip" 
              title="Tahun ajaran aktif tidak dapat dihapus">
            <i class="fas fa-trash"></i>
        </span>
    <?php else: ?>
        <!-- Jika tidak aktif, tombol hapus normal -->
        <a href="#" 
           onclick="confirmDelete(<?= $row['id_tahun_ajaran'] ?>, '<?= $row['tahun_ajaran'] ?> - Semester <?= ucfirst($row['semester']) ?>')"
           class="text-red-600 hover:text-red-800 mx-2"
           data-bs-toggle="tooltip" 
           title="Hapus Data">
            <i class="fas fa-trash"></i>
        </a>
    <?php endif; ?>
</td>
                            </tr>
                        <?php endwhile; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="5" class="px-6 py-12 text-center text-gray-500">
                                <i class="fas fa-folder-open text-5xl mb-3 text-gray-300"></i>
                                <p class="text-lg font-medium">Belum ada data tahun ajaran</p>
                                <p class="text-sm mt-1">Klik tombol "Tambah Tahun Ajaran" untuk menambahkan data</p>
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</main>

<!-- Modal Konfirmasi Hapus -->
<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content rounded-2xl">
            <div class="modal-header bg-red-500 text-white rounded-t-2xl">
                <h5 class="modal-title">
                    <i class="fas fa-exclamation-triangle mr-2"></i>Konfirmasi Hapus
                </h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body p-6">
                <p class="text-gray-600 mb-2">Apakah Anda yakin ingin menghapus data tahun ajaran:</p>
                <p class="font-semibold text-gray-800" id="deleteItemInfo"></p>
                <p class="text-sm text-red-600 mt-3">
                    <i class="fas fa-info-circle mr-1"></i>
                    Data yang sudah dihapus tidak dapat dikembalikan!
                </p>
            </div>
            <div class="modal-footer border-t border-gray-100 p-4">
                <button type="button" class="btn-outline-primary text-sm" data-bs-dismiss="modal">
                    <i class="fas fa-times mr-1"></i>Batal
                </button>
                <a href="#" id="confirmDeleteBtn" class="bg-red-500 text-white px-6 py-2 rounded-full hover:bg-red-600 transition text-sm">
                    <i class="fas fa-trash mr-1"></i>Ya, Hapus
                </a>
            </div>
        </div>
    </div>
</div>

<script>
// Fungsi konfirmasi hapus - panggil global function
function confirmDelete(id, info) {
    // Panggil fungsi global dari footer
    if (typeof window.confirmDelete === 'function') {
        window.confirmDelete(id, info, 'hapus.php');
    } else {
        // Fallback jika fungsi global tidak ada
        document.getElementById('deleteItemInfo').textContent = info;
        document.getElementById('confirmDeleteBtn').href = 'hapus.php?id=' + id;
        
        var deleteModal = new bootstrap.Modal(document.getElementById('deleteModal'));
        deleteModal.show();
    }
}

// Tooltip initialization
$(document).ready(function() {
    var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
    var tooltipList = tooltipTriggerList.map(function(tooltipTriggerEl) {
        return new bootstrap.Tooltip(tooltipTriggerEl);
    });
});
</script>

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