<?php

namespace App\Controller;

use App\Entity\Absence;
use App\Entity\Cas;
use App\Entity\Doba;
use App\Entity\Predmet;
use App\Entity\Suplovani;
use App\Entity\Trida;
use App\Entity\Ucitele;
use App\Entity\Zak;
use App\Repository\AbsenceRepository;
use App\Repository\CasRepository;
use App\Repository\DenRepository;
use App\Repository\DobaRepository;
use App\Repository\SuplovaniRepository;
use App\Repository\TridaRepository;
use App\Repository\UciteleRepository;
use App\Repository\ZakRepository;
use DateTime;
use Exception;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;

/**
 * @Route("/absence", name="absence.")
 */
class AbsenceController extends AbstractController
{
    /**
     * @Route("/create/{id}/{date}/{doba}/{predmet}/{cas}", name="create")
     * @ParamConverter("zak", options={"id" = "id"})
     * @ParamConverter("date", options={"date" = "date"})
     * @param Cas $cas
     * @param Zak $zak
     * @param DateTime $date
     * @param Doba $doba
     * @param Predmet $predmet
     * @param Request $request
     * @return Response
     */
    public function create(Cas $cas, Zak $zak, DateTime $date, Doba $doba, Predmet $predmet, Request $request)
    {
        $user = $this -> getUser();
        $z = $user -> getZak();
        $t = null;

        if($z != null) $t = $z -> getTrida();
        if($t != $zak -> getTrida() && ($user -> getRoles()[0] != 'ROLE_ADMIN' && $user -> getRoles()[0] != 'ROLE_TEACHER'))
            throw $this -> createAccessDeniedException("Přístup odepřen");

        $absence = new Absence();
        $absence -> setDatum($date) -> setZak($zak) -> setPredmet($predmet) -> setDoba($doba) -> setPritomen(true);

        foreach ($zak -> getAbsence() as $a)
        {
            if($a -> getDoba() -> getId() == $doba -> getId() && $a -> getDatum() == $date) $absence = $a;
        }

        $form = $this -> createFormBuilder()
            -> add('chybi', CheckboxType::class, ['data' => $absence -> getPritomen(), 'label' => 'Přítomen', 'required' => false])
            -> add('zak', EntityType::class, ['class' => Zak::class, 'label' => 'Žák', 'data' => $absence -> getZak()])
            -> add('doba', EntityType::class, ['class' => Doba::class, 'label' => 'Čas', 'data' => $absence -> getDoba()])
            -> add('datum', DateType::class, ['label' => 'Datum', 'data' => $absence -> getDatum()])
            -> add('save', SubmitType::class, ['label' => 'Uložit', 'attr' => ['class' => 'btn btn-outline-success']])
            ->getForm();


        $form -> handleRequest($request);

        if($form -> isSubmitted())
        {
            $absence -> setCas($cas);
            $absence -> setPritomen($form -> getData()['chybi']);
            $absence -> setDatum($form -> getData()['datum']);
            $absence -> setZak($form -> getData()['zak']);
            $absence -> setPredmet($predmet);
            $absence -> setDoba($form -> getData()['doba']);
            $em = $this->getDoctrine()->getManager();
            $em->persist($absence);
            $em->flush();
            //$this -> addFlash('success', 'Docházka byla uložena');
            return $this->redirect($this->generateUrl('absence.index', ['id' => $zak -> getTrida() -> getId(), 'date' => $date -> format('d-m-Y'), 'doba' => $doba -> getId(), 'redirect' => 1]));
        }
        return $this->render('absence/create.html.twig', ['zak' => $zak, 'datum' => $date, 'predmet' => $predmet, 'form' => $form->createView(), 'omluvenka' => 0]);
    }

