<?php
// gets select of jcards/insurance/reposession records in json for autocomplete
if(isset($_REQUEST['schtid'])){
include 'includes/database.php';
$db=new MySQLDatabase();
$db->open_connection();
$sitem = $_REQUEST['schtid'];
$s1 = strpos($sitem,"[");	$s2 = strpos($sitem,"]");
$sans1 = substr($sitem, $s1+1,$s2-$s1-1 ); 
$sans2 = substr($sitem, strpos($sitem,"]-")+2);
$qry = '';							
if($sans1==="JC"){ $qry = "SELECT 'TRACKER' as tp, jcards.jdate, jcards.jcno, jcards.regno, jcards.vehmod, jcards.vcolor, jcards.chasis, jcards.cname, CONCAT(jcards.imeino,' - ',devstock.mobno) as det FROM jcards, devstock WHERE jcards.imeino = devstock.imeino AND jcards.jcno = ".$sans2;
				   $hdate = "Install Date";
				  }
elseif($sans1==="IN"){ $qry = "SELECT 'INSURANCE' as tp, idate as jdate, insno as jcno, regno, vehmod, vcolor, chasis, cname, CONCAT(pno,'-V: ',inam,' -P: ',insprem) as det FROM insrecs WHERE insno = ".$sans2;
					   $hdate = "Insured Date";
					  }
elseif($sans1==="RO"){ $qry = "SELECT 'REPOSESSION' as tp, odate as jdate, repono as jcno, regno, vehmod, vcolor, chasis, cname, auct as det FROM reporders WHERE repono = ".$sans2;
					   $hdate = "Posessed Date";
					  }
$sql = mysqli_query($db->connection, $qry) or die (mysqli_error($db->connection) );
$lout='';
if ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)){
	$lout = $lout. '<table cellspacing="6px"> <th>Section</th> <th>'.$hdate.'</th> <th>Doc. No.</th> <th>Reg. No. </th> <th>Type </th> <th>Chasis No. </th> <th>Colour</th> <th>Customer</th> <th>Details</th>';
	$jdate = substr($row['jdate'],8,2)."-".substr($row['jdate'],5,2)."-".substr($row['jdate'],0,4);
    $lout = $lout.  '<tr><td>'.$row['tp'].'</td> <td>'.$jdate.'</td> <td>'.$row['jcno'].'</td> <td>'.$row['regno'].'</td> <td>'.$row['vehmod'].'</td> <td>'.$row['chasis'].'</td> <td>'.$row['vcolor'].'</td> <td>'.$row['cname'].'</td> <td>'.$row['det'].'</td> </tr></table>';
    }   
$db->close_connection();
echo $lout;
}
?>