<?php namespace App\Http\Controllers\designer;

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

use Illuminate\Http\Request;
use App\Models\designer\DesignerModel;
use App\Models\teamstore\TeamStoreModel;
use App\Models\MainModel;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;

class DesignerController extends Controller {

	public function index($templateid)
	{
		$newDesignerModel		=   new DesignerModel;
		$template_arrays      	=   $newDesignerModel->selectTemplate($templateid);
		// dd($template_arrays);
		$patterns = explode(",", $template_arrays[0]->PatternId);
		
		foreach($patterns as $patternId){
			$pattern_arrays[]      	=   $newDesignerModel->selectPatterns($patternId);
		}

		$templatepaths_arrays      	=   $newDesignerModel->selectTemplatePaths($templateid);
		$fonts_array      			=   $newDesignerModel->selectFonts();

		// var_dump($template_arrays);

		return view('designer.designer')->with('template_arrays', $template_arrays)
										->with('templatepaths_arrays', $templatepaths_arrays)
										->with('pattern_arrays', $pattern_arrays)
										->with('fonts_array', $fonts_array);
	}

	public function gradientAppend(Request $request)
	{

		$post 	=	$request->all();
		$gradientPrefix = $post['gradientPrefix'];
		$TrimCount = $post['TrimCount'];

		?>
		<linearGradient id="<?php echo $gradientPrefix . 'Body_Gradient' ; ?>" gradientUnits="userSpaceOnUse" x1="0%" y1="100%" x2="0%" y2="0%"> 
			<stop id="<?php echo $gradientPrefix . 'Body_Gradient_Color2' ; ?>" offset="0" stop-color="#FFFFFF" />
			<stop id="<?php echo $gradientPrefix . 'Body_Gradient_Color1' ; ?>" offset="1" stop-color="#000000" />
		</linearGradient>

		<linearGradient id="<?php echo $gradientPrefix . 'Body_Pattern_Gradient' ; ?>" gradientUnits="userSpaceOnUse" x1="0%" y1="100%" x2="0%" y2="0%"> 
			<stop id="<?php echo $gradientPrefix . 'Body_Pattern_Gradient_Color2' ; ?>" offset="0" stop-color="#FFFFFF" />
			<stop id="<?php echo $gradientPrefix . 'Body_Pattern_Gradient_Color1' ; ?>" offset="1" stop-color="#000000" />
		</linearGradient>
		
		<?php
		for($i = 1; $i <= $TrimCount; $i++ ){
			?>
				<linearGradient id="<?php echo $gradientPrefix . 'Trim_'.$i.'_Gradient' ; ?>" gradientUnits="userSpaceOnUse" x1="0%" y1="100%" x2="0%" y2="0%"> 
					<stop id="<?php echo $gradientPrefix . 'Trim_'.$i.'_Gradient_Color2' ; ?>" offset="0" stop-color="#FFFFFF" />
					<stop id="<?php echo $gradientPrefix . 'Trim_'.$i.'_Gradient_Color1' ; ?>" offset="1" stop-color="#000000" />
				</linearGradient>
				<linearGradient id="<?php echo $gradientPrefix . 'Trim_'.$i.'_Pattern_Gradient' ; ?>" gradientUnits="userSpaceOnUse" x1="0%" y1="100%" x2="0%" y2="0%"> 
					<stop id="<?php echo $gradientPrefix . 'Trim_'.$i.'_Pattern_Gradient_Color2' ; ?>" offset="0" stop-color="#FFFFFF" />
					<stop id="<?php echo $gradientPrefix . 'Trim_'.$i.'_Pattern_Gradient_Color1' ; ?>" offset="1" stop-color="#000000" />
				</linearGradient>
			<?php
		}
		
		
	}

