<?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 CmBundle\Entity\Periodos;

date_default_timezone_set('America/Mexico_City');

/**
 * Description of PeriodosController
 *
 * @author ALE
 */
class PeriodosController extends Controller {

    //put your code here
    public function periodosAction(Request $request) {
        $session = $request->getSession();
        if ($session->has("idEmpleado")) {
            
            if ($request->getMethod() == "POST") {
                $em = $this->getDoctrine()->getManager();

                $nomPeriodo = $request->get('periodo');
                $mesInicio = $request->get('mesInicio');
                $mesFinal = $request->get('mesFinal');
                $fecMov = date('Y-m-d');
                $cvePla = $session->get('plantel');
                $plantel = $em->getRepository('CmBundle:Planteles')->findOneBy(array('cvePla' => $cvePla));
                $estatus = $request->get('estatusP');

                $periodo = new Periodos();
                $periodo->setPeriodo($nomPeriodo);
                $periodo->setMesInicio(new \DateTime($mesInicio));
                $periodo->setMesFinal(new \DateTime($mesFinal));
                $periodo->setCvePla($plantel);
                $periodo->setEstatus($estatus);
                $periodo->setFecMov(new \DateTime($fecMov));
                $periodo->setCveUsu($session->get('identificador'));

                $em->persist($periodo);
                $em->flush();

                return $this->redirect($this->generateUrl('periodos'));
            } else {

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

                $periodos = $em->getRepository('CmBundle:Periodos')->findAll();
                        

                return $this->render('CmBundle:Periodos:alta-periodos.html.twig', array(
                            'periodos' => $periodos
                ));
            }
        } else {
            return $this->render('CmBundle:Seguridad:login.html.twig');
        }
    }

    public function actualizarAction(Request $request) {
        $session = $request->getSession();
        if ($session->has("idEmpleado")) {
            
            if ($request->getMethod() == "POST") {
                $em = $this->getDoctrine()->getManager();

                $idePer = $request->get('idePer');
                $nomPeriodo = $request->get('periodo');
                $mesInicio = $request->get('mesInicio');
                $mesFinal = $request->get('mesFinal');
                $fecMov = date('Y-m-d');
                $cvePla = $session->get('plantel');
                $plantel = $em->getRepository('CmBundle:Planteles')->findOneBy(array('cvePla' => $cvePla));
                $estatus = $request->get('estatusP');

                $periodo = $em->getRepository('CmBundle:Periodos')->findOneBy(array('idePer' => $idePer));
                $periodo->setPeriodo($nomPeriodo);
                $periodo->setMesInicio(new \DateTime($mesInicio));
                $periodo->setMesFinal(new \DateTime($mesFinal));
                $periodo->setCvePla($plantel);
                $periodo->setEstatus($estatus);
                $periodo->setFecMov(new \DateTime($fecMov));
                $periodo->setCveUsu($session->get('identificador'));

                $em->persist($periodo);
                $em->flush();

                $this->get('session')->getFlashBag()->add(
                        'correctoP', 'CORRECTO'
                );

                return $this->redirect($this->generateUrl('periodos-actualizar', array('idePer' => $periodo->getIdePer())));
            } else {

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

                $idePer = $request->get('idePer');
                $periodo = $em->getRepository('CmBundle:Periodos')->findOneBy(array('idePer' => $idePer));

                return $this->render('CmBundle:Periodos:editar-periodos.html.twig', array(
                            'p' => $periodo
                ));
            }
        } else {
            return $this->render('CmBundle:Seguridad:login.html.twig');
        }
    }

    public function datosAction(Request $request) {
        $session = $request->getSession();
        if ($session->has("idEmpleado")) {
            
            if ($request->getMethod() == "POST") {
                $em = $this->getDoctrine()->getManager();

                $idePer = $request->get('idePer');
                $periodo = $em->getRepository('CmBundle:Periodos')->findOneBy(array('idePer' => $idePer));

                return $this->render('CmBundle:Periodos:dato-periodo.html.twig', array(
                            'p' => $periodo
                ));
            } else {
                
            }
        } else {
            return $this->render('CmBundle:Seguridad:login.html.twig');
        }
    }

}
