<?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: A4;
            margin: 0;
        }
        
        body {
            font-family: 'Arial Narrow', Arial, sans-serif;
            width: 210mm;
            margin: 0;
            padding: 0;
            font-size: 10px;
            line-height: 1.2;
        }
        
        .receipt-container {
            width: 210mm;
            height: 148mm; /* Half of A4 (297mm/2) with some margin */
            padding: 8mm;
            box-sizing: border-box;
            border-bottom: 1px dashed #ccc;
        }
        
        .receipt-container:last-child {
            border-bottom: none;
        }
        
        .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: 3mm;
            font-size: 9px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            font-size: 9px;
        }
        th, td {
            padding: 2px 1px;
            border-bottom: 1px dashed #eee;
        }
        .total-row {
            border-top: 1px solid #000;
            padding-top: 2mm;
            margin-top: 2mm;
        }
        .receipt-title {
            font-size: 12px;
            font-weight: bold;
            color: #d00;
            margin: 2mm 0;
        }
        .receipt-number {
            color: #00d;
            font-weight: bold;
        }
        .student-info {
            background: #f5f5f5;
            padding: 2mm;
            border-radius: 2px;
            margin: 2mm 0;
        }
        
        /* Print specific styles */
        @media print {
            body {
                margin: 0;
                padding: 0;
            }
            .receipt-container {
                page-break-inside: avoid;
            }
        }
    </style>
</head>
<body>
    <?php 
    // Print two receipts on one page
    for($copy = 0; $copy < 2; $copy++): 
    ?>
    <div class="receipt-container">
        <div class="header text-center">
            <?php if ($settinglist[0]['image'] != "") { ?>
                <img style="height:55px; width: auto;" src="<?php echo base_url(); ?>/uploads/school_content/logo/<?php echo $settinglist[0]['image']; ?>"><br/>
            <?php } ?>
            <strong style="font-size: 12px;"><?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="receipt-title text-center">FEES RECEIPT</div>

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

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

        <table>
            <thead>
                <tr>
                    <th class="text-left">Fee Description</th>
                    <th class="text-right">Amount</th>
                    <th class="text-right">Discount</th>
                    <th class="text-right">Paid</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $total_amount = 0;
                $total_deposite_amount = 0;
                $total_discount_amount = 0;
                $total_fine_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 + $feeList->amount;
                        $total_discount_amount = $total_discount_amount + $fee_discount;
                        $total_deposite_amount = $total_deposite_amount + $fee_paid;
                        $total_fine_amount = $total_fine_amount + $fee_fine;
                        ?>
                        <tr>
                            <td class="text-left"><?php echo $feeList->name; ?></td>
                            <td class="text-right"><?php echo $currency_symbol . number_format($feeList->amount, 2); ?></td>
                            <td class="text-right"><?php echo $currency_symbol . number_format($fee_discount, 2); ?></td>
                            <td class="text-right"><?php echo $currency_symbol . number_format($fee_paid, 2); ?></td>
                        </tr>
                        <?php
                    }
                }
                ?>
            </tbody>
        </table>

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

        
        <div class="item-row">
            <span>Total Discount:</span>
            <span class="bold"><?php echo $currency_symbol . number_format($total_discount_amount, 2); ?></span>
        </div>
        
        <div class="item-row">
            <span>Total Fine:</span>
            <span class="bold"><?php echo $currency_symbol . number_format($total_fine_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="footer text-center">
            <div>Thank you for your payment!</div>
            <div style="font-size: 8px; margin-top: 2mm; color: #666;">
                <?php echo $settinglist[0]['bank_details1'] ? substr($settinglist[0]['bank_details1'], 0, 60) : ''; ?>
            </div>
            <div style="font-size: 8px; color: #666; margin-top: 1mm;">
                <?php echo $settinglist[0]['bank_details2'] ? substr($settinglist[0]['bank_details2'], 0, 60) : ''; ?>
            </div>
            <?php if($copy == 0): ?>
            <div style="font-size: 8px; color: #999; margin-top: 3mm;">
                (School Copy)
            </div>
            <?php else: ?>
            <div style="font-size: 8px; color: #999; margin-top: 3mm;">
                (Parent Copy)
            </div>
            <?php endif; ?>
        </div>
    </div>
    <?php endfor; ?>

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