<?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;

date_default_timezone_set('America/Mexico_City');

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

    //put your code here
    public function loginAction(Request $request) {
        $session = $request->getSession();
        if ($session->has("ideAlu")) {
            return $this->redirect($this->generateUrl('pa_home'));
        } else {
            return $this->render('CmBundle:PortalAlumno:pa-login.html.twig');
        }
    }

    public function checkAction(Request $request) {
        if ($request->getMethod() == 'POST') {
            //$salt = "#3cn0s1st4m4$3$3lM3j*r3qU1p0D3Pr0g4m4c!0n";
            //$username = password_hash($request->get('usuario'), PASSWORD_BCRYPT, array('salt' => $salt ));
            //$password = password_hash($request->get('pwd'), PASSWORD_BCRYPT, array('salt' => $salt ));
            //print_r($username);

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

            $username = $request->get('email');
            $password = $request->get('email');

            $usuario = $em->getRepository('CmBundle:Alumnos')->findOneBy(array('cveCtl' => $username));
            if ($usuario != NULL) {
                $session = $request->getSession();
                $session->set("ideAlu", $usuario->getIdeAlu());
                $session->set("nombre", $usuario->getNomAlu() . " " . $usuario->getApePat() . " " . $usuario->getApeMat());

                return $this->redirect($this->generateUrl('pa_home'));
            } else {
                $this->get('session')->getFlashBag()->add(
                        'mensaje', 'Dados incorrectos'
                );

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

    public function logoutAction(Request $request) {
        $session = $request->getSession();
        $session->clear();
        /* $this->get('session')->getFlashBag()->add(
          'mensaje', 'Se ha cerrado sesión exitosamente, gracias por visitarnos.'
          );
         * 
         */
        return $this->redirect($this->generateUrl('pa_acceso'));
    }

    public function indexAction(Request $request) {
        $session = $request->getSession();
        if ($session->has("ideAlu") != null) {
            if ($request->getMethod() == 'POST') {
                
            } else { // NO FUE POST
                $em = $this->getDoctrine()->getManager();
                $alumno = $em->getRepository('CmBundle:Alumnos')->findOneBy(array('ideAlu' => $session->get('ideAlu')));
                return $this->render('CmBundle:PortalAlumno:index-alumno.html.twig', array('a' => $alumno));
            }
        } else {
            //MANDAR A LOGIN
            return $this->redirect($this->generateUrl('login'));
        }
        // replace this example code with whatever you need
//        return $this->render('default/index.html.twig', [
//            'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
//        ]);
    }

    public function actualizarAlumnoAction(Request $request) {
        $session = $request->getSession();
        if ($session->has('ideAlu')) {
            if ($request->getMethod() == "POST") {

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

                $apePat = $request->get('apePat');
                $apeMat = $request->get('apeMat');
                $nomAlu = $request->get('nomAlu');
                $curp = $request->get('curp');
                $sexo = $request->get('sexAlu');
                $fecNac = $request->get('fecNac');
                $edad = $request->get('edad');
                $nacionalidad = $request->get('nacionalidad');
                $edo = $request->get('edoAlu');
                $mun = $request->get('munAlu');
                $calle = $request->get('calle');
                $numExt = $request->get('numExt');
                $numInt = $request->get('numInt');
                $colonia = $request->get('colonia');
                $cp = $request->get('codPos');
                $telefono = $request->get('telefono');
                $email = $request->get('email');

                $cerSec = 0;
                $cerPre = 0;
                $actNac = 0;
                $fotInf = 0;
                $copCurp = 0;
                $cerMed = 0;

                $cerSec2 = 0;
                $cerPre2 = 0;
                $actNac2 = 0;
                $fotInf2 = 0;
                $copCurp2 = 0;
                $cerMed2 = 0;

                if ($request->get('cerSec') == "on") {
                    $cerSec = 1;
                }
                if ($request->get('cerPre') == "on") {
                    $cerPre = 1;
                }
                if ($request->get('actNac') == "on") {
                    $actNac = 1;
                }
                if ($request->get('fotInf') == "on") {
                    $fotInf = 1;
                }
                if ($request->get('copCurp') == "on") {
                    $copCurp = 1;
                }
                if ($request->get('cerMed') == "on") {
                    $cerMed = 1;
                }

                if ($request->get('cerSec2') == "on") {
                    $cerSec2 = 1;
                }
                if ($request->get('cerPre2') == "on") {
                    $cerPre2 = 1;
                }
                if ($request->get('actNac2') == "on") {
                    $actNac2 = 1;
                }
                if ($request->get('fotInf2') == "on") {
                    $fotInf2 = 1;
                }
                if ($request->get('copCurp2') == "on") {
                    $copCurp2 = 1;
                }
                if ($request->get('cerMed2') == "on") {
                    $cerMed2 = 1;
                }

                $tipo_alumno = $request->get('tipoAlumno');


                $alumno = $em->getRepository('CmBundle:Alumnos')
                        ->findOneBy(array('ideAlu' => $request->get('ideAlu')));

                //$alumno->setCveCtl($numCtl);
                $alumno->setNomAlu($nomAlu);
                $alumno->setApePat($apePat);
                $alumno->setApeMat($apeMat);
                $alumno->setCurp($curp);
                $alumno->setFecNac(new \DateTime($fecNac));
                $alumno->setEdad($edad);
                $alumno->setSexo($sexo);
                $alumno->setNacionalidad($nacionalidad);
                $alumno->setCorreo($email);
                $alumno->setTelefono($telefono);
                $alumno->setTelefonoCelular($request->get('telefonoCelular'));
                $alumno->setCalle($calle);
                $alumno->setNumExt($numExt);
                $alumno->setNumInt($numInt);
                $alumno->setColLoc($colonia);
                $alumno->setMunDel($mun);
                $alumno->setEdo($edo);
                $alumno->setCp($cp);

                $alumno->setHorarioTurno(new \DateTime(date($request->get('horario'))));
                $alumno->setHorarioSalidaTurno(new \DateTime($request->get('horarioSalida')));

                $alumno->setComoEntero($request->get('comoEntero'));
                $alumno->setObservaciones($request->get('observaciones'));
                $alumno->setFacebook($request->get('facebook'));
                $alumno->setDependenciaEmpresa($request->get('dependencia'));
                $alumno->setDepto($request->get('depto'));

                $dias = json_encode($request->get('dias'));

                $alumno->setDiasTurno($dias);

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

                if ($alumno->getTipoAlumno() == 2) {
                    return $this->redirect($this->generateUrl('pa_home'));
                } else {
                    return $this->redirect($this->generateUrl('pa_home'));
                }
            }
        } else {
            return $this->redirect($this->generateUrl('pa_acceso'));
        }
    }

}