	public function setPattern(Request $request)
	{
		$newDesignerModel		=   new DesignerModel;
		
		$post 	=	$request->all();
		$patternId = $post['patternId'];

		$pattern_array    		=   $newDesignerModel->selectPatterns($patternId);
		$pattern_colors_array   =   $newDesignerModel->selectPatternColors($patternId);
		
		$Opacity 		=   $pattern_array[0]->Opacity;
		$Gradient 	=   $pattern_array[0]->Gradient;

		if($pattern_array[0]->NoOFColor != 0){

			$NoOFColor = $pattern_array[0]->NoOFColor;
			echo '<div class="form-group col-md-12 col-sm-12 col-xs-12">';
			echo 'Pattern Color';
			echo '</div>';
			echo '<div class="form-group col-md-12 col-sm-12 col-xs-12">';
			for($i=1 ; $i <= $pattern_array[0]->NoOFColor ; $i++){
				?>
				
					<div class="btn-group">
						<input type="text" class="patternColor" id="PatternColor<?php echo $i; ?>" value="<?php echo $pattern_colors_array[$i - 1]->PatternColor ?>" />
					</div>
				<?php
				if($Gradient == "TRUE"){
					?>
					<button type="button" class="btn" id="btn-body-pattern-gradient" ></button>
					<?php
				}
			}
			echo '</div>';
			?>
				<div class="form-group col-md-12 col-sm-12 col-xs-12">
					<div id="row-body-pattern-gradient" style="display:none;">
						<div class="row">						
							<div class="form-group col-md-12 col-sm-12 col-xs-12">
								Gradient Colors
							</div>
						</div>
						<div class="row">						
							<div class="form-group col-md-2 col-sm-2 col-xs-2">
								<input type="hidden" class="patternGradientColor pull-right" name="Body_Pattern_Gradient_Color1" id="Body_Pattern_Gradient_Color1" value="rgb(0, 0, 0)" />
							</div>
							<div class="form-group col-md-10 col-sm-10 col-xs-10">
								<input type="range" id="Offset_Body_Pattern_Gradient_Color1" data-gradient-id="Body_Pattern_Gradient_Color1" class="offsetGradientPattern form-control input-sm" value="100" min="0" max="10" />	
							</div>
						</div>
						<div class="row">
							<div class="form-group col-md-2 col-sm-2 col-xs-2">
								<input type="hidden" class="patternGradientColor pull-right" name="Body_Pattern_Gradient_Color2" id="Body_Pattern_Gradient_Color2" value="rgb(255, 255, 255)" />
							</div>
							<div class="form-group col-md-10 col-sm-10 col-xs-10">
								<input type="range" id="Offset_Body_Pattern_Gradient_Color2" data-gradient-id="Body_Pattern_Gradient_Color2" class="offsetGradientPattern form-control input-sm" value="0" min="0" max="10" />	
							</div>
						</div>
					</div>
				</div>
			<?php 
				if($Opacity == "TRUE"){ 
			?>
				<div class="row">
					<div class="col-md-12 col-sm-12 col-xs-12">
						<label>Opacity</label>
					</div>
				</div>
				<div class="row">
					<div class="form-group col-md-9 col-sm-9 col-xs-9">
						<input type="range" id="opacityPattern" class="form-control input-sm" value="10" min="0" max="10"  />
					</div>
					<div class="form-group col-md-3 col-sm-3 col-xs-3">
						<input type="text" style="margin-top:5px;" class="form-control input-sm  text-center" value="10" id="opacityPatternValue" />
					</div>
				</div>
				<?php 
				}
		}else{
			/// no display..
			$NoOFColor = 0;
		}

		?><input type="hidden"  name="body-pattern-name" id="body-pattern-name" value="<?php echo $patternId . "," . $NoOFColor; ?>"><?php
	}
	
	public function setTrimPattern(Request $request)
	{
		$newDesignerModel		=   new DesignerModel;
		
		$post 					=	$request->all();
		$patternId 				= 	$post['patternId'];
		$trim 					= 	$post['trim'];

		$pattern_array    		=   $newDesignerModel->selectPatterns($patternId);
		$pattern_colors_array   =   $newDesignerModel->selectPatternColors($patternId);

		$Opacity 		=   $pattern_array[0]->Opacity;
		$Gradient 	=   $pattern_array[0]->Gradient;

		if($pattern_array[0]->NoOFColor != 0){

			$NoOFColor = $pattern_array[0]->NoOFColor;
			echo '<div class="form-group col-md-12 col-sm-12 col-xs-12">';
			echo 'Pattern Color';
			echo '</div>';
			echo '<div class="form-group col-md-12 col-sm-12 col-xs-12">';
			for($i=1 ; $i <= $pattern_array[0]->NoOFColor ; $i++){
				?>
			
					<div class="btn-group">
						<input type="text" class="patternColor" data-trim-num="<?php echo $trim ?>"  id="<?php echo 'Trim_' . $trim . '_PatternColor'.$i; ?>" value="<?php echo $pattern_colors_array[$i - 1]->PatternColor ?>" />
					</div>
				<?php
				if($Gradient == "TRUE"){
					?>
					<!-- <button type="button" class="btn" id="btn-body-pattern-gradient" ></button> -->
					<?php
				}
			}
			echo '</div>';
			?>
				<div class="form-group col-md-12 col-sm-12 col-xs-12">
					<div id="<?php echo 'row-trim-'.$trim.'-pattern-gradient' ?>" style="display:none;">
						<div class="row">						
							<div class="form-group col-md-12 col-sm-12 col-xs-12">
								Gradient Colors
							</div>
						</div>	
						<div class="row">						
							<div class="form-group col-md-2 col-sm-2 col-xs-2">
								<input type="hidden" class="patternGradientColor pull-right" name="<?php echo 'Trim_' . $trim . '_Pattern_Gradient_Color1' ?>" id="<?php echo 'Trim_' . $trim . '_Pattern_Gradient_Color1' ?>" data-gradient-type="Pattern" data-trim-num="<?php echo $trim ?>" value="rgb(0, 0, 0)"  />
							</div>
							<div class="form-group col-md-10 col-sm-10 col-xs-10">

								<input type="range" id="<?php echo 'Offset_Trim_' . $trim . '_Pattern_Gradient_Color1' ?>" data-gradient-id="<?php echo 'Trim_' . $trim . '_Pattern_Gradient_Color1' ?>" class="offsetGradientPattern form-control input-sm" value="100" min="0" max="10" />	
							</div>
						</div>
						<div class="row">
							<div class="form-group col-md-2 col-sm-2 col-xs-2">
								<input type="hidden" class="patternGradientColor pull-right" name="<?php echo 'Trim_' . $trim . '_Pattern_Gradient_Color2' ?>" id="<?php echo 'Trim_' . $trim . '_Pattern_Gradient_Color2' ?>" data-gradient-type="Pattern" data-trim-num="<?php echo $trim ?>" value="rgb(255, 255, 255)"  />
							</div>
							<div class="form-group col-md-10 col-sm-10 col-xs-10">
								<input type="range" id="<?php echo 'Offset_Trim_' . $trim . '_Pattern_Gradient_Color2' ?>" data-gradient-id="<?php echo 'Trim_' . $trim . '_Pattern_Gradient_Color2' ?>" class="offsetGradientPattern form-control input-sm" value="0" min="0" max="10" />		
							</div>
						</div>
					</div>
				</div>
			<?php 
				if($Opacity == "TRUE"){ 
			?>
				<div class="row">
					<div class="col-md-12 col-sm-12 col-xs-12">
						<label>Opacity</label>
					</div>
				</div>
				<div class="row">
					<div class="form-group col-md-9 col-sm-9 col-xs-9">
						<input type="range" id="opacityPattern" class="form-control input-sm" value="10" min="0" max="10"  />
					</div>
					<div class="form-group col-md-3 col-sm-3 col-xs-3">
						<input type="text" style="margin-top:5px;" class="form-control input-sm  text-center" value="10" id="opacityPatternValue" />
					</div>
				</div>
				<?php 
				}
		}else{
			/// no display..
			$NoOFColor = 0;
		}

		?><input type="hidden"  name="<?php echo 'Trim_'. $trim . '_patternName' ?>" id="<?php echo 'Trim_'. $trim . '_patternName' ?>" value="<?php echo $patternId . "," . $NoOFColor; ?>"><?php
	}

