<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace CmBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use CmBundle\Entity\Eventos;

/**
 * Description of EventosController
 *
 * @author Omar
 */
class EventosController extends Controller {

    public function altaEventosAction(Request $request) {
        $session = $request->getSession();
        if ($session->has("idEmpleado") && ($session->get("rol") == 2 || $session->get("rol") == 1 || $session->get("rol") == 3 || $session->get("rol") == 4 )) {
            if ($request->getMethod() == 'POST') {

                $em = $this->getDoctrine()->getManager();

                $nomEve = $request->get('nomEve');
                $costo = $request->get('costo');
                $fecEve = $request->get('fecEve');
                $horIni = $request->get('horIni');
                $horFin = $request->get('horFin');
                $responsable = $request->get('responsable');
                $cvePla = $em->getRepository('CmBundle:Planteles')
                        ->findOneBy(array('idePla' => $session->get('plantel')));


                $evento = new Eventos();
                $evento->setNomEve($nomEve);
                $evento->setCosto($costo);
                $evento->setFecEve(new \DateTime(date($fecEve)));
                $evento->setHorIni(new \DateTime(date($horIni)));
                $evento->setHorFin(new \DateTime(date($horFin)));
                $evento->setResponsable($responsable);
                $evento->setCvePla($cvePla);
                $evento->setFecMov(new \DateTime(date('Y-m-d')));
                $evento->setCveUsu($session->get('identificador'));
                $em->persist($evento);
                $em->flush();

                return $this->redirect($this->generateUrl('consultar_eventos'));
            } else {
                $em = $this->getDoctrine()->getManager();
                $plantel = $em->getRepository('CmBundle:Planteles')
                        ->findOneBy(array('idePla' => $session->get('plantel')));
                return $this->render('CmBundle:Eventos:alta.html.twig', array('plantel' => $plantel));
            }
        } else {
            $this->get('session')->getFlashBag()->add(
                    'mensaje', 'Debe estar logeado para ver este contenido'
            );

            return $this->redirect($this->generateUrl('login'));
        }
    }

    public function consultarEventosAction(Request $request) {
        $session = $request->getSession();
        if ($session->has("idEmpleado") && ($session->get("rol") == 2 || $session->get("rol") == 1 || $session->get("rol") == 3 || $session->get("rol") == 4 )) {
            if ($request->getMethod() == 'POST') {
                
            } else {
                $em = $this->getDoctrine()->getManager();

                $eventos = $em->getRepository('CmBundle:Eventos')
                        ->findBy(array('cvePla' => $session->get('plantel')));

                return $this->render('CmBundle:Eventos:consultar.html.twig', array('eventos' => $eventos));
            }
        } else {
            $this->get('session')->getFlashBag()->add(
                    'mensaje', 'Debe estar logeado para ver este contenido'
            );

            return $this->redirect($this->generateUrl('login'));
        }
    }

    public function traerEventosAction(Request $request) {
        $session = $request->getSession();
        if ($session->has("idEmpleado") && ($session->get("rol") == 2 || $session->get("rol") == 1 || $session->get("rol") == 3 || $session->get("rol") == 4 )) {
            if ($request->getMethod() == 'POST') {
                $em = $this->getDoctrine()->getManager();

                $eventos = $em->getRepository('CmBundle:Eventos')
                        ->findBy(array('cvePla' => $session->get('plantel')));

                return $this->render('CmBundle:Eventos:tabla-eventos.html.twig', array('eventos' => $eventos));
            } else {
                
            }
        } else {
            $this->get('session')->getFlashBag()->add(
                    'mensaje', 'Debe estar logeado para ver este contenido'
            );

            return $this->redirect($this->generateUrl('login'));
        }
    }

}
