<?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\Curriculas;
use CmBundle\Entity\Planeaciones;
use Symfony\Component\HttpFoundation\Response;

date_default_timezone_set('America/Mexico_City');

/**
 * Description of CurriculaController
 *
 * @author omar
 */
class CurriculaController extends Controller
{

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

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

                $ideLic = $request->get('cveLic');
                $cveLic = $em->getRepository('CmBundle:Licenciaturas')
                    ->findOneBy(array('ideLic' => $ideLic));
                $modalidad = $request->get('modalidad');
                $numSem = $request->get('numSem');
                $numCua = $request->get('numCua');
                $clave = $request->get('clave');
                $nomAsignatura = $request->get('nomAsignatura');
                $tipCurr = $request->get('tipCurr');

                $cvePla = $em->getRepository('CmBundle:Planteles')
                    ->findOneBy(array('cvePla' => $session->get('plantel')));

                $mapa = $request->get('cveMapa');
                $cvePlan = $em->getRepository('CmBundle:Planeaciones')->findOneBy(array('idePlan' => $mapa));

                $curricula = new Curriculas();
                $curricula->setCveLic($cveLic);
                $curricula->setModalidad($modalidad);
                //$modalidad <= 0 :: CUATRIMESTRAL
                //$modalidad <= 1 :: SEMESTRAL
                if ($modalidad == 0) {
                    $curricula->setNumSem(0);
                    $curricula->setNumCua($numCua);
                } else {
                    $curricula->setNumSem($numSem);
                    $curricula->setNumCua(0);
                }

                $curricula->setClave($clave);
                $curricula->setNomAsignatura($nomAsignatura);
                $curricula->setTipCurr($tipCurr);
                $curricula->setCvePla($cvePla);
                $curricula->setEstatus(null);
                $curricula->setCvePlan($cvePlan);

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

