<?php

class Runs extends Page {

    function setMenu()
    {
        // $this->menu_text = 'Läufe';
        // $this->menu_image = 'fas fa-running';
        // $this->menu_priority = 3;
    }

    function overview()
    {
        $run = new Run();
        $rid = $this->params[0];
        if(!$run->exists($rid))
            return partial('error.html', ['errorMessage' => 'Dieser Lauf existiert nicht']);
        $run->load($rid);
        $t = new Tournament();
        $t->load($run->data['tournament']);

        $this->set('admin',$t->amIAdmin());
        $this->set('tournament', $t->data);
        $this->set('tournament_id', $run->data['tournament']);
        $this->set('run', $run->data);
        $this->set('run_id', $rid);
        $this->set('template', 'run.html');
    }

    function add()
    {
        $tournament = $this->params[0];
        $t = new Tournament();
        if(!$t->exists($tournament))
            return partial('error.html', ['errorMessage' => 'Dieses Turnier existiert nicht']);
        $t->load($tournament);
        if(!$t->amIAdmin($tournament))
            return partial('error.html', ['errorMessage' => 'Du bist kein Admin dieses Turniers']);
        else
        {
            $this->set('tournament', $t->data);
            $this->set('tournament_id', $tournament);
            $this->set('template', 'edit_run.html');
        }
    }

    function edit()
    {
        $rid = $this->params[0];
        $r = new Run();
        $t = new Tournament();
        if(!$r->exists($rid))
            return partial('error.html', ['errorMessage' => 'Dieser Run existiert nicht']);
        $r->load($rid);
        $tournament = $r->data['tournament'];
        $t->load($tournament);
        if(!$t->amIAdmin($tournament))
            return partial('error.html', ['errorMessage' => 'Du bist kein Admin dieses Turniers']);
        else
        {
            $this->set('tournament', $t->data);
            $this->set('tournament_id', $tournament);
            $this->set('run_id',$rid);
            $this->set('run',$r->data);
            $this->set('template', 'edit_run.html');
        }
    }

    function validate()
    {
        
        if($_REQUEST['submit']=='true')
        {
            $t = new Tournament();
            
            if(!$t->exists($_REQUEST['tournament_id']))
                return partial('error.html', ['errorMessage' => 'Dieses Turnier existiert nicht']);
            $t->load($_REQUEST['tournament_id']);
            
            if(!$t->amIAdmin($_REQUEST['tournament_id']))
                return partial('error.html', ['errorMessage' => 'Du bist kein Admin dieses Turniers']);
                
            $run = new Run();
            if($_REQUEST['run_id'])
            {
                if(!$run->exists($_REQUEST['run_id']))
                    return partial('error.html', ['errorMessage' => 'Dieser Lauf existiert nicht']);
                $run->load($_REQUEST['run_id']);
                if(!$run->data['tournament']==$t->id)
                    return partial('error.html', ['errorMessage' => 'Dieser Lauf gehört nicht zu diesem Turnier']);
            }
            else
                $run->id = gen_ulid();

            
    
            $run->data['tournament'] = $_REQUEST['tournament_id'];
            $run->data['name'] = trim($_REQUEST['name']);
            $run->data['category'] = $_REQUEST['category'];
            $run->data['length'] = $_REQUEST['length'];
            $run->data['time_standard'] = $_REQUEST['time_standard'];
            $run->data['time_max'] = $_REQUEST['time_max'];
            $run->data['referee'] = $_REQUEST['referee'];

            try{
                $runid = $run->save();
            }
            catch(Exception $e)
            {
                return partial('error.html', ['errorMessage' => 'Fehler beim Speichern des Laufs: '.$e->getMessage()]);
            }

            if(!in_array($runid, $t->data['runs']))
            {
                $t->data['runs'][] = $runid;
                $t->save();
            }

            $this->redirect('/tournaments/event/'.$t->id);
        }
        else return partial('error.html', ['errorMessage' => 'Fehler beim Speichern des Laufs']);
    }

    function maySeeThisPage() {
        if($_SESSION['user']) //wenn eingeloggt, kein problem
            return true;
        else return false;
    }
}