<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/nabila/includes/config.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/nabila/includes/functions.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/nabila/includes/header.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/nabila/includes/sidebar.php';

// Cek login
cek_login();

// Cek akses
if (!cek_akses('absensi', 'view')) {
    header("Location: " . BASE_URL . "/index.php?access=denied");
    exit();
}

// Filter tanggal
$tanggal = isset($_GET['tanggal']) ? $_GET['tanggal'] : date('Y-m-d');
$search = isset($_GET['search']) ? sanitize($_GET['search']) : '';

// Query untuk mengambil data absensi
$where = "WHERE a.tanggal = '$tanggal'";
if (!empty($search)) {
    $where .= " AND (k.nama LIKE '%$search%' OR k.nik LIKE '%$search%')";
}

$query = "SELECT a.*, k.nik, k.nama, k.jabatan 
          FROM absensi a 
          JOIN karyawan k ON a.karyawan_id = k.id 
          $where 
          ORDER BY k.nama ASC";
$result = mysqli_query($conn, $query);

// Ambil pengaturan jam kerja
$pengaturan = get_pengaturan();
?>

<div class="main-content flex-1 p-8">
    <div class="flex flex-col md:flex-row justify-between items-start md:items-center mb-6 gap-4">
        <div>
            <h2 class="text-3xl font-bold text-white">Data Absensi Harian</h2>
            <p class="text-slate-400">Menampilkan data kehadiran karyawan pada tanggal yang dipilih.</p>
        </div>
        <div class="flex space-x-3">
            <?php if (cek_akses('absensi', 'scan')): ?>
            <a href="<?php echo BASE_URL; ?>/modules/absensi/scan.php" class="bg-sky-600 hover:bg-sky-700 text-white font-bold py-2.5 px-5 rounded-lg transition-colors flex items-center gap-2">
                <i class="fas fa-qrcode"></i> Scan QR
            </a>
            <?php endif; ?>
            
            <?php if (cek_akses('absensi', 'add')): ?>
            <a href="<?php echo BASE_URL; ?>/modules/absensi/tambah.php" class="bg-indigo-600 hover:bg-indigo-700 text-white font-bold py-2.5 px-5 rounded-lg transition-colors flex items-center gap-2">
                <i class="fas fa-plus"></i> Tambah Manual
            </a>
            <?php endif; ?>
        </div>
    </div>

    <?php if (isset($_GET['success'])): ?>
    <div class="bg-green-500/20 border border-green-500/30 text-green-300 px-4 py-3 rounded-lg relative mb-6 text-sm flex items-center gap-3" role="alert">
        <i class="fas fa-check-circle"></i><span><?php echo htmlspecialchars($_GET['success']); ?></span>
    </div>
    <?php endif; ?>
    
    <?php if (isset($_GET['error'])): ?>
    <div class="bg-red-500/20 border border-red-500/30 text-red-300 px-4 py-3 rounded-lg relative mb-6 text-sm flex items-center gap-3" role="alert">
        <i class="fas fa-exclamation-circle"></i><span><?php echo htmlspecialchars($_GET['error']); ?></span>
    </div>
    <?php endif; ?>
    
    <div class="glass-effect p-6 rounded-xl shadow-lg">
        <form action="" method="GET" class="flex flex-wrap items-end gap-4 mb-6 pb-6 border-b border-slate-700/50">
            <div>
                <label for="tanggal" class="block text-slate-400 text-sm font-medium mb-2">Tanggal</label>
                <input type="date" name="tanggal" id="tanggal" value="<?php echo $tanggal; ?>" class="w-full border border-slate-600 bg-slate-700/50 text-slate-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-sky-500 px-3 py-2.5">
            </div>
            <div class="flex-grow">
                <label for="search" class="block text-slate-400 text-sm font-medium mb-2">Cari Karyawan</label>
                <input type="text" name="search" id="search" value="<?php echo htmlspecialchars($search); ?>" class="w-full border border-slate-600 bg-slate-700/50 text-slate-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-sky-500 px-3 py-2.5" placeholder="Masukkan nama atau NIK...">
            </div>
            <button type="submit" class="bg-slate-700 hover:bg-slate-600 text-white font-bold py-2.5 px-6 rounded-lg transition-colors flex items-center gap-2">
                <i class="fas fa-filter"></i> Terapkan
            </button>
        </form>
        
        <div class="overflow-x-auto">
            <table class="w-full text-left table-auto">
                <thead>
                    <tr class="border-b border-slate-700">
                        <th class="px-4 py-3 text-slate-300 font-semibold">No</th>
                        <th class="px-4 py-3 text-slate-300 font-semibold">Nama</th>
                        <th class="px-4 py-3 text-slate-300 font-semibold">Jabatan</th>
                        <th class="px-4 py-3 text-slate-300 font-semibold">Jam Masuk</th>
                        <th class="px-4 py-3 text-slate-300 font-semibold">Jam Keluar</th>
                        <th class="px-4 py-3 text-slate-300 font-semibold text-center">Status</th>
                        <th class="px-4 py-3 text-slate-300 font-semibold text-center">Aksi</th>
                    </tr>
                </thead>
                <tbody class="text-slate-400">
                    <?php if (mysqli_num_rows($result) > 0): ?>
                        <?php $no = 1; ?>
                        <?php while ($row = mysqli_fetch_assoc($result)): ?>
                        <tr class="hover:bg-slate-800/50">
                            <td class="px-4 py-3 border-b border-slate-800"><?php echo $no++; ?></td>
                            <td class="px-4 py-3 border-b border-slate-800">
                                <p class="font-medium text-slate-200"><?php echo htmlspecialchars($row['nama']); ?></p>
                                <p class="text-xs"><?php echo htmlspecialchars($row['nik']); ?></p>
                            </td>
                            <td class="px-4 py-3 border-b border-slate-800"><?php echo htmlspecialchars($row['jabatan']); ?></td>
                            <td class="px-4 py-3 border-b border-slate-800 <?php echo ($row['jam_masuk'] > $pengaturan['jam_masuk']) ? 'text-red-400 font-bold' : ''; ?>">
                                <?php echo $row['jam_masuk'] ?: '-'; ?>
                                <?php if ($row['status'] == 'hadir' && $row['jam_masuk'] > $pengaturan['jam_masuk']): ?>
                                <span class="text-xs text-red-500 block">Terlambat <?php echo hitung_keterlambatan($row['jam_masuk'], $pengaturan['jam_masuk']); ?> menit</span>
                                <?php endif; ?>
                            </td>
                            <td class="px-4 py-3 border-b border-slate-800 <?php echo ($row['jam_keluar'] && $row['jam_keluar'] < $pengaturan['jam_keluar']) ? 'text-yellow-400' : ''; ?>">
                                <?php echo $row['jam_keluar'] ?: '-'; ?>
                            </td>
                            <td class="px-4 py-3 border-b border-slate-800 text-center">
                                <span class="px-3 py-1 text-xs font-semibold rounded-full 
                                      <?php 
                                      switch ($row['status']) {
                                          case 'hadir': echo 'bg-green-500/20 text-green-300'; break;
                                          case 'izin': echo 'bg-blue-500/20 text-blue-300'; break;
                                          case 'sakit': echo 'bg-yellow-500/20 text-yellow-300'; break;
                                          case 'cuti': echo 'bg-purple-500/20 text-purple-300'; break;
                                          case 'alpha': echo 'bg-red-500/20 text-red-300'; break;
                                          default: echo 'bg-gray-500/20 text-gray-300';
                                      }
                                      ?>">
                                    <?php echo ucfirst($row['status']); ?>
                                </span>
                            </td>
                            <td class="px-4 py-3 border-b border-slate-800 text-center">
                                <?php if (cek_akses('absensi', 'edit')): ?>
                                <a href="<?php echo BASE_URL; ?>/modules/absensi/edit.php?id=<?php echo $row['id']; ?>" class="text-yellow-400 hover:text-yellow-300 mr-3" title="Edit">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <?php endif; ?>
                                
                                <?php if (cek_akses('absensi', 'delete')): ?>
                                <a href="<?php echo BASE_URL; ?>/modules/absensi/hapus.php?id=<?php echo $row['id']; ?>" class="text-red-500 hover:text-red-400" title="Hapus" onclick="return confirm('Apakah Anda yakin ingin menghapus data absensi ini?')">
                                    <i class="fas fa-trash"></i>
                                </a>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="7" class="px-4 py-10 text-center text-slate-500">Tidak ada data absensi untuk tanggal ini.</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/nabila/includes/footer.php';
?>