<?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\PrintPatternModel;
use App\Models\SizesModel;

class PrintPatternController extends Controller {

	public function showPrintTemplate()
	{
		$m 		= 	new PrintPatternModel;
		$data 	= 	$m->selectAllPrintTemplate();
		return view('sub_pages.sports_details')->with('data', $data);
	}

	public function displayAddPrintTemplatePage($tempcode)
	{
		$m 		= 	new SizesModel;
		$data 	= 	$m->selectAllSizes();

		//var_dump($tempcode);
		return view('sub_pages.add_print_template')->with('sizeslist', $data)->with('templatecode', $tempcode);
	}

	public function savePrintPattern (Request $request)
	{
		$m 		= new PrintPatternModel;

		$post 	=	$request->all();
	
		$templatecode = $post['templatecode'];
		$templateType = $post['templateType'];
		$templateSize = $post['templateSize'];

		$rawName = $request->file('preview_print_template')->getClientOriginalName();
		$imageExt = $request->file('preview_print_template')->getClientOriginalExtension();

		$custom_file_name	=	str_replace(' ','-',strtolower($rawName));
		$custom_file_name	= 	preg_replace("/\.[^.\s]{3,4}$/", "", $custom_file_name);
		

		$NewImageName = $templateSize.'.'.$imageExt;
		

		$thumbnail = "uniform-templates/".$templatecode."/".$templateType."/SIZES/" . $NewImageName;

		$data = array(
						'TemplateCode' 	=> $templatecode,
						'Path' 			=> $thumbnail,
						'Type'	 		=> $templateType,
						'Size' 			=> $templateSize
					);

		$i 	= 	$m->insertPrintPattern($data);
		//var_dump($data);
		if($i){
			$r = $request->file('preview_print_template')->move(
			    base_path() . "/public/images/uniform-templates/".$templatecode."/".$templateType."/SIZES/", $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>
	                Print Pattern is successfully uploaded.
	              </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 upload.
	              </div>';
		}
		

	}
}
