<?php
require('../../fpdf.php');
include "../../root.class.php";


$db = new db_safeguard();
$jobcard_res = $db->query("jobcards", "SELECT * FROM jobcards WHERE record_id = {$_GET['record_id']}");
$jobcard = $jobcard_res->fetch_assoc();

function get_username($user_id)
{
    $db = new db_safeguard();
    $res = $db->query("users", "SELECT username FROM users WHERE record_id = $user_id");
    return $res->fetch_assoc()['username'];
}

function get_team($team_id)
{
    $db = new db_safeguard();
    $res = $db->query("teams", "SELECT name FROM teams WHERE record_id = $team_id");
    return $res->fetch_assoc()['name'];
}


function get_client($client_id)
{
    $db = new db_safeguard();
    $res = $db->query("users", "SELECT `name` FROM clients WHERE record_id = $client_id");
    return $res->fetch_assoc()['name'];
}

function addSafeImage($pdf, $baseDir, $imageFile)
{
    // Ensure the image name is not empty
    if (empty($imageFile)) {
        $pdf->SetFont('Arial', 'I', 10);
        $pdf->Cell(100, 10, 'No image provided', 0, 1, 'L');
        return;
    }

    $imagePath = $baseDir . $imageFile;

    if (file_exists($imagePath)) {
        $pdf->Image($imagePath, $pdf->GetX(), $pdf->GetY(), 100, 100, 'PNG');
    } else {
        $pdf->SetFont('Arial', 'I', 10);
        $pdf->Cell(100, 10, 'Image not found: ' . basename($imagePath), 0, 1, 'L');
    }
}

$pdf = new FPDF();
$pdf->AliasNbPages();
$pdf->AddPage('p');

$pdf->SetFont('Arial', 'B', 20);
$pdf->Cell(200, 15, "JOB CARD : " . $jobcard['jc_no'], 0, 1, 'C');
$pdf->Cell(10, 5, '', 0, 1, 'L');
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(50, 8, "Date Created", "", 0, 'L');
$pdf->Cell(50, 8, $jobcard['date_created'], "", 1, 'L');
$pdf->Cell(50, 8, "Action Date", "", 0, 'L');
$pdf->Cell(50, 8, $jobcard['action_date'], "", 1, 'L');
// $pdf->Cell(50, 8, "Date Finished", "", 0, 'L');
// $pdf->Cell(50, 8, $jobcard['date_time_closed'], "", 1, 'L');
$pdf->Cell(10, 3, '', 0, 1, 'L');

$pdf->Cell(50, 8, "User Created", "", 0, 'L');
$pdf->Cell(50, 8, get_username($jobcard['user_id']), "", 1, 'L');
$pdf->Cell(50, 8, "Team Assigned", "", 0, 'L');
$pdf->Cell(50, 8, get_team($jobcard['team_assigned_id']), "", 1, 'L');
$pdf->Cell(10, 3, '', 0, 1, 'L');

$pdf->Cell(50, 8, "Client", "", 0, 'L');
$pdf->Cell(50, 8, $jobcard['client_id'], "", 1, 'L');
$pdf->Cell(50, 8, "Address", "", 0, 'L');
$pdf->Cell(50, 8, $jobcard['address'], "", 1, 'L');
$pdf->Cell(50, 8, "Contact Name", "", 0, 'L');
$pdf->Cell(50, 8, $jobcard['contact_name'], "", 1, 'L');
$pdf->Cell(50, 8, "Contact Number", "", 0, 'L');
$pdf->Cell(50, 8, $jobcard['contact_number'], "", 1, 'L');
$pdf->Cell(50, 8, "Other Number", "", 0, 'L');
$pdf->Cell(50, 8, $jobcard['other_number'], "", 1, 'L');
$pdf->Cell(50, 8, "Alternate Number", "", 0, 'L');
$pdf->Cell(50, 8, $jobcard['alternate_number'], "", 1, 'L');
$pdf->Cell(50, 8, "Drill Co Ordinates", "", 0, 'L');
$pdf->Cell(50, 8, $jobcard['drill_co_ordinates'], "", 1, 'L');
$pdf->Cell(10, 3, '', 0, 1, 'L');