    /**
     * @Route("/createUc/{ucitel}/{date}/{doba}/{predmet}", name="createUc")
     * @ParamConverter("date", options={"date" = "date"})
     * @ParamConverter("ucitel", options={"id" = "ucitel"})
     * @param Ucitele $ucitel
     * @param DateTime $date
     * @param Doba $doba
     * @param Predmet $predmet
     * @param Request $request
     * @param CasRepository $casRepository
     * @param UciteleRepository $uciteleRepository
     * @param SuplovaniRepository $suplovaniRepository
     * @return Response
     */
    public function createUc(Ucitele $ucitel, DateTime $date, Doba $doba, Predmet $predmet, Request $request, CasRepository $casRepository, UciteleRepository $uciteleRepository, SuplovaniRepository $suplovaniRepository)
    {
        if($this -> getUser() -> getRoles()[0] != 'ROLE_ADMIN') throw $this -> createAccessDeniedException("Přístup odepřen");

        $ucitele = $uciteleRepository -> findAll();
        $vysledek = array();
        $absence = new Absence();
        $suplovani = new Suplovani();
        $absence -> setDatum($date) -> setUcitele($ucitel) -> setPredmet($predmet) -> setDoba($doba) -> setPritomen(true);
        $sup = '';

        $cas = $this->findCasUc($casRepository, $date, $doba, $ucitel);

        foreach($ucitel -> getAbsence() as $a)
        {
            if($a -> getDoba() -> getId() == $doba -> getId() && $a -> getDatum() == $date)
            {
                foreach ($suplovaniRepository -> findAll() as $s)
                {
                    if($s -> getAbsence() -> getId() == $a -> getId())
                    {
                        $sup = $s -> getUcitel();
                    }
                }
                $absence = $a;
            }
        }

        foreach ($ucitele as $u)
        {
            $p = 1;
            if($u -> getTrida() != null) $p = $this->findPredmetUc($casRepository, $date, $doba, $u);
            if($p == null) array_push($vysledek, $u);
        }

        $form = $this->createFormBuilder()
            ->add('chybi', CheckboxType::class, ['data' => $absence -> getPritomen(), 'label' => 'Přítomen', 'required' => false])
            ->add('ucitel', EntityType::class, ['class' => Ucitele::class, 'label' => 'Učitel', 'data' => $absence -> getUcitele()])
            ->add('supl_ucitel', EntityType::class, ['class' => Ucitele::class, 'label' => 'Suplující Učitel', 'placeholder' => $sup, 'choices' => $vysledek, 'required' => false])
            ->add('doba', EntityType::class, ['class' => Doba::class, 'label' => 'Čas', 'data' => $absence -> getDoba()])
            ->add('datum', DateType::class, ['label' => 'Datum', 'data' => $absence -> getDatum()])
            ->add('save', SubmitType::class, ['label' => 'Uložit', 'attr' => ['class' => 'btn btn-outline-success']])
            ->getForm();

        $form -> handleRequest($request);

        if($form -> isSubmitted())
        {
            $absence -> setPritomen($form -> getData()['chybi']);
            $absence -> setDatum($form -> getData()['datum']);
            $absence -> setUcitele($form -> getData()['ucitel']);
            $absence -> setPredmet($predmet);
            $absence -> setDoba($form -> getData()['doba']);
            $absence -> setCas($cas);

            $suplovani -> setAbsence($absence);
            $suplovani -> setUcitel($form -> getData()['supl_ucitel']);
            $em = $this->getDoctrine()->getManager();
            $em -> persist($absence);
            $em -> flush();
            if($form -> getData()['supl_ucitel'] != null) $em -> persist($suplovani);
            $em -> flush();
            //$this -> addFlash('success', 'Docházka byla uložena');
            return $this->redirect($this->generateUrl('absence.absenceUc', ['datum' => $date -> format('d-m-Y'), 'doba' => $doba -> getId()]));
        }

        return $this->render('absence/create.html.twig', ['ucitel' => $ucitel, 'predmet' => $predmet, 'form' => $form->createView(), 'omluvenka' => 2]);
    }

