<?php

namespace VGM\WOBundle\Controller;

use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use VGM\WOBundle\Entity\CUnit;
use VGM\WOBundle\Form\CUnitType;

/**
 * NDepartment controller.
 *
 */
class CUnitController extends Controller
{

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

        $entities = $em->getRepository('VGMWOBundle:CUnit')->findBy(array(), array('level' => 'ASC'));

        return $this->render('VGMWOBundle:CUnit:index.html.twig', array( 'entities' => $entities ));
    }
    /**
     * Creates a new NDepartment entity.
     *
     */
    public function createAction(Request $request)
    {
        $entity = new CUnit();
        $form = $this->createCreateForm($entity, new CUnitType());
        $form->handleRequest($request);

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

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

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

    /**
     * Creates a form to create a NDepartment entity.
     *
     * @param NDepartment $entity The entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createCreateForm(CUnit $entity, CUnitType $type = null)
    {
        $form = null;

        if($type == null){
            $form = $this->createForm(new CUnitType(), $entity, array(
                'action' => $this->generateUrl('unit_create'),
                'method' => 'POST',
            ));

            $form->add('submit', 'submit', array('label' => 'Create'));
        }
        else{
            $form = $this->createForm($type, $entity, array(
                'action' => $this->generateUrl('unit_create'),
                'method' => 'POST',
            ));
        }

        return $form;
    }

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

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

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

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

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

        $form   = $this->createEditForm($entity, new CUnitType());

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

    /**
     * Creates a form to edit a NDepartment entity.
     *
     * @param NDepartment $entity The entity
     *
     * @return \Symfony\Component\Form\Form The form
     */
    private function createEditForm(CUnit $entity, CUnitType $type = null)
    {
        $form = null;

        if($type == null){
            $form = $this->createForm(new CUnitType(), $entity, array(
                'myid' => $entity->getId(),
                'action' => $this->generateUrl('unit_update', array('id' => $entity->getId())),
                'method' => 'PUT',
            ));

            $form->add('submit', 'submit', array('label' => 'Update'));
        }
        else{
            $form = $this->createForm($type, $entity, array(
                'action' => $this->generateUrl('unit_update', array('id' => $entity->getId())),
                'method' => 'PUT',
            ));

        }

        return $form;
    }

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

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

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

        //$deleteForm = $this->createDeleteForm($id);
        $editForm = $this->createEditForm($entity, new CUnitType());
        $editForm->handleRequest($request);

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

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

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

    /**
     * Deletes a NDepartment 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:NDepartment')->find($id);

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

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

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

    /**
     * Creates a form to delete a NDepartment 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('vgm_departments_delete', array('id' => $id)))
            ->setMethod('DELETE')
            ->add('submit', 'submit', array('label' => 'Delete'))
            ->getForm();
    }

    /**
     * Ajax functions
     */
    //Proof ajax function
    public function getUnitsAjaxAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();

        /**
         * Getting data request
         * initializing others vars
         */
        $units = $em->getRepository('VGMWOBundle:CUnit')->findAllUnits();

        $units_list = [];

        foreach($units as $unit){
            $units_list[] = array("id" => $unit->getId(), "name" => $unit->__toString());
        }

        $response = array("code" => 100, "success" => true, "departments" => $units_list, "message" => "Ok");

        return new Response(json_encode($response));
    }
}
