<?php
if (!isset($_GET['id'])) {
    header('Location: 505');
    exit;
}
include_once "global.php";

if (isset($_GET["id"])) {
    $staffId = $functions->decode($_GET["id"]);
    $staffData = $productClass->getStaffById($staffId);
}

if ($staffData === false) {
    header('Location: 505');
    exit;
}

$pageTitle = 'Edit Staff - ';
include_once 'header.php';
?>

<div class="card">
    <div class="card-body p-4">
        <h5 class="mb-4">Edit Staff</h5>
        <form id="staffEditForm" class="row g-3" method="post">
            <input type="hidden" name="staffId" value="<?php echo $staffId; ?>">
            <div class="col-md-6">
                <label for="userId" class="form-label">Name <span>*</span></label>
                <select id="userId" name="userId" class="form-select" required>
                    <option value="" disabled selected>Select...</option>
                    <?php
                    $users = $productClass->AllEmployeesAndManagersArray();
                    foreach ($users as $user) {
                        $userId = $user["acc_id"];
                        $name = $user["acc_name"];
                        $selectedUser = ($staffData['userId'] == $userId) ? 'selected' : '';
                        echo "<option value='$userId' $selectedUser>$name</option>";
                    }
                    ?>
                </select>
            </div>
            <div class="col-md-6">
                <label for="designation" class="form-label">Designation <span>*</span></label>
                <select id="designation" name="designation[]" class="multiple-select-field"
                    data-placeholder="Select Designation" multiple>
                    <?php
                    $designations = $productClass->AllDesignationArray();
                    $selectedDesignations = $staffData['designation_ids'] ?? [];
                    foreach ($designations as $designation) {
                        $Id = $designation["id"];
                        $designationTitle = $designation["designation"];
                        $selected = in_array($Id, $selectedDesignations) ? "selected" : "";
                        echo "<option value='$Id' $selected>$designationTitle</option>";
                    }
                    ?>
                </select>
            </div>
            <div class="col-md-6">
                <label for="checkintime" class="form-label">Check-In Time <span>*</span></label>
                <input type="time" name="checkintime" id="checkintime" class="form-control"
                    value="<?php echo $staffData["checkintime"]; ?>" required>
            </div>
            <div class="col-md-6">
                <label for="checkouttime" class="form-label">Check-Out Time <span>*</span></label>
                <input type="time" name="checkouttime" id="checkouttime" class="form-control"
                    value="<?php echo $staffData["checkouttime"]; ?>" required>
            </div>
            <div class="col-md-6">
                <label for="salary" class="form-label">Salary</label>
                <input type="number" name="salary" id="salary" class="form-control"
                    value="<?php echo $staffData["salary"]; ?>">
            </div>
            <div class="col-md-12">
                <button type="submit" class="btn btn-grd-primary px-4">Submit</button>
            </div>
        </form>
    </div>
</div>

<?php
include_once 'footer.php';
?>