	public function getTemplateDefaultColors(Request $request)
	{
		$newDesignerModel		=   new DesignerModel;
		$post 					=	$request->all();

		$templateCode 			= 	$post['templateCode'];
		
		$default_template_color_array    		=   $newDesignerModel->selectDefaultTemplateColor($templateCode);
		// var_dump($default_template_color_array);
		
		return response()->json(array(
			'success' => true,
			'default_template_color_array' => $default_template_color_array
		));
		
	}

	public function getFontDisplay(Request $request){

		$newDesignerModel		=   new DesignerModel;
		
		$post 	=	$request->all();
		$getFontFamily = $post['fontFamily'];
		
		$font_array      	=   $newDesignerModel->selectFontsByFontFamily($getFontFamily);

		foreach($font_array as $row){
			
			$fontNameDisplay 	=    $row->fontNameDisplay;
        	$additionalSize 	=    $row->additionalSize;
		}

		$arr = array(
			'fontNameDisplay'	=> $fontNameDisplay,
			'additionalSize'  	=> $additionalSize
		);
		echo json_encode($arr);

	}

	public function tabClipartContent()
	{
		
		$newDesignerModel		=   new DesignerModel;
		
		$clipart_cat_array      =   $newDesignerModel->selectClipartCategories();
		?>

		<div class="row">
			<div class="col-md-12">
				<h4>Clipart</h4>
			</div>
		</div>
		<hr>
		<div id="show-clipart-content">
			<div class="row">
				<?php
					foreach($clipart_cat_array as $row){
						if($row->UserId != null){
							if(!Auth::guest()){
								if(Auth::user()->id == $row->UserId){
									?>
										<div class="form-group col-md-6">
											<a href="#" class="clipart-category" data-id="<?php echo $row->Id ?>" data-type="category" data-title="<?php echo $row->CategoryName ?>">
												<div style="height:100px; background-color:#f4f4f4; border-radius: 5px;" class="text-center">
													<span style="position:absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); font-size: 16px;"><?php echo $row->CategoryName  ?></span>
												</div>
											</a>
										</div>
									<?php
								}
							}
						}
					}

					foreach($clipart_cat_array as $row){
						if($row->UserId == null){
						?>
							<div class="form-group col-md-6">
								<a href="#" class="clipart-category" data-id="<?php echo $row->Id ?>" data-type="category" data-title="<?php echo $row->CategoryName ?>">
									<div style="height:100px; background-color:#f4f4f4; border-radius: 5px;" class="text-center">
										<span style="position:absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); font-size: 16px;"><?php echo $row->CategoryName  ?></span>
									</div>
								</a>
							</div>
						<?php 
						}	
					}
				?>
			</div>
		</div>
		<div class="display-flex" id="featured-cliparts"></div>
		<?php

	}

	public function getCliparts(Request $request)
	{
		
		$newDesignerModel		=   new DesignerModel;
		$post 					=	$request->all();

		$categoryId = $post['categoryId'];
		$req_type = $post['req_type'];
		$cat_name = $post['cat_name'];
		$clipart_array      	=   $newDesignerModel->selectClipartByCategory($categoryId);

		?>
		<div class="row">
			<div class="col-md-6">
				<h4><?php echo $cat_name ?></h4>
			</div>
			<div class="col-md-6">
				<button class="btn btn-sm btn-default pull-right addMarginLeft" id="btn-close-clipart-properties" title="Close Clipart Properties"><i class="fa fa-remove" aria-hidden="true"></i> Close</button>
			</div>
		</div>
		<br>
		<?php

		foreach($clipart_array as $row){
			?>
				<div class="form-group col-md-3"> 
					<div class="thumbnail clipart-thumnail"> 
						<a href="#" class="img-clipart" data-link="<?php  echo $row->SVGFilename; ?>"><img src="<?php echo config('site_config.uploads') . 'cliparts/'. $row->SVGFilename; ?>" width="100%"></a>
					</div> 
				</div>
			<?php
		}


	
	}
	
