<?php
require('classes/fpdf.php');
require('classes/db.class.php');

$db = new db();

$where_data = $_POST['where_data'];

$drying_bins_res = $db->exec_query('bins', ['*'], '', '', '', '', $where_data);

$bins_total = $drying_bins_res->num_rows;
$shell = 0;
$no_shell = 0;

while ($drying_bin = $drying_bins_res->fetch_assoc()) {

    $variety_res = $db->exec_query('variety',['*'],'','','','',"record_id = {$drying_bin['variety_id']}");

    while ($variety = $variety_res->fetch_assoc()) {

        $unq_bin_res = $db->query("SELECT DISTINCT(drying_bin_id) FROM bins WHERE $where_data AND variety_id = {$drying_bin['variety_id']}");

        $count_bin = $unq_bin_res->num_rows;

        $variety['name'] . ' :' . $count_bin;
    }

    $total++;
}

$pdf = new FPDF();

$pdf->AliasNbPages();

$pdf->AddPage('P');

$table_top = [50, 28, 16, 65, 35, 55, 30, 40, 25, 20, 20, 20];

$pdf->SetFont('Arial', 'B', 20);
$pdf->Cell(200, 15, "DRYING BINS 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", 1, 'L');

$pdf->Cell(-2);
$pdf->Cell($table_top[8], 5, $bins_total, "BR", 1, 'L');

$pdf->Cell(10, 10, '', 0, 1, 'L');
$pdf->Cell(-2);
$pdf->Cell($table_top[8], 5, "BATCH NO", "LBR", 0, 'L');
$pdf->Cell($table_top[7], 5, "VARIETY", "BR", 0, 'l');
$pdf->Cell($table_top[2], 5, "WEIGHT", "BR", 0, 'l');
$pdf->Cell($table_top[7], 5, "DATE", "BR", 0, 'l');
$pdf->Cell($table_top[7], 5, "DRYING BIN", "BR", 1, 'l');

$bins_res = $db->exec_query('bins', ['*']);


while ($bins = $bins_res->fetch_assoc()) {
    // get farm name
    $variety_name_res = $db->exec_query('variety', ['*'], '', '', '', '', "record_id = {$bins['variety_id']}");
    $variety_name = $variety_name_res->fetch_assoc();

    $drying_bin_res = $db->exec_query('drying_bins', ['*'], '', '', '', '', "record_id = {$bins['drying_bin_id']}");
    $drying_bins = $drying_bin_res->fetch_assoc();

    $pdf->Cell(-2);
    $pdf->Cell($table_top[8], 6, $bins['batch_id'], "LBR", 0, 'L');
    $pdf->Cell($table_top[7], 6, $variety_name['name'], "BR", 0, 'l');
    $pdf->Cell($table_top[2], 6, $bins['weight'], "BR", 0, 'l');
    $pdf->Cell($table_top[7], 6, $bins['date'], "BR", 0, 'l');
    $pdf->Cell($table_top[7], 6, $drying_bins['name'], "BR", 1, 'l');

}

$pdf->Output("I");
