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

if ( !$user['superAdmin'] ) {
$txt .= "not super admin\n";
$file = fopen(PATH_LOGS.'/btb.delete-selection-group.log', "a");
fwrite($file, $txt);
fclose($file);

    session_start();
    $_SESSION['error'] = 'You do not have permission to delete groups';

    header('Location: '.URL_APP);
    exit;
}

if ( !isset($_GET['groupId']) ) {
$txt .= "no group id\n";
$file = fopen(PATH_LOGS.'/btb.delete-selection-group.log', "a");
fwrite($file, $txt);
fclose($file);

    session_start();
    $_SESSION['error'] = 'No group ID';

    header('Location: '.URL_APP_ADMIN);
    exit;
}

if ( !$group = $db->row("SELECT * FROM selection_group WHERE id = ?", $_GET['groupId']) ) {
$txt .= "no group by that id\n";
$file = fopen(PATH_LOGS.'/btb.delete-selection-group.log', "a");
fwrite($file, $txt);
fclose($file);

    session_start();
    $_SESSION['error'] = 'Invalid group';

    header('Location: '.URL_APP_ADMIN);
    exit;
}

$txt .= "group before delete:\n";
$txt .= print_r($group, true);

$db->delete('selection_item', ['groupId' => $group['id']]);
$txt .= "selections deleted\n";
$db->delete('selection_group', ['id' => $group['id']]);
$txt .= "group deleted\n";

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

session_start();
$_SESSION['error'] = 'Group deleted';

header('Location: '.URL_APP.'/selections/?projectId='.$group['projectId']);
exit;
?>