<?php

class Dogs extends Page {

    function setMenu()
    {
        $this->menu_text = 'Meine Hunde';
        $this->menu_image = 'far fa-dog';
        $this->menu_priority = 0;
    }

    function setSubmenu()
    {
        $this->addSubmenuItem('Hunde anzeigen','/dogs','far fa-list-alt');
        $this->addSubmenuItem('Hund hinzufügen','/dogs/add','fas fa-plus-circle');
    }
    
    function index() {
        $dogs = $_SESSION['user']->data['dogs'];
        $doggos = [];

        foreach($dogs as $key => $dogid)
        {
            //var_dump($dogid);
            $dog = new Dog();
            try{
                $dog->load($dogid);
            }
            catch(Exception $e)
            {
                error_log("Dog $dogid not found. Deleting from user");
                unset($_SESSION['user']->data['dogs'][$key]);
                $_SESSION['user']->save();
                continue;
            }
            if($dog->data)
                $doggos[] = array_merge($dog->data,['id'=>$dogid]);
        }

        //var_dump($doggos);

        if(count($doggos) > 0)
        {
            $this->set('doggos',$doggos);
            $this->set('template', 'list.html');
        }
        else
        {
            $this->set('template','/templates/partials/info.html');
            $this->set('infoTitle', 'Keine Hunde gefunden');
            $this->set('infoMessage', 'Du hast aktuell noch keine Hunde angelegt. Klicke im Menü auf "Hund hinzufügen" um einen Hund anzulegen.');
        }
    }

    function add() {
        $this->set('template', 'edit.html');
    }

    function edit() {
        if($_REQUEST['submit'])
        {
            $id = $_REQUEST['dog_id'];
            $name = $_REQUEST['name'];
            $kennel_name = $_REQUEST['kennel_name'];
            $dog_breed = $_REQUEST['dog_breed'];
            $dog_size = $_REQUEST['dog_size'];
            $dog_birthday = $_REQUEST['dog_birthday'];
            $agi_height_category = $_REQUEST['agi_height_category'];

            $error = false;
            if(!$name || !$dog_birthday )
                $error = 'Bitte zumindest Name und Geburtsdatum angeben';
            else if(!strtotime($dog_birthday))
                $error = 'Das Geburstdatum ist ungültig. Bitte die Eingabe prüfen';
            
            if($error){
                $this->set('errorMessage', $error);
                $this->set('template', '/templates/partials/error.html');
                return;
            }
            else
            {
                $dog = new Dog();
                if($id)
                    $dog->load($id);

                $dog->name = $name;
                $dog->kennel_name = $kennel_name;
                $dog->breed = $dog_breed;
                $dog->size = $dog_size;
                $dog->birthday = $dog_birthday;
                $dog->agility_size = $agi_height_category;

                try
                {
                    $dogid = $dog->save();
                }
                catch(Exception $e)
                {
                    $this->set('template', '/templates/partials/error.html');
                    $this->set('errorTitle', 'Error');
                    $this->set('errorMessage', $e->getMessage());
                    return;
                }

                //var_dump($_SESSION['user']->data['dogs']);

                if(!is_array($_SESSION['user']->data['dogs']) || !in_array($dogid, $_SESSION['user']->data['dogs'])) // new dog!
                {
                    $_SESSION['user']->data['dogs'][] = $dogid;
                    try
                    {
                        $_SESSION['user']->save();
                    }
                    catch(Exception $e)
                    {
                        $this->set('template', '/templates/partials/error.html');
                        $this->set('errorTitle', 'Error');
                        $this->set('errorMessage', $e->getMessage());
                        return;
                    }
                    $this->redirect('/dogs');
                }
                else
                {
                    $this->set('template', '/templates/partials/success.html');
                    $this->set('successMessage', "Daten erfolgreich gespeichert");
                    return;
                }
                
            }
        }
        else
        {
            //checken ob der den hund eh bearbeiten darf
            $id = $this->params[0];
            $dog = new Dog();
            $dog->load($id);
            $this->set('dogdata',$dog->data);
            $this->set('dogid',$dog->id);
            $this->set('template', 'edit.html');
        }
    }

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

}