<?php
$fee_no=$this->uri->segment('3');
$get_fee_info=$this->NewFeeModel->get_invoice_data($fee_no);
if($get_fee_info){
    foreach($get_fee_info as $fee_info){
        
    
        
        $sid=$fee_info['sid'];
        $student_table='student';
        $course_id=$fee_info['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){
                

                $get_student_info=$this->NewFeeModel->get_item_data($sid,$student_table);
                if($get_student_info){
                    foreach($get_student_info as $student_info){
                        
                        if($fee_info['balance_amount_status'] ==1){

?>

<div class="container bg-white mt-3 mb-3 pt-3 pb-3">
    <div class="row">
        <div class="col-sm-12">
            
            <ul class="breadcrumb">
                <li class="breadcrumb-item"><a href="<?php echo base_url();?>Welcome">Home</a></li>
                <li class="breadcrumb-item"><a href="<?php echo base_url();?>Fee/all_students">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">Pay Balance Fee</li>
            </ul>
            
            <div class="table-responsive">
                <!--<table class="table table-bordered table-striped">-->
                <!--    <tr>-->
                <!--        <th></th>-->
                <!--        <th></th>-->
                <!--        <th></th>-->
                <!--        <th></th>-->
                <!--        <th></th>-->
                <!--        <th></th>-->
                <!--        <th></th>-->
                <!--        <th></th>-->
                <!--    </tr>-->
                <!--</table>-->
            </div>
            
            <form method="post">
                <div class="form-group">
                    <label>Student Name</label>
                    <input type="text" name="student_name" value="<?php echo $student_info['student_name']; ?>" class="form-control" readonly>
                </div>
                
                <div class="form-group">
                    <label>Father's Name</label>
                    <input type="text" name="father_name" value="<?php echo $student_info['father_name']; ?>" class="form-control" readonly>
                </div>
                
                <div class="form-group">
                    <label>Course</label>
                    <input type="text" class="form-control" name="course_info" value="<?php echo $course_info['course']; if($course_info['subject_stream']){echo " "; echo $course_info['subject_stream'];}?>" readonly>
                </div>
                
                <div class="form-group">
                    <label>Fee Invoice No</label>
                    <input type="number" id="fee_no" class="form-control" value="<?php echo $fee_no;?>" readonly>
                    <input type="hidden" id="sid" value="<?php echo $sid;?>" >
                    <input type="hidden" id="course_id" value="<?php echo $course_id;?>">
                    
                </div>
                
                <div class="form-group">
                    <label>Fee Date</label>
                    <input type="text" class="form-control" name="invoice_fee_date" value="<?php echo date('d-M-Y',strtotime($fee_info['fee_date'])); ?>" readonly>
                    
                </div>
                
                <div class="form-group">
                    <label>Est. Fee</label>
                    <input type="number" class="form-control" value="<?php echo $fee_info['estimate_amount'];?>" readonly>
                </div>
                
                <div class="form-group">
                    <label>Discount</label>
                    <input type="number" class="form-control" value="<?php echo $fee_info['discount'];?>" readonly>
                    
                </div>
                <?php
                if($fee_info['balance_amount_added'] !=0){
                    ?>
                <div class="form-group">
                    <label>Previous (Balance added)</label>
                    <input type="number" class="form-control" value="<?php echo $fee_info['balance_amount_added'];?>" readonly>
                </div>
                    <?php
                }
                if($fee_info['extra_amount_used'] !=0){
                ?>
                <div class="form-group">
                    <label>Previous (Extra Used)</label>
                    <input type="number" class="form-control" value="<?php echo $fee_info['extra_amount_used'];?>" readonly>
                </div>
                <?php 
                }
                ?>
                <div class="form-group">
                    <label>Received Fee</label>
                    <input type="number" class="form-control" value="<?php echo $fee_info['received_amount'];?>" readonly>
                </div>
                

                
                <div class="form-group">
                    <label>Balance fee amount to be paid</label>
                    <input type="number" id="estimate_fee_amount" class="form-control" value="<?php echo $fee_info['balance_amount'];?>" readonly>
                </div>
                
                <div class="form-group">
                    <label>Discount</label>
                    <input type="number" class="form-control" value="0" id="discount_amount" onclick='myFunction()'>
                </div>
                
                <div class="form-group">
                    <label>Discount Permission By</label>
                    <select class="form-control" id="discount_permission_by">
                        <?php
                        $staff_table='school_staff';
                        $get_staff_data=$this->NewFeeModel->get_data($staff_table);
                        if($get_staff_data){
                            foreach($get_staff_data as $staff_data){
                                if($staff_data['all_permission']== 1 || $staff_data['school_management'] ==1 || $staff_data['admin'] ==1){
                        ?>
                                    <option value="<?php echo $staff_data['name'];?>"><?php echo $staff_data['name'];?></option>
                        <?php
                                }
                            }
                        }
                        ?>
                        
                    </select>
                </div>
                <div class="form-group">
                    <label>Received</label>
                    <input type="number" class="form-control" value="0" id="received_fee_amount" placeholder="Received Fee" onclick='myFunction()'>
                </div>
                
                <div class="form-group">
                    <label>Fee Date</label>
                    <input type="date" class="form-control" id="new_fee_date" value="<?php echo date('Y-m-d');?>" >
                </div>
                
                <div class="form-group">
                    <label>New Balance Fee</label>
                    <input type="number" class="form-control" id="new_balance_fee" value="" readonly>
                </div>
                
                
                <div class="form-group">
                    <label>New Extra Fee</label>
                    <input type="number" class="form-control" id="new_extra_fee" value="" readonly>
                </div>
                
                <div class="form-group">
                    <label>Payment Mode</label>
                    <select class="form-control" id="payment_mode">
                        <option value="cash">CASH</option>
                        <option value="bank-transfer">Bank Transfer</option>
                        <option value="UPI">UPI</option>
                        <option value="cheque">Cheque</option>
                    </select>
                </div>

                
                <div class="form-group">
                    <label>Fee Note</label>
                    <input type="text" id="fee_note" class="form-control" placeholder="Fee Note">
                </div>
                
                <div class="form-group">
                    <!--<label></label>-->
                    <button class="btn btn-sm btn-warning text-danger" id="reboot_btn" type="button">
                        <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-bootstrap-reboot" viewBox="0 0 16 16">
                            <path d="M1.161 8a6.84 6.84 0 1 0 6.842-6.84.58.58 0 1 1 0-1.16 8 8 0 1 1-6.556 3.412l-.663-.577a.58.58 0 0 1 .227-.997l2.52-.69a.58.58 0 0 1 .728.633l-.332 2.592a.58.58 0 0 1-.956.364l-.643-.56A6.812 6.812 0 0 0 1.16 8z"/>
                            <path d="M6.641 11.671V8.843h1.57l1.498 2.828h1.314L9.377 8.665c.897-.3 1.427-1.106 1.427-2.1 0-1.37-.943-2.246-2.456-2.246H5.5v7.352h1.141zm0-3.75V5.277h1.57c.881 0 1.416.499 1.416 1.32 0 .84-.504 1.324-1.386 1.324h-1.6z"/>
                        </svg>
                    </button>
                    <button type="button" id="pay_balance_fee" value="" class="btn btn-sm btn-success">Pay Fee</button>
                </div>
                
            </form>
            
            
        </div>
    </div>
</div>

<?php               
                        }else if($fee_info['balance_amount_status'] ==2){
?>
    <div class="container mt-3 mb-3 bg-white pt-3 pb-3">
        <div class="row">
            <div class="col-sm-12">
                
                
                <ul class="breadcrumb">
                    <li class="breadcrumb-item"><a href="<?php echo base_url();?>Welcome">Home</a></li>
                    <li class="breadcrumb-item"><a href="<?php echo base_url();?>Fee/all_students">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">Pay Balance Fee</li>
                </ul>
                
                <div class="table-responsive">
                    <table class="table table-bordered table-striped">
                            <tr>
                                <th colspan="2" class="text-center">Invoice Inforamtion</th>
                            </tr>
                            <tr>
                                <th>Fee No</th>
                                <td><?php echo $fee_no;?></td>
                            </tr>
                            <tr>
                                <th>Student Name</th>
                                <td><?php echo $student_info['student_name']; ?></td>
                            </tr>
                            <tr>
                                <th>Father's Name</th>
                                <td><?php echo $student_info['father_name'];?></td>
                            </tr>
                            <tr>
                                <th>Course</th>
                                <td><?php echo $course_info['course']; echo " "; echo $course_info['subject_stream'];?></td>
                            </tr>
                            <tr>
                                <th>Previous Balance Added</th>
                                <td><?php echo $fee_info['balance_amount_added'];?></td>
                            </tr>
                            <tr>
                                <th>Extra Used</th>
                                <td><?php echo $fee_info['extra_amount_used'];?></td>
                            </tr>
                            <tr>
                                <th>Est. Fee</th>
                                <td><?php echo $fee_info['estimate_amount'];?></td>
                            </tr>
                            <tr>
                                <th>Discount</th>
                                <td><?php echo $fee_info['discount'];?></td>
                            </tr>
                            <tr>
                                <th>Received</th>
                                <td><?php echo $fee_info['received_amount'];?></td>
                            </tr>
                            <tr>
                                <th>Balance</th>
                                <td><?php echo $fee_info['balance_amount'];?></td>
                            </tr>
                            <tr>
                                <th>Fee Date</th>
                                <td><?php echo date('d-M-Y',strtotime($fee_info['fee_date']));?></td>
                            </tr>
                            <tr>
                                <th>View Invoice</th>
                                <td><a href="<?php echo base_url();?>Fee/invoice/<?php echo $fee_no;?>" class="btn btn-sm btn-warning">Invoice</a></td>
                            </tr>
                    </table>
                    
                    <table class="table table-bordered">
                        <tr>
                            <th>Fee No</th>
                            <th>Est Fee</th>
                            <th>Discount</th>
                            <th>Received</th>
                            <th>Blance / Extra</th>
                            <th>Invoice</th>
                            
                        </tr>
                        <?php
                        $get_balance_fee_invoices_for_fee_no=$this->NewFeeModel->get_balance_fee_invoices_for_fee_no($fee_no,$sid);
                        if($get_balance_fee_invoices_for_fee_no){
                            foreach($get_balance_fee_invoices_for_fee_no as $balance_fee_invoices){
                        ?>
                        <tr class="text-success">
                            <td><?php echo $balance_fee_invoices['fee_no'];?></td>
                            <td><?php echo $balance_fee_invoices['estimate_amount'];?></td>
                            <td><?php echo $balance_fee_invoices['discount'];?></td>
                            <td><?php echo $balance_fee_invoices['received_amount'];?></td>
                            <td><?php echo $balance_fee_invoices['balance_amount']; echo " / "; echo $balance_fee_invoices['extra_amount']?></td>
                            <td><a href="<?php echo base_url();?>Fee/invoice/<?php echo $balance_fee_invoices['fee_no'];?>">Print</a></td>
                        </tr>
                        <?php
                            }
                        }
                        ?>    
                    </table>
                </div>
                
                
            </div>
        </div>
    </div>
<?php
                        }  
                    }   
                }
            }
        }
    
    ?>

    
    <?php
    }
}
?>


