<?php
/*
 * Developer : John D'Alto (jdalto@yahoo.com)
 * Date : 9/1/2012
 * All code (c)2012 JLS Consulting, all rights reserved
 */

namespace Dorcas\TabithaBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Dorcas\TabithaBundle\Entity\Client;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Dorcas\TabithaBundle\Form\Type\ClientInformationType;
//use Dorcas\TabithaBundle\Form\Type\FinanceType;
//use Dorcas\TabithaBundle\Entity\Finance;
use Dorcas\TabithaBundle\Form\Type\NotesType;
use Dorcas\TabithaBundle\Entity\Notes;


class NotesController extends Controller
{
    
    protected function getClient($client_id)
    {
        $em = $this->getDoctrine()
                    ->getEntityManager();

        $client = $em->getRepository('DorcasTabithaBundle:Client')->find($client_id);

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

        return $client;
    }
    
    
        public function newAction($client_id)
    {
        $client = $this->getClient($client_id);

        $note = new Notes();
        $note->setClient($client);
        $form   = $this->createForm(new NotesType(), $note);

        return $this->render('DorcasTabithaBundle:Notes:noteformaction.html.twig', array(
            'note' => $note,
            'form'   => $form->createView()
        ));
    }
    

    /**
     * Show a client entry
     */
    public function showAction($client_id)
    {
        $em = $this->getDoctrine()->getEntityManager();

        $client = $em->getRepository('DorcasTabithaBundle:Client')->find($client_id);

        if (!$client) {
            // throw $this->createNotFoundException('Unable to find client.');
            return $this->redirect($this->generateUrl('DorcasTabithaBundle_clientinformation_new'));
        }
      
        $notes = $em->getRepository('DorcasTabithaBundle:Notes')
                   ->getNotesForClient($client->getClientId());

    return $this->render('DorcasTabithaBundle:Notes:noteform.html.twig', array(
        'client'      => $client,
        'notes'  => $notes
        ));
    }
    
    
        public function createAction($client_id)
    {
   
        $client = $this->getClient($client_id);

        $note  = new Notes();
        
        // Get user from session
        $user = $this->get('security.context')->getToken()->getUser();

        $note->setUser($user->getUsername());
        
        $note->setClient($client);
        $request = $this->getRequest();
        $form    = $this->createForm(new NotesType(), $note);
        $form->bindRequest($request);
        
        $navnumber = $request->request->get("passnav");

        if ($form->isValid()) {
            // Make sure that a note was entered
            if($note->getNote()){
                $em = $this->getDoctrine()
                       ->getEntityManager();
                $em->persist($note);
                $em->flush();
                //$this->get('session')->setFlash('notice', 'New note information has been saved!');
            }
            else if($navnumber == null) {
              $this->get('session')->setFlash('notice', 'Please make sure you have entered a note before saving!');  
            }
       
            switch ($navnumber) {
                        case 9:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_homepage'));
                            break;
                        case 1:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_clientinformation_show', array(
                        'client_id' => $client->getClientId())));
                            break;                  
                        case 2:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_finance_show', array(
                        'client_id' => $client->getClientId())));
                            break;
                        case 3:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_notes_show', array(
                        'client_id' => $client->getClientId())));
                            break;
                        case 4:
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_assistance_show', array(
                        'client_id' => $client->getClientId())));
                            break;
                        case "5":
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_referrals_show', array(
                        'client_id' => $client->getClientId())));
                            break;
                        case "6":
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_assistance_show', array(
                        'client_id' => $client->getClientId())));
                            break;
                        case "7":
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_users_show'));
                            break;
                        default:
                           return $this->redirect($this->generateUrl('DorcasTabithaBundle_notes_show', array(
                        'client_id' => $client->getClientId())));
                    } 
            /*
            return $this->redirect($this->generateUrl('DorcasTabithaBundle_notes_show', array(
                'client_id' => $note->getClient()->getClientId())) . '#note-' . $note->getNoteId()
            ); */
        }

        return $this->render('DorcasTabithaBundle:Notes:notelayout.html.twig', array(
            'note' => $note,
            'form'    => $form->createView()
        ));
    }
      
    /**
     * Used by Note EDIT
     */
        public function updateAction($note_id)
        {   
            $request = $this->get('request');

            if (is_null($note_id)) {
                $postData = $request->get('testimonial');
                $client_id = $postData['client_id'];
            }

            $em = $this->getDoctrine()->getEntityManager();
            $note = $em->getRepository('DorcasTabithaBundle:Notes')->find($note_id);
            $form = $this->createForm(new NotesType(), $note);

            if ($request->getMethod() == 'POST') {
                $form->bindRequest($request);

                if ($form->isValid()) {
                    // save the object to the database
                    $em->flush();
                    $this->get('session')->setFlash('notice', 'Your edit has succeeded!  
                        If you do not see the change, please refresh the page.');
                    
                    // Exit now!  Window closing.
                    exit();

                    // Commented because I close the window right away
                   // return $this->render('DorcasTabithaBundle:Notes:noteformedit.html.twig', array(
                   // 'note'      => $note,
                   // 'form' => $form->createView(),
                   // ));
                }
                 $this->get('session')->setFlash('notice', 'Your edit failed, please retry!');
                 // Exit now!  Window closing.
                exit();
            }
            return $this->render('DorcasTabithaBundle:Notes:noteformedit.html.twig', array(
            'note'      => $note,
            'form' => $form->createView(),
            ));
        }  
        
        public function messageAction($message)
    {
        return new Response($message);   

    }
    
}
