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

date_default_timezone_set('America/Mexico_City');

/**
 * Description of EstatusController
 *
 * @author omar
 */
class EstatusController extends Controller{
    //put your code here
    public function altaEstatusAction(Request $request) {
        $session = $request->getSession();
        if ($session->has("idEmpleado") && ($session->get("rol") == 2 || $session->get("rol") == 1)  ) {
            $session->set("vista", "estatus");
            if($request->getMethod() == "POST"){
                $cveSta = $request->get('cveSta');
                $desSta = $request->get('desSta');
                $tipo = $request->get('tipSta');
                
                $estatus = new Estatus();
                $estatus->setCveEst($cveSta);
                $estatus->setDesEst($desSta);
                $estatus->setTipEst($tipo);
                $estatus->setFecMov(new \DateTime(date('Y-m-d') ) );
                $estatus->setCveUsu( $session->get('identificador') );
                
                $em = $this->getDoctrine()->getManager();
                $em->persist($estatus);
                $em->flush();
                
                return $this->redirect($this->generateUrl('consulta_estatus'));
            }else{
                // Renderizar formulario de Estatus
                return $this->render('CmBundle:Estatus:alta-estatus.html.twig');
            }
        } else {
            return $this->render('CmBundle:Seguridad:login.html.twig');
        }
    }
    
    public function consultaEstatusAction(Request $request) {
        $session = $request->getSession();
        if ($session->has("idEmpleado") && ($session->get("rol") == 2 || $session->get("rol") == 1)  ) {
            $session->set("vista", "estatus");
            
            $em = $this->getDoctrine()->getManager();
            
            $estatus = $em->getRepository('CmBundle:Estatus')
                    ->findBy(array(),array('tipEst' => 'ASC'));
            return $this->render('CmBundle:Estatus:consulta-estatus.html.twig',array('estatus' => $estatus) );
        } else {
            return $this->render('CmBundle:Seguridad:login.html.twig');
        }
    }
    
}