                return $this->redirect($this->generateUrl('curricula'));
            } else {
                // Renderizar formulario de Estatus
                $em = $this->getDoctrine()->getManager();

                $licenciaturas = $em->getRepository('CmBundle:Licenciaturas')
                    ->findBy(array(), array('cveNivAca' => 'ASC', 'nomLic' => 'ASC'));
                $plantel = $em->getRepository('CmBundle:Planteles')
                    ->findOneBy(array('cvePla' => $session->get('plantel')));

                $periodos = $em->getRepository('CmBundle:Periodos')
                    ->findBy(array('estatus' => 1, 'cvePla' => $plantel->getIdePla()));

                $curriculas = $em->getRepository('CmBundle:Curriculas')
                    ->findBy(array(), array('cveLic' => 'DESC', 'cvePla' => 'DESC'));

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

    public function asignaturasAction(Request $request)
    {
        $session = $request->getSession();
        if ($session->has("idEmpleado")) {
            if ($request->getMethod() == "POST") {

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

                $ideLic = $request->get('cveLic');
                $mapCur = $request->get('mapa');

                $cveLic = $em->getRepository('CmBundle:Licenciaturas')
                    ->findOneBy(array('ideLic' => $ideLic));

                $curriculas = $em->getRepository('CmBundle:Curriculas')
                    ->findBy(array('cveLic' => $cveLic->getIdeLic(), 'cvePlan' => $mapCur));

                return $this->render('CmBundle:Curriculas:tabla-materias.html.twig', array(
                    'asignaturas' => $curriculas
                ));
            } else {
                // Renderizar formulario de Estatus
                $em = $this->getDoctrine()->getManager();
            }
        } else {
            return $this->render('CmBundle:Seguridad:login.html.twig');
        }
    }

    public function editarAsignaturasAction(Request $request)
    {
        $session = $request->getSession();
        if ($session->has("idEmpleado")) {
            if ($request->getMethod() == "POST") {

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

                $ideCur = $request->get('ideCur');
                $idAsignatura = $request->get("idAsignatura");
                $claveAsignatura = $request->get("claveAsignatura");
                $nombreAsignatura = $request->get("nombreAsignatura");
                $creditos = $request->get("creditos");

                $asignatura = $em->getRepository('CmBundle:Curriculas')
                    ->findOneBy(array('ideCur' => $ideCur));

                if ($asignatura != null) {
                    /*
                    if ($idAsignatura != null) {
                        $asignatura->setIdAsignatura($idAsignatura);
                    } else {
                        $asignatura->setIdAsignatura(null);
                    }
                    */
                    if ($claveAsignatura != null) {
                        $asignatura->setClave($claveAsignatura);
                    } else {
                        $asignatura->setClave(null);
                    }
                    if ($nombreAsignatura != null) {
                        $asignatura->setNomAsignatura($nombreAsignatura);
                    } else {
                        $asignatura->setNomAsignatura(null);
                    }
                    /*
                    if ($creditos != null) {
                        $asignatura->setCreditos($creditos);
                    } else {
                        $asignatura->setCreditos(null);
                    }
                    */

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

                    return $this->render('CmBundle:Curriculas:correcto-editar-asignatura.html.twig');
                } else {
                    return $this->render('CmBundle:Curriculas:incorrecto-editar-asignatura.html.twig');
                }
            } else {
                // Renderizar formulario de Estatus
                $em = $this->getDoctrine()->getManager();
            }
        } else {
            return $this->render('CmBundle:Seguridad:login.html.twig');
        }
    }

    public function mapaCurricularAction(Request $request)
    {
        $session = $request->getSession();
        if ($session->has("idEmpleado")) {
            if ($request->getMethod() == "POST") {

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

                $ideLic = $request->get('cveLic');
                $cveLic = $em->getRepository('CmBundle:Licenciaturas')
                    ->findOneBy(array('ideLic' => $ideLic));
                $clave = $request->get('clave');
                $nomPlan = $request->get('nomMapa');

                $cvePla = $em->getRepository('CmBundle:Planteles')
                    ->findOneBy(array('cvePla' => $session->get('plantel')));

                $fecIni = $request->get('fechaI');

                $planeacion = new Planeaciones();
                $planeacion->setNomPlan($nomPlan);
                $planeacion->setCvePlan($clave);
                $planeacion->setEstatus(1);
                $planeacion->setCveLic($cveLic);
                $planeacion->setCvePlantel($cvePla);
                $planeacion->setFecIni(new \DateTime($fecIni));
                $planeacion->setCveInstitucion(null);
                $planeacion->setFecFin(null);
                $planeacion->setCveUsu($session->get('identificador'));

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

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

                return $this->redirect($this->generateUrl('mapa_curricular'));
            } else {
                // Renderizar formulario de Estatus
                $em = $this->getDoctrine()->getManager();

                $licenciaturas = $em->getRepository('CmBundle:Licenciaturas')
                    ->findBy(array(), array('cveNivAca' => 'ASC', 'nomLic' => 'ASC'));

                $dql = "SELECT p "
                    . "FROM CmBundle:Planeaciones p, CmBundle:Licenciaturas l "
                    . "WHERE "
                    . "p.cveLic = l.ideLic "
                    . "ORDER BY "
                    . "l.cveNivAca, l.nomLic";
                $query = $em->createQuery($dql);
                $mapas = $query->getResult();

                return $this->render('CmBundle:Curriculas:alta-mapa-curricular.html.twig', array(
                        'licenciaturas' => $licenciaturas,
                        'mapas' => $mapas
                    )
                );
            }
        } else {
            return $this->render('CmBundle:Seguridad:login.html.twig');
        }
    }

    public function consultarMapaCurricularAction(Request $request)
    {
        $session = $request->getSession();
        if ($session->has("idEmpleado")) {
            if ($request->getMethod() == "POST") {

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

                $ideLic = $request->get('cveLic');
                $cveLic = $em->getRepository('CmBundle:Licenciaturas')
                    ->findOneBy(array('ideLic' => $ideLic));
                $clave = $request->get('clave');
                $nomPlan = $request->get('nomMapa');

                $cvePla = $em->getRepository('CmBundle:Planteles')
                    ->findOneBy(array('cvePla' => $session->get('plantel')));

                $fecIni = $request->get('fechaI');

                $planeacion = new Planeaciones();
                $planeacion->setCveLic($cveLic);
                $planeacion->setCvePlan($clave);
                $planeacion->setCvePlantel($cvePla);
                $planeacion->setEstatus(1);
                $planeacion->setNomPlan($nomPlan);
                $planeacion->setFecIni(new \DateTime($fecIni));
                $planeacion->setCveUsu($session->get('identificador'));

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

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

                return $this->redirect($this->generateUrl('mapa_curricular'));
            } else {
                // Renderizar formulario de Estatus
                $em = $this->getDoctrine()->getManager();

                $cveLic = $em->getRepository('CmBundle:Licenciaturas')
                    ->findOneBy(array('ideLic' => $request->get('cveLic')));

                print_r($request->get('cveLic'));

                $planeaciones = $em->getRepository('CmBundle:Planeaciones')
                    ->findBy(array('cveLic' => $cveLic));

                return $this->render('CmBundle:Curriculas:listas-mapa-curricular.html.twig', array(
                    'mapasCurriculares' => $planeaciones));
            }
        } else {
            return $this->render('CmBundle:Seguridad:login.html.twig');
        }
    }

    public function ajaxConsultarMapaCurricularAction(Request $request)
    {
        $session = $request->getSession();
        if ($session->has("idEmpleado")) {
            if ($request->getMethod() == "POST") {

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

                $ideLic = $request->get('cveLic');
                $cveLic = $em->getRepository('CmBundle:Licenciaturas')
                    ->findOneBy(array('ideLic' => $ideLic));
                $clave = $request->get('clave');
                $nomPlan = $request->get('nomMapa');

                $cvePla = $em->getRepository('CmBundle:Planteles')
                    ->findOneBy(array('cvePla' => $session->get('plantel')));

                $fecIni = $request->get('fechaI');

                $planeacion = new Planeaciones();
                $planeacion->setCveLic($cveLic);
                $planeacion->setCvePlan($clave);
                $planeacion->setCvePlantel($cvePla);
                $planeacion->setEstatus(1);
                $planeacion->setNomPlan($nomPlan);
                $planeacion->setFecIni(new \DateTime($fecIni));
                $planeacion->setCveUsu($session->get('identificador'));

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

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

                return $this->redirect($this->generateUrl('mapa_curricular'));
            } else {
                // Renderizar formulario de Estatus
                $em = $this->getDoctrine()->getManager();

                $cveLic = $em->getRepository('CmBundle:Licenciaturas')
                    ->findOneBy(array('ideLic' => $request->get('cveLic')));

                $planeaciones = $em->getRepository('CmBundle:Planeaciones')
                    ->findBy(array('cveLic' => $cveLic, 'estatus' => 1));

                return $this->render('CmBundle:Curriculas:select-mapa-curricular.html.twig', array(
                    'mapasCurriculares' => $planeaciones));
            }
        } else {
            return $this->render('CmBundle:Seguridad:login.html.twig');
        }
    }

    public function imprimirMapaCurricularAction(Request $request)
    {
        $session = $request->getSession();
        if ($session->has("idEmpleado")) {
            if ($request->getMethod() == "POST") {

            } else {// ES GET
                $em = $this->getDoctrine()->getManager();

                $idePlan = $request->get('idePlan');

                $cvePlan = $em->getRepository('CmBundle:Planeaciones')
                    ->findOneBy(array('idePlan' => $idePlan));

                $lisAsi = $em->getRepository('CmBundle:Curriculas')
                    ->findBy(array('cvePlan' => $cvePlan));

                $pdf = new PDFFormatosCEEPAM('P', 'mm', 'letter');
                $pdf->SetFont('Arial', '', 12);
                $pdf->AliasNbPages();
                $pdf->AddPage();
                $pdf->mapaCurricular($cvePlan, $lisAsi);

                return new Response($pdf->Output(utf8_decode('PLAN_' . $cvePlan->getNomPlan() . "_" . $cvePlan->getCvePlan() . '.pdf'), 'I'), 200, array('Content-Type' => 'application/pdf'));
            }
        } else {
            return $this->render('CmBundle:Seguridad:login.html.twig');
        }
    }


}