    /**
     * @Route("/showUc/{ucitel}/{dateZ}/{dateK}", name="showUc")
     * @param Ucitele $ucitel
     * @param DateTime $dateZ
     * @param DateTime $dateK
     * @param DenRepository $denRepository
     * @param CasRepository $casRepository
     * @param DobaRepository $dobaRepository
     * @param SuplovaniRepository $suplovaniRepository
     * @param Request $request
     * @return RedirectResponse|Response
     * @throws Exception
     */
    public function showUc(Ucitele $ucitel, DateTime $dateZ, DateTime $dateK, DenRepository $denRepository, CasRepository $casRepository, DobaRepository $dobaRepository, SuplovaniRepository $suplovaniRepository, Request $request)
    {
        if($this -> getUser() -> getUcitel() != null && $this -> getUser() -> getUcitel() -> getId() != $ucitel -> getId() && $this -> getUser() -> getRoles()[0] != 'ROLE_ADMIN') throw $this -> createAccessDeniedException("Přístup odepřen");

        $den = $denRepository -> findAll();
        $cas = $casRepository -> findAll();
        $doba = $dobaRepository -> findAll();
        $suplovani = $suplovaniRepository -> findAll();
        $vysledek = array();

        foreach ($suplovani as $s)
        {
            if($s -> getUcitel() -> getId() == $ucitel -> getId() && $s -> getAbsence() -> getDatum() >= $dateZ && $s -> getAbsence() -> getDatum() <= $dateK)
            {
                array_push($vysledek, [$s, $this -> findDay(new DateTime($s -> getAbsence() -> getDatum() -> format('d-m-Y')))]);
            }
        }

        $form = $this -> createFormBuilder()
            -> add('save', SubmitType::class, ['label' => 'Zobrazit', 'attr' => ['class' => 'btn btn-outline-primary']])
            -> add('dateZ',DateType::Class, ['label' => 'Pondělí', 'data' => $dateZ])
            -> add('dateK',DateType::Class, ['label' => 'Pátek', 'data' => $dateK])
            -> getForm();

        $form->handleRequest($request);

        if($form -> isSubmitted())
        {
            foreach ($suplovani as $s)
            {
                if($s -> getUcitel() -> getId() == $ucitel -> getId() && $s -> getAbsence() -> getDatum() >= $form -> getData()['dateZ'] && $s -> getAbsence() -> getDatum() <= $form -> getData()['dateK'])
                {
                    array_push($vysledek, [$s, $this -> findDay(new DateTime($s -> getAbsence() -> getDatum()))]);
                }
            }
        }
        return $this->render('absence/showUc.html.twig', ['den' => $den, 'cas' => $cas, 'doba' => $doba, 'ucitel' => $ucitel, 'form' => $form -> createView(), 'suplovani' => $vysledek]);
    }

