<?php
    $sid=$this->uri->segment('3');
    $table='student';
    $acedemic_session=$this->session->userdata('acedemic_session');
    $course_id='';
    $subject_stream='';
    $course='';
    $get_student_info=$this->NewFeeModel->get_item_data($sid,$table);
    if($get_student_info){
        foreach($get_student_info as $student_info){
            $get_course_data=$this->NewFeeModel->get_studdents_current_course_data($sid,$acedemic_session);
            if($get_course_data){
                foreach($get_course_data as $course_data){
                    $course_id=$course_data['course_id'];
                }
            }
            $courses_table='courses';
            $get_course_info=$this->NewFeeModel->get_item_data($course_id,$courses_table);
            if($get_course_info){
                foreach($get_course_info as $course_info){
                    $course=$course_info['course'];
                    $subject_stream=$course_info['subject_stream'];
                }
            }
?>

<style>
    .school-header h1{text-align:center;font-size:22px;font-weight:700;}
    h2{text-align:center;font-size:18px;font-weight:600;}
    .student-name{width:40%;float:left;}
    .student-father-name{width:40%;float:left;}
    .student-course{width:20%;float:left;}
    .student-contact-info{width:50%;float:left;}
    .student-contact-email-info{width:50%;float:left;}
</style>
<div class="container-fluid mt-3 mb-3 bg-white">
    <div class="row">
        <div class="col-sm-12 pt-3 pb-3">
            <ul class="breadcrumb">
                <li class="breadcrumb-item">Home</li>
                <li class="breadcrumb-item"><a href="<?php echo base_url();?>Fee/all_student">Fee</a></li>
                <li class="breadcrumb-item"><a href="<?php echo base_url();?>Fee/pay/<?php echo $sid;?>"><?php echo $student_info['student_name'];?></a></li>
                <li class="breadcrumb-item">Ledger</li>
            </ul>
            <div class="table-responsive">
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th colspan="2">
                                <div class="profile-div text-center">
                                    <img src="<?php echo base_url();?>assets/student-profile-pic.jpg" style="border-radius:50%;">
                                </div>
                            </th>
                        </tr>
                        <tr>
                            <th style="text-align:right;">Student Name : </th>
                            <td><?php echo $student_info['student_name'];?></td>
                        </tr>
                        <tr>
                            <th style="text-align:right;">Class : </th>
                            <td><?php echo $course; echo " "; echo $subject_stream;?></td>
                        </tr>
                        <tr>
                            <th style="text-align:right;" >Father's Name : </th>
                            <td><?php echo $student_info['father_name'];?></td>
                        </tr>
                        <tr>
                            <th style="text-align:right;">Mob. No. : </th>
                            <td><?php echo $student_info['contact_no'];?></td>
                        </tr>
                        <tr>
                            <th style="text-align:right;">Address : </th>
                            <td><?php echo $student_info['address']; ?></td>
                        </tr>
                    </thead>
                </table>
            </div>
                
            
            
            <div class="table-responsive mt-3">
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>Month & Date</th>
                            <th>Particulars</th>
                            <th>PaymentMode</th>
                            <th>Amount</th>
                            <!--<th>Debit</th>-->
                            <!--<th>Credit</th>-->
                            <th>Balance</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        // $ledger_table='student_ledger_'.$acedemic_session.'';
                        $ledger_table='student_ledger';
                        $balance=0;
                        $get_ledger_data=$this->LedgerModel->get_ledger_data($sid,$acedemic_session,$ledger_table);
                        if($get_ledger_data){
                            foreach($get_ledger_data as $ledger_data){
                                $fee_months='';
                                if($ledger_data['fee_type'] == 'monthly_fee' && $ledger_data['inv_no'] !=0){
                                    $get_fee_invoice_data=$this->LedgerModel->get_invoice_info($ledger_data['inv_no']);
                                    if($get_fee_invoice_data){
                                        foreach($get_fee_invoice_data as $fee_invoice_data){
                                            $fee_months=$fee_invoice_data['months'];
                                        }
                                    }
                                    
                                }
                                $balance=$balance + $ledger_data['debit'] - $ledger_data['credit'];
                        ?>
                        <tr>
                            <td><?php echo date('d-M-Y',strtotime($ledger_data['date'])); ?></td>
                            <td><?php echo $ledger_data['particulars']; if($fee_months !=''){?><span style="text-transform:uppercase;">(<?php echo $fee_months;?>)</span><?php } ?></td>
                            
                            <td><?php echo $ledger_data['payment_mode']; ?></td>
                            <td>
                                <?php if($ledger_data['credit'] !=0){echo $ledger_data['credit'];}?>
                                <?php if($ledger_data['debit'] !=0){echo $ledger_data['debit'];}?>
                                <?php echo "<b>".$ledger_data['trans_type']."</b>"; ?>
                            </td>
                            <!--<td><?php if($ledger_data['debit'] !=0){echo $ledger_data['debit'];}?></td>-->
                            <!--<td><?php if($ledger_data['credit'] !=0){echo $ledger_data['credit'];}?></td>-->
                            <td><?php echo $balance; ?></td>
                            
                        </tr>
                        <?php
                            }
                        }
                        ?>
                        
                    </tbody>
                </table>
            </div>
            
        </div>
    </div>
</div>
<?php
    }
}
?>