<?php
// Include the TCPDF library
require_once('../tcpdf/tcpdf.php');
include './conn.php';

// Set title and heading
$title = 'Lorenzo Group';
$heading = 'My Leaves';

session_start();
if ($_SESSION['sid'] == session_id() && $_SESSION['user'] == "Staff") {
    $staff_id = $_SESSION['staff_id'];

    $sql1 = "SELECT * FROM lms.staff WHERE staff_id = '" . $staff_id . "'";
    $sql2 = "SELECT * FROM lms.leave_requests WHERE staff_id = '" . $staff_id . "'";

    $result1 = mysqli_query($conn, $sql1);
    $result2 = mysqli_query($conn, $sql2);

    // Create a new TCPDF instance
    $pdf = new TCPDF();

    // Set document information
    $pdf->SetCreator('Lorenzo');
    $pdf->SetAuthor('Lorenzo');
    $pdf->SetTitle($title); // Set title
    $pdf->SetSubject($heading); // Set heading
    $pdf->SetKeywords('Staff, Leave, Report');

    // Add a page
    $pdf->AddPage();

    // Set font for the heading
    $pdf->SetFont('helvetica', 'B', 16); // Bold, 16pt font

    // Add heading to the PDF
    $pdf->Cell(0, 10, $heading, 0, 1, 'C'); // Centered heading

    // Set font for the table headers
    $pdf->SetFont('helvetica', 'B', 12); // Bold, 11pt font

    // Get current date
    $currentDate = date('Y-m-d');

    // Add date to the top right corner
    $pdf->Cell(0, 10, 'Date: ' . $currentDate, 0, 1, 'R'); // Right-aligned

    // Set font for the table content
    $pdf->SetFont('helvetica', '', 12); // Regular, 10pt font

    // Initialize a variable to store names
// Initialize variables to store names and IDs with labels
$names = 'Name:<br>';
$id = 'ID:<br>';

// Loop through query results and concatenate names and IDs
while ($row = mysqli_fetch_array($result1)) {
    $names .= $row['first_name'] . ' ' . $row['middle_name'] . ' ' . $row['last_name'] . '<br>';
    $id .= $row['staff_id'] . '<br>'; // Assuming staff_id is fetched from the database
}

// Output the concatenated names and IDs with labels
$pdf->writeHTML($names, true, false, true, false, '');
$pdf->writeHTML($id, true, false, true, false, '');

// Add a table to the PDF
$html = "<table>
            <tr>
           
                <th width='180px'><b>Leave Type</b></th>
                <th width='180px'><b>Start</b></th>
                <th width='180px'><b>End</b></th>
                <th width='80px'><b>Days</b></th>
                <th width='180px'><b>Leave Status</b></th>
                <th width='180px'><b>Reason</b></th>


            </tr>";

// Loop through query results and add rows to the table
while ($row = mysqli_fetch_array($result2)) {
    $html .= "<tr>
                <td width='180px'>" . $row['leave_type'] . "</td>
                <td width='180px'>" . $row['start_date'] . "</td>
                <td width='80px'>" . $row['end_date'] . "</td>
                <td width='180px'>" . $row['days_requested'] . "</td>
                <td width='180px'>" . $row['leave_status'] . "</td>
                <td width='180px'>" . $row['reason'] . "</td>

            </tr>";
}

$html .= "</table>";
$pdf->writeHTML($html, true, false, true, false, '');



    // Close and output PDF
    $pdf->Output('my leaves' . date('Y-m-d') . '.pdf', 'D');

    // Close the database connection
    mysqli_close($conn);
}
?>
