<?php
$ver = '3.1';
/* -------------------------------------------------------------------
| last update 09/12/2022
| ----------------------------------------------------------------- */
include_once('includes/config.inc.php');
include_once(SITE_ROOT.'includes/functions.inc.php');
//$DEBUG=1;
		$_SESSION['DEBUG_MESSAGE'] .= '<br>Details Version: '.$ver;
		
$referer = $_SERVER['HTTP_REFERER'];
 $pageTitle = SITE_NAME;

// clean the inputs
$id = mysqli_real_escape_string($connection,$_GET['id']);
		$_SESSION['DEBUG_MESSAGE'] .= '<br>Stock Details ID: '.$id;

include_once(SITE_ROOT.'includes/header.inc.php');
?>
<div class="container">
	<div class="row">
		<div class="col-md-12">
			<h1><?=$pageTitle?></h1>
		</div>
	</div>
<?php
if (empty($id)) {
?>
	<div class="row">
		<div class="col-md-12">
			 <p>No stock item specified in your query!
			   <br />Please return to the <a href="<?=$referer; ?>">previous page</a></p>
		</div>
	</div>
<?php
}
 else {
       $query = "SELECT *	   
				 FROM ".STOCK_TABLE." AS s
	             WHERE s.id = '".$id."' 
				 AND disabled='0' 
				 LIMIT 1 ";

			$_SESSION['DEBUG_MESSAGE'] .= "<br />ID: ".$id;

            if (!($result = mysqli_query($connection,$query)))
                show_mysqli_error('details');

				$row = mysqli_fetch_array($result,MYSQLI_ASSOC);

// Create a heading
$heading = "";
if (!(empty($row['make']))) {
	$heading .= " ".htmlentities($row['make'],ENT_QUOTES,"ISO-8859-1"); 
}
if (!(empty($row['model']))) {
	$heading .= " ".htmlentities($row['model'],ENT_QUOTES,"ISO-8859-1");
}
	$price = ((empty($row['price'])) ? "Call for details"  :  "&pound;".number_format($row['price']));
		$_SESSION['DEBUG_MESSAGE'] .= "<br />Price: ".$price;

	$sales_type = '<span style="color:#c00;">'.ucwords($row['sales']).'!</span>';
		$_SESSION['DEBUG_MESSAGE'] .= "<br />Sales Type: ".$sales_type;

	$description = $row['text'];

	$features = "";
if (!(empty($row['title']))) {
		$features .= "		<li>".htmlentities(stripslashes($row['title']),ENT_QUOTES)."</li>\n";
}
if (!(empty($row['feature'])))  {
		$features .= "		<li>".htmlentities(stripslashes($row['feature']),ENT_QUOTES)."</li>\n";
}
if (!(empty($row['bhp'])) ) {
		$features .= "		<li>".htmlentities(stripslashes($row['bhp']),ENT_QUOTES)."HP</li>\n";
}
$tyres = "";
	if (!(empty($row['tyre_size'])) ) 
		$tyres .= "		<li>Tyre Size: ".htmlentities(stripslashes($row['tyre_size']),ENT_QUOTES)."</li>\n";

$details = "";
// add these to used sales records
if (!(empty($row['cat_no']))) {
		$details .= "		<li>Ref: ".htmlentities(stripslashes($row['cat_no']),ENT_QUOTES)."</li>\n";
}
if (!(empty($row['serial']))) {
		$details .= "		<li>Serial No: ".htmlentities(stripslashes($row['serial']),ENT_QUOTES)."</li>\n";
}
if (preg_match('/Used/i',$row['sales'])) { 
 
	if (!(empty($row['reg']))) {
		$details .= "		<li>Year/Reg: ".htmlentities(stripslashes($row['reg']),ENT_QUOTES)."</li>\n";
	}
// Add this to tyres attrib
	if (!(empty($row['tyre_cond']))) {
		$tyres .= "		<li>Tyre Condition: ".htmlentities(stripslashes($row['tyre_cond']),ENT_QUOTES)."</li>\n";
	}
// Add this to features list
	if (!(empty($row['hours']))) {
		$features .= "		<li>".$row['hours']." Recorded Hours</li>\n";
	}
}
// Add this to details attrib
if (!(empty($details))) {
		$details = "<p><b>Details</b></p>\n<ul>".$details.$tyres."</ul>";
}
if (!(empty($features))) {
		$features = "<p><b>Main Features</b></p><ul>\n".$features."</ul>\n";
}
$details = $features.$details;
	if (mysqli_num_rows($result) < 1) {
		$sales_type = "";
		$heading = "";
		$price = "";
		$details = "<p>Sorry no record matching that id found!</p>";
	}
	$image_src = ( (!empty($row['image'])) ? $row['image'] : "default.gif" );
	$imagefile = '<img src="'.IMAGE_ROOT.'/assets/uploads/'.$image_src.'" data-large-src="'.IMAGE_ROOT.'/assets/copies/'.$image_src.'"alt="'.$heading.'">';

		// create a remote session in case the use clicks enquire about this item
		$_SESSION['remote_enquiry'] = "Enquiry about: Ref: ".$row['cat_no'].' '.$heading;
		$_SESSION['DEBUG_MESSAGE'] .= '<br>Enquiry Session Value:'.$_SESSION['enquiry'];
?>
<!-- images-->
	<div class="row">
	    <div class="col-md-7">
		   <div id="pgwSlideshow">
				<ul class="pgwSlideshow">			
<?php
if ($row['image']) {
			print ('<li>'.$imagefile.'</li>'."\n");
}
			$image_set = array();
			 $image_title = array();
			  get_thumbnail_images(mysqli_real_escape_string($connection,$_GET['id']));

				for($n=0;$n<count($image_set);$n++)  {
					print('<li><img src="'.IMAGE_ROOT.'/assets/uploads/'.$image_set[$n].'" data-large-src="'.IMAGE_ROOT.'/assets/copies/'.$image_set[$n].'" alt="'.$image_title[$n].'"></li>'."\n");
					
					$_SESSION['DEBUG_MESSAGE'] .= '<br>Thumb: '.$n.' '.$image_title[$n];
				}
?>
				</ul>
			</div>
<?php  if ($row['rep_images'])  {  ?>
	<p style="text-align:center;"><em>Images are representative</em></p>
<?php } ?>	
		</div>
		<div class="col-md-5">
			<!-- details -->			
				<div id="stock_record">
<?php if ($row['not_in_stock'])  { ?>
				<h3 style="color: red;">Arriving Soon!</h3> 
<?php } ?>
				<p><b><?=$sales_type.$heading; ?></b></p>
				<p class="price">Price: <?=$price; ?></p>
				<?=$details; ?>
				<p><b>Description</b></p>
				<?=$description; ?>			
				<p id="next" style="color: #fff;background-color: #c20101;font-weight: 700;padding: 5px;font-size: 16px;">What to do next</p>				
				<ul>
				 <li><a href="<?=$referer; ?>">Return to previous page</a></li>
				 <li><a href="/contact-us/<?=$row['id']; ?>">Enquire about this sales item</a></li>
				 <li><a href="<?=TERMS_URL; ?>" title="Terms and Conditions">See Terms &amp; Conditions</a></li>
				</ul>	
			</div>
		</div>		
	</div><!-- end row-->	
</div><!-- ends container -->
<?php
} 
include_once(SITE_ROOT.'includes/footer.inc.php');
?>
