<style>
.signature {
        text-align: right;
        margin-top: 60px;
        font-weight: 500;
    }
</style>

<?php
if (!function_exists('app_document_signatory_html')) {
    $this->load->helper('settings');
}
$signatory_html = app_document_signatory_html();

// Header / meta (from controller or first row)
$first = (!empty($report) && is_array($report)) ? reset($report) : null;

$laboratory      = 'Defence Electronics Research Laboratory, Hyderabad - 500005';
$supply_order_no          = $first->Basket_Name ?? '';
$supply_order_dt        = date('jS F Y', strtotime($today));
$received_by     = 'the Director, DLRL, Hyderabad' ;
$discount       = $first->Discount ?? '';
$conversion_rates  = $conversion_rates  ?? ($first->Conversion_rates ?? '');
$ukp_rate          = $first->UKP_Rate ?? '';
$usd_rate          = $first->USD_Rate ?? '';
$eur_rate          = $first->EUR_Rate ?? '';

if ($conversion_rates === '' && ($ukp_rate !== '' || $usd_rate !== '' || $eur_rate !== '')) {
    $parts = [];
    if ($ukp_rate !== '') $parts[] = '1 UKP=Rs.' . $ukp_rate;
    if ($usd_rate !== '') $parts[] = '1 USD = Rs.' . $usd_rate;
    if ($eur_rate !== '') $parts[] = '1 EUR = Rs.' . $eur_rate;
    $conversion_rates = 'Conversion Rates: ' . implode('  ;   ', $parts);
}

// Format dates like Excel sample if they look like Y-m-d
//$fmt_crv_date = $crv_date;
$fmt_so_date  = $supply_order_dt;
//if (!empty($crv_date) && strtotime($crv_date)) {
  //  $fmt_crv_date = date('F Y', strtotime($crv_date));
//}
if (!empty($supply_order_dt) && strtotime($supply_order_dt)) {
    $fmt_so_date = date('jS F Y', strtotime($supply_order_dt));
}

// Totals from query rows (Final Amount)
$grand_total = 0;
if (!empty($report)) {
    foreach ($report as $r) {
        $grand_total += (float)($r->Final_Amount ?? $r->Final_amount ?? $r->Cost ?? 0);
    }
}
$rounded_total   = $rounded_total ?? round($grand_total);
$amount_in_words = $amount_in_words ?? '';

// Optional: Indian amount in words if controller did not pass it
if ($amount_in_words === '' && function_exists('convert_number_to_words')) {
    $amount_in_words = convert_number_to_words($rounded_total);
}
?>

