<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\Request1;	
use Illuminate\Http\Request;
use App\Models\SportsModel;

class SportsController extends Controller {

	public function displayAllSports()
	{
		$m 		= new SportsModel;
		$data 	 = $m->selectAllSports();
		return view('sub_pages.sports')->with('row', $data);
	}

	public function displayAddSportPage()
	{
		return view('sub_pages.add_sports');
	}

	public function saveNewSports(Request $request)
	{
		$m 			= 	new SportsModel;
		$post 	=	$request->all();

		$rawName = date('Ymd') . "-" . time().'-'.$request->file('previewImg')->getClientOriginalName();
		$imageExt = $request->file('previewImg')->getClientOriginalExtension();

		$custom_file_name	=	str_replace(' ','-',strtolower($rawName));
		$custom_file_name      = preg_replace("/\.[^.\s]{3,4}$/", "", $custom_file_name);
		$NewImageName = $custom_file_name.'.'.$imageExt;
		$thumbnail = "images/sports-thumbnails/" . $NewImageName;
		$data = array(
						'SportsName' => $post['sportsName'],
						'URL' => $post['generatedUrl'],
						'Thumbnail' => $thumbnail,
						'IsActive' => 'FALSE',

					);
		$i 	= 	$m->insertToSports($data);

		if($i){
			$request->file('previewImg')->move(
			    base_path() . '/public/images/sports-thumbnails', $NewImageName
			);

			echo '<div class="alert alert-success alert-dismissible">
	                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
	                <h4><i class="icon fa fa-check"></i> Success!</h4>
	                Sports is successfully added.
	              </div>';
		}else{
			echo '<div class="alert alert-danger alert-dismissible">
	                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
	                <h4><i class="icon fa fa-check"></i> Error!</h4>
	                Failed to saved new sports.
	              </div>';
		}

	}

	public function sportsDetails($q)
	{
		$m 		= 	new SportsModel;
		$data 	= 	$m->selectSports($q);
		return view('sub_pages.sports_details')->with('data', $data);
	}

	public function updateSports(Request $request)
	{
		$m 			= 	new SportsModel;
		$post 	=	$request->all();

		if(count($post) > 3){
			$rawName = date('Ymd') . "-" . time().'-'.$request->file('previewImg')->getClientOriginalName();
			$imageExt = $request->file('previewImg')->getClientOriginalExtension();

			$custom_file_name	=	str_replace(' ','-',strtolower($rawName));
			$custom_file_name	= preg_replace("/\.[^.\s]{3,4}$/", "", $custom_file_name);
			$NewImageName = $custom_file_name.'.'.$imageExt;
			$thumbnail = "images/sports-thumbnails/" . $NewImageName;
			$data = array(
							'SportsName' => $post['sportsName'],
							'URL' => $post['generatedUrl'],
							'Thumbnail' => $thumbnail

						);
			$request->file('previewImg')->move(
			    base_path() . '/public/images/sports-thumbnails', $NewImageName
			);
		}else{
			$data = array(
							'SportsName' => $post['sportsName'],
							'URL' => $post['generatedUrl']
						);

		}

		$i 	= 	$m->upateSportsDetails($data, $post['_id']);

		if($i){

			echo '<div class="alert alert-success alert-dismissible">
	                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
	                <h4><i class="icon fa fa-check"></i> Success!</h4>
	                Sports details is successfully updated.
	              </div>';
		}else{
			echo '<div class="alert alert-warning alert-dismissible">
	                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
	                <h4><i class="icon fa fa-check"></i> Warning!</h4>
	                No changes made.
	              </div>';
		}
	}

	public function selectSportsName()
	{
		//if(Request1::ajax()){
			$m 		= new SportsModel;
			$data 	= $m->getSportsName();
			?>
				<option value="">--Select Sports--</option>
				<?php
					foreach ($data as $row) {
						echo '<option value="'.$row->Id.'">'.$row->SportsName.'</option>';
					}
				?>
			<?php
		//}
	}
}
