<?php
$this->load->view('partials/report_start', $this->data);
$sets = isset($material_stat_sets) && is_array($material_stat_sets) ? $material_stat_sets : array(
    'Application Notes' => isset($al_report) ? $al_report : array(),
    'Books' => isset($book_report) ? $book_report : array(),
    'E-Books' => isset($ebook_report) ? $ebook_report : array(),
    'E-Reports' => isset($ereport_report) ? $ereport_report : array(),
    'Institutional Repository' => isset($ir_report) ? $ir_report : array(),
    'Journals' => isset($jrl_report) ? $jrl_report : array(),
    'Patents' => isset($patent_report) ? $patent_report : array(),
    'Reports' => isset($reports_report) ? $reports_report : array(),
    'Standards Data' => isset($sd_report) ? $sd_report : array(),
);

if (!function_exists('material_stat_row_status')) {
    function material_stat_row_status($row)
    {
        foreach (array('STATUS', 'Status', 'status') as $k) {
            if (array_key_exists($k, $row) && $row[$k] !== null && $row[$k] !== '') {
                return trim((string) $row[$k]);
            }
        }
        foreach ($row as $k => $v) {
            if (stripos((string) $k, 'status') !== false && $v !== null && $v !== '') {
                return trim((string) $v);
            }
        }
        foreach ($row as $k => $v) {
            if (stripos((string) $k, 'count') !== false || strcasecmp((string) $k, 'Material') === 0) {
                continue;
            }
            if (!is_numeric($v) && $v !== null && $v !== '') {
                return trim((string) $v);
            }
        }
        return '';
    }
}

if (!function_exists('material_stat_row_count')) {
    function material_stat_row_count($row)
    {
        foreach (array('COUNT', 'Count', 'count') as $k) {
            if (array_key_exists($k, $row) && is_numeric($row[$k])) {
                return (float) $row[$k];
            }
        }
        foreach ($row as $k => $v) {
            if (stripos((string) $k, 'count') !== false && is_numeric($v)) {
                return (float) $v;
            }
        }
        $last = null;
        foreach ($row as $k => $v) {
            if (strcasecmp((string) $k, 'Material') === 0) {
                continue;
            }
            if (is_numeric($v)) {
                $last = (float) $v;
            }
        }
        return $last;
    }
}

if (!function_exists('material_stat_final_total')) {
    function material_stat_final_total($rows)
    {
        $fallback = null;
        foreach ($rows as $r) {
            $row = is_array($r) ? $r : (array) $r;
            $status = material_stat_row_status($row);
            $count = material_stat_row_count($row);
            if ($count === null) {
                continue;
            }
            // Prefer Final; keep last numeric row as fallback (Final is usually last).
            $fallback = $count;
            $norm = strtolower(preg_replace('/\s+/', '', $status));
            if ($norm === 'final' || strpos($norm, 'final') !== false || $norm === 'total') {
                return $count;
            }
        }
        return $fallback;
    }
}
?>
<form method="post">
<?php if (!empty($searched)) { ?>
  <div class="row">
    <?php foreach ($sets as $label => $rows) {
        $total = material_stat_final_total($rows);
        ?>
      <div class="col-md-4 col-sm-6 mb-3">
        <div class="border rounded p-3 bg-white">
          <div class="text-muted small text-uppercase"><?php echo htmlspecialchars($label); ?></div>
          <div style="font-size:1.4rem;font-weight:600;color:#0f2a44;"><?php echo $total === null ? '0' : (int) $total; ?></div>
          <div class="small text-muted">Final total</div>
        </div>
      </div>
    <?php } ?>
  </div>
  <?php
    $flat = array();
    foreach ($sets as $label => $rows) {
        foreach ($rows as $r) {
            $row = is_array($r) ? $r : (array) $r;
            $row['Material'] = $label;
            $flat[] = $row;
        }
    }
    $cols = array('Material' => 'Material');
    if (!empty($flat)) {
        foreach (array_keys($flat[0]) as $k) {
            if ($k !== 'Material') {
                $cols[$k] = $k;
            }
        }
    }
    echo '<div class="report-results-toolbar">';
    echo '<p class="report-summary mb-0"><strong>' . count($flat) . '</strong> statistics row(s)</p>';
    $this->load->view('partials/report_exports');
    echo '</div>';
    $this->load->view('partials/report_datatable', array(
        'columns' => $cols,
        'rows' => $flat,
        'table_id' => 'statsTable',
    ));
}
?>
</form>
<?php $this->load->view('partials/report_end'); ?>