$pdf->Cell(50, 8, "Payment Method", "", 0, 'L');
$pdf->Cell(50, 8, $jobcard['payment_method'], "", 1, 'L');
$pdf->Cell(50, 8, "Cash Amount", "", 0, 'L');
$pdf->Cell(50, 8, "R " . $jobcard['cash_amount'], "", 1, 'L');
$pdf->Cell(10, 3, '', 0, 1, 'L');

// $pdf->Cell(10, 5, '', 0, 1, 'L');
$pdf->Cell(50, 8, "Diesel Start", "", 0, 'L');
$pdf->Cell(50, 8, $jobcard['diesel_start'], "", 1, 'L');
$pdf->Cell(50, 8, "Diesel Stop", "", 0, 'L');
$pdf->Cell(50, 8, $jobcard['diesel_stop'], "", 1, 'L');
$pdf->Cell(50, 8, "Water Flow", "", 0, 'L');
$pdf->Cell(50, 8, $jobcard['water_flow'], "", 1, 'L');
$pdf->Cell(50, 8, "Water Strike", "", 0, 'L');
$pdf->Cell(50, 8, $jobcard['water_strike'], "", 1, 'L');
$pdf->Cell(50, 8, "Compressor Hours", "", 0, 'L');
$pdf->Cell(50, 8, $jobcard['compressor_hours'], "", 1, 'L');
$pdf->Cell(50, 8, "Interested In Pump", "", 0, 'L');
$pdf->Cell(50, 8, $jobcard['interested_in_pump'], "", 1, 'L');


$pdf->SetFont('Arial', 'B', 15);
$pdf->Cell(200, 15, "JOBCARD SUMMARY", 0, 1, 'C');
$pdf->Cell(10, 5, '', 0, 1, 'L');
$pdf->SetFont('Arial', '', 10);

$job_card_res = $db->query("jobcard_timeline", "SELECT * FROM jobcard_timeline WHERE jobcard_id = {$_GET['record_id']} AND type != 'DEPARTURE' AND type != 'ARRIVED' ORDER BY record_id ASC");
if ($job_card_res->num_rows == 0) {
    $error = 1;
} else {
    $drilling_meters = $riem_meters = $casing_meters = $blasting_meters = 0;
    $total_riem_time = $total_drilling_time = $total_casing_time = $total_blasting_time = 0;

    while ($job_card = $job_card_res->fetch_assoc()) {
        switch ($job_card['type']) {
            case 'RIEM_START':
                $riem_start = $job_card['date_time'];
                break;

            case 'RIEM_STOP':
                if (isset($riem_start)) {
                    $riem_stop = $job_card['date_time'];
                    $riem_meters += $job_card['meters'];
                    $total_riem_time += strtotime($riem_stop) - strtotime($riem_start);
                }
                break;

            case 'DRILLING_START':
                $drilling_start = $job_card['date_time'];
                break;

            case 'DRILLING_STOP':
                if (isset($drilling_start)) {
                    $drilling_stop = $job_card['date_time'];
                    $drilling_meters += $job_card['meters'];
                    $total_drilling_time += strtotime($drilling_stop) - strtotime($drilling_start);
                }
                break;

            case 'CASING_START':
                $casing_start = $job_card['date_time'];
                break;

            case 'CASING_STOP':
                if (isset($casing_start)) {
                    $casing_stop = $job_card['date_time'];
                    $casing_meters += $job_card['meters'];
                    $total_casing_time += strtotime($casing_stop) - strtotime($casing_start);
                }
                break;

            case 'BLASTING_START':
                $blasting_start = $job_card['date_time'];
                break;

            case 'BLASTING_STOP':
                if (isset($blasting_start)) {
                    $blasting_stop = $job_card['date_time'];
                    $blasting_meters += $job_card['meters'];
                    $total_blasting_time += strtotime($blasting_stop) - strtotime($blasting_start);
                }
                break;
        }
    }

    $total_work_time = $total_riem_time + $total_drilling_time + $total_casing_time + $total_blasting_time;
    $total_meters = $riem_meters + $drilling_meters + $casing_meters + $blasting_meters;
}

$pdf->Cell(80, 8, "DESCRIPTION", 1, 0, 'L');
$pdf->Cell(50, 8, "TOTAL METERS", 1, 0, 'L');
$pdf->Cell(50, 8, "TOTAL TIME", 1, 1, 'L');

$pdf->Cell(80, 8, "RIEM", "BL", 0, 'L');
$pdf->Cell(50, 8, $riem_meters, "LBR", 0, 'L');
$pdf->Cell(50, 8, date('H:i', $total_riem_time), "LBR", 1, 'L');