	public function clipartProperties(Request $request)
	{
		$newDesignerModel		=   new DesignerModel;
		$post 					=	$request->all();

		?>

		<div class="row">
			<div class="col-md-12">
				<h4>Clipart Properties</h4>
			</div>
		</div>
		<hr>
		<div class="row">
			<div class="form-group col-md-12">
				<button class="btn btn-sm btn-default pull-right addMarginLeft" id="btn-close-clipart-properties" title="Close Clipart Properties"><i class="fa fa-remove" aria-hidden="true"></i> Close</button>
				<!-- <button type="button" class="btn btn-danger pull-right btn-sm remove addMarginLeft" title="Remove object"><i class="fa fa-trash" aria-hidden="true"></i></button> -->
				<!-- <button type="button" class="btn btn-danger pull-right btn-sm addMarginLeft pasteObject" title="Paste object(s)"><i class="fa fa-paste" aria-hidden="true"></i></button>  -->
				<!-- <button type="button" class="btn btn-danger pull-right btn-sm addMarginLeft copyObject" title="Duplicate object"><i class="fa fa-copy" aria-hidden="true"></i></button>  -->
			</div>
		</div>

		<div class="row">
			<div class="form-group col-md-3 col-sm-3 col-xs-3">
				<label>Color(s)</label>
			</div>
			<div class="form-group col-md-9 col-sm-9 col-xs-9">
				<?php 
					// var_dump($_POST['PathId']);
					foreach($post['PathId'] as  $key => $arrayData ){
						// echo $arrayData['id'];
						?>
						<div class="btn-group">
							<input type="text" data-number="<?php echo $arrayData['ran_num']; ?>" data-id="<?php echo $arrayData['id']; ?>" class="clipartColor" value="<?php echo $arrayData['fill'];  ?>"/>
						</div>
						<?php
					}

				?>
			</div>
			
		</div>
		<div class="row">
			<div class="form-group col-md-3 col-sm-3 col-xs-3">
				<label>Opacity</label>
			</div>
			<div class="form-group col-md-6 col-sm-6 col-xs-6">
				<input type="range"  class="form-control input-sm opacityTextSlider" value="1" min="0" max="10"  />
			</div>
			<div class="form-group col-md-3 col-sm-3 col-xs-3">
				<input type="text" style="margin-top:5px;" class="form-control input-sm  text-center opacityTextValue" />
			</div>
		</div>
		<div class="row">
			<div class="form-group col-md-3 col-sm-3 col-xs-3">
				<label>Rotate</label>
			</div>
			<div class="form-group col-md-6 col-sm-6 col-xs-6">

				<input type="range" class="form-control input-sm rotateTextSlider" value="0" min="-180" max="180"  />
			</div>
			<div class="form-group col-md-3 col-sm-3 col-xs-3">
				<input type="text" style="margin-top:5px;" class="form-control input-sm text-center rotateTextValue"/>
			</div>
		</div>
		<div class="row">
			<div class="form-group col-md-5 col-sm-6 col-xs-6">
				<label>Layer Order</label><br>
				<button class="btn btn-sm btn-default sendSelectedObjectToFront" title="Bring Forward"></button>
				<button class="btn btn-sm btn-default sendSelectedObjectBack"  title="Send Backward"></button>
			</div>
			<div class="form-group col-md-7 col-sm-6 col-xs-6">
				<label>Position</label><br>
				<!-- <button class="btn btn-sm btn-default " style="margin:2px;" onclick="centerOnly();">Center</button>
				<button class="btn btn-sm btn-default" style="margin:2px;" onclick="centerVer();"> <img src="images/align-v1.png" height="16px;" /> Center Vertical</button>
				<button class="btn btn-sm btn-default" style="margin:2px;"  onclick="centerHor();" > <img src="images/align-h1.png" height="16px;" /> Center Horizontal</button> -->
				<!-- <button class="btn btn-sm btn-default btn-send-middle" style="margin:2px;" onclick="centerOnly();"></button>
				<button class="btn btn-sm btn-default center-vertical" style="margin:2px;" onclick="centerVer();"></button>
				<button class="btn btn-sm btn-default center-horizontal" style="margin:2px;"  onclick="centerHor();" ></button> -->
				<button class="btn btn-sm btn-default" style="min-width:35px;" title="Center Vertical" onclick="centerVer();"><i class="fa fa-arrows-v"></i></button>
				<button class="btn btn-sm btn-default" style="min-width:35px;" title="Center Horizontal" onclick="centerHor();" ><i class="fa fa-arrows-h"></i></button>
				<button class="btn btn-sm btn-default flip-vertically" style="min-width:35px;" title="Flip Vertically"><i class="fa fa-angle-double-up"></i></button>
				<button class="btn btn-sm btn-default flip-horizontally" style="min-width:35px;" title="Flip Horizontally"><i class="fa fa-angle-double-right"></i></button>
			</div>
		</div>

		<?php
	}
	