<style>
    .crv-wrap { font-family: DejaVu Sans, Arial, Helvetica, sans-serif; font-size: 10px; color: #000; }
    .crv-title {
        text-align: center;
        font-size: 15px;
        font-weight: bold;
        margin: 2px 0 12px;
        letter-spacing: 0.04em;
        border-bottom: 1.5px solid #222;
        padding-bottom: 6px;
    }
    .crv-line { margin: 4px 0; font-size: 12px; font-weight: bold; }
    .crv-intro { margin: 8px 0 12px; font-size: 11px; font-weight: normal; font-family: "Times New Roman", Times, serif; line-height: 1.4; }
    .crv-table { border-collapse: collapse; width: 100%; table-layout: fixed; }
    .crv-table th, .crv-table td {
        border: 1px solid #222;
        padding: 4px 3px;
        vertical-align: top;
        word-wrap: break-word;
        font-size: 9px;
        line-height: 1.3;
    }
    .crv-table th { text-align: center; font-weight: bold; background: #f3f3f3; }
    .crv-table td { text-align: left; }
    .crv-table .c { text-align: center; }
    .crv-table .r { text-align: right; }
    .crv-footer { margin-top: 14px; font-size: 11px; }
    .crv-footer .round-row { width: 100%; border-collapse: collapse; }
    .crv-footer .round-row td { border: none; padding: 3px 4px; font-weight: bold; }
    .dyn { color: #000; }
    .signature { margin-top: 48px; line-height: 1.45; }
</style>

<div class="crv-wrap" id="content">

    <p class="crv-title">Receipt / Issue and Expense Voucher</p>

    <!-- <table style="width:100%;border-collapse:collapse;margin:0 0 10px 0;">
        <tr>
            <td style="border:none;width:55%;"></td>
            <td style="border:none;text-align:right;font-size:12px;font-weight:bold;">
                CRV No :
                <span class="dyn"><?php echo htmlspecialchars((string) (isset($document_number) ? $document_number : '')); ?></span>
            </td>
        </tr>
    </table> -->

    <p class="crv-line">
        Laboratory/Establishment:
        <span class="dyn"><?php echo htmlspecialchars($laboratory); ?></span>
    </p>
    <p class="crv-line">
        CRV No :
        <span class="dyn"><?php echo htmlspecialchars((string) (isset($document_number) ? $document_number : '')); ?></span>
    </p>

    <p class="crv-line">
        Supply Order No.
        <span class="dyn"><?php echo htmlspecialchars($supply_order_no); ?></span>
        , dt:
        <span class="dyn"><?php echo htmlspecialchars($fmt_so_date); ?></span>
    </p>

    <p class="crv-intro">
        The following publications listed below have been received by
        <span class="dyn"><?php echo htmlspecialchars($received_by); ?></span>.
    </p>

    <table class="crv-table">
        <thead>
            <tr>
                <th style="width:4%;">S.NO.</th>                
                <th style="width:7%;">Acc_No.</th>
                <th style="width:12%;">Author</th>
                <th style="width:18%;">Title</th>
                <th style="width:9%;">Publisher</th>
                <th style="width:5%;">Year</th>
                <th style="width:9%;">ISBN</th>                
                <th style="width:5%;">Curr.</th>
                <th style="width:5%;">Price</th>                
                <th style="width:5%;">Discount</th>
                <th style="width:5%;">Final Amount</th>
            </tr>
        </thead>
        <tbody>
        <?php
        $sno = 1;
        if (!empty($report)) {
            foreach ($report as $row) {                
                $acc_no    = $row->ACC_NO ;
                $author    = $row->Author ?? '';
                $title     = $row->Title ?? '';
                $publisher = $row->Publisher ?? '';
                $year      = $row->Publication_year ?? $row->Year ?? '';
                $isbn      = $row->ISBN ?? '';
                $curr=  $row->Currency_code ?? ''; 
                $price     = (float)($row->RRP ?? 0);
                $discount  = (float)($row->Discount ?? 0);
                $final_amt = $price - (($price * $discount) / 100);
                //$price     = $row->RRP ?? 0;                
                //$discount  = $row->Discount ?? '';
                //$final_amt = $row->Final_Amount ?? $row->Final_amount ?? $row->Cost ?? 0;
        ?>
            <tr>
                <td class="c dyn"><?php echo $sno++; ?></td>                
                <td class="c dyn"><?php echo htmlspecialchars((string)$acc_no); ?></td>
                <td class="dyn"><?php echo htmlspecialchars((string)$author); ?></td>
                <td class="dyn"><?php echo htmlspecialchars((string)$title); ?></td>
                <td class="dyn"><?php echo htmlspecialchars((string)$publisher); ?></td>
                <td class="c dyn"><?php echo htmlspecialchars((string)$year); ?></td>
                <td class="c dyn"><?php echo htmlspecialchars((string)$isbn); ?></td>
               <!-- <td class="c dyn"><?php echo htmlspecialchars((string)$price_code); ?></td>-->
                <td class="c dyn"><?php echo htmlspecialchars((string)$curr); ?></td>
                <td class="r dyn"><?php echo is_numeric($price) ? number_format((float)$price, 2) : htmlspecialchars((string)$price); ?></td>
                <!--<td class="r dyn"><?php echo is_numeric($amount) ? number_format((float)$amount, 2) : htmlspecialchars((string)$amount); ?></td>-->
                <td class="r dyn">
    <?php
    if (is_numeric($discount)) {
        echo rtrim(rtrim(number_format((float)$discount, 2, '.', ''), '0'), '.') . '%';
    } else {
        echo htmlspecialchars((string)$discount);
    }
    ?>
</td>
                <td class="r dyn"><?php echo number_format((float)$final_amt, 2); ?></td>
            </tr>
        <?php
            }
        }
        ?>
        </tbody>
    </table>

    <div class="crv-footer">
        <table class="round-row">
            <tr>
                <td style="width:70%;"></td>
                <td style="width:18%; text-align:right;">Rounded off to Rs.</td>
                <td style="width:12%; text-align:right;" class="dyn">
                    <?php echo number_format((float)$rounded_total, 0) . '-00'; ?>
                </td>
            </tr>
        </table>

        <p style="margin:10px 0; font-weight:bold;" class="dyn">
            <?php
            if ($amount_in_words !== '') {
                echo '(' . htmlspecialchars($amount_in_words) . ')';
            }
            ?>
        </p>

        <p style="margin:10px 0;">
            Certified that the book (s) mentioned above have been taken on charge in the accession Register
        </p>

        <p style="margin:6px 0; font-weight:bold;" class="dyn">
            <?php
            echo 'Agreed Rate = ' . rtrim(rtrim(number_format((float)$discount, 3, '.', ''), '0'), '.');          
            ?> %
        </p>

        <p style="margin:6px 0; font-weight:bold;" class="dyn">
            <?php echo htmlspecialchars((string)$conversion_rates); ?>
        </p>
    </div>
    
    <?php if ($signatory_html !== '') { ?>
    <div class="signature">
    <?php echo $signatory_html; ?>
</div>
    <?php } ?>

</div>