    /**
     * @Route("/{id}/{date}/{doba}/{redirect}", name="index")
     * @param bool $redirect
     * @param DateTime $date
     * @param Doba $doba
     * @param Trida $tridavar
     * @param Request $request
     * @param TridaRepository $tridaRepository
     * @param ZakRepository $zakRepository
     * @param AbsenceRepository $absenceRepository
     * @param CasRepository $casRepository
     * @return RedirectResponse|Response
     */
    public function index(bool $redirect, DateTime $date, Doba $doba, Trida $tridavar, Request $request, TridaRepository $tridaRepository, ZakRepository $zakRepository, AbsenceRepository $absenceRepository, CasRepository $casRepository)
    {
        $user = $this -> getUser();
        $zak = $user -> getZak();
        $t = null;

        if($zak != null) $t = $zak -> getTrida();
        if($t != $tridavar && ($user -> getRoles()[0] != 'ROLE_ADMIN' && $user -> getRoles()[0] != 'ROLE_TEACHER'))
            throw $this -> createAccessDeniedException("Přístup odepřen");

        $trida = $tridaRepository -> findAll();
        $absence = $absenceRepository -> findAll();
        $zaci = $zakRepository -> findAll();

        $ucitel = $user -> getUcitel();
        $rozvrh = $casRepository -> findAll();
        $vysledek = null;
        $mc = new MainController;

        foreach ($rozvrh as $r)
        {
            if($r -> getUcitel() == $ucitel)
            {
                $vysledek = $mc -> findTime($r);
            }
        }
        if($vysledek == null) $vysledek = $casRepository -> findOneBy(['id' => 1]);

        if($redirect)
        {
            $filterForm = $this->createFormBuilder()
                ->add('trida', EntityType::class, ['class' => Trida::class, 'label' => 'Třída', 'data' => $tridavar, 'required' => false])
                ->add('datum', DateType::class, ['label' => 'Datum', 'data' => $date])
                ->add('cisHod', EntityType::class, ['class' => Doba::class, 'label' => 'Hodina', 'data' => $doba])
                ->add('zobrazit', SubmitType::class, ['label' => 'Docházka Pro třídu:', 'attr' => ['class' => 'btn btn-outline-primary']])
                ->getForm();
        }
        else
        {
                $filterForm = $this->createFormBuilder()
                    ->add('trida', EntityType::class, ['class' => Trida::class, 'label' => 'Třída', 'data' => $vysledek->getTrida(), 'required' => false])
                    ->add('datum', DateType::class, ['label' => 'Datum', 'data' => $date])
                    ->add('cisHod', EntityType::class, ['class' => Doba::class, 'label' => 'Hodina', 'data' => $vysledek->getDoba()])
                    ->add('zobrazit', SubmitType::class, ['label' => 'Docházka Pro třídu:', 'attr' => ['class' => 'btn btn-outline-primary']])
                    ->getForm();
        }

        $filterForm -> handleRequest($request);

        if($filterForm -> isSubmitted())
        {
            if($filterForm -> getData()['trida'] != null)
            {
                $predmet = $this->findPredmet($casRepository, $filterForm->getData()['datum'], $filterForm->getData()['cisHod'], $filterForm->getData()['trida']);
                $cas = $this -> findCas($casRepository, $filterForm->getData()['datum'], $filterForm->getData()['cisHod'], $filterForm->getData()['trida']);
            }
            else
            {
                $predmet = $this->findPredmet($casRepository, $date, $doba, $tridavar);
                $cas = $this->findCas($casRepository, $date, $doba, $tridavar);
            }

            return $this->render('absence/index.html.twig', ['zaci' => $zaci, 'absence' => $absence,
                'trida' => $trida, 'tridavar' => $filterForm -> getData()['trida'], 'cas' => $cas,
                'filterForm' => $filterForm->createView(), 'datum' => $filterForm -> getData()['datum'],'predmet' => $predmet,
                'doba' => $filterForm -> getData()['cisHod'], 'rozvrh' => $vysledek, 'control' => new AbsenceController, 'casrep' => $casRepository]);
        }

        if($redirect) {
            $cas = $this->findCas($casRepository, $date, $doba, $tridavar);
            $predmet = $this->findPredmet($casRepository, $date, $doba, $tridavar);
            return $this->render('absence/index.html.twig', ['zaci' => $zaci, 'absence' => $absence, 'trida' => $trida,
                'tridavar' => $tridavar, 'filterForm' => $filterForm->createView(), 'predmet' => $predmet,
                'datum' => $date, 'doba' => $doba, 'rozvrh' => $vysledek, 'cas' => $cas]);
        }
        return $this->render('absence/index.html.twig', ['zaci' => $zaci, 'absence' => $absence, 'trida' => $trida,
            'tridavar' => $vysledek->getTrida(), 'filterForm' => $filterForm->createView(), 'predmet' => $vysledek->getPredmet(),
            'datum' => $date, 'doba' => $vysledek->getDoba(), 'rozvrh' => $vysledek, 'cas' => $vysledek]);
    }

