<?php

namespace App\Http\Controllers;

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

use Illuminate\Http\Request;
use App\Models\TemplatesModel;
use App\Models\SportsModel;
use App\Models\PrintPatternModel;

class TemplatesController extends Controller
{

	public function displayTemplates()
	{
		return view('sub_pages.templates');
	}

	public function displayEditTemplatePage($q)
	{
		//return view('sub_pages.edit_template');
		$m 					= 	new TemplatesModel;
		$m1 				= 	new SportsModel;
		$m2 				= 	new PrintPatternModel;

		$templateData 		= 	$m->selectTemplate($q);
		$templatePathsData 	= 	$m->selectTemplatePaths($q); // plain svg for visualizer
		$templateTypes 		= 	$m->selectTemplateTypes();
		$sportsList 		= 	$m1->getSportsName();
		$printPatternList 	= 	$m2->selectAllPrintTemplate($q);

		return view('sub_pages.edit_template')->with('templatedata', $templateData)->with('templatepath', $templatePathsData)->with('sportsname', $sportsList)->with('templatetype', $templateTypes)->with('printpattern', $printPatternList);
	}


	public function getTemplates(Request $request)
	{
		$m 		= 	new TemplatesModel;
		$post 	=	$request->all();
		$data 	= 	$m->selectTemplates($post['id']);

		// /public/images/sports-thumbnails

		if (!empty($data)) {
			foreach ($data as $row) {
?>
				<div class="col-md-4 col-sm-4 col-xs-6">
					<div class="text-center">
						<h3><?php echo $row->TemplateName ?></h3>
					</div>
					<div class="sports-border">
						<a href=""><img src="<?php echo url('public') . "/" . $row->Thumbnail ?>" alt="Sports" height="400px;" class="img img-responsive product-center" /></a>
						<div class="sport-edit-btn">
							<a href="<?php echo   url('admin/templates') . "/edit/" . $row->TemplateCode . "/" ?>" class="btn btn-primary btn-block"><i class="fa fa-edit"></i> Edit</a>
						</div>
					</div>
				</div>
		<?php
			}
		} else {
			echo '<script>
					alert("No available Template/s");
					</script>';
		}
	}

	public function displayAddTemplatePage()
	{
		return view('sub_pages.add_template');
	}

	public function getTemplateTypes()
	{
		//if(Request::ajax()){
		$m 		= new TemplatesModel;
		$data 	= $m->selectTemplateTypes();
		?>

		<option value="">--Select Type--</option>
		<?php
		foreach ($data as $row) {
			echo '<option value="' . $row->TemplateType . '">' . $row->TemplateType . '</option>';
		}
		?>
<?php
		//}
	}

	public function getLastId()
	{
		//if(Request::ajax()){
		$m 		= new TemplatesModel;
		$data 	= $m->selectTemplateLastId();
		echo $templateCode = "TEMP-" . str_pad($data->Id + 1, 5, '0', STR_PAD_LEFT);
	}

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

		$templateCode = $post['templateCode'];
		$sportName = $post['sportName'];
		$templateName = $post['templateName'];
		$templateType = $post['templateType'];
		$numberOfTrims = $post['numberOfTrims'];
		$getSkins = $post['getSkins'];
		$tempateImage = $post['tempateImage'];

		$rawName = date('Ymd') . "-" . time() . '-' . $request->file('tempateImage')->getClientOriginalName();
		$imageExt = $request->file('tempateImage')->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/templates/thumbnail/" . $NewImageName;

		$data = array(
			'SportsId' => $sportName,
			'TemplateCode' => $templateCode,
			'Thumbnail' => $thumbnail,
			'TemplateName' => $templateName,
			'TemplateType' => $templateType,
			'Trim'	=> $numberOfTrims,
			'PatternId' => $getSkins,
			'IsActive' => 'TRUE'
		);

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


