<?php
$this->load->view('report_pdf/pdf_header');
$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(),
);
?>
<p style="text-align:center;font-size:14px;margin:5px 0;">
    <b>OVERALL MATERIAL STATISTICS</b>
</p>

<p style="text-align:right;font-size:13px;margin:5px 0;">
    Date: <?php echo date("d/m/Y", strtotime($today)); ?>
</p>

<div id="content" style="margin-top: 30px;">
    <?php foreach ($sets as $label => $rows) { ?>
        <h5 style="color:brown;margin-top:18px;"><?php echo htmlspecialchars(strtoupper($label)); ?></h5>
        <table style="border-collapse:collapse;width:100%;margin-bottom:12px;" border="1">
            <thead>
                <tr>
                    <th>STATUS</th>
                    <th>COUNT</th>
                </tr>
            </thead>
            <tbody>
                <?php if (!empty($rows)) { ?>
                    <?php foreach ($rows as $row) {
                        $status = '';
                        $count = '';
                        if (isset($row['STATUS'])) {
                            $status = $row['STATUS'];
                        } elseif (isset($row['status'])) {
                            $status = $row['status'];
                        }
                        if (isset($row['COUNT'])) {
                            $count = $row['COUNT'];
                        } elseif (isset($row['count'])) {
                            $count = $row['count'];
                        } else {
                            foreach ($row as $k => $v) {
                                if (stripos($k, 'count') !== false) {
                                    $count = $v;
                                    break;
                                }
                            }
                        }
                        ?>
                        <tr>
                            <td><?php echo htmlspecialchars((string) ($status !== '' ? $status : 'N/A')); ?></td>
                            <td><?php echo htmlspecialchars((string) ($count !== '' ? $count : 'N/A')); ?></td>
                        </tr>
                    <?php } ?>
                <?php } else { ?>
                    <tr><td colspan="2">No data found.</td></tr>
                <?php } ?>
            </tbody>
        </table>
    <?php } ?>
</div>