    /**
     * @Route("/show/{zak}", name="show")
     * @param Zak $zak
     * @param AbsenceRepository $absenceRepository
     * @param Request $request
     * @return Response
     */
    public function show(Zak $zak, AbsenceRepository $absenceRepository, Request $request)
    {
        $user = $this -> getUser();
        $absence = $absenceRepository -> findAll();
        $gc = new GradesController();

        if(($user -> getZak() != null && $user -> getZak() -> getId() != $zak -> getId()) && ($user -> getRoles()[0] != 'ROLE_ADMIN' && $user -> getRoles()[0] != 'ROLE_TEACHER'&& $user -> getRoles()[0] != 'ROLE_PARENT'))
            throw $this -> createAccessDeniedException("Přístup odepřen");

        $countall = 0;
        $count = 0;
        if($user -> getRodic() != null)
        {   //tohle cely je jenom overovani validity
            foreach($user -> getRodic() -> getZak() as $z)
            {   $countall++;
                if( $z -> getId() != $zak -> getId()) $count++;
            }
            if($count >= $countall)
            {
                $countall = 0;
                $count = 0;
                throw $this->createAccessDeniedException("Přístup odepřen");
            }
        }

        $form = $this -> createFormBuilder()
            -> add('rok', ChoiceType::class, ['required' => true, 'choices' => $gc -> roky()])
            -> add('vyber', SubmitType::class, ['label' => 'Zobrazit rok', 'attr' => ['class' => 'btn btn-outline-primary btn-group']])
            -> getForm();

        $form -> handleRequest($request);

        if($form -> isSubmitted())
        {
            return $this -> render('absence/show.html.twig', ['absence' => $absence, 'zak' => $zak, 'rok' => $form -> getData()['rok'], 'form' => $form -> createView()]);
        }

        return $this -> render('absence/show.html.twig', ['absence' => $absence, 'zak' => $zak, 'rok' => date('Y'), 'form' => $form -> createView()]);
    }

    /**
     * @Route("/omluvenka/{absence}/{new}", name="omluvenka")
     * @param Absence $absence
     * @param bool $new
     * @param Request $request
     * @param CasRepository $rozvrhy
     * @return RedirectResponse|Response
     */
    public function omluvenka(Absence $absence,bool $new, Request $request, CasRepository $rozvrhy)
    {
        $user = $this -> getUser();
        $rodic = $user -> getRodic();

        if($rodic -> getZak() != $absence -> getZak() && ($user -> getRoles()[0] != 'ROLE_ADMIN' && $user -> getRoles()[0] != 'ROLE_TEACHER'&& $user -> getRoles()[0] != 'ROLE_PARENT'))
            throw $this -> createAccessDeniedException("Přístup odepřen");

        $countall = 0;
        $count = 0;
        if($user -> getRodic() != null)
        {   //tohle cely je jenom overovani validity
            foreach($user -> getRodic() -> getZak() as $z)
            {   $countall++;
                if( $z -> getId() != $absence -> getZak() -> getId()) $count++;
            }
            if($count >= $countall)
            {
                $countall = 0;
                $count = 0;
                throw $this->createAccessDeniedException("Přístup odepřen");
            }
        }

        if($new)
        {
            $form = $this->createFormBuilder()
                ->add('chybi', CheckboxType::class, ['data' => true, 'label' => 'Přítomen', 'required' => false])
                ->add('zak', EntityType::class, ['class' => Zak::class, 'label' => 'Žák', 'attr' => ['disabled' => true]])
                ->add('doba', EntityType::class, ['class' => Doba::class, 'label' => 'Čas'])
                ->add('datum', DateType::class, ['label' => 'Datum'])
                ->add('poznamka', TextType::class, ['label' => 'Omluvenka', 'attr' => ['placeholder' => 'Omluvenka']])
                ->add('save', SubmitType::class, ['label' => 'Uložit', 'attr' => ['class' => 'btn btn-outline-success']])
                ->getForm();
        }
        else
        {
            $form = $this->createFormBuilder()
                ->add('chybi', CheckboxType::class, ['data' => $absence -> getPritomen(), 'label' => 'Přítomen', 'required' => false, 'attr' => ['disabled' => true]])
                ->add('zak', EntityType::class, ['class' => Zak::class, 'label' => 'Žák', 'data' => $absence -> getZak(), 'attr' => ['disabled' => true]])
                ->add('doba', EntityType::class, ['class' => Doba::class, 'label' => 'Čas', 'data' => $absence -> getDoba(), 'attr' => ['disabled' => true]])
                ->add('datum', DateType::class, ['label' => 'Datum', 'data' => $absence -> getDatum(), 'attr' => ['disabled' => true]])
                ->add('poznamka', TextType::class, ['label' => 'Omluvenka', 'attr' => ['placeholder' => 'Omluvenka']])
                ->add('save', SubmitType::class, ['label' => 'Uložit', 'attr' => ['class' => 'btn btn-outline-success']])
                ->getForm();
        }
        $form -> handleRequest($request);

        if ($form -> isSubmitted())
        {
            if($new)
            {
                $predmet = $this->findPredmet($rozvrhy, $form -> getData()['datum'], $form -> getData()['doba'], $absence -> getZak() -> getTrida());
                if($predmet == null)
                {
                    $this -> addFlash('danger', 'Zadejte prosím korektní hodinu dle rozvrhu');
                    return $this -> render('absence/create.html.twig', ['absence' => $absence,'predmet' => $absence -> getPredmet(), 'form' => $form -> createView(), 'omluvenka' => 1] );
                }
                if($form -> getData()['datum'] < date('now'))
                {
                    $this -> addFlash('danger', 'Zadejte prosím správné datum');
                    return $this -> render('absence/create.html.twig', ['absence' => $absence,'predmet' => $absence -> getPredmet(), 'form' => $form -> createView(), 'omluvenka' => 1]);
                }
                $abs = new Absence();
                $abs -> setZak($absence -> getZak()) -> setPredmet($predmet) -> setDoba($form -> getData()['doba'])
                    -> setDatum($form -> getData()['datum']) -> setPritomen($form -> getData()['chybi'])
                    -> setPoznamka($form -> getData()['poznamka']);
            }
            else
            {
                $abs = $absence;
                $abs -> setPoznamka($form -> getData()['poznamka']);
            }

            $em = $this->getDoctrine()->getManager();
            $em->persist($abs);
            $em->flush();

            $this->addFlash('success', 'Omluvenka byla uložena');
            return $this -> redirect($this->generateUrl('absence.show', ['zak' => $absence -> getZak() -> getId()]));
        }

        return $this -> render('absence/create.html.twig', ['absence' => $absence,'predmet' => $absence -> getPredmet(), 'form' => $form -> createView(), 'omluvenka' => 1]);
    }

