<?php
include 'includes/auth.php';
include 'db.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $housekeepers = $_POST['housekeeper'] ?? [];
    $schedule_dates = $_POST['schedule_date'] ?? [];
    $assigned = $_POST['assigned'] ?? [];

    foreach ($housekeepers as $room_id => $staff_name) {
        $schedule_date = $schedule_dates[$room_id] ?? date('Y-m-d');
        $is_assigned = isset($assigned[$room_id]) && $assigned[$room_id] === 'Yes' ? 'Yes' : 'No';

        // Update housekeeping_status table
        $stmt = $conn->prepare("
            INSERT INTO housekeeping_status (room_id, housekeeper, schedule_date, housekeeping_status)
            VALUES (?, ?, ?, 'Needs Cleaning')
            ON DUPLICATE KEY UPDATE
                housekeeper=VALUES(housekeeper),
                schedule_date=VALUES(schedule_date),
                housekeeping_status='Needs Cleaning'
        ");
        $stmt->bind_param("iss", $room_id, $staff_name, $schedule_date);
        $stmt->execute();
        $stmt->close();
    }

    header("Location: cleaning_schedule.php");
    exit;
} else {
    die("Invalid request.");
}
?>