<?php
$datetime = date("Y-m-d H:i:s");
$txt  = "\n$datetime - ".$_SERVER['PHP_SELF']."\n";
$txt .= print_r($user, true);
$txt .= print_r($_POST, true);

//make sure user is superAdmin
if ( !$user['superAdmin'] ) {
$txt .= "403 not superAdmin\n";

    http_response_code(403);
    echo json_encode('You do not have permission to delete plans');
    exit;
}

//validate plan id
if ( !$plan = $db->row("SELECT * FROM plan WHERE id = ?", $_POST['planId']) ) {
$txt .= "400 invalid plan ID\n";

    http_response_code(400);
    echo json_encode('Invalid plan ID');
    exit;
} 
$txt .= print_r($plan, true);

$db->delete('plan', ['id' => $_POST['planId']]);

$txt .= "done\n\n\n\n";
$file = fopen(PATH_LOGS.'/btb.delete-plans.log', "a");
fwrite($file, $txt);
fclose($file);

$plans = $db->run("SELECT * FROM plan WHERE projectId = ? ORDER BY createdAt DESC", $plan['projectId']);
include '../template/plans.php';