<?php
include ("db.php");
require("library/fpdf.php");

class PDF extends FPDF
{
/* Page header */
function Header()
{

    $this->SetFont('Arial','I',30);
    /* Move to the right */
    $this->Cell(105);
    $connect = traffic_db_connect();
    $start = $_POST['start'] ?? '';
    $end = $_POST['end'] ?? '';
    $car = $_POST['car'] ?? '';
    $result = mysqli_query($connect, "SELECT * FROM trips WHERE car='$car' AND start_mileage BETWEEN '$start' and '$end' ORDER BY id DESC ");
    $get = mysqli_fetch_array($result);
    $this->Cell(130,20, '',0,0,'C');
    $this->Cell(90,20, $get['car'] ?? $car,0,0,'C');

    $this->Ln(20);
}
/* Page footer */
function Footer()
{
    /* Position at 1.5 cm from bottom */
    $this->SetY(-15);
    /* Arial italic 8 */
    $this->SetFont('Arial','I',8);
    /* Page number */
    $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}

/* Instanciation of inherited class */
$pdf = new PDF('L', 'mm', array(570,350));
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','B',13);


$pdf->cell(30, 10, "Date", 1, 0, 'C');
$pdf->cell(50, 10, "Driver", 1, 0, 'C');
$pdf->cell(47, 10, "Client", 1, 0, 'C');
$pdf->cell(135, 10, "Description", 1, 0, 'C');
$pdf->cell(23, 10, "Start-Time", 1, 0, 'C');
$pdf->cell(23, 10, "End-Time", 1, 0, 'C');
$pdf->cell(24, 10, "Total-Time", 1, 0, 'C');
$pdf->cell(30, 10, "Start Mileage", 1, 0, 'C');
$pdf->cell(30, 10, "End Mileage", 1, 0, 'C');
$pdf->cell(25, 10, "Fueling", 1, 0, 'C');
$pdf->cell(25, 10, "Mileage", 1, 0, 'C');
$pdf->cell(25, 10, "Amount $", 1, 0, 'C');
$pdf->cell(25, 10, "Literage", 1, 0, 'C');
$pdf->cell(25, 10, "Cost/trip $", 1, 0, 'C');
$pdf->cell(38, 10, "Expense A/c", 1, 1, 'C');

$pdf->SetFont('Arial', '', 11);
$connect = traffic_db_connect();
$start = $_POST['start'] ?? '';
$end = $_POST['end'] ?? '';
$car = $_POST['car'] ?? '';
$result = mysqli_query($connect, "SELECT * FROM trips WHERE car='$car'  AND start_mileage BETWEEN '$start' and '$end' ORDER BY start_mileage ASC  ");
$result2 = mysqli_query($connect, "SELECT * FROM trips WHERE car='$car'  AND start_mileage BETWEEN '$start' and '$end' ORDER BY start_mileage ASC  ");
$distance = array();
while($row = mysqli_fetch_array($result)){
  $distance[] = (float)$row['end_mileage'] - (float)$row['start_mileage'];
}
$totalDistance = array_sum($distance);
$cost = 0;
while($row = mysqli_fetch_array($result2)){
  $mileage = (float)$row['end_mileage'] - (float)$row['start_mileage'];
$pdf ->SetTextColor(0,0,0);
$pdf ->SetFont("Arial","",12);
$pdf->cell(30,10, $row['date'], 1, 0, 'C');
$pdf->cell(50,10, $row['username'], 1, 0, 'C');
$pdf->cell(47,10, $row['client'], 1, 0, 'C');
$pdf->cell(135,10, $row['description'], 1, 0, 'C');
$pdf->cell(23,10, $row['start_time'] , 1, 0, 'C');
$pdf->cell(23,10, $row['end_time2'], 1, 0, 'C');
ob_start();
$dt = (strtotime($row['end_time']) - strtotime($row['start_time']))/60 + (strtotime($row['end_time2']) - strtotime($row['start_time2']))/60;
echo sprintf("%d : %02d ", floor($dt/60), $dt%60);
$time = ob_get_clean();
 $pdf->cell(24,10,$time ,1,0,'C');
$pdf->cell(30,10, $row['start_mileage'], 1, 0, 'C');
$pdf->cell(30,10, $row['end_mileage'], 1, 0, 'C');
$pdf->cell(25,10, $row['station_mileage'], 1, 0, 'C');
$pdf -> SetTextColor(255,0,0);
$pdf -> SetFont("Arial","B",12);
$pdf->cell(25,10, $mileage , 1, 0, 'C');
$total_cost = mysqli_query($connect, "SELECT DISTINCT amount from trips WHERE car='$car' AND start_mileage BETWEEN '$start' and '$end' AND amount > 0");
$get = mysqli_fetch_array($total_cost);
$pdf->cell(25,10, $row['amount'], 1, 0, 'C');
$cost = isset($get['amount']) && is_numeric($get['amount']) ? (float)$get['amount'] : 0;
$cost_per_trip = $totalDistance > 0 ? ($cost*$mileage/$totalDistance) : 0;
ob_start();
echo number_format((float)$cost_per_trip,2,'.','');
$costs = ob_get_clean();

$pdf->cell(25,10, $row['litres'], 1, 0, 'C');
$pdf->cell(25,10, $costs, 1, 0, 'C');
$pdf->SetFont("Arial", "",8);
$pdf->cell(38,10, $row['expense_account'], 1, 1, 'C');
}
$pdf ->SetTextColor(0,0,0);
$pdf -> SetFont("Arial","B",20);
$pdf->cell(23,10, '', 0, 0, 'C');
$pdf->cell(23,10, '', 0, 0, 'C');
$pdf->cell(23,10, '', 0, 0, 'C');
$pdf->cell(23,10, '', 0, 0, 'C');
$pdf->cell(30,10, '', 0, 0, 'C');
$pdf->cell(30,10, '', 0, 0, 'C');
$pdf->cell(110,10, '', 0, 0, 'C');
$pdf->cell(152,10, '', 0, 0, 'C');
ob_start();
print_r($totalDistance);
$tot = ob_get_clean();
$pdf->cell(33,10, $tot, 0, 0, 'C');
$pdf->cell(25,10, '', 0, 0, 'C');
$pdf->cell(18,10, '', 0, 0, 'C');
ob_start();
$result3 = mysqli_query($connect, "SELECT DISTINCT litres FROM trips WHERE car='$car'  AND start_mileage BETWEEN '$start' and '$end' ORDER BY start_mileage ASC");
while($row2 = mysqli_fetch_array($result3)){
$literage = $row2['litres'];
$litresValue = is_numeric($row2['litres']) ? (float)$row2['litres'] : 0;
$get2 = $litresValue > 0 ? $totalDistance/$litresValue : 0;
$total_l = number_format($get2,2,'.','');
// echo $total_l;
  $litres = ob_get_clean();

}


ob_start();
$tot_cost = array();
$result3 = mysqli_query($connect, "SELECT * FROM trips WHERE car='$car'  AND start_mileage BETWEEN '$start' and '$end' ORDER BY start_mileage ASC  ");
while($tot = mysqli_fetch_array($result3)){
$tot_cost[] = $totalDistance > 0 ? ((float)$tot['end_mileage'] - (float)$tot['start_mileage'])/$totalDistance*$cost : 0;
}
$sum_ = array_sum($tot_cost);
$tott = number_format($sum_,2,'.','');
echo $tott;
$totcost2 = ob_get_clean();
$pdf->cell(30,10, $totcost2, 0, 0, 'C');

$pdf->Output();
?>