	public function saveDesign(Request $request)
	{
		$newDesignerModel		=   new DesignerModel;
		$post 					=	$request->all();
		$clientId = Auth::user()->id;
		$design_name = str_replace(array("\r", "\n"), '', $post['design_name']);
		$tempDecode = json_decode($_POST['templateDetails']);

		$getTemPlateCode = $tempDecode[0]->templateCode;
		$designCode = sha1(time() . "-" .date('ymd'));

		if(isset($post['json_Front'])){
			$json_Front = $post['json_Front'];
		}else{
			$json_Front = null;
		}

		if(isset($post['json_Back'])){
			$json_Back = $post['json_Back'];
		}else{
			$json_Back = null;
		}

		if(isset($post['json_Left'])){
			$json_Left = $post['json_Left'];
		}else{
			$json_Left = null;
		}

		if(isset($post['json_Right'])){
			$json_Right = $post['json_Right'];
		}else{
			$json_Right = null;
		}

		// if(isset($post['json_Shirts_Front'])){
		// 	$json_Jersey_Front = $post['json_Shirts_Front'];
		// }

		// if(isset($post['json_Shirts_Back'])){
		// 	$json_Jersey_Back = $post['json_Shirts_Back'];
		// }

		try {
			$design_info = array(
				'ClientId' 		=> $clientId,
				'DesignName' 	=> $design_name,
				'TemplateCode' 	=> $getTemPlateCode,
				'DesignCode' 	=> $designCode,
				'TemplateDesign'=> $post['templateDetails'],
				'ContentFront'	=> $json_Front,
				'ContentBack' 	=> $json_Back,
				'ContentLeft' 	=> $json_Left,
				'ContentRight' 	=> $json_Right
			);
		}
		
		catch (\Exception $e) {
			return $e->getMessage();
		}
		
		$res = $newDesignerModel->insertClientDesign($design_info);

		if($res){

			// dd(Uuid::generate());
			return redirect('/designer/preview/'.$designCode);
			// return redirect('http://localhost/crew-designer/mydesign/'.$designCode.'/');
			
		}
		// else{
		// 	dd($res);
		// }
		// dd($design_info);
	}

	public function getDesign($designCode){
		
		$newDesignerModel		=   new DesignerModel;
		$newTeamStoreModel		=   new TeamStoreModel;
		$newMainModel			=   new MainModel;
		$client_design_array    =   $newDesignerModel->selectClientDesign($designCode);
		
		$teamstore_array = array();

		$clientrole = Auth::user()->role;
		
		if($clientrole == "store_owner"){
			$store_id = Auth::user()->store_id;
			$teamstore_array    =   $newTeamStoreModel->selectTeamStore('Id',  $store_id);
			// var_dump(str_slug($teamstore_array[0]->StoreName, '-'));
			// var_dump($this->createSlug('shorts-white-1'));
		}
		// var_dump($this->getProductCode());
		$templatepaths_arrays   =   $newDesignerModel->selectTemplatePathsByTemplateCode($client_design_array[0]->TemplateCode);
		$array_cat_name = $newMainModel->selectCategoryName($client_design_array[0]->TemplateCode);
		// $array_cat_name = $newMainModel->selectCategoryName($client_design_array[0]->TemplateCode);
		$array_templates = $newDesignerModel->selectTemplate(md5($client_design_array[0]->TemplateCode));
		// var_dump($array_templates);
		return view('designer.design_preview')
											->with('client_design_array', $client_design_array)
											->with('templatepaths_arrays', $templatepaths_arrays)
											->with('teamstore_array', $teamstore_array)
											->with('array_cat_name', $array_cat_name)
											->with('array_templates', $array_templates);
	}
	
	protected function getProductCode()
	{
		//if(Request::ajax()){
			$m 		= new DesignerModel;
			$data 	= $m->selectTeamStoreProductLastId();

			if($data){
				$id = $data->Id;
			}else{
				$id = 0;
			}
			return $templateCode = date('y') .'-' . str_pad($id + 1, 10, '0',STR_PAD_LEFT);
	}
	