    /**
     * @Route("/Uc/{datum}/{doba}", name="absenceUc")
     * @param DateTime $datum
     * @param Doba $doba
     * @param Request $request
     * @param UciteleRepository $uciteleRepository
     * @param CasRepository $casRepository
     * @param AbsenceRepository $absenceRepository
     * @param SuplovaniRepository $suplovaniRepository
     * @return Response
     */
    public function absenceUc(DateTime $datum, Doba $doba, Request $request, UciteleRepository $uciteleRepository, CasRepository $casRepository, AbsenceRepository $absenceRepository, SuplovaniRepository $suplovaniRepository)
    {
        if($this -> getUser() -> getRoles()[0] != 'ROLE_ADMIN') throw $this -> createAccessDeniedException("Přístup odepřen");

        $ucitele = $uciteleRepository -> findAll();
        $rozvrh = $casRepository -> findAll();
        $absence = $absenceRepository -> findAll();
        $suplovani = $suplovaniRepository -> findAll();
        $vysledek = array();
        $predmet = null;
        $abs = null;

        $form = $this->createFormBuilder()
            ->add('datum', DateType::class, ['label' => 'Datum', 'data' => $datum])
            ->add('doba', EntityType::class, ['class' => Doba::class, 'label' => 'Hodina', 'data' => $doba])
            ->add('zobrazit', SubmitType::class, ['label' => 'Docházka Pro:', 'attr' => ['class' => 'btn btn-outline-primary']])
            ->getForm();

        $form -> handleRequest($request);

        if($form -> isSubmitted())
        {
            foreach ($ucitele as $ucitel)
            {
                foreach ($absence as $a)
                {
                    if($a -> getDatum() == $form -> getData()['datum'] && $a -> getDoba() == $form -> getData()['doba']
                        && $a -> getUcitele() != null && $a -> getUcitele() -> getId() == $ucitel -> getId())
                    {
                        $abs = $a;
                    }
                }
                if($ucitel -> getTrida() != null) $predmet = $this->findPredmetUc($casRepository, $form->getData()['datum'], $form->getData()['doba'], $ucitel);
                if($predmet != null)
                {
                    array_push($vysledek, [$ucitel, $predmet, $abs]);
                }
                $predmet = null; $abs = null;
            }

            return $this->render('absence/indexUc.html.twig', ['vysledek' => $vysledek, 'absence' => $absence,
                'form' => $form->createView(), 'datum' => $form -> getData()['datum'],
                'doba' => $form -> getData()['doba'], 'rozvrh' => $rozvrh, 'suplovani' => $suplovani]);
        }

        foreach ($ucitele as $ucitel)
        {
            foreach ($absence as $a)
            {
                if($a -> getDatum() == $datum && $a -> getDoba() -> getId() == $doba -> getId()
                    && $a -> getUcitele() != null && $a -> getUcitele() -> getId() == $ucitel -> getId())
                {
                    $abs = $a;
                }
            }
            if($ucitel -> getTrida() != null) $predmet = $this->findPredmetUc($casRepository, $datum, $doba, $ucitel);
            if($predmet != null)
            {
                array_push($vysledek, [$ucitel, $predmet, $abs]);
            }
            $predmet = null; $abs = null;
        }

        return $this->render('absence/indexUc.html.twig', ['vysledek' => $vysledek, 'absence' => $absence,
            'form' => $form->createView(), 'suplovani' => $suplovani,
            'datum' => $datum, 'doba' => $doba, 'rozvrh' => $rozvrh,]);
    }

