<?php
require_once __DIR__ . '/../connect.php';

$id = filterRequest("id");
$status = filterRequest("statusComplaint");
$resolution = filterRequest("resolution");

if ($id && $status) {
    $sql = "UPDATE `complaint` SET `statusComplaint` = :status, `resolution` = :resolution";
    
    if ($status == 'Resolved') {
        $sql .= ", `date_resolved` = CURRENT_TIMESTAMP";
    }
    
    $sql .= " WHERE `id` = :id";

    $stmt = $con->prepare($sql);
    $stmt->bindParam(':status', $status);
    $stmt->bindParam(':resolution', $resolution);
    $stmt->bindParam(':id', $id);

    if ($stmt->execute()) {
        jsonSuccess(null, "Complaint updated successfully");
    } else {
        jsonError("Failed to update complaint");
    }
} else {
    jsonError("Missing required fields");
}
?>