	public function saveDesignDetails(Request $request)
    {	
		$newTeamStoreModel		=   new TeamStoreModel;
		$newDesignerModel		=   new DesignerModel;
		$clientrole = Auth::user()->role;
		$post 					=	$request->all();
		// var_dump($post);
		if($clientrole == "store_owner"){

			if(isset($post['sale_chk'])){
				$designName = $post['designName'];
				$designCode = $post['designCode'];
				$templateCode = $post['templateCode'];
				$itemName = $post['itemName'];
				$itemDescription = $post['itemDescription'];
				$storeId = $post['storeId'];
				$randomChar = str_random(5);
				$producturl = str_slug($itemName .'-'. $randomChar, '-');
				$item_price = str_replace('$ ', '', $post['item_price']);

				$item_details = array(
					'TeamStoreId' 			=> $storeId,
					'ProductCode' 			=> $this->getProductCode(),
					'ProductName' 			=> $itemName,
					'ProductPrice' 			=> $item_price,
					'ProductDescription' 	=> $itemDescription,
					'ProductURL'			=> $producturl,
					'ProductForm' 			=> 'jersey-form',
					'PrivacyStatus' 		=> 'private'
				);

				$newDesignerModel->updateClientDesign($designName, $designCode);
				
				$res 		= $newTeamStoreModel->insertTeamStoreProduct($item_details);
				if($res['i']){
					$templatepaths_array = $newDesignerModel->selectTemplatePathsByTemplateCode($templateCode);
					
					foreach($templatepaths_array as $row){
						$imageClass = null;

						if($row->Side == 'Front'){
							$imageClass = 'active';
						}

						$productId = $res['lastId'];
						$thumbnail = $designCode . '-' . strtolower($row->Side) . '-' . 'thumbnail.png';
						
						$thumbnail_array[] = array(
							'ProductId' 		=> $productId,
							'Image' 			=> $thumbnail,
							'ImageClass' 		=> $imageClass
						);	
					}
					
					$k = $newTeamStoreModel->insertTeamStoreProductThumbnails($thumbnail_array);
					
					if($k = "true"){
						$i = 1;
					}else{
						$i = 0;
					}
				}

				return $i;
			
			}else{
				$designName = $post['designName'];
				$designCode = $post['designCode'];
				$templateCode = $post['templateCode'];
				$i = $newDesignerModel->updateClientDesign($designName, $designCode);

				return $i;
			}
		}else{
			$designName = $post['designName'];
			$designCode = $post['designCode'];
			$templateCode = $post['templateCode'];
			$i = $newDesignerModel->updateClientDesign($designName, $designCode);

			return $i;
		}
	}
	
	public function saveRoster(Request $request)
    {
		$post =	$request->all();
		$newDesignerModel		=   new DesignerModel;
		$newTeamStoreModel		=   new TeamStoreModel;
		
		// var_dump($post);

		
		$design_code = $post['designCode'];
		$order_names = $post['order_names'];
		$order_number = $post['order_number'];
		$design_name = $post['design_name'];
		
		$order_jersey_size = $post['order_jersey_size'];
		$order_shorts_size = $post['order_shorts_size'];


		foreach($order_names as $key => $val){
			
			if($order_jersey_size[$key] != "none" && $order_shorts_size[$key] != "none"){
				$order = "Both";
				$array_default_price_jersey = $newDesignerModel->getDefaultPrice($order_jersey_size[$key], 'JERSEY');
				$array_default_price_shorts = $newDesignerModel->getDefaultPrice($order_shorts_size[$key], 'SHORTS');
				$price = $array_default_price_jersey[0]->Price + $array_default_price_shorts[0]->Price;
				//var_dump($price);
			
			}else if($order_jersey_size[$key] == "none" && $order_shorts_size[$key] != "none"){
				$order = "Shorts";
				$array_default_price_jersey = 0;
				$array_default_price_shorts = $newDesignerModel->getDefaultPrice($order_shorts_size[$key], 'SHORTS');
				$price = $array_default_price_jersey + $array_default_price_shorts[0]->Price;
				//var_dump($price);
			
			}else if($order_jersey_size[$key] != "none" && $order_shorts_size[$key] == "none"){
				$order = "Jersey";
				$array_default_price_jersey = $newDesignerModel->getDefaultPrice($order_jersey_size[$key], 'JERSEY');
				$array_default_price_shorts = 0;
				$price = $array_default_price_jersey[0]->Price + $array_default_price_shorts;
				// var_dump($price);
			}

			if($request->session()->has('cartkey')){
				$cartKey = $request->session()->get('cartkey');
			}else{
			   $request->session()->put('cartkey', sha1(time() . str_random(6)));
			   $cartKey = $cartKey = $request->session()->get('cartkey');
			}

			$items[] = array(
				'Order'			=> $order,
				'CartKey' 	 	=> $cartKey,
				'DesignCode'    => $design_code,
				'ProductName'	=> $design_name,
				'Name' 		 	=> $order_names[$key],
				'Number'     	=> $order_number[$key],
				'JerseySize'    => $order_jersey_size[$key],
				'ShortsSize'    => $order_shorts_size[$key],
				'Price'			=> $price,
				'Quantity'		=> 1
			);

		}

		$i 	= 	$newTeamStoreModel->insertToCart($items);
		return redirect('cart');
		
	}

	public function editDesign($designcode)
	{	
		$newDesignerModel		=   new DesignerModel;
		
		$client_design_array	=   $newDesignerModel->selectClientDesign($designcode);
		$template_arrays      	=   $newDesignerModel->selectTemplate(md5($client_design_array[0]->TemplateCode));
		// var_dump($template_arrays);
		$patterns = explode(",", $template_arrays[0]->PatternId);
		
		foreach($patterns as $patternId){
			$pattern_arrays[]      	=   $newDesignerModel->selectPatterns($patternId);
		}

		$templatepaths_arrays      	=   $newDesignerModel->selectTemplatePaths(md5($client_design_array[0]->TemplateCode));
		$fonts_array      			=   $newDesignerModel->selectFonts();

		// var_dump($client_design_array);

		return view('designer.designer_edit')
										->with('client_design_array', $client_design_array)
										->with('template_arrays', $template_arrays)
										->with('templatepaths_arrays', $templatepaths_arrays)
										->with('pattern_arrays', $pattern_arrays)
										->with('fonts_array', $fonts_array);
	}

