<style>
    h1{font-size:22px;}
</style>

<?php
$sid =$this->uri->segment('3');
?>
<style>
    body{background:#EAEDED;}
</style>
<div class="container-fluid bg-white mt-3 mb-3">
    <div class="row">
        <div class="col-sm-12 pt-3 pb-3">
            <div class="p-3">
                <h1 class="text-center">Opening Balance Fee Payment</h1>
                <hr>
            <?php
            $table='student';
            $get_student_info=$this->NewFeeModel->get_item_data($sid,$table);
            if($get_student_info){
                foreach($get_student_info as $student_info){
                    ?>

                    
                    <?php
                    $get_opening_balance=$this->OpeningBalanceModel->check_opening_balancce($sid);
                    if($get_opening_balance){
                        foreach($get_opening_balance as $opening_balance){
                            
                            $check_opening_balance_fee=$this->OpeningBalanceModel->check_opening_balancce_fee($sid);
                            if($check_opening_balance_fee){
                                foreach($check_opening_balance_fee as $opening_balance_fee_data){
                                   $balance_opening_fee=$opening_balance_fee_data['balance_amount'];
                                   if($balance_opening_fee ==0 ){
                                       ?>
                                       
                                  
                                       <?php
                                   }else{
  
                                   }
                                }
                            }else{
                            
                            $balance_opening_fee=$opening_balance['opening_fee_amount'];
                            }
                            
                            if($balance_opening_fee == 0){
                                echo "<p class='alert alert-success'>Opening Balance Paid</p>";
                            }else{
                            ?>
                            <div class="form-group">
                                <label>Opening Fee Amount</label>
                                <input type="number" class="form-control" id="opening_fee_amount" value="<?php echo $balance_opening_fee; ?>" readonly>
                                <input type="hidden" id="fee_for_ac_sess" value="<?php echo $opening_balance['fee_for_ac_sess'];?>" class="form-control">
                            </div>
                            <!--<div class="form-group">-->
                            <!--    <label>Opening Fee Amount</label>-->
                            <!--    <input type="number" class="form-control" id="opening_fee_amount" value="<?php echo $opening_balance['opening_fee_amount']; ?>" readonly>-->
                            <!--</div>-->
                                                        <div class="form-group">
                                <label>Opening balance note</label>
                                <input type="text" class="form-control" id="opening_balance_note" value="<?php echo $opening_balance['fee_note'];?>" readonly> 
                            </div>
                            <div class="form-group">
                                <label>Discount</label>
                                <input type="number" value="0" class="form-control" id="discount" placeholder="Discount" onclick='myFunction()'>
                            </div>
                            <div class="form-group">
                                <label>Estimate Fee</label>
                                <input type="number" value="" id="estimate_fee"  class="form-control" readonly>
                            </div>
                            <div class="form-group">
                                <label>Received Amount</label>
                                <input type="number" value="0" class="form-control" id="received_amount" placeholder="Received Amount" onclick='myFunction()'>
                            </div>
                            
                            <div class="form-group">
                                <label>Balance Fee</label>
                                <input type="number" class="form-control" id="balance_fee_amount" readonly>
                            </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>Fee Date :</label>
                                <input type="date" class="form-control" id="fee_date" value="<?php echo date('Y-m-d');?>">
                            </div>
                            
                            <div class="form-group">
                                <label>Fee Mode</label>
                                <select class="form-control" id="fee_mode" onchange='myFunction()'>
                                    <option value="cash">CASH</option>
                                    <option value="UPI">UPI</option>
                                    <option value="cheque">Cheque</option>
                                    <option value="bank-transfer">Bank Transfer</option>
                                    <option value="other">Other</option>
                                </select>
                            </div>
                            
                            <div class="form-group">
                                <button class="btn btn-sm btn-success" id="calculate_fee">Calculate Fee</button>
                                <button class="btn btn-sm btn-success" id="opening_fee_submit">Submit</button>
                            </div>
                            <?php
                            }
                        }
                    }else{
                        ?>
                        <p>No Opeing Balance Found</p>
                        <?php
                    }
                    
                    
                    ?>
                    
                    <?php
                }
            }
            
            ?>
            
            </div>
            <div></div>
        </div>
    </div>
</div>
<script>
function myFunction(){
    $("#opening_fee_submit").hide();
}
    $(document).ready(function(){
        // alert();
        $("#opening_fee_submit").hide();
        $("#calculate_fee").click(function(){
            $("#opening_fee_submit").show();
            var discount=$("#discount").val();
            var received_amount=$("#received_amount").val();
            var opening_fee_amount=$("#opening_fee_amount").val();
            var estimate_fee=opening_fee_amount - discount;
            var balance_fee_amount=opening_fee_amount - received_amount - discount;
            $("#estimate_fee").val(estimate_fee); 
            $("#balance_fee_amount").val(balance_fee_amount); 
        });
        
       $("#opening_fee_submit").click(function(){
           
           
           var sid='<?php echo $sid;?>';
           var discount=$("#discount").val();
           var opening_fee_amount=$("#opening_fee_amount").val();
           var opening_balance_note=$("#opening_balance_note").val();
           var received_amount=$("#received_amount").val();
           var fee_date=$("#fee_date").val();
           var fee_note=$("#fee_note").val();
           var fee_for_ac_sess=$("#fee_for_ac_sess").val();
           var payment_mode=$("#fee_mode").val();
           var balance_fee_amount=$("#balance_fee_amount").val();
           var tid="<?php echo $this->session->userdata('tid');?>";
        //   alert(fee_date);
        //   alert(received_amount);
            if(fee_date == ''){
                swal("Select Fee Date");
            }
             if(fee_date !='' && received_amount !=0){
          
                $.ajax({
                    url: '<?php echo base_url() ?>Fee/add_opening_fee',
                    type: 'POST', 
                    data: { 
                            sid:sid,
                            discount:discount,
                            opening_fee_amount:opening_fee_amount,
                            opening_balance_note:opening_balance_note,
                            received_amount:received_amount,
                            fee_date:fee_date,
                            tid:tid,
                            fee_note:fee_note,
                            fee_for_ac_sess:fee_for_ac_sess,
                            payment_mode:payment_mode,
                            balance_fee_amount:balance_fee_amount
                    },
                    success: function(data){
                        swal(data);
                        location.reload(); 
                    }
                });
                
            }else{
                swal("Enter Received Amount");
            }
        
        
        
       }); 
    });
</script>
