<?php $currency_symbol = $this->customlib->getSchoolCurrencyFormat(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <title><?php echo $this->lang->line('fees_receipt'); ?></title>
    <style type="text/css">
        @page {
            size: 80mm auto;
            margin: 0;
        }
        body {
            font-family: 'Arial Narrow', Arial, sans-serif;
            width: 72mm;
            margin: 0;
            padding: 2mm;
            font-size: 10px;
            line-height: 1.2;
        }
        .text-center { 
            text-align: center; 
        }
        .text-left { 
            text-align: left; 
        }
        .text-right { 
            text-align: right; 
        }
        .bold { 
            font-weight: bold; 
        }
        .item-row { 
            display: flex; 
            justify-content: space-between;
            margin: 1mm 0;
        }
        .line { 
            border-bottom: 1px dashed #000; 
            margin: 2mm 0; 
        }
        .header {
            margin-bottom: 2mm;
        }
        .footer {
            margin-top: 2mm;
            font-size: 9px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            font-size: 9px;
        }
        th, td {
            padding: 1px;
        }
        .total-row {
            border-top: 1px solid #000;
            padding-top: 1mm;
            margin-top: 1mm;
        }
    </style>
</head>
<body>
    <div class="header text-center">
        <?php if ($settinglist[0]['image'] != "") { ?>
            <img style="height:20px; width: auto;" src="<?php echo base_url(); ?>/uploads/school_content/logo/<?php echo $settinglist[0]['image']; ?>"><br/>
        <?php } ?>
        <strong style="font-size: 11px;"><?php echo $settinglist[0]['name']; ?></strong><br>
        <span style="font-size: 9px;"><?php echo $settinglist[0]['address']; ?></span><br>
        <span style="font-size: 9px;">Tel: <?php echo $settinglist[0]['phone']; ?></span>
    </div>

    <div class="line"></div>

    <div class="item-row">
        <div class="text-left">
            <strong>RECEIPT</strong><br>
            <strong>No: 
            <?php 
            if (!empty($feearray)) {
                foreach ($feearray as $fee_key => $fee_receipt) {
                    echo $fee_receipt->receipt;
                    break; // Just get first receipt number
                }
            }
            ?>
            </strong>
        </div>
        <div class="text-right">
            Date: <?php echo date('d/m/Y'); ?>
        </div>
    </div>

    <div class="item-row">
        <div class="text-left">
            <strong>
            <?php 
            if (!empty($feearray)) {
                echo $feearray[0]->firstname . " " . $feearray[0]->lastname;
            }
            ?>
            </strong><br>
            Class: <?php echo !empty($feearray) ? $feearray[0]->class . " (" . $feearray[0]->section . ")" : ''; ?>
        </div>
    </div>

    <div class="line"></div>

    <table>
        <thead>
            <tr>
                <th class="text-left">Fee Description</th>
                <th class="text-right">Amount</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $total_amount = 0;
            $total_deposite_amount = 0;
            $total_discount_amount = 0;
            
            if (!empty($feearray)) {
                foreach ($feearray as $fee_key => $feeList) {
                    if($feeList->is_system){
                        $feeList->amount = $feeList->student_fees_master_amount;
                    }

                    $fee_discount = 0;
                    $fee_paid = 0;
                    $fee_fine = 0;
                    $fee_converted = 0;
                    
                    if (!empty($feeList->amount_detail)) {
                        $fee_deposits = json_decode(($feeList->amount_detail));
                        foreach ($fee_deposits as $fee_deposits_key => $fee_deposits_value) {
                            $payment_mode = $fee_deposits_value->payment_mode;
                            $amount = $fee_deposits_value->amount;

                            if($payment_mode == $currency_symbol){
                                $this->db->select('*');
                                $this->db->from('bank_rates');
                                $this->db->where('home_currency',$payment_mode);
                                $query1 = $this->db->get();
                                $row1_rate = $query1->row();
                                $home_rate = $row1_rate->home_rate;
                                $fee_converted = $home_rate * $amount;
                            } else {
                                $this->db->select('*');
                                $this->db->from('bank_rates');
                                $this->db->where('foreign_currency',$payment_mode);
                                $query3 = $this->db->get();
                                $row1_rate2 = $query3->row();
                                $foreign_rate = $row1_rate2->foreign_rate;
                                $fee_converted = ($amount / $foreign_rate) * 1;
                            }
                            
                            $fee_paid = $fee_paid + $fee_converted;
                            $fee_discount = $fee_discount + $fee_deposits_value->amount_discount;
                            $fee_fine = $fee_fine + $fee_deposits_value->amount_fine;
                        }
                    }
                    
                    $total_amount = $total_amount + $fee_converted;
                    $total_discount_amount = $total_discount_amount + $fee_discount;
                    $total_deposite_amount = $total_deposite_amount + $fee_paid;
                    ?>
                    <tr>
                        <td class="text-left"><?php echo $feeList->name . " - " . $feeList->type; ?></td>
                        <td class="text-right"><?php echo $currency_symbol . number_format($feeList->amount, 2); ?></td>
                    </tr>
                    <?php
                }
            }
            ?>
        </tbody>
    </table>

    <div class="line"></div>

    <div class="item-row total-row">
        <span>Total Amount:</span>
        <span class="bold"><?php echo $currency_symbol . number_format($total_amount, 2); ?></span>
    </div>
    
    <div class="item-row">
        <span>Discount:</span>
        <span class="bold"><?php echo $currency_symbol . number_format($total_discount_amount, 2); ?></span>
    </div>
    
    <div class="item-row">
        <span>Amount Paid:</span>
        <span class="bold"><?php echo $currency_symbol . number_format($total_deposite_amount, 2); ?></span>
    </div>

    <div class="line"></div>

    <div class="footer text-center">
        <div>Thank you for your payment!</div>
        <div style="font-size: 8px; margin-top: 1mm;">
            <?php echo $settinglist[0]['bank_details1'] ? substr($settinglist[0]['bank_details1'], 0, 40) . '...' : ''; ?>
        </div>
    </div>

    <script>
        window.print();
        setTimeout(function() {
            window.close();
        }, 500);
    </script>
</body>
</html>