    /**
     * @Route("/delete/{suplovani}/", name="delete")
     * @param Suplovani $suplovani
     * @return RedirectResponse
     */
    public function remove(Suplovani $suplovani)
    {
        $this -> denyAccessUnlessGranted('ROLE_ADMIN', null, "Přístup odepřen");

        $datum = $suplovani ->getAbsence() -> getDatum() -> format('d-m-Y');
        $doba = $suplovani -> getAbsence() -> getDoba() -> getId();

        $em = $this -> getDoctrine() -> getManager();
        $em -> remove($suplovani);
        $em -> flush();
        $this -> addFlash('success', 'Suplování bylo smazáno');
        return $this -> redirect($this -> generateUrl('absence.absenceUc',['datum' => $suplovani ->getAbsence() -> getDatum() -> format('d-m-Y'), 'doba' => $doba]));
    }

    /**
     * @Route("/all/{dateZ}/{dateK}", name="all")
     * @param DateTime $dateZ
     * @param DateTime $dateK
     * @param SuplovaniRepository $suplovaniRepository
     * @param AbsenceRepository $absenceRepository
     * @param Request $request
     * @return Response
     * @throws Exception
     */
    public function all(DateTime $dateZ, DateTime $dateK, SuplovaniRepository $suplovaniRepository, AbsenceRepository $absenceRepository, Request $request)
    {
        $this -> denyAccessUnlessGranted('ROLE_ADMIN', null, "Přístup odepřen");

        $vysledek = array();

        $form = $this -> createFormBuilder()
            -> add('save', SubmitType::class, ['label' => 'Zobrazit', 'attr' => ['class' => 'btn btn-outline-primary']])
            -> add('dateZ',DateType::Class, ['label' => 'Pondělí', 'data' => $dateZ])
            -> add('dateK',DateType::Class, ['label' => 'Pátek', 'data' => $dateK])
            -> getForm();

        $form->handleRequest($request);

        if($form -> isSubmitted())
        {
            foreach ($suplovaniRepository ->findAll() as $s)
            {
                if($s -> getAbsence() -> getDatum() >= $form -> getData()['dateZ'] && $s -> getAbsence() -> getDatum() <= $form -> getData()['dateK'])
                {
                    array_push($vysledek, [$s, $this -> findDay(new DateTime($s -> getAbsence() -> getDatum() -> format('d-m-Y')))]);
                }
            }

            return $this -> render('absence/all.html.twig', ['dateZ' => $form -> getData()['dateZ'], 'dateK' => $form -> getData()['dateK'],
            'suplovani' => $suplovaniRepository -> findAll(), 'absence' => $absenceRepository -> findAll(), 'vysledek' => $vysledek, 'form' => $form -> createView()]);
        }

        foreach ($suplovaniRepository ->findAll() as $s)
        {
            if($s -> getAbsence() -> getDatum() >= $dateZ && $s -> getAbsence() -> getDatum() <= $dateK)
            {
                array_push($vysledek, [$s, $this -> findDay(new DateTime($s -> getAbsence() -> getDatum() -> format('d-m-Y')))]);
            }
        }

        return $this -> render('absence/all.html.twig', ['dateZ' => $dateZ, 'dateK' => $dateK, 'vysledek' => $vysledek,
            'suplovani' => $suplovaniRepository -> findAll(), 'absence' => $absenceRepository -> findAll(), 'form' => $form -> createView()]);
    }

