<?php

namespace VGM\WOBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use VGM\WOBundle\Entity\NPaySchedule;
use VGM\WOBundle\Form\NDepartmentType;
use VGM\WOBundle\Form\NPayScheduleType;

/**
 * NPaySchedule controller.
 *
 */
class NPayScheduleController extends Controller
{

    /**
     * Lists all NPaySchedule entities.
     *
     */
    public function indexAction($page)
    {
        $em = $this->getDoctrine()->getManager();

        $cant_schedules = $em->getRepository('VGMWOBundle:NPaySchedule')->countSchedules();
        $schedules_per_page = $this->container->getParameter('schedules_per_page');
        $last_page = ceil($cant_schedules / $schedules_per_page);
        $previous_page = $page > 1 ? $page - 1 : 1;
        $next_page = $page < $last_page ? $page + 1 : $last_page;

        $offset = ($page - 1) > -1? ($page - 1) * $schedules_per_page: null;
        $entities = $em->getRepository('VGMWOBundle:NPaySchedule')
            ->getSchedules($schedules_per_page, $offset);

        return $this->render('VGMWOBundle:NPaySchedule:index.html.twig', array(
                'entities' => $entities,
                'last_page' => $last_page,
                'previous_page' => $previous_page,
                'current_page' => $page,
                'next_page' => $next_page,
                'cant_schedules' => $cant_schedules
            ));
    }
    /**
     * Creates a new NPaySchedule entity.
     *
     */
    public function createAction(Request $request)
    {
        $entity = new NPaySchedule();
        $form = $this->createCreateForm($entity);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

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

        return $this->render('VGMWOBundle:NPaySchedule:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }

    /**
     * Creates a form to create a NPaySchedule entity.
     *
     * @param NPaySchedule $entity The entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createCreateForm(NPaySchedule $entity)
    {
        $form = $this->createForm(new NPayScheduleType(), $entity, array(
            'action' => $this->generateUrl('NPaySchedules_create'),
            'method' => 'POST',
        ));

        //$form->add('submit', 'submit', array('label' => 'Create'));

        return $form;
    }

    /**
     * Displays a form to create a new NPaySchedule entity.
     *
     */
    public function newAction()
    {
        $entity = new NPaySchedule();
        $form   = $this->createCreateForm($entity);

        return $this->render('VGMWOBundle:NPaySchedule:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }

    /**
     * Finds and displays a NPaySchedule entity.
     *
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('VGMWOBundle:NPaySchedule')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find NPaySchedule entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('VGMWOBundle:NPaySchedule:show.html.twig', array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Displays a form to edit an existing NPaySchedule entity.
     *
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('VGMWOBundle:NPaySchedule')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find NPaySchedule entity.');
        }

        $editForm = $this->createForm(new NPayScheduleType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('VGMWOBundle:NPaySchedule:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
    * Creates a form to edit a NPaySchedule entity.
    *
    * @param NPaySchedule $entity The entity
    *
    * @return \Symfony\Component\Form\Form The form
    */
    private function createEditForm(NPaySchedule $entity)
    {
        $form = $this->createForm(new NPayScheduleType(), $entity, array(
            'action' => $this->generateUrl('NPaySchedules_update', array('id' => $entity->getId())),
            'method' => 'PUT',
        ));

        $form->add('submit', 'submit', array('label' => 'Update'));

        return $form;
    }
    /**
     * Edits an existing NPaySchedule entity.
     *
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('VGMWOBundle:NPaySchedule')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find NPaySchedule entity.');
        }

        $deleteForm = $this->createDeleteForm($id);
        $editForm = $this->createForm(new NPayScheduleType(), $entity);
        $editForm->handleRequest($request);

        if ($editForm->isValid()) {
            $em->flush();

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

        return $this->render('VGMWOBundle:NPaySchedule:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }
    /**
     * Deletes a NPaySchedule entity.
     *
     */
    public function deleteAction(Request $request, $id)
    {
        $form = $this->createDeleteForm($id);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository('VGMWOBundle:NPaySchedule')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find NPaySchedule entity.');
            }

            $em->remove($entity);
            $em->flush();
        }

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

    /**
     * Creates a form to delete a NPaySchedule entity by id.
     *
     * @param mixed $id The entity id
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createDeleteForm($id)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('NPaySchedules_delete', array('id' => $id)))
            ->setMethod('DELETE')
            ->add('submit', 'submit', array('label' => 'Delete'))
            ->getForm()
        ;
    }
}
