<?php
require('../../fpdf.php');
include "../../root.class.php";
$db = new db_safeguard();


$pdf = new FPDF();
$pdf->AliasNbPages();
$pdf->AddPage('p');

$res_data = $db->query("jobcard_timeline", "SELECT * FROM jobcard_timeline WHERE jobcard_id = {$_GET['record_id']} AND type != 'DEPARTURE' AND type != 'ARRIVED' ORDER BY type DESC");
// echo "SELECT * FROM jobcard_timeline WHERE jobcard_id = {$_GET['record_id']} AND type != DEPARTURE AND type != ARRIVED ORDER BY type DESC";

if ($res_data->num_rows > 0) {

    $jobcard_res = $db->query("jobcards", "SELECT * FROM jobcards WHERE record_id = {$_GET['record_id']}");
    $jobcard = $jobcard_res->fetch_assoc();

    $pdf->SetFont('Arial', 'B', 20);
    $pdf->Cell(200, 15, "JOBCARD SUMMARY : " . $jobcard['jc_no'], 0, 1, 'C');
    $pdf->Cell(10, 5, '', 0, 1, 'L');
    $pdf->SetFont('Arial', '', 10);

    $pdf->Cell(50, 8, "TYPE", 1, 0, 'L');
    $pdf->Cell(50, 8, "METERS", 1, 0, 'L');
    $pdf->Cell(50, 8, "DATE", 1, 1, 'L');

    while ($jc_timeline = $res_data->fetch_assoc()) {

        if ($jc_timeline['type'] == "RIEM_STOP" || $jc_timeline['type'] == "RIEMING_STOP") {
            $riem_meters += $jc_timeline['meters'];
        }

        if ($jc_timeline['type'] == "DRILLING_STOP") {
            $drilling_meters += $jc_timeline['meters'];
        }

        if ($jc_timeline['type'] == "CASING_STOP") {
            $casing_meters += $jc_timeline['meters'];
        }

        $pdf->Cell(50, 8, $jc_timeline['type'], 1, 0, 'L');
        $pdf->Cell(50, 8, $jc_timeline['meters'], 1, 0, 'L');
        $pdf->Cell(50, 8, $jc_timeline['date_time'], 1, 1, 'L');

    }

    $pdf->AddPage('p');

    $pdf->Cell(10, 5, '', 0, 1, 'L');

    $pdf->Cell(50, 8, " ", 1, 0, 'LBR');
    $pdf->Cell(50, 8, "TOTAL", 1, 1, 'LBR');

    $pdf->Cell(50, 8, "TOTAL RIEM METERS", 1, 0, 'LBR');
    $pdf->Cell(50, 8, $riem_meters, "LBR", 1, 'LBR');

    $pdf->Cell(50, 8, "TOTAL DRILLING METERS", 1, 0, 'LBR');
    $pdf->Cell(50, 8, $drilling_meters, "LBR", 1, 'LBR');

    $pdf->Cell(50, 8, "TOTAL CASING METERS", 1, 0, 'LBR');
    $pdf->Cell(50, 8, $casing_meters, "LBR", 1, 'LBR');

    $pdf->Cell(10, 10, '', 0, 1, 'L');

    $pdf->Cell(50, 8, "NAME", 1, 0, 'L');
    $pdf->Cell(50, 8, "VALUE", 1, 1, 'L');

    $pdf->Cell(50, 8, "Interested in Pump", "LB", 0, 'L');
    $pdf->Cell(50, 8, $jobcard['interested_in_pump'], "LBR", 1, 'L');

    $pdf->Cell(50, 8, "Water Flow", "LB", 0, 'L');
    $pdf->Cell(50, 8, $jobcard['water_flow'], "LBR", 1, 'L');

    $pdf->Cell(50, 8, "Compressor Hours", "LB", 0, 'L');
    $pdf->Cell(50, 8, $jobcard['compressor_hours'], "LBR", 1, 'L');

    $pdf->Cell(50, 8, "Diesel Start", "LB", 0, 'L');
    $pdf->Cell(50, 8, $jobcard['diesel_start'], "LBR", 1, 'L');

    $pdf->Cell(50, 8, "Diesel Stop", "LB", 0, 'L');
    $pdf->Cell(50, 8, $jobcard['diesel_stop'], "LBR", 1, 'L');

    //get signatures for this jobcard and user
    if (file_exists("../jobcards/signatures/client-jc" . $jobcard['jc_no'] . $_SESSION['user_id'] . "-signature.png")) {
        $pdf->image("../jobcards/signatures/client-jc" . $jobcard['jc_no'] . $_SESSION['user_id'] . "-signature.png", 10, 265, 30, 30);
        $pdf->Cell(50, 8, "Client Signature", 0, 0, 'L');
    }

    if (file_exists("../jobcards/signatures/manager-jc" . $jobcard['jc_no'] . "-" . $_SESSION['user_id'] . "-signature.png")) {
        $pdf->image("../jobcards/signatures/manager-jc" . $jobcard['jc_no'] . "-" . $_SESSION['user_id'] . "-signature.png", 10, 265, 30, 30);
        $pdf->Cell(50, 8, "Client Signature", 0, 0, 'L');
    }

    // $pdf->image('../jobcards/signatures/logo.png', 10, 265, 30, 30);
    // $pdf->Cell(50, 8, "Client Signature", 0, 0, 'L');

    // $pdf->image('../jobcards/signatures/logo.png', 10, 265, 30, 30);
    // $pdf->Cell(50, 8, "Client Signature", 0, 0, 'L');

} else {
    $pdf->Cell(50, 8, "NAME", 1, 0, 'L');
    $pdf->Cell(50, 8, "VALUE", 1, 1, 'L');

    $pdf->Cell(50, 8, " ", "LB", 0, 'L');
    $pdf->Cell(50, 8, "DATA NOT FOUND", "LBR", 1, 'L');
}

$pdf->Cell(10, 5, '', 0, 1, 'L');

//add images
// $pdf->Image('../../images/logo.png', 10, 265, 30, 30);

// $pdf->Image('../../images/logo.png', 10, 265, 30, 30);

$pdf->Output("I");