<script>
    function myFunction(){
        $("#pay_balance_fee").hide();
    }
    
    $(document).ready(function(){
        $("#pay_balance_fee").hide();
        $(document).on("click","#reboot_btn",function(){
            $("#pay_balance_fee").show();
            var estimate_fee_amount=parseInt($("#estimate_fee_amount").val());
            var discount_amount=parseInt($("#discount_amount").val());
            var received_fee_amount=parseInt($("#received_fee_amount").val());
            // alert("Hello");
            var total_amount=discount_amount + received_fee_amount;
            var new_balance_amount=0;
            var new_extra_amount=0;
            
            if(total_amount < estimate_fee_amount){
                new_balance_amount= estimate_fee_amount - discount_amount - received_fee_amount;    
            }else if(total_amount > estimate_fee_amount){
                new_extra_amount= total_amount - estimate_fee_amount;
            }
            
            $("#new_balance_fee").val(new_balance_amount);
            $("#new_extra_fee").val(new_extra_amount);
            
            
            
            // alert(new_balance_amount);
            // alert(new_extra_amount);
            // alert(new_balance_amount);
            
        })
        
        $(document).on("click","#pay_balance_fee",function(){
            var sid=$("#sid").val();
            var fee_no=$("#fee_no").val();
            var course_id=$("#course_id").val();
            var new_fee_date=$("#new_fee_date").val();
            
            var estimate_fee_amount=parseInt($("#estimate_fee_amount").val());
            var discount_amount=parseInt($("#discount_amount").val());
            var received_fee_amount=parseInt($("#received_fee_amount").val());
            
            var new_balance_fee=parseInt($("#new_balance_fee").val());
            var new_extra_fee=parseInt($("#new_extra_fee").val());
            var payment_mode=$("#payment_mode").val();
            var fee_note=$("#fee_note").val();          
            var discount_permission_by=$("#discount_permission_by").val()
            // alert(new_fee_date);
            // alert(estimate_fee_amount);
            // alert(discount_amount);
            // alert(received_fee_amount);
            // alert(new_balance_fee);
            // alert(new_extra_fee);
            
            if(received_fee_amount == 0){
                swal("Enter Received Amount !");
            }else{
            $.ajax({
                url: '<?php echo base_url() ?>Fee/add_balance_fee/',
                type: 'POST', 
                data: {sid:sid,fee_no:fee_no,course_id:course_id,new_fee_date:new_fee_date,estimate_fee_amount:estimate_fee_amount,discount_amount:discount_amount,received_fee_amount:received_fee_amount,new_balance_fee:new_balance_fee,new_extra_fee:new_extra_fee,fee_note:fee_note,payment_mode:payment_mode,discount_permission_by:discount_permission_by},
                success: function(data){
                    // $("#list_invoice").html(data);
                    alert(data);
                }
            });
            }
            
        });
    });
</script>