<?php $app = require '../../../app/inc.php';

use App\Teamtable\Team;

$item    = filter_input(INPUT_GET, 'item', FILTER_VALIDATE_INT);
$confirm = filter_input(INPUT_GET, 'confirm', FILTER_VALIDATE_BOOLEAN);

$model = $app->model('Teamtable');

// item is NULL if not set
if ($item === NULL)
{
    $app->session->flash('Kunne ikke slette lag: Mangler parameter.', 'danger');
    $app->redirect('index.php');
}

$team = $model->get($item);
if (!$team)
{
    // team does not exist
    $app->session->flash('Kunne ikke slette lag: Lag eksisterer ikke', 'danger');
    $app->redirect('index.php');
}

// show confirmation page
if ($confirm)
{
    $app->view('template/header', ['title' => 'Bekreft sletting']);
    $app->view('pages/teamtable/edit/delete', ['team' => $team]);
    $app->view('template/footer');
    die();
}

// all is good, lets delete the team
$model->delete($team->id);
$app->session->flash("Slettet lag: {$team->name}", "success");
$app->redirect('index.php');