<?php include_once('database_connection.php');


if(isset($_GET['action']))
{


if($_GET['action']=="ListVehicles")
{
$data=array();
$sql="select row_number() over(order by [VehicleID]) as row_num, vm.*,mv.MasterValue as Make,vm.Make as VehicleBrandID
,mv1.MasterValue as Model,vm.model as VehicleModelID,mv2.MasterValue as VIN,vm.VIN as VehicleTypeID,mv3.MasterValue as color,
vm.Color as VehicleColorID,mv4.MasterValue as vehicleyear
   ,vm.yr as VehicleYearID,'' as col_Action from RAC_VehicleMaster as vm
left outer join Master_Values as mv  on mv.MasterValueID=convert(bigint,vm.make)
left outer join Master_Values as mv1  on mv1.MasterValueID=convert(bigint,vm.model)
 left outer join Master_Values as mv2  on mv2.MasterValueID=convert(bigint,vm.vin )
 left outer join Master_Values as mv3  on mv3.MasterValueID=convert(bigint,vm.Color)
 left outer join Master_Values as mv4 on mv4.MasterValueID=convert(bigint,vm.yr)    
 where vm.Active=1

 ";
$result=odbc_exec($connection,$sql);
		while($row=odbc_fetch_array($result))
		{

				$data[]=$row;
		}
		echo json_encode($data);
}

if($_GET['action']=="InsertVehicle")
{
//print_r($_POST);

$CustomerType=$_POST['CustomerType'];
    $Supplier_SupplierID=$_POST['Supplier_SupplierID'];
    $Mobile=$_POST['Mobile'];
    $customer_id=$_POST['customer_id'];
    $CustomerName=$_POST['CustomerName'];
    $Email=$_POST['Email'];
    $TRN=$_POST['TRN'];
    $PlateNo=$_POST['PlateNo'];
    $ChassisNo=$_POST['ChassisNo'];
    $KMRun=$_POST['KMRun'];
    $EntryDate=$_POST['EntryDate'];
    $ard=explode("-", $EntryDate);
    
    $EntryDate=$ard[2]."-".$ard[1]."-".$ard[0];

$VehicleID=getMaxId("RAC_VehicleMaster","VehicleID",$connection)+1;

    $VehicleBrand=$_POST['VehicleBrand'];
    $VehicleModel=$_POST['VehicleModel'];
    $VehicleType=$_POST['VehicleType'];
    $VehicleColor=$_POST['VehicleColor'];
    $ManufacturerYear=$_POST['ManufacturerYear'];
    $PreparedBy=$_POST['PreparedBy'];
    $MulkiyaNo=$_POST['MulkiyaNo'];
    $ParkingNo=$_POST['ParkingNo'];
    $KeyNo=$_POST['KeyNo'];
$customer_id=0;
 $sql="INSERT INTO [dbo].[RAC_VehicleMaster]
           ([VehicleID]
           ,[VehicleName]
           ,[RegNo]
           ,[VIN]
           ,[Make]
           ,[Model]
           ,[Yr]
           ,[Color]
           ,[InitiateDate]
           ,[OdoMeter]
           ,[VehicleStatusID]
           ,[Description]
           ,[PictureLocation1]
           ,[PictureLocation2]
           ,[PictureLocation3]
           ,[PictureLocation4]
           ,[Notes]
           ,parkingno , keyno ,mulkiyano ,plateno,chasisno
           	,GLCode,CustomerId,Active
       ) values($VehicleID,'$CustomerName','$TRN','$VehicleBrand','$VehicleModel','$VehicleType','$ManufacturerYear','$VehicleColor','$EntryDate',$KMRun,1,'','','','','','$Email','$ParkingNo','$KeyNo','$MulkiyaNo','$PlateNo','$ChassisNo','$Supplier_SupplierID',$customer_id,1)";

odbc_exec($connection, $sql) or die(odbc_error().$sql);

$res=array('status'=>"ok",'description'=>'Record Saved Sucessfully');
		echo json_encode($res);

 }
}

?>