<?php
$page_title = 'Daftar Absensi Harian';
require_once('includes/load.php');
page_require_level(4);

// Filter data
$start_date = isset($_GET['start_date']) ? $db->escape($_GET['start_date']) : date('Y-m-d');
$end_date = isset($_GET['end_date']) ? $db->escape($_GET['end_date']) : date('Y-m-d');
$search_term = isset($_GET['search']) ? $db->escape($_GET['search']) : '';
$per_page = isset($_GET['per_page']) ? (int)$_GET['per_page'] : 25;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$start = ($page - 1) * $per_page;

$where_clauses = ["a.tanggal BETWEEN '{$start_date}' AND '{$end_date}'"];
if (!empty($search_term)) {
    $where_clauses[] = " (e.first_name LIKE '%{$search_term}%' OR e.last_name LIKE '%{$search_term}%') ";
}

$sql_count = "SELECT COUNT(*) AS total FROM absensi a LEFT JOIN employees e ON a.employee_id = e.id WHERE " . implode(' AND ', $where_clauses);
$total_rows = find_by_sql($sql_count)[0]['total'];
$total_pages = ceil($total_rows / $per_page);

$sql = "SELECT a.*, e.first_name, e.last_name, e.job_title FROM absensi a ";
$sql .= "LEFT JOIN employees e ON a.employee_id = e.id ";
$sql .= "WHERE " . implode(' AND ', $where_clauses);
$sql .= " ORDER BY a.tanggal DESC, e.first_name ASC LIMIT {$per_page} OFFSET {$start}";

$absensi_harian = find_by_sql($sql);
?>

<?php include_once('layouts/header.php'); ?>
<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
</div>
<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading clearfix">
                <strong>
                    <span class="glyphicon glyphicon-list"></span>
                    <span>Daftar Absensi Harian</span>
                </strong>
                <a href="add_absensi.php" class="btn btn-info pull-right">Tambah Absensi</a>
            </div>
            <div class="panel-body">
                <form action="" method="GET" class="form-inline mb-4">
                    <div class="form-group">
                        <label for="start_date">Dari Tanggal:</label>
                        <input type="date" name="start_date" id="start_date" class="form-control" value="<?php echo htmlspecialchars($start_date); ?>">
                    </div>
                    <div class="form-group">
                        <label for="end_date">Sampai Tanggal:</label>
                        <input type="date" name="end_date" id="end_date" class="form-control" value="<?php echo htmlspecialchars($end_date); ?>">
                    </div>
                    <div class="form-group">
                        <label for="search">Cari Nama:</label>
                        <input type="text" name="search" id="search" class="form-control" placeholder="Nama Karyawan" value="<?php echo htmlspecialchars($search_term); ?>">
                    </div>
                    <button type="submit" class="btn btn-primary">Filter</button>
                    <a href="absensi_harian.php" class="btn btn-default">Reset</a>
                </form>
                <div class="table-responsive">
                    <table class="table table-bordered table-striped">
                        <thead>
                            <tr>
                                <th class="text-center">#</th>
                                <th>Nama Karyawan</th>
                                <th class="text-center">Tanggal</th>
                                <th class="text-center">Status</th>
                                <th class="text-center">Waktu Masuk</th>
                                <th class="text-center">Waktu Keluar</th>
                                <th>Keterangan</th>
                                <th class="text-center">Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (!empty($absensi_harian)): ?>
                                <?php $i = $start + 1; ?>
                                <?php foreach ($absensi_harian as $absensi): ?>
                                    <tr>
                                        <td class="text-center"><?php echo (int)$i++; ?></td>
                                        <td><?php echo remove_junk($absensi['first_name'] . ' ' . $absensi['last_name']); ?></td>
                                        <td class="text-center"><?php echo htmlspecialchars($absensi['tanggal']); ?></td>
                                        <td class="text-center">
                                            <span class="label <?php 
                                                if ($absensi['status'] == 'Hadir') echo 'label-success';
                                                elseif ($absensi['status'] == 'Sakit') echo 'label-warning';
                                                elseif ($absensi['status'] == 'Izin') echo 'label-info';
                                                elseif ($absensi['status'] == 'Alpha') echo 'label-danger';
                                                else echo 'label-primary';
                                            ?>">
                                                <?php echo remove_junk($absensi['status']); ?>
                                            </span>
                                        </td>
                                        <td class="text-center"><?php echo htmlspecialchars($absensi['waktu_masuk'] ?? 'N/A'); ?></td>
                                        <td class="text-center"><?php echo htmlspecialchars($absensi['waktu_keluar'] ?? 'N/A'); ?></td>
                                        <td><?php echo remove_junk($absensi['keterangan'] ?? 'Tidak ada'); ?></td>
                                        <td class="text-center">
                                            <div class="btn-group">
                                                <a href="edit_absensi.php?id=<?php echo (int)$absensi['id'];?>" class="btn btn-xs btn-warning" title="Edit" data-toggle="tooltip">
                                                    <i class="glyphicon glyphicon-pencil"></i>
                                                </a>
                                                <a href="delete_absensi.php?id=<?php echo (int)$absensi['id'];?>" class="btn btn-xs btn-danger" title="Hapus" data-toggle="tooltip" onclick="return confirm('Apakah Anda yakin ingin menghapus data absensi ini?');">
                                                    <i class="glyphicon glyphicon-remove"></i>
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="8" class="text-center">Tidak ada data absensi ditemukan dalam rentang tanggal ini.</td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
                <?php if ($total_pages > 1): ?>
                    <nav aria-label="Page navigation">
                        <ul class="pagination pull-right">
                            <li class="page-item <?php if ($page <= 1) echo 'disabled'; ?>">
                                <a class="page-link" href="?page=<?php echo $page - 1; ?>&start_date=<?php echo urlencode($start_date); ?>&end_date=<?php echo urlencode($end_date); ?>&search=<?php echo urlencode($search_term); ?>">Prev</a>
                            </li>
                            <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                                <li class="page-item <?php if ($i == $page) echo 'active'; ?>">
                                    <a class="page-link" href="?page=<?php echo $i; ?>&start_date=<?php echo urlencode($start_date); ?>&end_date=<?php echo urlencode($end_date); ?>&search=<?php echo urlencode($search_term); ?>"><?php echo $i; ?></a>
                                </li>
                            <?php endfor; ?>
                            <li class="page-item <?php if ($page >= $total_pages) echo 'disabled'; ?>">
                                <a class="page-link" href="?page=<?php echo $page + 1; ?>&start_date=<?php echo urlencode($start_date); ?>&end_date=<?php echo urlencode($end_date); ?>&search=<?php echo urlencode($search_term); ?>">Next</a>
                            </li>
                        </ul>
                    </nav>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
<?php include_once('layouts/footer.php'); ?>