<?php

namespace App\Http\Controllers;

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

use Illuminate\Http\Request;
use App\Models\MainModel;
// use Illuminate\Support\Facades\Request;
use Analytics;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Auth;

class MainController extends Controller
{

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		return view('sublayouts.index');
	}

	public function sports()
	{
		// if(Request::ajax()){
		$m 		= new MainModel;
		$fetchData 	 = $m->selectAllSports();
		//var_dump($fetchData);

		foreach ($fetchData as $row) {
?>
			<div class="col-md-3 col-sm-6 col-xs-12 list-sport">
				<a href="<?php echo url('sports') . "/" . $row->URL; ?>"><img src="<?php echo config('site_config.uploads')  . 'sports-thumbnails/' . $row->Thumbnail; ?>" alt="" class="img img-responsive product-center" /></a>
				<h3 class="text-center sports-title"><?php echo $row->SportsName ?></h3>
			</div>
<?php
		}

		// }else{
		// 	  return response()->view('errors/403');
		// }
	}

	public function templatesCat($url)
	{
		$m 		= new MainModel;

		$data 	 = $m->selectSportsId($url);
		$categoryids = array();

		foreach ($data as $row) {
			$categoryids[] =  $row->Category;
		}

		$array_sports 	 = $m->selectSportsByURL($url);
		$array_category 	 = $m->selectCategory($categoryids);
		// $array_templateby_category = $m->selectTemplatesByCategory($url, $id);

		// var_dump($array_category);

		return view('sublayouts.sports-category')
			->with('array_sports', $array_sports)
			->with('row', $array_category);
	}


	public function templatesByCategory($url, $id)
	{
		$m 		= new MainModel;
		$data 	 = $m->selectTemplatesByCategory($url, $id);
		$categoryids = array();

		$categoryids[] = $id;
		$array_category 	 = $m->selectCategory($categoryids);
		// var_dump($array_category);
		return view('sublayouts.sports-styles')
			->with('cat', $url)
			->with('row', $data)
			->with('array_category', $array_category);
		// if(count($data) > 1){
		// 	return view('sublayouts.sports-styles')
		// 									->with('cat', $url)
		// 									->with('row', $data);
		// }else{
		// 	$url = url('/designer'). '/'.$data[0]->HashTemplateCode; // desinger url
		// 	return redirect()->to($url);
		// }
	}


	public function fetchTemplates()
	{
		// if(Request::ajax()){
		// $m 		= new MainModel;
		//
		// $data 	 = $m->selectSportsId($url);
		// echo $data[0]->id;
		//$fetchData 	 = $m->selectSportsTemplates();


		//var_dump($fetchData);

		// }else{
		// 	  return response()->view('errors/403');
		// }

	}

	//call this from blade view
	public static function getCountCart()
	{
		$m 	= 	new MainModel;

		if (Session::get('cartkey')) {

			$cartKey = Session::get('cartkey');

			echo $i 	= 	$m->cartCount($cartKey);
		} else {
			echo 0;
		}
	}

	public function countCart(Request $request)
	{

		$m 	= 	new MainModel;

		if ($request->session()->has('cartkey')) {

			$cartKey = $request->session()->get('cartkey');

			echo $i 	= 	$m->cartCount($cartKey);
		} else {
			echo 0;
		}
	}


	public function removeCartItem($id)
	{

		$m              =   new MainModel;

		$row            =   $m->removeItem($id);
		if ($row > 0) {
			// \Session::flash('message', 'Record successfully deleted.');
			echo '<script>
        			alert("Item removed");
        		</script>';
			return redirect('cart');
		}
	}


	public function trackingPage()
	{

		if (Auth::check()) {
			$userId = Auth::user()->id;
		} else {
			$userId = "";
		}
		return view('merchbay.track_order')
			->with('userId', $userId);
	}
}