    public function findPredmet(CasRepository $rozvrhy, DateTime $datum, Doba $doba, Trida $trida)
    {
        $datum = $datum -> format('D');
        $den = null;
        $result = null;
        switch ($datum)
        {
            case 'Mon': $den = "Pondělí"; break;
            case 'Tue': $den = "Úterý"; break;
            case 'Wed': $den = "Středa"; break;
            case 'Thu': $den = "Čtvrtek"; break;
            case 'Fri': $den = "Pátek"; break;
        }

        foreach ($rozvrhy -> findAll() as $r) {
            if ($den == $r -> getDen() -> getDen() && $r -> getDoba() -> getId() == $doba -> getId() && $r -> getTrida() -> getId() == $trida -> getId()) {
                $result = $r -> getPredmet();
            }
        }
        return $result;
    }

    public function findCas(CasRepository $rozvrhy, DateTime $datum, Doba $doba, Trida $trida)
    {
        $datum = $datum -> format('D');
        $den = null;
        $result = null;
        switch ($datum)
        {
            case 'Mon': $den = "Pondělí"; break;
            case 'Tue': $den = "Úterý"; break;
            case 'Wed': $den = "Středa"; break;
            case 'Thu': $den = "Čtvrtek"; break;
            case 'Fri': $den = "Pátek"; break;
        }

        foreach ($rozvrhy -> findAll() as $r) {
            if ($den == $r -> getDen() -> getDen() && $r -> getDoba() -> getId() == $doba -> getId() && $r -> getTrida() -> getId() == $trida -> getId()) {
                $result = $r;
            }
        }
        return $result;
    }

    public function findCasUc(CasRepository $rozvrhy, DateTime $datum, Doba $doba, Ucitele $ucitel)
    {
        $datum = $datum -> format('D');
        $den = null;
        $result = null;
        switch ($datum)
        {
            case 'Mon': $den = "Pondělí"; break;
            case 'Tue': $den = "Úterý"; break;
            case 'Wed': $den = "Středa"; break;
            case 'Thu': $den = "Čtvrtek"; break;
            case 'Fri': $den = "Pátek"; break;
        }

        foreach ($rozvrhy -> findAll() as $r) {
            if ($den == $r -> getDen() -> getDen() && $r -> getDoba() -> getId() == $doba -> getId() && $r -> getUcitel() -> getId() == $ucitel -> getId()) {
                $result = $r;
            }
        }
        return $result;
    }

    public function findPredmetUc(CasRepository $rozvrhy, DateTime $datum, Doba $doba, Ucitele $ucitel)
    {
        $datum = $datum -> format('D');
        $den = null;
        $result = null;
        switch ($datum)
        {
            case 'Mon': $den = "Pondělí"; break;
            case 'Tue': $den = "Úterý"; break;
            case 'Wed': $den = "Středa"; break;
            case 'Thu': $den = "Čtvrtek"; break;
            case 'Fri': $den = "Pátek"; break;
        }

        foreach ($rozvrhy -> findAll() as $r) {
            if ($den == $r -> getDen() -> getDen() && $r -> getDoba() -> getId() == $doba -> getId() && $r -> getUcitel() -> getId() == $ucitel -> getId()) {
                $result = $r -> getPredmet();
            }
        }
        return $result;
    }

    public function findDay(DateTime $date)
    {
        $d = $date -> format('l');
        $den = null;

        if ($d == "Monday") $den = "Pondělí";
        if ($d == "Tuesday") $den = "Úterý";
        if ($d == "Wednesday") $den = "Středa";
        if ($d == "Thursday") $den = "Čtvrtek";
        if ($d == "Friday") $den = "Pátek";

        return $den;
    }
}
