<?php 
if(defined('TENANT_URL') && !empty(TENANT_URL)) {
    $_REQUEST['tenant'] = TENANT_URL;
}


if(file_exists('../../../'.$_REQUEST['tenant'].'/config.php')){require_once('../../../'.$_REQUEST['tenant'].'/config.php');}
if(file_exists('/config.php')){require_once('/config.php'); }

switch($_REQUEST['FUNCTION']){
default:break;

case "getVehicles":
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `vehicles` ORDER BY `desc` ASC")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();$iv=$stmt->get_result();$stmt->close();
$ret.='
<table class="table table-striped table-bordered table-sm">
<thead class="table-dark">
<tr><th>VEHICLE</th><th>REG</th><th>TAX DUE</th><th>INSURANCE</th><th>MOT DUE</th><th>MILEAGE CLAIMED</th><th></th></tr>
</thead>
<tbody>
';
while($id=mysqli_fetch_array($iv)){
if($id['tax']!=""){$tax=date('d-m-Y',$id['tax']);}else{$tax="Not specified";}
if($id['insurance']!=""){$insurance=date('d-m-Y',$id['insurance']);}else{$insurance="Not specified";}
if($id['mot']!=""){$mot=date('d-m-Y',$id['mot']);}else{$mot="Not specified";}


$ret.='<tr id="row'.$id['id'].'">
<td style="vertical-align: middle;"><i class="fad fa-car fa-2x me-2"></i> '.$id['desc'].'</td>
<td style="vertical-align: middle;"><span class="nplate">'.$id['reg'].'</span></td>
<td style="vertical-align: middle;">'.$tax.'</td>
<td style="vertical-align: middle;">'.$insurance.'</td>
<td style="vertical-align: middle;">'.$mot.'</td>
<td style="vertical-align: middle;">'.$id['mileage'].'</td>
<td style="vertical-align: middle;"><a href="javascript:void(0)" class="btn btn-sm btn-info me-4" onclick="editVehicle(\''.$id['id'].'\')"><i class="fad fa-edit"></i></a><a href="javascript:void(0)"  class="btn btn-sm btn-danger" onclick="confirmRemoveVehicle(\''.$id['id'].'\')"><i class="fad fa-trash"></i></a></td>
</tr>';
}
$ret.='<table>';
echo $ret;
break;

case "createVehicle":
$stmt = $GLOBALS['con']->prepare("INSERT INTO `vehicles` VALUES(
'',
'".$_REQUEST['reg']."',
'".strtotime($_REQUEST['tdate'])."',
'".strtotime($_REQUEST['idate'])."',
'".strtotime($_REQUEST['mot'])."',
'".$_REQUEST['miles']."',
'".$_REQUEST['desc']."',
'".$_REQUEST['engine']."',
'".$_REQUEST['fuel']."'
)")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();$stmt->close();
echo "success";
break;

case "updateVehicle":
$stmt = $GLOBALS['con']->prepare("UPDATE `vehicles` SET
`reg`='".$_REQUEST['reg']."',
`tax`='".strtotime($_REQUEST['tdate'])."',
`insurance`='".strtotime($_REQUEST['idate'])."',
`mot`='".strtotime($_REQUEST['mot'])."',
`mileage`='".$_REQUEST['miles']."',
`desc`='".$_REQUEST['desc']."',
`engine`='".$_REQUEST['engine']."',
`fuel`='".$_REQUEST['fuel']."'
WHERE `id`='".$_REQUEST['id']."'")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();$stmt->close();
echo "success";
break;


case "del":
$stmt = $GLOBALS['con']->prepare("DELETE FROM `vehicles` WHERE `id`='".$_REQUEST['id']."'")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();
echo "s";
break;

}?>