<?php
$page_title = 'Manajemen Perangkat';
require_once('includes/load.php');

// Tingkat akses yang diperlukan: Level 2 (atau sesuai kebutuhan)
page_require_level(2);

// Ambil semua data karyawan
$all_employees = find_all('employees');

if(isset($_POST['update_device'])){
    $req_fields = array('employee_id', 'device_id');
    validate_fields($req_fields);

    if(empty($errors)){
        $employee_id = (int)$_POST['employee_id'];
        $device_id   = remove_junk($db->escape($_POST['device_id']));

        // Query untuk memeriksa apakah device_id sudah terdaftar di karyawan lain
        $check_device = $db->query("SELECT COUNT(*) as count FROM employees WHERE device_id = '{$device_id}' AND id != '{$employee_id}'");
        $device_exists = $db->fetch_assoc($check_device);

        if($device_exists['count'] > 0 && !empty($device_id)) {
            $session->msg('d', 'Device ID sudah terdaftar untuk karyawan lain.');
            redirect('manage_devices.php', false);
        }

        // Gunakan prepared statement untuk UPDATE
        $query = "UPDATE employees SET device_id = ? WHERE id = ?";
        $stmt = $db->prepare($query);
        $stmt->bind_param("si", $device_id, $employee_id);

        if($stmt->execute()){
            $session->msg('s', "Device ID berhasil diperbarui.");
            redirect('manage_devices.php', false);
        } else {
            $session->msg('d', "Gagal memperbarui Device ID.");
            redirect('manage_devices.php', false);
        }
    } else {
        $session->msg("d", $errors);
        redirect('manage_devices.php', false);
    }
}
?>

<?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-phone"></span>
                    <span>Manajemen Perangkat Karyawan</span>
                </strong>
            </div>
            <div class="panel-body">
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>Nama Karyawan</th>
                            <th>Jabatan</th>
                            <th>Device ID Terdaftar</th>
                            <th class="text-center" style="width: 25%;">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach($all_employees as $employee): ?>
                        <tr>
                            <td><?php echo remove_junk($employee['first_name'] . ' ' . $employee['last_name']); ?></td>
                            <td><?php echo remove_junk($employee['job_title']); ?></td>
                            <td><?php echo empty($employee['device_id']) ? '<span class="text-danger">Belum Terdaftar</span>' : remove_junk($employee['device_id']); ?></td>
                            <td class="text-center">
                                <form method="post" action="manage_devices.php" class="form-inline">
                                    <input type="hidden" name="employee_id" value="<?php echo (int)$employee['id']; ?>">
                                    <div class="input-group">
                                        <input type="text" class="form-control" name="device_id" placeholder="Device ID" value="<?php echo remove_junk($employee['device_id']); ?>">
                                        <span class="input-group-btn">
                                            <button type="submit" name="update_device" class="btn btn-warning btn-sm">
                                                <span class="glyphicon glyphicon-ok"></span> Perbarui
                                            </button>
                                        </span>
                                    </div>
                                </form>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

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