	public function editGradientAppend(Request $request)
	{

		$post 	=	$request->all();

		$gradientFor = $post['gradientFor'];
		$gradientPrefix = $post['gradientPrefix'];
		$TrimCcount = $post['trimCount'];

		$stop_color_1 = $post['stop_color_1'];
		$stop_color_2 = $post['stop_color_2'];
		$offset_1 = $post['offset_1'];
		$offset_2 = $post['offset_2'];
		
		if($gradientFor == "Body"){
			?>
				<linearGradient id="<?php echo $gradientPrefix . 'Body_Gradient' ; ?>" gradientUnits="userSpaceOnUse" x1="0%" y1="100%" x2="0%" y2="0%"> 
					<stop id="<?php echo $gradientPrefix . 'Body_Gradient_Color2' ; ?>" offset="<?php echo $offset_2 ?>" stop-color="<?php echo $stop_color_2 ?>" />
					<stop id="<?php echo $gradientPrefix . 'Body_Gradient_Color1' ; ?>" offset="<?php echo $offset_1 ?>" stop-color="<?php echo $stop_color_1 ?>" />
				</linearGradient>
			<?php
		}
		if($gradientFor == "Body_Pattern"){
			?>
				<linearGradient id="<?php echo $gradientPrefix . 'Body_Pattern_Gradient' ; ?>" gradientUnits="userSpaceOnUse" x1="0%" y1="100%" x2="0%" y2="0%"> 
					<stop id="<?php echo $gradientPrefix . 'Body_Pattern_Gradient_Color2' ; ?>" offset="<?php echo $offset_2 ?>" stop-color="<?php echo $stop_color_2 ?>" />
					<stop id="<?php echo $gradientPrefix . 'Body_Pattern_Gradient_Color1' ; ?>" offset="<?php echo $offset_1 ?>" stop-color="<?php echo $stop_color_1 ?>" />
				</linearGradient>
				
			<?php
		}
		
		if($gradientFor == "Trim"){
			?>
			<linearGradient id="<?php echo $gradientPrefix . 'Trim_'.$TrimCcount.'_Gradient' ; ?>" gradientUnits="userSpaceOnUse" x1="0%" y1="100%" x2="0%" y2="0%"> 
				<stop id="<?php echo $gradientPrefix . 'Trim_'.$TrimCcount.'_Gradient_Color2' ; ?>" offset="<?php echo $offset_2 ?>" stop-color="<?php echo $stop_color_2 ?>" />
				<stop id="<?php echo $gradientPrefix . 'Trim_'.$TrimCcount.'_Gradient_Color1' ; ?>" offset="<?php echo $offset_1 ?>" stop-color="<?php echo $stop_color_1 ?>" />
			</linearGradient>
			<?php
		}
		if($gradientFor == "Trim_Pattern"){
			?>
			<linearGradient id="<?php echo $gradientPrefix . 'Trim_'.$TrimCcount.'_Pattern_Gradient' ; ?>" gradientUnits="userSpaceOnUse" x1="0%" y1="100%" x2="0%" y2="0%"> 
				<stop id="<?php echo $gradientPrefix . 'Trim_'.$TrimCcount.'_Pattern_Gradient_Color2' ; ?>" offset="<?php echo $offset_2 ?>" stop-color="<?php echo $stop_color_2 ?>" />
				<stop id="<?php echo $gradientPrefix . 'Trim_'.$TrimCcount.'_Pattern_Gradient_Color1' ; ?>" offset="<?php echo $offset_1 ?>" stop-color="<?php echo $stop_color_1 ?>" />
			</linearGradient>
			<?php
		}
	}

	public function editPatternProperties(Request $request)
	{
		$newDesignerModel		=   new DesignerModel;
		$post 	=	$request->all();
		$patternId = $post['patternId'];
		
		$pattern_array	=   $newDesignerModel->selectPatterns($patternId);
		
		foreach($pattern_array as $row){
			$small =    $row->SVGPath;
			$large =    $row->SVGPath_Actual;
		}

		$arr = array(
			'small' => config('site_config.uploads') . $small ,
			'large' => config('site_config.uploads') . $large
		);
		
		return json_encode($arr);

		// var_dump($arr);
	}

