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

$db = new db_safeguard();

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

$batch_no_res = $db->query("batch_data", "SELECT * FROM batch_data WHERE record_id = '{$_GET['record_id']}'");
$batch_no = $batch_no_res->fetch_assoc();

$check_equipment_res = $db->query("equipment_checklist", "SELECT * FROM cracking_equipment_check_list WHERE batch_no = '{$batch_no['batch_no']}' AND check_no = {$_GET['check_no']}");
$equipment = $check_equipment_res->fetch_assoc();

// $cracking_equipment_res = $db->query("equipment_checklist", "SELECT * FROM cracking_equipment_check_list WHERE batch_no = '{$batch_no['batch_no']}'");
// $equipment = $cracking_equipment_res->fetch_assoc();

$pdf->SetFont('Arial', 'B', 20);
$pdf->Cell(180, 15, "EQUIPMENT REPORT FOR FORM {$_GET['check_no']}", 0, 1, 'C');
$pdf->Cell(10, 5, '', 0, 1, 'L');
$pdf->SetFont('Arial', '', 10);

$pdf->Cell(100, 10, 'Date Time:', 0, 0, 'L');
$pdf->Cell(100, 10, $equipment['date_time'], "", 1, 'L');

$pdf->Cell(100, 10, 'Batch no:', 0, 0, 'L');
$pdf->Cell(100, 10, $batch_no['batch_no'], "", 1, 'L');

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

$pdf->Cell(40, 8, "Equipment Name", 1, 0, 'L');
$pdf->Cell(100, 8, "Question", 1, 0, 'L');
$pdf->Cell(20, 8, "Answer", 1, 0, 'L');
$pdf->Cell(20, 8, "Comment", 1, 1, 'L');

if ($check_equipment_res->num_rows == 0) {
    $pdf->Cell(100, 10, 'NO EQUIPMENT CHECKED YET', 0, 0, 'L');
    $pdf->Cell(10, 5, '', 0, 1, 'L');
    $pdf->SetFont('Arial', '', 10);
} else {
    while ($check_equipment = $check_equipment_res->fetch_assoc()) {

        $equipment_res = $db->query("equipment_list", "SELECT * FROM equipment_list WHERE record_id = '{$check_equipment['equipment_id']}'");
        $equipment = $equipment_res->fetch_assoc();

        $pdf->Cell(40, 8, $equipment['name'], "LB", 0, 'L');
        $pdf->Cell(100, 8, $check_equipment['question'], "LBR", 0, 'L');
        $pdf->Cell(20, 8, $check_equipment['text'], "LBR", 0, 'L');
        $pdf->Cell(20, 8, $check_equipment['comment'], "BR", 1, 'L');

    }
}

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

$pdf->Output("I");
