<?php
$page_title = 'Daftar Karyawan';

// Sesuaikan path agar sesuai dengan lokasi baru
require_once('includes/load.php');
require_once('app/controllers/EmployeeController.php');

page_require_level(4);

// Inisialisasi controller dan dapatkan data
$employeeController = new EmployeeController();

// Cek jika ada permintaan ekspor sebelum menampilkan HTML
if (isset($_GET['export_excel'])) {
    $employeeController->export();
    exit;
}

// Dapatkan data dari controller
$data = $employeeController->index();
extract($data); // Gunakan extract() untuk membuat variabel dari array

include_once('layouts/header.php');
?>
<style>
    .dashboard-panel {
        display: flex;
        justify-content: space-between;
        flex-wrap: wrap;
    }
    .dashboard-panel .col-md-3 {
        flex: 1 1 23%;
    }
    .panel-heading i {
        margin-right: 5px;
    }
    .btn-group .btn {
        margin-right: 2px;
    }
</style>

<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
</div>

<div class="row dashboard-panel">
    <div class="col-md-3">
        <div class="panel panel-info">
            <div class="panel-heading">
                <i class="glyphicon glyphicon-user"></i> Total Karyawan
            </div>
            <div class="panel-body text-center">
                <h2><?php echo number_format($total_employees_count); ?></h2>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="panel panel-success">
            <div class="panel-heading">
                <i class="glyphicon glyphicon-ok-circle"></i> Karyawan Aktif
            </div>
            <div class="panel-body text-center">
                <h2><?php echo number_format($active_employees_count); ?></h2>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="panel panel-danger">
            <div class="panel-heading">
                <i class="glyphicon glyphicon-time"></i> Karyawan Cuti
            </div>
            <div class="panel-body text-center">
                <h2><?php echo number_format($on_leave_employees_count); ?></h2>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <i class="glyphicon glyphicon-briefcase"></i> Total Departemen
            </div>
            <div class="panel-body text-center">
                <h2><?php echo number_format($total_departments_count); ?></h2>
            </div>
        </div>
    </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-th"></span>
                    <span>Daftar Karyawan</span>
                </strong>
                <a href="add_employee.php" class="btn btn-info pull-right">Tambah Karyawan</a>
            </div>
            <div class="panel-body">
                <form action="" method="GET" class="form-horizontal mb-4">
                    <div class="form-group clearfix">
                        <div class="col-md-3">
                            <input type="text" name="search" class="form-control" placeholder="Cari nama atau ID..." value="<?php echo htmlspecialchars($search_term); ?>">
                        </div>
                        <div class="col-md-3">
                            <select name="department_id" class="form-control">
                                <option value="0">Semua Departemen</option>
                                <?php foreach ($departments as $dept): ?>
                                    <option value="<?php echo (int)$dept['department_id']; ?>" <?php if ($department_id == $dept['department_id']) echo 'selected'; ?>>
                                        <?php echo htmlspecialchars($dept['name']); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-2">
                            <select name="status" class="form-control">
                                <option value="">Semua Status</option>
                                <option value="Active" <?php if ($status == 'Active') echo 'selected'; ?>>Aktif</option>
                                <option value="On Leave" <?php if ($status == 'On Leave') echo 'selected'; ?>>Cuti</option>
                                <option value="Terminated" <?php if ($status == 'Terminated') echo 'selected'; ?>>Keluar</option>
                            </select>
                        </div>
                        <div class="col-md-2">
                            <select name="per_page" class="form-control">
                                <option value="10" <?php if ($per_page == 10) echo 'selected'; ?>>10 per halaman</option>
                                <option value="25" <?php if ($per_page == 25) echo 'selected'; ?>>25 per halaman</option>
                                <option value="50" <?php if ($per_page == 50) echo 'selected'; ?>>50 per halaman</option>
                                <option value="100" <?php if ($per_page == 100) echo 'selected'; ?>>100 per halaman</option>
                            </select>
                        </div>
                        <div class="col-md-2">
                            <button type="submit" class="btn btn-primary">Filter</button>
                            <a href="employees.php" class="btn btn-default">Reset</a>
                            <a href="?export_excel=1&search=<?php echo urlencode($search_term); ?>&department_id=<?php echo $department_id; ?>&status=<?php echo urlencode($status); ?>" class="btn btn-success pull-right">Export Excel</a>
                        </div>
                    </div>
                </form>

                <div class="table-responsive">
                    <table class="table table-bordered table-striped">
                        <thead>
                            <tr>
                                <th class="text-center" style="width: 50px;">#</th>
                                <th>Nama Lengkap</th>
                                <th class="text-center">Departemen</th>
                                <th class="text-center">Jabatan</th>
                                <th class="text-center">Tanggal Masuk</th>
                                <th class="text-center">Status</th>
                                <th class="text-center">Kontak Darurat</th>
                                <th class="text-center" style="width: 100px;">Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if (!empty($employees)): ?>
                                <?php $i = $start + 1; ?>
                                <?php foreach ($employees as $employee) : ?>
                                    <tr>
                                        <td class="text-center"><?php echo (int)$i++; ?></td>
                                        <td><?php echo remove_junk($employee['first_name']) . ' ' . remove_junk($employee['last_name']); ?></td>
                                        <td class="text-center"><?php echo remove_junk($employee['department']); ?></td>
                                        <td class="text-center"><?php echo remove_junk($employee['job_title']); ?></td>
                                        <td class="text-center"><?php echo htmlspecialchars($employee['hire_date']); ?></td>
                                        <td class="text-center">
                                            <?php if ($employee['status'] == 'Active'): ?>
                                                <span class="label label-success"><?php echo htmlspecialchars($employee['status']); ?></span>
                                            <?php elseif ($employee['status'] == 'On Leave'): ?>
                                                <span class="label label-warning"><?php echo htmlspecialchars($employee['status']); ?></span>
                                            <?php else: ?>
                                                <span class="label label-danger"><?php echo htmlspecialchars($employee['status']); ?></span>
                                            <?php endif; ?>
                                        </td>
                                        <td class="text-center"><?php echo htmlspecialchars($employee['kontak_darurat']); ?></td>
                                        <td class="text-center">
                                            <div class="btn-group">
                                                <a href="edit_employee.php?id=<?php echo (int)$employee['id'];?>" class="btn btn-xs btn-warning" title="Edit" data-toggle="tooltip">
                                                    <i class="glyphicon glyphicon-pencil"></i>
                                                </a>
                                                <a href="delete_employee.php?id=<?php echo (int)$employee['id'];?>" class="btn btn-xs btn-danger" title="Hapus" data-toggle="tooltip" onclick="return confirm('Apakah Anda yakin ingin menghapus karyawan 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 karyawan ditemukan.</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; ?>&per_page=<?php echo $per_page; ?>&department_id=<?php echo $department_id; ?>&search=<?php echo urlencode($search_term); ?>&status=<?php echo urlencode($status); ?>">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; ?>&per_page=<?php echo $per_page; ?>&department_id=<?php echo $department_id; ?>&search=<?php echo urlencode($search_term); ?>&status=<?php echo urlencode($status); ?>"><?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; ?>&per_page=<?php echo $per_page; ?>&department_id=<?php echo $department_id; ?>&search=<?php echo urlencode($search_term); ?>&status=<?php echo urlencode($status); ?>">Next</a>
                            </li>
                        </ul>
                    </nav>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<?php include_once('layouts/footer.php'); ?>