	public function editSetPattern(Request $request)
	{
		$newDesignerModel		=   new DesignerModel;

		$post 	=	$request->all();
		$patternId = $post['patternId'];
        $patternType = $post['patternType'];
        $patternFor = $post['patternFor'];
        $pcolors = $post['pcolors'];
        $gradientColor = $post['gradientColor'];
        

		if($patternType == "Custom"){
			$table = "client_patterns";
		}else{
			$table = "patterns";
        }
        
        foreach($gradientColor as $p){
            foreach($p as $key => $val){
                ${$key} =  $val;
            }
        }

		$pattern_array	=   $newDesignerModel->selectPatternsByTable($table, $patternId);

		foreach($pattern_array as $row){
			$Opacity = $row->Opacity;
			$Gradient = $row->Gradient;
			if($row->NoOFColor != 0){
                
				$NoOFColor = $row->NoOFColor;
				echo '<div class="form-group col-md-12 col-sm-12 col-xs-12">';
				echo 'Pattern Color';
				echo '</div>';
				echo '<div class="form-group col-md-12 col-sm-12 col-xs-12">';
                
                foreach($pcolors as $p){
                    foreach($p as $key => $val){
                        ?>
                            <div class="btn-group">
								<input type="text" class="patternColor" id="<?php echo $key ?>" value="<?php echo $val ?>" />
							</div>
                        <?php
                    }
                }
                 
              
                if($Gradient == "TRUE"){
                    ?>
                        <button type="button" style="<?php echo ($patternFor == 'Gradient') ? 'background: linear-gradient('.$gradientColor1.','.$gradientColor2.')' : '' ?>" class="btn" id="btn-body-pattern-gradient"><?php echo ($patternFor == 'Gradient') ? '<i class="fa fa-2 fa-check" aria-hidden="true"></i>' : ''  ?></button>
                    <?php
                }
				echo '</div>';
				?>
				
                <div class="form-group col-md-12 col-sm-12 col-xs-12">
					<div id="row-body-pattern-gradient" style="display:<?php echo ($patternFor == 'Gradient') ? 'block' : 'none'  ?>;">
						<div class="row">						
							<div class="form-group col-md-12 col-sm-12 col-xs-12">
								Gradient Colors
							</div>
						</div>
						<div class="row">						
							<div class="form-group col-md-2 col-sm-2 col-xs-2">
								<input type="hidden" class="patternGradientColor pull-right" data-gradient-type="Body" name="Body_Pattern_Gradient_Color1" id="Body_Pattern_Gradient_Color1" value="<?php echo ($patternFor == 'Gradient') ?  $gradientColor1 : 'rgb(0, 0, 0)'  ?>" />
							</div>
							<div class="form-group col-md-10 col-sm-10 col-xs-10">
								<input type="range" id="Offset_Body_Pattern_Gradient_Color1" data-gradient-id="Body_Pattern_Gradient_Color1" class="offsetGradientPattern form-control input-sm" value="<?php echo ($patternFor == 'Gradient') ? $gradientColorOffset1 / 10  : '100'  ?>" min="0" max="10" />	
							</div>
						</div>
                        <!-- gradientColorOffset1 -->
						<div class="row">
							<div class="form-group col-md-2 col-sm-2 col-xs-2">
								<input type="hidden" class="patternGradientColor pull-right" data-gradient-type="Body" name="Body_Pattern_Gradient_Color2" id="Body_Pattern_Gradient_Color2" value="<?php echo ($patternFor == 'Gradient') ?  $gradientColor2 : 'rgb(255, 255, 255)'  ?>" />
							</div>
							<div class="form-group col-md-10 col-sm-10 col-xs-10">
								<input type="range" id="Offset_Body_Pattern_Gradient_Color2" data-gradient-id="Body_Pattern_Gradient_Color2" class="offsetGradientPattern form-control input-sm" value="<?php echo ($patternFor == 'Gradient') ? $gradientColorOffset2 / 10  : '0'  ?>" min="0" max="10" />	
							</div>
						</div>
					</div>
				</div>
				
				<?php if($Opacity == "TRUE"){ ?>
				<div class="row">
					<div class="col-md-12 col-sm-12 col-xs-12">
						<label>Opacity</label>
					</div>
				</div>
				<div class="row">
					<div class="form-group col-md-9 col-sm-9 col-xs-9">
						<input type="range" id="opacityPattern" class="form-control input-sm" value="10" min="0" max="10"  />
					</div>
					<div class="form-group col-md-3 col-sm-3 col-xs-3">
						<input type="text" style="margin-top:5px;" class="form-control input-sm  text-center" value="10" id="opacityPatternValue" />
					</div>
				</div>
				<?php }
			}else{
				/// no display..
				$NoOFColor = 0;
			}

		}
		
		?>
			<input type="hidden"  name="body-pattern-name" id="body-pattern-name" value="<?php echo $patternId . "," . $NoOFColor; ?>">
		<?php
		
	}
	
	public function buyForm(Request $request){
		$post 	=	$request->all();
		// echo $post['temp_code'];
		$newDesignerModel		=   new DesignerModel;
		$jersey_sizes	=   $newDesignerModel->getAvailableSizes($post['temp_code'], 'JERSEY');
		$shorts_sizes	=   $newDesignerModel->getAvailableSizes($post['temp_code'], 'SHORTS');
		return view("designer.buy_form")->with('jersey_sizes', $jersey_sizes)
										->with('shorts_sizes', $shorts_sizes);
	}

	public function saveBase64Images(Request $request) {
		$post 	=	$request->all();
		@list($type, $file_data) = explode(';', $post['base64_image']);
		$extension = explode('/', mime_content_type($post['base64_image']))[1];
    @list(, $file_data) = explode(',', $file_data); 
		$imageName = $post['side'] . '_' . $post['image_count'] . '_' . microtime(true) . '_' . Auth::user()->id . '.'.$extension;
		
		$filesystem = Storage::disk('sftp');
		$filesystem->getDriver()->getAdapter()->setDirectoryPerm(0755);
		$filesystem->put('/merchbay/designer-images/' . $imageName, base64_decode($file_data));
    // Storage::disk('uploads_local')->put($imageName, base64_decode($file_data));	
		return response()->json(array(
			'success' => true,
			'url' => config('site_config.uploads') . 'designer-images/'  . $imageName
		));
	}
}