<?php
require('../fpdf186/fpdf.php');


require_once '../models/db_connection.php';
require_once '../models/fee_model.php';




$fee = new Fee($conn);


// var_dump($fee->getAllPayments());
// die();
class PDF extends FPDF
{
    // Page header
    function Header()
    {
        // Logo and company name
        $this->SetFont('Arial', 'B', 14);
        $this->Cell(0, 10, 'MF LAKE DRIVING SCHOOL ', 0, 1, 'C');
        $this->SetFont('Arial', 'B', 12);
        $this->Cell(0, 10, 'Payment Report From ' . $_GET['from'] . '  To ' . $_GET['to'], 0, 1, 'C');

        // Report date
        $this->SetFont('Arial', '', 10);
        $this->Cell(0, 10, 'Report Date: ' . date('Y-m-d'), 0, 1, 'C');

        // Line break
        $this->Ln(10);
    }

    // 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');
    }

    // Load data
    function LoadData($file)
    {
        // Read file lines
        $lines = file($file);
        $data = array();
        foreach ($lines as $line)
            $data[] = explode(',', trim($line));
        return $data;
    }

    // Table
    function ImprovedTable($header, $data)
    {
        // Column widths
        $w = array(10, 15, 60, 40, 25, 25, 23);
        // Header
        for ($i = 0; $i < count($header); $i++)
            $this->Cell($w[$i], 7, $header[$i], 1, 0, 'C');
        $this->Ln();
        // Data
        $x = 0;
        $totalPayment = 0;
        $remaining = 0;
        foreach ($data as $row) {
            $totalPayment += $row['total_paid'];
            $remaining += $row['remaining_balance'];
            $this->Cell($w[0], 6, ++$x, 'LR');
            $this->Cell($w[1], 6, $row['en_id'], 'LR');
            $this->Cell($w[2], 6, $row['fullname'], 'LR');
            $this->Cell($w[3], 6, $row['phone_number'], 'LR', 0, 'R');
            $this->Cell($w[4], 6, $row['total_paid'], 'LR', 0, 'R');
            $this->Cell($w[5], 6, $row['remaining_balance'], 'LR', 0, 'R');
            $this->Cell($w[6], 6, $row['payment_date'], 'LR', 0, 'R');
            $this->Ln();
        }
        // Closing line
        $this->Cell(array_sum($w), 0, '', 'T');
        $this->Cell(-50, 10, 'Total Payment: ' . number_format($totalPayment), 0, 1, 'C');
        $this->Cell(array_sum($w), 0, '', 'T');
        $this->Cell(-50, 10, 'Remaining Payment: ' . number_format($remaining), 0, 1, 'C');
    }
}

$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial', '', 10);


// Table data
$header = array('#', 'ID', 'Fullname', 'Phone', 'Amount','Remaining', 'Payment Date');
$data = array(
    array(1, 'YTG0V2', 'My student', '300,000', '220,000', '80,000'),
    array(2, 'ZR1M9V', 'Marry Masubwa', '300,000', '100,000', '200,000')
);
if (isset($_GET['from']) && $_GET['from'] != '') {
    $data =  $fee->getAllPaymentsDate($_GET['from'], $_GET['to']);
} else {
    $data =  $fee->getAllPayments();
}




// Print table
$pdf->ImprovedTable($header, $data);

// Output PDF
$pdf->Output();
