<?php
$this->load->view('report_pdf/pdf_header');
$period = isset($period_label) ? $period_label : date('M Y');
$report = isset($report) ? $report : array();
$today_label = !empty($today) ? date('d/m/Y', strtotime($today)) : date('d/m/Y');
$lac_members = isset($lac_members) && is_array($lac_members) && count($lac_members)
    ? array_values($lac_members)
    : array();
$member_count = max(1, count($lac_members));
if (empty($lac_members)) {
    $lac_members = array('M1');
    $member_count = 1;
}
$show_annexure = !isset($show_annexure) ? true : (bool) $show_annexure;
$show_sign = !isset($show_sign) ? true : (bool) $show_sign;
$repeat_header = !isset($repeat_header) ? true : (bool) $repeat_header;

// Base columns + equal-width recommendation columns (like the sample grid)
$base_widths = array(3.5, 12, 22, 11, 4.5, 10, 9);
$base_total = array_sum($base_widths);
$rec_total = max(18, 100 - $base_total);
$rec_width = round($rec_total / $member_count, 2);
$colspan_total = 7 + $member_count;
?>
<style>
    @page {
        margin: 22px 18px 50px 18px;
    }

    body {
        font-family: DejaVu Sans, sans-serif;
        color: #111;
        font-size: 11px;
        margin: 0;
        padding: 0;
    }

    .lac-meta {
        width: 100%;
        border-collapse: collapse;
        margin: 0 0 4px 0;
    }

    .lac-meta td {
        border: none;
        padding: 0;
        text-align: right;
        font-size: 12px;
        line-height: 1.45;
        font-weight: bold;
    }

    .lac-date-line {
        min-width: 120px;
        padding: 0 4px 0px 4px;
        text-align: left;
        font-weight: normal;
    }

    .lac-annexure {
        text-align: right;
        font-size: 13px;
        font-weight: bold;
        margin: 2px 0 10px 0;
    }

    .lac-title {
        text-align: center;
        font-size: 14px;
        font-weight: bold;
        margin: 0 0 12px 0;
        line-height: 1.4;
    }

    .lac-title span {
        display: inline-block;
        border-bottom: 1px solid #000;
        padding-bottom: 2px;
    }

    .lac-table {
        width: 100%;
        border-collapse: collapse;
        table-layout: fixed;
        font-size: 10px;
        line-height: 1.3;
    }

    .lac-table th,
    .lac-table td {
        border: 1px solid #222;
        padding: 3px 3px;
        vertical-align: top;
        word-wrap: break-word;
        overflow-wrap: break-word;
    }

    .lac-table th {
        text-align: center;
        font-weight: bold;
        background: #f3f3f3;
        font-size: 9.5px;
        line-height: 1.2;
    }

    .lac-table td {
        text-align: left;
    }

    .lac-table .c-center {
        text-align: center;
    }

    .lac-table .c-rec {
        text-align: center;
        height: 16px;
    }

    .lac-sign {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 18px;
        width: 100%;
        margin-top: 0;
        border-collapse: collapse;
    }

    .lac-sign td {
        border: none;
        vertical-align: top;
        padding: 0;
        font-size: 11px;
        line-height: 1.45;
        font-weight: 600;
    }

    .lac-sign-left {
        text-align: left;
        width: 42%;
    }

    .lac-sign-right {
        text-align: right;
        width: 42%;
    }

    .lac-sign-mid {
        width: 16%;
    }

    .lac-table {
        margin-bottom: 56px;
    }
</style>

<?php if ($show_annexure) { ?>
<table class="lac-meta" width="100%" cellspacing="0" cellpadding="0">
    <tr>
        <td>
            Date : <span class="lac-date-line"><?php echo htmlspecialchars($today_label); ?></span>
        </td>
    </tr>
</table>
<p class="lac-annexure">Annexure - A</p>
<p class="lac-title">
    <span>List of New Titles for LAC Recommendations <?php echo htmlspecialchars($period); ?></span>
</p>
<?php } ?>

<table class="lac-table" width="100%" cellspacing="0" cellpadding="0">
    <?php if ($repeat_header) { ?>
    <thead>
        <tr>
            <th rowspan="2" width="<?php echo $base_widths[0]; ?>%">SNO</th>
            <th rowspan="2" width="<?php echo $base_widths[1]; ?>%">Author</th>
            <th rowspan="2" width="<?php echo $base_widths[2]; ?>%">Title</th>
            <th rowspan="2" width="<?php echo $base_widths[3]; ?>%">Publisher</th>
            <th rowspan="2" width="<?php echo $base_widths[4]; ?>%">Year</th>
            <th rowspan="2" width="<?php echo $base_widths[5]; ?>%">Subject</th>
            <th rowspan="2" width="<?php echo $base_widths[6]; ?>%">ISBN</th>
            <th colspan="<?php echo (int) $member_count; ?>">Recommendations of LAC Members</th>
        </tr>
        <tr>
            <?php foreach ($lac_members as $code) { ?>
                <th class="c-rec" width="<?php echo $rec_width; ?>%"><?php echo htmlspecialchars($code); ?></th>
            <?php } ?>
        </tr>
    </thead>
    <?php } ?>
    <tbody>
        <?php foreach ($report as $row) { ?>
            <tr>
                <td class="c-center"><?php echo htmlspecialchars((string) ($row->S_No ?? '')); ?></td>
                <td><?php echo htmlspecialchars((string) ($row->Author ?? '')); ?></td>
                <td><?php echo htmlspecialchars((string) ($row->Title ?? '')); ?></td>
                <td><?php echo htmlspecialchars((string) ($row->Publisher ?? '')); ?></td>
                <td class="c-center"><?php echo htmlspecialchars((string) ($row->Year ?? '')); ?></td>
                <td><?php echo htmlspecialchars((string) ($row->Subject ?? '')); ?></td>
                <td><?php echo htmlspecialchars((string) ($row->ISBN ?? '')); ?></td>
                <?php for ($i = 1; $i <= $member_count; $i++) {
                    $key = 'M' . $i;
                ?>
                    <td class="c-rec"><?php echo htmlspecialchars((string) ($row->{$key} ?? '')); ?></td>
                <?php } ?>
            </tr>
        <?php } ?>
        <?php if (empty($report)) { ?>
            <tr>
                <td colspan="<?php echo (int) $colspan_total; ?>" class="c-center">No titles found for the selected period.</td>
            </tr>
        <?php } ?>
    </tbody>
</table>

<?php
$sign_left_html = function_exists('app_lac_signatory_left_html') ? app_lac_signatory_left_html() : '';
$sign_right_html = function_exists('app_lac_signatory_right_html') ? app_lac_signatory_right_html() : '';
?>
<?php if ($show_sign && ($sign_left_html !== '' || $sign_right_html !== '')) { ?>
<table class="lac-sign" width="100%" cellspacing="0" cellpadding="0">
    <tr>
        <td class="lac-sign-left">
            <?php echo $sign_left_html; ?>
        </td>
        <td class="lac-sign-mid">&nbsp;</td>
        <td class="lac-sign-right">
            <?php echo $sign_right_html; ?>
        </td>
    </tr>
</table>
<?php } ?>
