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

date_default_timezone_set('America/Mexico_City');

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

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

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

                $rolEmp = $request->get('rolEmp');
                $cargo = "";
                if($rolEmp == 1){
                    $cargo = "BASE";
                }elseif ($rolEmp ==2) {
                    $cargo = "CONTROL ESCOLAR";
                }elseif ($rolEmp == 3) {
                    $cargo = "CAJA";
                }elseif ($rolEmp == 4) {
                    $cargo = "ADMIN EU";
                }elseif ($rolEmp == 200) {
                    $cargo = "CLAVES";
                }
                $cveEmp = $request->get('cveEmp');
                $nomEmp = $request->get('nomEmp');
                $apePat = $request->get('apePat');
                $apeMat = $request->get('apeMat');
                //$estatus = $request->get('estatus');
                $fecIng = $request->get('fecIng');
                $cvePlantel = $em->getRepository('CmBundle:Planteles')->findOneBy(array('idePla' => $request->get('cvePla')));
                $pass = $request->get('pass');
                $camPass = $request->get('camPass');
                $telefono = $request->get('telefono');
                $correo = $request->get('correo');
                $calle = $request->get('calle');
                $colLoc = $request->get('colLoc');
                $munDel = $request->get('munDel');
                $edo = $request->get('edo');
                $cp = $request->get('cp');
                
                $tipo = $request->get('estatus');
                $estatus = $em->getRepository('CmBundle:Estatus')
                        ->findOneBy(array('ideEst' => $tipo));

                $empleado = new Empleados();
                $empleado->setRolEmp($rolEmp);
                $empleado->setIdentificador($request->get('identificador'));
                $empleado->setCveEmp($cveEmp);
                $empleado->setNomEmp($nomEmp);
                $empleado->setApePat($apePat);
                $empleado->setApeMat($apeMat);
                $empleado->setEstatus($estatus);
                $empleado->setCargo($cargo);
                $empleado->setFecIng(new \DateTime($fecIng));
                $empleado->setCvePlantel($cvePlantel);
                $empleado->setPass($pass);
                $empleado->setCamPass(0);
                $empleado->setTelefono($telefono);
                $empleado->setCorreo($correo);
                $empleado->setCalle($calle);
                $empleado->setColLoc($colLoc);
                $empleado->setMunDel($munDel);
                $empleado->setEdo($edo);
                $empleado->setCp($cp);
                $empleado->setFecMov(new \DateTime(date('Y-m-d')));
                $empleado->setCveUsu($session->get('identificador') );
                
                $em->persist($empleado);
                $em->flush();

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

                $estatus = $em->getRepository('CmBundle:Estatus')
                        ->findAll();
                $planteles = $em->getRepository('CmBundle:Planteles')
                        ->findAll();
                return $this->render('CmBundle:Usuario:alta-usuario.html.twig', array('estatus' => $estatus,'planteles' => $planteles));
            }
        } else {
            return $this->render('CmBundle:Seguridad:login.html.twig');
        }
    }

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

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

            $usuarios = $em->getRepository('CmBundle:Empleados')
                    ->findAll();
            return $this->render('CmBundle:Usuario:consulta-usuario.html.twig', array('usuarios' => $usuarios));
        } else {
            return $this->render('CmBundle:Seguridad:login.html.twig');
        }
    }

}
