<?php
require __DIR__ . "/koneksi.php";

$where = [];
$params = [];
$types = '';

if (!empty($_POST['from'])) {
    $where[] = "created_at >= ?";
    $params[] = $_POST['from'] . " 00:00:00";
    $types .= 's';
}

if (!empty($_POST['to'])) {
    $where[] = "created_at <= ?";
    $params[] = $_POST['to'] . " 23:59:59";
    $types .= 's';
}

if (!empty($_POST['pc_id'])) {
    $where[] = "pc_client_id = ?";
    $params[] = (int)$_POST['pc_id'];
    $types .= 'i';
}

if (!$where) {
    echo json_encode([
        "status" => "error",
        "message" => "Minimal tentukan filter!"
    ]);
    exit;
}

$sql = "DELETE FROM pc_monitoring WHERE " . implode(" AND ", $where);

$stmt = $conn->prepare($sql);
$stmt->bind_param($types, ...$params);
$stmt->execute();

echo json_encode([
    "status" => "ok",
    "message" => "Data berhasil dihapus: " . $stmt->affected_rows
]);
