<?php
require "../api.php";

$api = new spamhasiApi;

if (!$api->checkAuth()) {
    die('{"status":500, "error":"Unauthorized"}');
}

$db = $api->getDB();

$sql = "SELECT * FROM opfer";
$stmt = $db->query($sql);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

$all_opfer = [];

if ($result) {
    foreach ($result as $opfer_res) {
        $opfer["id"] = $opfer_res["id"];
        $opfer["name"] = $opfer_res["name"];
        $opfer["mail"] = $opfer_res["mail"];
        $opfer["number"] = $opfer_res["number"];

        if ($opfer_res["status"] == 1) {
            $opfer["enabled"] = true;
        }else{
            $opfer["enabled"] = false;
        }

        $all_opfer[] = $opfer;
    }

    $json["opfer"] = $all_opfer;
}else{
    $json["opfer"] = [];
}

$json["status"] = 200;

die(json_encode($json));
?>