<?php

session_start();


if (!isset($_SESSION["id"])) {
    echo json_encode(["ok" => false, "error_code" => 100]);
    exit();
}

if (!isset($_POST["id"])) {
    echo json_encode(["ok" => false, "error_code" => 101]);
    exit();
}

$_POST["id"] = (int)$_POST["id"];
if ($_POST["id"] < 1) {
    echo json_encode(["ok" => false, "error_code" => 102]);
    exit();
}


include("../../config.php");
$admins_function = new \Functions\Admins();
$sends_function = new \Functions\Sends();
$admin_info = $admins_function->get_by_id($_SESSION["id"])[0];
$root = "api/cancel_send.php";
if (in_array($root, json_decode($admin_info->permissions))) {
    $send_info = $sends_function->get_by_id($_POST["id"])[0];

    if ($send_info->status < 2) {

        $sends_function->update(
            $send_info->id,
            [
                "status" => 2
            ]
        );

        echo json_encode(["ok" => true]);
        exit();
    } else {
        echo json_encode(["ok" => false, "error_code" => 103]);
        exit();
    }
} else {
    echo json_encode(["ok" => false, "error_code" => 104]);
    exit();
}