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


class ClientInformationController extends Controller
{
    
    
    /**
     * 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('Dorcas: Unable to find client.');
            return $this->redirect($this->generateUrl('DorcasTabithaBundle_clientinformation_new'
            ));
        }
        
        // hard code in state = NC
        $client->setState('NC');
        $client->setLicenseState('NC');
        
        // change zero date to blank
        if( $client->getBirthDate()->format('Y') <= '1901') $client->setBirthDate(null);
        
        $form = $this->createForm(new ClientInformationType(), $client);
        return $this->render('DorcasTabithaBundle:ClientInformation:clientinformationformupdate.html.twig', array(
        'client'      => $client,
        'form' => $form->createView(),
        ));
    }

    
     public function clientinformationformAction(Request $request)
    {
        $client = new Client();
        // hard code in state = NC
        // hardcode in city = Cary
        $client->setState('NC');
        $client->setCity('Cary');
        $client->setLicenseState('NC');
        
        $form = $this->createForm(new ClientInformationType(), $client);
        if ($request->getMethod() == 'POST') {
            $form->bindRequest($request);

            $navnumber = $request->request->get("passnav");
                if ($form->isValid()) {
                    $em = $this->getDoctrine()
                       ->getEntityManager();
                    $em->persist($client);
                    $em->flush();
                    
                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_new', 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_new', array(
                        'client_id' => $client->getClientId())));
                            break;
                        case "7":
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_users_show'));
                            break;
                        default:
                           return $this->redirect($this->generateUrl('DorcasTabithaBundle_finance_new', array(
                        'client_id' => $client->getClientId())));
                    } 
            
                }
                if ($navnumber == 9){
                    $this->get('session')->setFlash('notice', 'Warning: Your changes to Client Information were not saved!');
                return $this->redirect($this->generateUrl('DorcasTabithaBundle_homepage'));
                }
        }

        return $this->render('DorcasTabithaBundle:ClientInformation:clientinformationform.html.twig', array(
        'form' => $form->createView(),
        ));

    }
    
    
    
    public function updateAction($client_id)
    {
    $request = $this->get('request');

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

    $em = $this->getDoctrine()->getEntityManager();
    $client = $em->getRepository('DorcasTabithaBundle:Client')->find($client_id);
    $form = $this->createForm(new ClientInformationType(), $client);

    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);
        $navnumber = $request->request->get("passnav");
        if ($form->isValid()) {
            // save the object to the database
            $em->flush();
            
            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 "7":
                            return $this->redirect($this->generateUrl('DorcasTabithaBundle_users_show'));
                            break;
                        default:
                           return $this->redirect($this->generateUrl('DorcasTabithaBundle_finance_show', array(
                        'client_id' => $client->getClientId())));
            } 
        }
        if ($navnumber == 9){
                    $this->get('session')->setFlash('notice', 'Warning: Your changes to Client Information were not saved!');
                return $this->redirect($this->generateUrl('DorcasTabithaBundle_homepage'));
                }
    }

    return $this->render('DorcasTabithaBundle:ClientInformation:clientinformationformupdate.html.twig', array(
    'client'      => $client,
    'form' => $form->createView(),
    ));
    }

}
