<?php

require_once "autoload.php";
use edu\uga\cs\recdawgs\logic\impl\LogicLayerImpl;
$logicLayer = new LogicLayerImpl();
$teamId = $_POST['teamId'];
try {
    // find team
    $team = $logicLayer->findTeam(null, $teamId)->current();
    // delete team
    $logicLayer->deleteTeam($team);
    $successMsg = urlencode("Team successfully deleted!");
    header("Location: ../teams.php");
    //echo $persistenceId;
} catch (\edu\uga\cs\recdawgs\RDException $rde) {
    $error_msg = urlencode($rde->string);
    die($error_msg);
    header("Location: ../team.php?status={$error_msg}");
} catch (Exception $e) {
    $errorMsg = urlencode("Unexpected error");
    die($errorMsg);
    header("Location: ../team.php?status={$errorMsg}");
}
exit;
$newTeamName = null;
$student = null;
$league = null;
try {
    // updated information
    if (trim($_POST['teamName']) == "" or $_POST['teamName'] == null) {
        $newTeamName = null;
    } else {
        $newTeamName = trim($_POST['teamName']);
    }
    if (intval($_SESSION['userId']) != -1) {
        $student = $logicLayer->findStudent(null, intval($_SESSION['userId']))->current();
    }
    if (intval($_POST['leagueId']) != -1) {
        $league = $logicLayer->findLeague(null, intval($_POST['leagueId']))->current();
    }
    // find team
    $team = $logicLayer->findTeam(null, $_POST['teamId'])->current();
    // update team
    $logicLayer->updateTeam($team->getName(), $newTeamName, $student, $league, null);
    $successMsg = urlencode("Team name successfully updated to {$newTeamName}!");
    header("Location: ../teams.php?status={$successMsg}");
    //echo $persistenceId;
} catch (\edu\uga\cs\recdawgs\RDException $rde) {
    $error_msg = urlencode($rde->string);
    header("Location: ../teams.php?status={$error_msg}");
} catch (Exception $e) {
    $errorMsg = urlencode("Unexpected error");
    header("Location: ../teams.php?status={$errorMsg}");
}
exit;