<?php $currency_symbol = $this->customlib->getSchoolCurrencyFormat(); ?>
<style type="text/css">
    body, html {
        width: 210mm !important;
        height: auto !important;
        margin: 0 !important;
        padding: 0 !important;
        font-size: 8px !important;
    }

    .copy-title {
        text-align: center !important;
        font-weight: bold;
        text-decoration: underline;
        margin: 5px 0;
    }

    .multi-receipt-container {
        width: 210mm !important;
        display: flex !important;
        flex-wrap: wrap !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    .receipt-container {
        width: 105mm !important;
        min-height: 148mm !important;
        height: auto !important;
        margin: 1mm !important;
        padding: 1mm !important;
        border: 1px solid #000;
        box-sizing: border-box;
        page-break-inside: avoid;
    }

    .invoice {
        padding: 0 !important;
        margin: 0 !important;
    }

    .header img {
        height: 15mm !important;
        width: 100% !important;
        object-fit: contain;
    }

    address, h5, .footer, .text, p, div, span {
        text-align: left !important;
        font-size: 8px !important;
        line-height: 1.2 !important;
        margin: 0 0 1.5mm 0 !important;
    }

    .text-center {
        text-align: center !important;
    }
    
    .text-right {
        text-align: right !important;
    }

    .table {
        width: 100% !important;
        font-size: 7px !important;
        margin-bottom: 1mm !important;
        table-layout: fixed;
    }

    .table th, .table td {
        padding: 1px !important;
        line-height: 1.1 !important;
        word-wrap: break-word;
    }

    .table-responsive {
        overflow-x: auto;
        overflow-y: visible;
    }

    /* Column width classes */
    .fees-group-col { width: 15%; }
    .fees-code-col { width: 10%; }
    .due-date-col { width: 8%; }
    .status-col { width: 8%; }
    .amount-col { width: 8%; }
    .receipt-no-col { width: 8%; }
    .mode-col { width: 8%; }
    .date-col { width: 8%; }
    .paid-col { width: 8%; }
    .fine-col { width: 8%; }
    .fine-discount-col { width: 8%; }
    .discount-col { width: 8%; }
    .balance-col { width: 8%; }

    @media print {
        @page {
            size: A4 landscape;
            margin: 2mm !important;
        }
        
        body, html {
            width: 210mm !important;
            height: auto !important;
            font-size: 8px !important;
        }
        
        .receipt-container {
            width: 105mm !important;
            height: auto !important;
            min-height: 148mm !important;
            margin: 1mm !important;
            padding: 1mm !important;
            border: 1px solid #000;
        }
        
        .page-break {
            display: block;
            page-break-before: always;
        }
        
        .table {
            font-size: 7px !important;
        }
    }
</style>

<html lang="en">
    <head>         
        <title><?php echo $this->lang->line('fees_receipt'); ?></title>    
        <link rel="stylesheet" href="<?php echo base_url(); ?>backend/bootstrap/css/bootstrap.min.css"> 
        <link rel="stylesheet" href="<?php echo base_url(); ?>backend/dist/css/AdminLTE.min.css">
    </head>

    <body>       
        <?php 
        $print_copy = explode(',', $settinglist[0]['is_duplicate_fees_invoice']);
        $has_office_copy = in_array('0', $print_copy);
        $has_student_copy = in_array('1', $print_copy);
        $has_bank_copy = in_array('2', $print_copy);
        ?>

        <div class="container"> 
            <!-- First row with two receipts side by side -->
            <div class="multi-receipt-container">
                <?php if($has_office_copy): ?>
                <div class="receipt-container">
                    <!-- Office Copy Content -->
                    <div class="row">
                        <div id="content" class="col-lg-12 col-sm-12">
                            <div class="invoice">
                                <div class="row header">
                                    <div class="col-sm-12">
                                        <img src="<?php echo $this->media_storage->getImageURL('/uploads/print_headerfooter/student_receipt/'.$this->setting_model->get_receiptheader()); ?>" style="height: 100px;width: 100%;">
                                    </div>
                                </div> 
                                
                                <div class="row">
                                    <div class="col-md-12 text text-center copy-title">
                                        <?php echo $this->lang->line('office_copy'); ?>
                                    </div>
                                </div>
                                
                                <div class="row">                           
                                    <div class="col-xs-6">
                                        <br/>
                                        <address>
                                            <strong><?php echo $this->customlib->getFullName($feeList->firstname, $feeList->middlename, $feeList->lastname, $sch_setting->middlename, $sch_setting->lastname); ?></strong><?php echo " (".$feeList->admission_no.")"; ?><br>
                                            <?php echo $this->lang->line('father_name'); ?>: <?php echo $feeList->father_name; ?><br>
                                            <?php echo $this->lang->line('class'); ?>: <?php echo $feeList->class . " (" . $feeList->section . ")"; ?>
                                        </address>
                                    </div>
                                    <div class="col-xs-6 text-right">
                                        <br/>
                                        <address>
                                            <strong>Date: <?php
                                                $date = date('d-m-Y');
                                                echo date($this->customlib->getSchoolDateFormat(), $this->customlib->dateyyyymmddTodateformat($date));
                                                ?></strong><br/>
                                            <strong><?php echo $this->lang->line('academic_year'). ":  " .$sch_setting->session; ?></strong><br/>
                                        </address>                               
                                    </div>
                                </div>
                                
                                <hr style="margin-top: 0px;margin-bottom: 0px;" />
                                
                                <div class="row">
                                    <?php if (!empty($feeList)) { ?>
                                    <div class="table-responsive">
                                        <table class="table table-striped">
                                            <thead>
                                                <tr>
                                                    <th class="fees-group-col"><?php echo $this->lang->line('fees_group'); ?></th>
                                                    <th class="fees-code-col"><?php echo $this->lang->line('fees_code'); ?></th>
                                                    <th class="due-date-col"><?php echo $this->lang->line('due_date'); ?></th>
                                                    <th class="status-col"><?php echo $this->lang->line('status'); ?></th>
                                                    <th class="amount-col text-right"><?php echo $this->lang->line('amount'); ?></th>
                                                    <th class="receipt-no-col text-center"><?php echo $this->lang->line('receipt_id'); ?></th>
                                                    <th class="mode-col text-center"><?php echo $this->lang->line('mode'); ?></th>
                                                    <th class="date-col"><?php echo $this->lang->line('date'); ?></th>
                                                    <th class="paid-col text-right"><?php echo $this->lang->line('paid'); ?></th>
                                                    <th class="fine-col text-right"><?php echo $this->lang->line('fine'); ?></th>
                                                    <th class="fine-discount-col text-right"><?php echo $this->lang->line('fine_discount'); ?></th>
                                                    <th class="discount-col text-right"><?php echo $this->lang->line('discount'); ?></th>
                                                    <th class="balance-col text-right"><?php echo $this->lang->line('balance'); ?></th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php
                                                $fee_discount = 0;
                                                $fee_paid = 0;
                                                $fee_fine = 0;
                                                $fee_fine_disc = 0;
                                                $prev_bal = 0;

                                                if ($feeList->is_system) {
                                                    $feeList->amount = $feeList->student_fees_master_amount;
                                                }

                                                if (!empty($feeList->amount_detail)) {
                                                    $prev_bal = $feeList->amount;
                                                    $fee_deposits = json_decode(($feeList->amount_detail));

                                                    foreach ($fee_deposits as $fee_deposits_key => $fee_deposits_value) {
                                                        $fee_paid = $fee_paid + $fee_deposits_value->amount;
                                                        $fee_discount = $fee_discount + $fee_deposits_value->amount_discount;
                                                        $fee_fine = $fee_fine + $fee_deposits_value->amount_fine;
                                                        $fee_fine_disc = $fee_fine_disc + (isset($fee_deposits_value->fine_discount) ? $fee_deposits_value->fine_discount : 0);
                                                        $prev_bal -= ($fee_deposits_value->amount + $fee_deposits_value->amount_discount);
                                                    }
                                                }

                                                $feetype_balance = $prev_bal;
                                                $total_paid = $fee_paid;
                                                ?>
                                                <tr class="dark-gray">
                                                    <td class="fees-group-col">
                                                        <?php
                                                        if ($feeList->is_system) {
                                                            echo $this->lang->line($feeList->name) . " (" . $this->lang->line($feeList->type) . ")";
                                                        } else {
                                                            echo $feeList->name . " (" . $feeList->type . ")";
                                                        }
                                                        ?>
                                                    </td>
                                                    <td class="fees-code-col">
                                                        <?php
                                                        if ($feeList->is_system) {
                                                            echo $this->lang->line($feeList->code);
                                                        } else {
                                                            echo $feeList->code;
                                                        }
                                                        ?>
                                                    </td>
                                                    <td class="due-date-col">
                                                        <?php
                                                        if ($feeList->due_date) {
                                                            echo date($this->customlib->getSchoolDateFormat(), $this->customlib->dateyyyymmddTodateformat($feeList->due_date));
                                                        }
                                                        ?>
                                                    </td>
                                                    <td class="status-col">
                                                        <?php
                                                        if ($feetype_balance == 0) {
                                                            echo $this->lang->line('paid');
                                                        } else if (!empty($feeList->amount_detail)) {
                                                            echo $this->lang->line('partial');
                                                        } else {
                                                            echo $this->lang->line('unpaid');
                                                        }
                                                        ?>
                                                    </td>
                                                    <td class="amount-col text-right">
                                                        <?php echo $currency_symbol . amountFormat($feeList->amount);
                                                        if (($feeList->due_date != "0000-00-00" && $feeList->due_date != null) && (strtotime($feeList->due_date) < strtotime(date('Y-m-d')))) {
                                                            echo '<span class="text text-danger"> + ' . $currency_symbol . amountFormat($feeList->fine_amount) . '</span>';
                                                        }
                                                        ?>
                                                    </td>
                                                    <td colspan="3"></td>
                                                    <td class="paid-col text-right"><?php echo $currency_symbol . amountFormat($fee_paid); ?></td>
                                                    <td class="fine-col text-right"><?php echo $currency_symbol . amountFormat($fee_fine); ?></td>
                                                    <td class="fine-discount-col text-right"><?php echo $currency_symbol . amountFormat($fee_fine_disc); ?></td>
                                                    <td class="discount-col text-right"><?php echo $currency_symbol . amountFormat($fee_discount); ?></td>
                                                    <td class="balance-col text-right">
                                                        <?php
                                                        if ($feetype_balance > 0) {
                                                            echo $currency_symbol . amountFormat($feetype_balance);
                                                        }
                                                        ?>
                                                    </td>
                                                </tr>

                                                <?php
                                                $fee_deposits = json_decode(($feeList->amount_detail));
                                                if (is_object($fee_deposits)) {
                                                    $sub_prev_bal1 = $feeList->amount;
                                                    foreach ($fee_deposits as $fee_deposits_key => $fee_deposits_value) {
                                                        $sub_prev_bal1 -= ($fee_deposits_value->amount + $fee_deposits_value->amount_discount);
                                                        ?>
                                                        <tr class="white-td">
                                                            <td colspan="5" class="text-right"><img src="<?php echo base_url(); ?>backend/images/table-arrow.png" alt="" /></td>
                                                            <td class="receipt-no-col text-center"><?php echo $fee_deposits_value->receipt_id; ?></td>
                                                            <td class="mode-col text-center"><?php echo $this->lang->line(strtolower($fee_deposits_value->payment_mode)); ?></td>
                                                            <td class="date-col text-center">
                                                                <?php echo date($this->customlib->getSchoolDateFormat(), $this->customlib->dateyyyymmddTodateformat($fee_deposits_value->date)); ?>
                                                            </td>
                                                            <td class="paid-col text-right"><?php echo $currency_symbol . amountFormat($fee_deposits_value->amount); ?></td>
                                                            <td class="fine-col text-right"><?php echo $currency_symbol . amountFormat($fee_deposits_value->amount_fine); ?></td>
                                                            <td class="fine-discount-col text-right"><?php echo $currency_symbol . amountFormat($fee_deposits_value->fine_discount); ?></td>
                                                            <td class="discount-col text-right"><?php echo $currency_symbol . amountFormat($fee_deposits_value->amount_discount); ?></td>
                                                            <td class="balance-col text-right"><?php echo $currency_symbol . amountFormat($sub_prev_bal1); ?></td>
                                                        </tr>
                                                        <?php
                                                    }
                                                }
                                                ?>
                                            </tbody>
                                        </table>
                                    </div>
                                    <?php } ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <?php endif; ?>

                <?php if($has_student_copy): ?>
                <div class="receipt-container">
                    <!-- Student Copy Content -->
                    <div class="row">
                        <div id="content" class="col-lg-12 col-sm-12">
                            <div class="invoice">
                                <!-- Content same as office copy, just change "office_copy" to "student_copy" -->
                                <div class="row header">
                                    <div class="col-sm-12">
                                        <img src="<?php echo $this->media_storage->getImageURL('/uploads/print_headerfooter/student_receipt/'.$this->setting_model->get_receiptheader()); ?>" style="height: 100px;width: 100%;">
                                    </div>
                                </div> 
                                
                                <div class="row">
                                    <div class="col-md-12 text text-center copy-title">
                                        <?php echo $this->lang->line('student_copy'); ?>
                                    </div>
                                </div>
                                
                                <!-- Rest of student copy content (same as office copy) -->
                                <!-- ... -->
                            </div>
                        </div>
                    </div>
                </div>
                <?php endif; ?>
            </div>

            <!-- Bank copy (full width) -->
            <?php if($has_bank_copy): ?>
            <div class="page-break"></div>
            <div class="multi-receipt-container">
                <div class="receipt-container" style="width: 210mm !important;">
                    <!-- Bank Copy Content -->
                    <div class="row">
                        <div id="content" class="col-lg-12 col-sm-12">
                            <div class="invoice">
                                <!-- Content same as office copy, just change "office_copy" to "bank_copy" -->
                                <div class="row header">
                                    <div class="col-sm-12">
                                        <img src="<?php echo $this->media_storage->getImageURL('/uploads/print_headerfooter/student_receipt/'.$this->setting_model->get_receiptheader()); ?>" style="height: 100px;width: 100%;">
                                    </div>
                                </div> 
                                
                                <div class="row">
                                    <div class="col-md-12 text text-center copy-title">
                                        <?php echo $this->lang->line('bank_copy'); ?>
                                    </div>
                                </div>
                                
                                <!-- Rest of bank copy content (same as office copy) -->
                                <!-- ... -->
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php endif; ?>
        </div>
        
        <div class="clearfix"></div>
        <footer></footer>
    </body>
</html>