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

// Ambil ID dari URL
$employee_id = (int)$_GET['id'];
if (!$employee_id) {
    redirect('employees_harian.php', false);
}

// Cari data karyawan berdasarkan ID
$employee = find_by_id('employees_harian', $employee_id);
if (!$employee) {
    $session->msg('d', 'Karyawan tidak ditemukan.');
    redirect('employees_harian.php');
}

if(isset($_POST['edit_employee_harian'])){
    $req_fields = array('first_name','last_name', 'email', 'hire_date', 'job_title', 'status');
    validate_fields($req_fields);

    if(empty($errors)){
        $first_name = remove_junk($db->escape($_POST['first_name']));
        $last_name  = remove_junk($db->escape($_POST['last_name']));
        $email      = remove_junk($db->escape($_POST['email']));
        $phone      = remove_junk($db->escape($_POST['phone_number']));
        $hire_date  = remove_junk($db->escape($_POST['hire_date']));
        $job_title  = remove_junk($db->escape($_POST['job_title']));
        $status     = remove_junk($db->escape($_POST['status']));
        $kontak_darurat = remove_junk($db->escape($_POST['kontak_darurat']));

        $sql = "UPDATE employees_harian SET 
                first_name = '{$first_name}',
                last_name = '{$last_name}',
                email = '{$email}',
                phone_number = '{$phone}',
                hire_date = '{$hire_date}',
                job_title = '{$job_title}',
                status = '{$status}',
                kontak_darurat = '{$kontak_darurat}'
                WHERE id = '{$employee_id}'";
        
        if($db->query($sql)){
            $session->msg('s',"Data karyawan harian berhasil diperbarui. ");
            redirect('employees_harian.php', false);
        } else {
            $session->msg('d','Gagal memperbarui data karyawan harian.');
            redirect('edit_employee_harian.php?id=' . (int)$employee['id'], false);
        }
    } else {
        $session->msg("d", $errors);
        redirect('edit_employee_harian.php?id=' . (int)$employee['id'], 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-9">
        <div class="panel panel-default">
            <div class="panel-heading">
                <strong>
                    <span class="glyphicon glyphicon-th"></span>
                    <span>Edit Karyawan Harian</span>
                </strong>
            </div>
            <div class="panel-body">
                <div class="col-md-12">
                    <form method="post" action="edit_employee_harian.php?id=<?php echo (int)$employee['id']; ?>" class="clearfix">
                        <div class="form-group">
                            <label for="first_name">Nama Depan</label>
                            <input type="text" class="form-control" name="first_name" value="<?php echo htmlspecialchars($employee['first_name']); ?>" required>
                        </div>
                        <div class="form-group">
                            <label for="last_name">Nama Belakang</label>
                            <input type="text" class="form-control" name="last_name" value="<?php echo htmlspecialchars($employee['last_name']); ?>" required>
                        </div>
                        <div class="form-group">
                            <label for="email">Email</label>
                            <input type="email" class="form-control" name="email" value="<?php echo htmlspecialchars($employee['email']); ?>" required>
                        </div>
                        <div class="form-group">
                            <label for="phone_number">Nomor Telepon</label>
                            <input type="text" class="form-control" name="phone_number" value="<?php echo htmlspecialchars($employee['phone_number']); ?>">
                        </div>
                        <div class="form-group">
                            <label for="hire_date">Tanggal Masuk</label>
                            <input type="date" class="form-control" name="hire_date" value="<?php echo htmlspecialchars($employee['hire_date']); ?>" required>
                        </div>
                        <div class="form-group">
                            <label for="job_title">Jabatan</label>
                            <input type="text" class="form-control" name="job_title" value="<?php echo htmlspecialchars($employee['job_title']); ?>" required>
                        </div>
                        <div class="form-group">
                            <label for="status">Status</label>
                            <select class="form-control" name="status" required>
                                <option value="Active" <?php if($employee['status'] == 'Active') echo 'selected'; ?>>Aktif</option>
                                <option value="On Leave" <?php if($employee['status'] == 'On Leave') echo 'selected'; ?>>Cuti</option>
                                <option value="Terminated" <?php if($employee['status'] == 'Terminated') echo 'selected'; ?>>Keluar</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="kontak_darurat">Kontak Darurat</label>
                            <input type="text" class="form-control" name="kontak_darurat" value="<?php echo htmlspecialchars($employee['kontak_darurat']); ?>">
                        </div>
                        <button type="submit" name="edit_employee_harian" class="btn btn-info">Perbarui Karyawan</button>
                        <a href="employees_harian.php" class="btn btn-default">Batal</a>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

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