		if ($i) {
			$request->file('tempateImage')->move(
				base_path() . '/public/images/templates/thumbnail',
				$NewImageName
			);

			//for front jersey
			if (!empty($request->file('svgJerseyFront')->getClientOriginalName())) {

				$svgName = $request->file('svgJerseyFront')->getClientOriginalName();
				$svgThumbnail = "uniform-templates/" . $templateCode . "/DISPLAY/" . $svgName;
				//var_dump($svgThumbnail);
				$Templatedata = array(
					'TemplateCode' => $templateCode,
					'Type' => 'Jersey',
					'Side' => 'Front',
					'Path' => $svgThumbnail,
					'IsActive' => 'TRUE'
				);

				$i 	= 	$m->insertTempaltePaths($Templatedata);
				if ($i) {
					$request->file('svgJerseyFront')->move(
						base_path() . '/public/images/uniform-templates/' . $templateCode . '/DISPLAY',
						$svgName
					);
				}
			}

			if (!empty($request->file('svgJerseyBack')->getClientOriginalName())) {

				$svgName = $request->file('svgJerseyBack')->getClientOriginalName();
				$svgThumbnail = "uniform-templates/" . $templateCode . "/DISPLAY/" . $svgName;

				$Templatedata = array(
					'TemplateCode' => $templateCode,
					'Type' => 'Jersey',
					'Side' => 'Back',
					'Path' => $svgThumbnail,
					'IsActive' => 'TRUE'
				);

				$i 	= 	$m->insertTempaltePaths($Templatedata);
				if ($i) {
					$request->file('svgJerseyBack')->move(
						base_path() . '/public/images/uniform-templates/' . $templateCode . '/DISPLAY',
						$svgName
					);
				}
			}

			if (!empty($request->file('svgShortRight')->getClientOriginalName())) {

				$svgName = $request->file('svgShortRight')->getClientOriginalName();
				$svgThumbnail = "uniform-templates/" . $templateCode . "/DISPLAY/" . $svgName;

				$Templatedata = array(
					'TemplateCode' => $templateCode,
					'Type' => 'Shorts',
					'Side' => 'Right',
					'Path' => $svgThumbnail,
					'IsActive' => 'TRUE'
				);

				$i 	= 	$m->insertTempaltePaths($Templatedata);
				if ($i) {
					$request->file('svgShortRight')->move(
						base_path() . '/public/images/uniform-templates/' . $templateCode . '/DISPLAY',
						$svgName
					);
				}
			}

			if (!empty($request->file('svgShortLeft')->getClientOriginalName())) {

				$svgName = $request->file('svgShortLeft')->getClientOriginalName();
				$svgThumbnail = "uniform-templates/" . $templateCode . "/DISPLAY/" . $svgName;

				$Templatedata = array(
					'TemplateCode' => $templateCode,
					'Type' => 'Shorts',
					'Side' => 'Left',
					'Path' => $svgThumbnail,
					'IsActive' => 'TRUE'
				);

				$i 	= 	$m->insertTempaltePaths($Templatedata);
				if ($i) {
					$request->file('svgShortLeft')->move(
						base_path() . '/public/images/uniform-templates/' . $templateCode . '/DISPLAY',
						$svgName
					);
				}
			}

			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 updateTemplate(Request $request)
	{
		$m 		= 	new TemplatesModel;
		$post 	=	$request->all();
		// var_dump($post);
		// echo count($post);
		// var_dump($post);
		// var_dump(array_key_exists('svgShortRight', $post));

		$templateCode = $post['templateCode'];
		$sportName = $post['sportName'];
		$templateName = $post['templateName'];
		$templateType = $post['templateType'];
		$numberOfTrims = $post['numberOfTrims'];
		$getSkins = $post['getSkins'];

		if (array_key_exists('tempateImage', $post)) {

			$tempateImage = $post['tempateImage'];

			$rawName = date('Ymd') . "-" . time() . '-' . $request->file('tempateImage')->getClientOriginalName();
			$imageExt = $request->file('tempateImage')->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/templates/thumbnail/" . $NewImageName;

			$data = array(
				'SportsId' => trim($sportName),
				'TemplateCode' => $templateCode,
				'Thumbnail' => $thumbnail,
				'TemplateName' => $templateName,
				'TemplateType' => trim($templateType),
				'Trim'	=> $numberOfTrims,
				'PatternId' => $getSkins
			);

			$request->file('tempateImage')->move(
				base_path() . '/public/images/templates/thumbnail',
				$NewImageName
			);
		} else {

			$data = array(
				'SportsId' => trim($sportName),
				'TemplateCode' => $templateCode,
				//'Thumbnail' => $thumbnail,
				'TemplateName' => $templateName,
				'TemplateType' => trim($templateType),
				'Trim'	=> $numberOfTrims,
				'PatternId' => $getSkins
			);
		}

		$i 	= $m->updateNewTemplate($data, $templateCode);

		if (array_key_exists('svgJerseyFront', $post)) {
			//echo 'meron jerset front';

			$svgName = $request->file('svgJerseyFront')->getClientOriginalName();
			$svgThumbnail = "uniform-templates/" . $templateCode . "/DISPLAY/" . $svgName;

			$Templatedata = array(
				'Type' => 'Jersey',
				'Side' => 'Front',
				'Path' => $svgThumbnail
			);

			$i 	= 	$m->updateTemplatePaths($Templatedata, $post['id_svgJerseyFront']);
			if ($i) {
				$request->file('svgJerseyFront')->move(
					base_path() . '/public/images/uniform-templates/' . $templateCode . '/DISPLAY',
					$svgName
				);
				//echo 'image move success';
			}
		}

		if (array_key_exists('svgJerseyBack', $post)) {

			$svgName = $request->file('svgJerseyBack')->getClientOriginalName();
			$svgThumbnail = "uniform-templates/" . $templateCode . "/DISPLAY/" . $svgName;

			$Templatedata = array(
				'Type' => 'Jersey',
				'Side' => 'Back',
				'Path' => $svgThumbnail
			);
			$i 	= 	$m->updateTemplatePaths($Templatedata, $post['id_svgJerseyBack']);
			if ($i) {

				$request->file('svgJerseyBack')->move(
					base_path() . '/public/images/uniform-templates/' . $templateCode . '/DISPLAY',
					$svgName
				);
			}
		}

		if (array_key_exists('svgShortRight', $post)) {
			$svgName = $request->file('svgShortRight')->getClientOriginalName();
			$svgThumbnail = "uniform-templates/" . $templateCode . "/DISPLAY/" . $svgName;

			$Templatedata = array(
				'Type' => 'Shorts',
				'Side' => 'Right',
				'Path' => $svgThumbnail
			);

			$i 	= 	$m->updateTemplatePaths($Templatedata, $post['id_svgShortRight']);
			if ($i) {
				$request->file('svgShortRight')->move(
					base_path() . '/public/images/uniform-templates/' . $templateCode . '/DISPLAY',
					$svgName
				);
			}
		}

		if (array_key_exists('svgShortLeft', $post)) {
			$svgName = $request->file('svgShortLeft')->getClientOriginalName();
			$svgThumbnail = "uniform-templates/" . $templateCode . "/DISPLAY/" . $svgName;

			$Templatedata = array(
				'Type' => 'Shorts',
				'Side' => 'Left',
				'Path' => $svgThumbnail
			);

			$i 	= 	$m->updateTemplatePaths($Templatedata, $post['id_svgShortLeft']);
			if ($i) {
				$request->file('svgShortLeft')->move(
					base_path() . '/public/images/uniform-templates/' . $templateCode . '/DISPLAY',
					$svgName
				);
			}
		}

		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>
	                Template is successfully updated.
	              </div>';
	}
}
