<?php
require('classes/fpdf.php');
require('classes/db.class.php');

$db = new db();

$where_data = $_POST['where_data'];
$batch_res = $db->exec_query('batches', ['*'],'','','','',$where_data);

$batch_total = $batch_res->num_rows;
$shell = 0;
$no_shell = 0;

while ($batch = $batch_res->fetch_assoc()) {
    if ($batch['shell'] == "YES") {
        $shell++;
    }
    if ($batch['shell'] == "NO") {
        $no_shell++;
    }
    $total++;
}

$pdf = new FPDF();

$pdf->AliasNbPages();

$pdf->AddPage('L');

$table_top = [50, 28, 16, 65, 35, 55, 30, 40, 25, 20, 20, 20];

$pdf->SetFont('Arial', 'B', 20);
$pdf->Cell(200, 15, "NUTS RECEIVED REPORT ", 0, 1, 'C');
$pdf->Cell(10, 5, '', 0, 1, 'L');
$pdf->SetFont('Arial', '', 10);

$pdf->Cell(-2);

$pdf->Cell($table_top[8], 7, 'TOTAL', "BR", 0, 'L');
$pdf->Cell($table_top[8], 7, 'SHELL', "BR", 0, 'L');
$pdf->Cell($table_top[8], 7, 'NO SHELL', "BR", 1, 'L');

$pdf->Cell(-2);
$pdf->Cell($table_top[8], 5, $batch_total, "BR", 0, 'L');
$pdf->Cell($table_top[8], 5, $shell, "BR", 0, 'L');
$pdf->Cell($table_top[8], 5, $no_shell, "BR", 1, 'L');


$pdf->Cell(10, 10, '', 0, 1, 'L');
$pdf->Cell(-2);
$pdf->Cell($table_top[2], 5, "PV", "LBR", 0, 'L');
$pdf->Cell($table_top[2], 5, "FFA", "BR", 0, 'l');
$pdf->Cell($table_top[8], 5, "VARIETY", "BR", 0, 'l');
$pdf->Cell($table_top[2], 5, "WEIGHT", "BR", 0, 'l');
$pdf->Cell($table_top[2], 5, "SIZE", "BR", 0, 'l');
$pdf->Cell($table_top[7], 5, "SOUND NOT SOUND", "BR", 0, 'l');
$pdf->Cell($table_top[2], 5, "MOLD", "BR", 0, 'l');
$pdf->Cell($table_top[9], 5, "MOISTURE", "BR", 0, 'l');
$pdf->Cell($table_top[8], 5, "CLIENT", "BR", 0, 'l');
$pdf->Cell($table_top[2], 5, "SHELL", "BR", 0, 'l');
$pdf->Cell($table_top[7], 5, "DATE", "B", 1, 'l');

$shell_res = $db->exec_query('batches',['*']);

while ($shell = $shell_res->fetch_assoc()) {
    // get farm name
    $variety_name_res = $db->exec_query('variety',['*'],'','','','',"record_id = {$shell['variety_id']}");
    $variety_name = $variety_name_res->fetch_assoc();

    $client_name_res = $db->exec_query('clients',['*'],'','','','',"record_id = {$shell['client_id']}");
    $client_name = $client_name_res->fetch_assoc();

    $pdf->Cell(-2);
    $pdf->Cell($table_top[2], 6, $shell['pv'], "LBR", 0, 'L');
    $pdf->Cell($table_top[2], 6, $shell['ffa'], "BR", 0, 'l');
    $pdf->Cell($table_top[8], 6, $variety_name['name'], "BR", 0, 'l');
    $pdf->Cell($table_top[2], 6, $shell['weight'], "BR", 0, 'l');
    $pdf->Cell($table_top[2], 6, $shell['size'], "BR", 0, 'l');
    $pdf->Cell($table_top[7], 6, $shell['sound_not_sound'], "BR", 0, 'l');
    $pdf->Cell($table_top[2], 6, $shell['mold'], "BR", 0, 'l');
    $pdf->Cell($table_top[9], 6, $shell['moisture'], "BR", 0, 'l');
    $pdf->Cell($table_top[8], 6, $client_name['client_name'], "BR", 0, 'l');
    $pdf->Cell($table_top[2], 6, $shell['shell'], "BR", 0, 'l');
    $pdf->Cell($table_top[7], 6, $shell['date'], "B", 1, 'l');

}

// $pdf->AddPage('L');

// $pdf->SetFont('Arial', 'B', 20);
// $pdf->Cell(200, 15, "NUTS RECEIVED REPORT ", 0, 1, 'C');
// $pdf->Cell(10, 5, '', 0, 1, 'L');
// $pdf->SetFont('Arial', '', 10);

// $batch_res = $db->exec_query('batches',['*']);

// $varieties_res = $db->query("SELECT DISTINCT(variety_id) FROM batches WHERE $where_data");

// while($variety = $varieties_res->fetch_assoc()){
//     $variety_name_res = $db->exec_query('variety',['*'],'','','','',"record_id = {$variety['variety_id']}");
//     $varieties = $variety_name_res->fetch_assoc();

//     $pdf->SetFont('Arial', 'B', 15);
//     $pdf->Cell(200, 15, "{$varieties['name']} Received Summary", 0, 1, 'C');
//     $pdf->SetFont('Arial', '', 10);

//     $pdf->Cell($table_top[8], 8, "CLIENT", 1, 0, 'L');
//     $pdf->Cell($table_top[7], 8, "TOTAL", 1, 0, 'L');
//     $pdf->Cell($table_top[7], 8, "TOTAL WEIGHT", 'LTB', 1, 'L');

//     $unq_client_res = $db->query("SELECT DISTINCT(client_id) FROM batches WHERE $where_data AND variety_id = {$variety['variety_id']}");

//     while($unq_client = $unq_client_res->fetch_assoc()){
//         $client_name_res = $db->exec_query('clients',['*'],'','','','',"record_id = {$unq_client['client_id']}");
//         $client_name = $client_name_res->fetch_assoc();

//         $client_count_res = $db->query("SELECT * FROM batches WHERE $where_data AND client_id = {$unq_client['client_id']} AND variety_id = {$variety['variety_id']}");
//         $client_count = $client_count_res->num_rows;

//         $pdf->Cell($table_top[8], 8, $client_name['client_name'], 1, 0, 'L');

//         $pdf->Cell($table_top[7], 8, $client_count, 1, 1, 'L');
//     }
// }

$pdf->Output("I");