$pdf->Cell(80, 8, "DRILLING", "LB", 0, 'L');
$pdf->Cell(50, 8, $drilling_meters, "LBR", 0, 'L');
$pdf->Cell(50, 8, date('H:i', $total_drilling_time), "LBR", 1, 'L');

$pdf->Cell(80, 8, "CASING", "BL", 0, 'L');
$pdf->Cell(50, 8, $casing_meters, "LBR", 0, 'L');
$pdf->Cell(50, 8, date('H:i', $total_casing_time), "LBR", 1, 'L');

$pdf->Cell(80, 8, "BLASTING", "LB", 0, 'L');
$pdf->Cell(50, 8, $blasting_meters, "LBR", 0, 'L');
$pdf->Cell(50, 8, date('H:i', $total_blasting_time), "LBR", 1, 'L');

$pdf->Cell(80, 8, "TOTALS", "LB", 0, 'L');
$pdf->Cell(50, 8, $total_meters, "LBR", 0, 'L');
$pdf->Cell(50, 8, date('H:i', $total_work_time), "LBR", 1, 'L');


$pdf->AddPage('p');

$pdf->SetFont('Arial', 'B', 15);
$pdf->Cell(200, 15, "JOBCARD TIMELINE", 0, 1, 'C');
$pdf->Cell(10, 5, '', 0, 1, 'L');
$pdf->SetFont('Arial', '', 10);

$pdf->Cell(50, 8, "DESCRIPTION", 1, 0, 'L');
$pdf->Cell(30, 8, "METERS", 1, 0, 'L');
$pdf->Cell(50, 8, "DATE TIME", 1, 1, 'L');
// $pdf->Cell(50, 8, "AMOUNT", 1, 1, 'L');



$job_card_time_res = $db->query("jobcard_timeline", "SELECT * FROM jobcard_timeline WHERE jobcard_id = {$_GET['record_id']} AND type != 'DEPARTURE' AND type != 'ARRIVED' ORDER BY record_id ASC");
if ($job_card_time_res->num_rows == 0) {
    $error = 1;
} else {
    while ($job_card = $job_card_time_res->fetch_assoc()) {
        $pdf->Cell(50, 8, $job_card['type'], "LB", 0, 'L');
        $pdf->Cell(30, 8, $job_card['meters'], "LBR", 0, 'L');
        $pdf->Cell(50, 8, $job_card['date_time'], "LBR", 1, 'L');
        // $pdf->Cell(50, 8, $job_card['amount'], "LBR", 1, 'L');
    }
}
$pdf->Cell(10, 5, '', 0, 1, 'L');

$pdf->AddPage('p');

$pdf->SetFont('Arial', 'B', 20);
$pdf->Cell(200, 15, "NOTES", 0, 1, 'C');
$pdf->Cell(10, 5, '', 0, 1, 'L');
$pdf->SetFont('Arial', '', 10);

$i = 1;

$notes_res = $db->query("notes", "SELECT * FROM notes WHERE jobcard_id = {$_GET['record_id']} ORDER BY record_id ASC");
if ($notes_res->num_rows == 0) {
    $error = 1;
} else {
    while ($notes = $notes_res->fetch_assoc()) {


        $pdf->Cell(50, 8, "NOTE NO :", 0, 0, 'L');
        $pdf->Cell(50, 8, $i, 0, 1, 'L');

        $pdf->Cell(50, 8, "DESCRIPTION :", 0, 0, 'L');
        $pdf->Cell(50, 8, $notes['reason'], 0, 1, 'L');

        $pdf->Cell(50, 8, "ADDITIONAL NOTES", 0, 0, 'L');
        $pdf->Cell(50, 8, $notes['note'], 0, 1, 'L');

        $pdf->Cell(50, 8, "DATE TIME : ", 0, 0, 'L');
        $pdf->Cell(35, 8, $notes['date_time'], 0, 1, 'L');

        if ($notes['reason'] == "CURRENT BIT") {
            addSafeImage($pdf, "../jobcards/drilling_bit/", $notes['image']);
        } else {
            addSafeImage($pdf, "../jobcards/notes/", $notes['image']);
        }

        $pdf->Cell(10, 10, '', 0, 1, 'L');

        $i++;
    }
}


$pdf->Output("I");