<?php

class FeedbackController extends \BaseController {

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		$title = "Feedback Form";
		$schools = School::orderBy('name','asc');
		$user = Auth::user();
		return View::make('global.feedback', compact('title', 'schools', 'user'));
	}

	public function send()
	{
        /** Validation rules */
        $validator = Validator::make(
            array(
                'type' => Input::get('type'),
                'email' => Input::get('email'),
                'comment' => Input::get('comment')
            ),
            array(
                'type' => 'required|numeric|integer',
                'email' => 'required|email',
                'comment' => 'required'
            )
        );

        /** If validation fails */
        if ($validator->fails())
        {
            /** Prepare error message */
            $message = '<p>Error(s) in the form</p><ul>';

            foreach ($validator->messages()->all('<li>:message</li>') as $validationError)
            {
                $message.=$validationError;
            }

            $message.='</ul>';

            /** Send error message and old data */
            Session::flash('status', 'warning');
            Session::flash('message', $message);
            return Redirect::back()->withInput();
        }
        else
        {
        	$data = array('type'=>'', 'comment'=>'', 'email'=>'');
        	$data['comment']=Input::get('comment');
        	$data['email']=Input::get('email');

        	switch (Input::get('type')) {
        		case '0':
        			$data['type']='Error Report';
        			break;

        		case '1':
        			$data['type']='Question';
        			break;

        		case '2':
        			$data['type']='Suggestion';
        			break;

        		case '3':
        			$data['type']='Other';
        			break;

        		default:
        			$data['type']='Other';
        			break;
        	}

          	Mail::send('emails.feedback-email-copy', $data, function($message){
        		$message
        		->to(Input::get('email'), Auth::user()->first_name.' '.Auth::user()->surnames)
                ->cc('oeae.uprrp@upr.edu')
        		->subject('Feedback for OLAS');
    		});

            Session::flash('status', 'success');
            Session::flash('message', '<p>Success! Check your email for a copy of your message. Thank you for your feedback. </p>');
            return Redirect::back();
        }
	}

}