<style>
    .w_25{
        width:25%;
        float:left;
        padding: 10px 10px;
    }
    
    .w_100{
        width:100%;
        display:inline-block;
        padding: 10px 10px;
    }
</style>

<?php
$fee_no=$this->uri->segment('3');
$acedemic_session=$this->session->userdata('acedemic_session');
$get_fee_data=$this->NewFeeModel->get_invoice_data($fee_no);
if($get_fee_data){
    foreach($get_fee_data as $fee_data){
        $sid=$fee_data['sid'];
        $course_id='';
        $new_admission="";
        $get_student_courses_data=$this->NewFeeModel->get_studdents_current_course_data($sid,$acedemic_session);
        if($get_student_courses_data){
            foreach($get_student_courses_data as $courses_data){
                $course_id=$courses_data['course_id'];
                $new_admission=$courses_data['new_admission'];
            }
        }
        
        
        $extra_amount_used_refrence=$fee_data['extra_amount_used_refrence'];
        $balance_amount_added_refrence=$fee_data['balance_amount_added_refrence'];
        $calibrated_prev_balance_amount=0;
        $calibrated_prev_extra_amount=0;
        
        if($balance_amount_added_refrence != 0){
            $get_calibrated_balance_amount=$this->NewFeeModel->get_invoice_data($balance_amount_added_refrence);
            if($get_calibrated_balance_amount){
                foreach($get_calibrated_balance_amount as $calibrated_balance_amount_info){
                    $calibrated_prev_balance_amount = $calibrated_prev_balance_amount + $calibrated_balance_amount_info['balance_amount'];
                }
            }
        }
        
        if($extra_amount_used_refrence !=0){
            $get_extra_amount_used_refrence_info=$this->NewFeeModel->get_invoice_data($extra_amount_used_refrence);
            if($get_extra_amount_used_refrence_info){
                foreach($get_extra_amount_used_refrence_info as $extra_amount_used_refrence_info){
                    $calibrated_prev_extra_amount= $calibrated_prev_extra_amount + $extra_amount_used_refrence_info['extra_amount'];
                }
            }
        }
        
        
        
        
        $estimate_amount=$fee_data['estimate_amount'];
        $received_amount=$fee_data['received_amount'];
        $discount=$fee_data['discount'];
        $balance_amount=$fee_data['balance_amount'];
        $extra_amount=$fee_data['extra_amount'];
        $fee_date=$fee_data['fee_date'];
        
    
        // $course_id=$fee_data['course_id'];
        
        $apr_status=$fee_data['april'];
        $may_status=$fee_data['may'];
        $jun_status=$fee_data['june'];
        $jul_status=$fee_data['july'];
        $aug_status=$fee_data['august'];
        $sep_status=$fee_data['september'];
        $oct_status=$fee_data['october'];
        $nov_status=$fee_data['november'];
        $dec_status=$fee_data['december'];
        $jan_status=$fee_data['january'];
        $feb_status=$fee_data['february'];
        $mar_status=$fee_data['march'];
        
        $get_cal_estimate_fee=0;
        $apr_fee=0;
        $may_fee=0;
        $jun_fee=0;
        $jul_fee=0;
        $aug_fee=0;
        $sep_fee=0;
        $oct_fee=0;
        $nov_fee=0;
        $dec_fee=0;
        $jan_fee=0;
        $feb_fee=0;
        $mar_fee=0;
        $get_month_fee_data=$this->NewFeeModel->course_fee_structure_for_session($acedemic_session,$course_id);
        // $get_cal_estimate_fee=$get_cal_estimate_fee + 0;    
        if($apr_status ==1){
            if($get_month_fee_data){
                foreach($get_month_fee_data as $month_fee_data){
                    // if($month_fee_data['april'] == 1){
                    //     $apr_fee=$apr_fee + $month_fee_data['amount'];
                    // }
                    
                    
                    if($new_admission == "Yes"){
                        $apr_fee= $apr_fee + $month_fee_data['amount'];    
                    }else{
                        if($month_fee_data['fee_head'] == 2 || $month_fee_data['fee_head'] == 1){
                            
                        }else{
                            $apr_fee= $apr_fee + $month_fee_data['amount'];
                        }
                    }
                }
            }
        }
        
        if($may_status ==1){
            if($get_month_fee_data){
                foreach($get_month_fee_data as $month_fee_data){
                    if($month_fee_data['may'] == 1){
                        $may_fee=$may_fee + $month_fee_data['amount'];
                    }
                }
            }
        }
        
        
        if($jun_status ==1){
            if($get_month_fee_data){
                foreach($get_month_fee_data as $month_fee_data){
                    if($month_fee_data['june'] == 1){
                        $jun_fee=$jun_fee + $month_fee_data['amount'];
                    }
                }
            }
        }
        
        if($jul_status ==1){
            if($get_month_fee_data){
                foreach($get_month_fee_data as $month_fee_data){
                    if($month_fee_data['july'] == 1){
                        $jul_fee=$jul_fee + $month_fee_data['amount'];
                    }
                }
            }
        }
        
        if($aug_status ==1){
            if($get_month_fee_data){
                foreach($get_month_fee_data as $month_fee_data){
                    if($month_fee_data['august'] == 1){
                        $aug_fee=$aug_fee + $month_fee_data['amount'];
                    }
                }
            }
        }


        if($sep_status ==1){
            if($get_month_fee_data){
                foreach($get_month_fee_data as $month_fee_data){
                    if($month_fee_data['september'] == 1){
                        $sep_fee=$sep_fee + $month_fee_data['amount'];
                    }
                }
            }
        }
        
        if($oct_status ==1){
            if($get_month_fee_data){
                foreach($get_month_fee_data as $month_fee_data){
                    if($month_fee_data['october'] == 1){
                        $oct_fee=$oct_fee + $month_fee_data['amount'];
                    }
                }
            }
        }
        
        if($nov_status ==1){
            if($get_month_fee_data){
                foreach($get_month_fee_data as $month_fee_data){
                    if($month_fee_data['november'] == 1){
                        $nov_fee=$nov_fee + $month_fee_data['amount'];
                    }
                }
            }
        }
        
        if($dec_status ==1){
            if($get_month_fee_data){
                foreach($get_month_fee_data as $month_fee_data){
                    if($month_fee_data['december'] == 1){
                        $dec_fee=$dec_fee + $month_fee_data['amount'];
                    }
                }
            }
        }
        
        if($jan_status ==1){
            if($get_month_fee_data){
                foreach($get_month_fee_data as $month_fee_data){
                    if($month_fee_data['january'] == 1){
                        $jan_fee=$jan_fee + $month_fee_data['amount'];
                    }
                }
            }
        }
        
        if($feb_status ==1){
            if($get_month_fee_data){
                foreach($get_month_fee_data as $month_fee_data){
                    if($month_fee_data['february'] == 1){
                        $feb_fee=$feb_fee + $month_fee_data['amount'];
                    }
                }
            }
        }
        
        if($mar_status ==1){
            if($get_month_fee_data){
                foreach($get_month_fee_data as $month_fee_data){
                    if($month_fee_data['march'] == 1){
                        $mar_fee=$mar_fee + $month_fee_data['amount'];
                    }
                }
            }
        }
        
        $get_cal_estimate_fee=$apr_fee + $may_fee + $jun_fee + $jul_fee + $aug_fee + $sep_fee + $oct_fee + $nov_fee + $dec_fee + $jan_fee + $feb_fee + $mar_fee;
        
        
?>

<div class="container-fluid bg-white mt-3 mb-3">
    <div class="row">
        <div class="col-sm-12 pt-3 pb-3">
            <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/cy_invoic">Fee Invoices</a>
                </li>
                <li class="breadcrumb-item">Edit Fee Data</li>
            </ul>
            <div>
                <!--<h1 style="font-size:22px;font-weight:600;" class="text-center text-success">Edit Fee Data</h1>-->
                <!--<hr>-->
                <div class="table-responsive">
                    <table class="table table-bordered table-striped">
                        <tbody>
                            <?php
                            
                            $student_table='student';
                            $get_student_info=$this->NewFeeModel->get_item_data($sid,$student_table);
                            if($get_student_info){
                                foreach($get_student_info as $student_info){
                                

                            ?>
                            <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>
                            <?php 
                            // $acedemic_session=$fee_data['acedemic_session'];
                            // $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'];
                                    $course_table='courses';
                                    $get_course_info=$this->NewFeeModel->get_item_data($course_id,$course_table);
                                    if($get_course_info){
                                        foreach($get_course_info as $course_info){
                            ?>
                            <tr>
                                <th>Class</th>
                                <td><?php echo $course_info['course']; if($course_info['subject_stream'] !=''){echo " - "; echo $course_info['subject_stream'];} ?></td>
                            </tr>
                            <?php
                                        }
                                    }
                            //     }
                            // }
                            ?>

                            <?php
                                }
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
                
                <div class="table-responsive">
                    <table class="table table-bordered table-striped">
                        <thead>
                            <tr>
                                <th>Estimate Amount</th>
                                <th>Received Amount</th>
                                <th>Discount</th>
                                <th>Prev. Balance</th>
                                <th>Prev. Extra</th>
                                <th>Balance</th>
                                <th>Extra</th>
                                <th>Fee Date</th>
                                <th>Bill Book Number</th>
                                
                                <th>Txn No.</th>
                                <th>Fee Info</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td><?php echo $fee_data['estimate_amount']; ?></td>
                                <td><?php echo $fee_data['received_amount']; ?></td>
                                <td><?php echo $fee_data['discount']; ?></td>
                                <td><?php echo $fee_data['balance_amount_added'];?></td>
                                <td><?php echo $fee_data['extra_amount_used'];?></td>
                                <td><?php echo $fee_data['balance_amount']; ?></td>
                                <td><?php echo $fee_data['extra_amount']; ?></td>
                                <td><?php echo date('d-M-Y',strtotime($fee_data['fee_date']));?></td>
                                <td><?php echo $fee_data['bill_book_number']; ?></td>


                                <td><?php echo $fee_data['txn_no']; ?></td>
                                <td><?php echo $fee_data['fee_info'];?></td>
                            </tr>
                            <tr>
                                <th colspan="2">Fee Months</th>
                                <td colspan="9"><span style="text-transform:uppercase;font-size:12px;font-weight:bold;" class="text-success"><?php echo $fee_data['months']; ?></span></td>
                            </tr>
                            <tr>
                                <th colspan="2">Fee Note</th>
                                <td colspan="9" class="text-success" style="font-size:12px;font-weight:bold;"><?php echo $fee_data['fee_note']; ?></td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                <div class="table-responsive">
                    <table class="table table-bordered table-striped">
                        <thead>
                            <tr>
                                <th colspan="7">Calibrated Fee</th>
                            </tr>
                            <tr>
                                <th>Est. Fee</th>
                                <th>Prev. Bal.</th>
                                <th>Prev. Extra</th>
                                <th>Received Fee</th>
                                <th>Discount</th>
                                <th>Balance</th>
                                <th>Extra</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td><?php echo $get_cal_estimate_fee;?></td>
                                <td><?php echo $calibrated_prev_balance_amount;?></td>
                                <td><?php echo $calibrated_prev_extra_amount;?></td>
                                <?php
                                $cal_balance_amount=0;
                                $cal_extra_amount=0;
                                $cal_total_amount=  ($get_cal_estimate_fee) - ($discount) - ($calibrated_prev_extra_amount) + ($calibrated_prev_balance_amount);
                                if($cal_total_amount > $received_amount){
                                    $cal_balance_amount=$cal_total_amount - $received_amount;
                                }else if($received_amount > $cal_total_amount){
                                    $cal_extra_amount=$received_amount - $cal_total_amount;
                                }
                                ?>
                                <?php 
                                
                                
                                // $cal_balance_amount=$get_cal_estimate_fee - $received_amount - $discount;
                                ?>
                                <td><?php echo $received_amount;?></td>
                                <td><?php echo $discount;?></td>
                                <td><?php echo $cal_balance_amount;?></td>
                                <td><?php echo $cal_extra_amount;?></td>
                                
                            </tr>
                        </tbody>
                    </table>
                </div>
                
                <div class="table-responsive">
                    <table class="table table-bordered table-striped">
                        <thead></thead>
                        <tbody id="fee_month_data">
    
                        </tbody>
                    </table>
                </div>
                
                <form method="post" action="#" class="form-outer">
                    <div class="w-100">
                        <h1 class="main-heading">Update Fee Information</h1><hr>
                    <?php
                    if($success){
                        ?>
                        <div class="alert alert-success"><?php echo $success;?></div>
                        <?php
                    }
                    
                    if($error){
                        ?>
                        <div class="alert alert-danger"><?php echo $error;?></div>
                        <?php
                    }
                    ?>

                    <div class="form-group w_100">
                        <label>Fee Months</label>
                        <input type="text" value="<?php echo $fee_data['months'];?>" class="form-control" style="text-transform:uppercase;" readonly>
                    </div>
                    
                    
                    <div class="form-group w_25">
                        <label>Estimate Amount</label>
                        <input type="number" id="estimate_fee" value="<?php echo $get_cal_estimate_fee; ?>" class="form-control" readonly>
                    </div>
                    
                    <div class="form-group w_25">
                        <label>Prev. Balance</label>
                        <input type="number" id="prev_bal" value="<?php echo $calibrated_prev_balance_amount;?>" class="form-control" readonly>
                    </div>
                    
                    <div class="form-group w_25">
                        <label>Prev. Extra</label>
                        <input type="number" id="prev_extra" value="<?php echo $calibrated_prev_extra_amount;?>" class="form-control" readonly>
                    </div>
                    
                    <div class="form-group w_25">
                        <label>Discount</label>
                        <input type="number" id="discount" value="<?php echo $fee_data['discount']; ?>" class="form-control" onclick='myFunction()'>
                    </div>

                    <div class="form-group w_25">
                        <label>Total</label>
                        <input type="number" id="cal_total_amount" value="<?php echo $cal_total_amount; ?>" class="form-control" name="cal_total" readonly>
                    </div>
                    
                    <div class="form-group w_25">
                        <label>Received Amount</label>
                        <input type="hidden" id="fee_no" value="<?php echo $fee_data['fee_no'];?>">
                        <input type="hidden" id="sid" value="<?php echo $fee_data['sid'];?>">
                        <input type="number" id="received_fee" name="received_amount" value="<?php echo $fee_data['received_amount'];?>" class="form-control" required  onclick='myFunction()'>
                    </div>

                    <div class="form-group w_25">
                        <label>Balance</label>
                        <input type="number" id="balance_amount" value="<?php echo $cal_balance_amount; ?>" class="form-control" readonly>
                    </div>
                    <div class="form-group w_25">
                        <label>Extra Amount</label>
                        <input type="number" id="extra_amount" value="<?php echo $cal_extra_amount; ?>" class="form-control" readonly>
                    </div>
                    <div class="form-group w_25">
                        <label>Fee Date</label>
                        <input type="date" value="<?php echo $fee_data['fee_date'];?>" id="fee_date" class="form-control" required>
                    </div>
                    <div class="form-group w_25">
                        <label>Fee Note</label>
                        <input type="text" id="fee_note" class="form-control" value="<?php if($fee_data['fee_note'] !=''){echo $fee_data['fee_note'];}?>" placeholder="Fee Note" >
                    </div>
                    
                    <div class="form-group w_25">
                        <label>Bill Book Number</label>
                        <input type="text" id="bill_book_number" class="form-control" value="<?php if($fee_data['bill_book_number'] !=''){ echo $fee_data['bill_book_number']; } ?>" placeholder="Bill Book Number" >
                    </div>
                    
                    <div class="form-group w_25">
                        <label>Transaction No.</label>
                        <input type="text" id="txn_no" vlaue="<?php if($fee_data['txn_no'] !=''){ echo $fee_data['txn_no']; } ?>" class="form-control" placeholder="Transaction No." >
                    </div>
                    
                    <div class="form-group w_25">
                        <label>Fee Info</label>
                        <input type="text" id="fee_info" value="<?php if($fee_data['fee_info'] !=''){ echo $fee_data['fee_info']; } ?>" class="form-control" placeholder="Fee Info"  >
                    </div>
                    
                    <div class="form-group w_100">
                        <button type="button" class="btn btn-sm btn-warning text-white font-weight-bolder" id="calculate_fee_edit">
                            <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" class="btn btn-sm btn-success ml-3" id="submit_fee_edit">Update Fee</button>
                    </div>
                    </div>
                </form>
            </div>
            
        </div>
    </div>
</div>

<script>

 function myFunction(){
        $("#submit_fee_edit").hide();   
        // alert("Hi");
    }
$(document).ready(function(){
    $("#submit_fee_edit").hide();   
    get_monthly_structure();
    function get_monthly_structure(){
        var acedemic_session =<?php echo $acedemic_session?>;
        var course =<?php echo $course_id;?>;
        var status_4=<?php echo $apr_status; ?>;
        var status_5=<?php echo $may_status; ?>;
        var status_6=<?php echo $jun_status; ?>;
        var status_7=<?php echo $jul_status; ?>;
        var status_8=<?php echo $aug_status; ?>;
        var status_9=<?php echo $sep_status; ?>;
        var status_10=<?php echo $oct_status; ?>;
        var status_11=<?php echo $nov_status; ?>;
        var status_12=<?php echo $dec_status; ?>;
        var status_1=<?php echo $jan_status; ?>;
        var status_2=<?php echo $feb_status; ?>;
        var status_3=<?php echo $mar_status; ?>;
        $.ajax({
            url: '<?php echo base_url() ?>Fee/get_monthly_structure/',
            type: 'POST',
            data: {acedemic_session:acedemic_session,course:course,status_4:status_4,status_5:status_5,status_6:status_6,status_7:status_7,status_8:status_8,status_9:status_9,status_10:status_10,status_11:status_11,status_12:status_12,status_1:status_1,status_2:status_2,status_3:status_3},
            success: function(data){
                // alert(data);
                $("#fee_month_data").html(data);
            }
        });
    }       
    
    $("#calculate_fee_edit").click(function(){
        // alert("Hello");
        $("#submit_fee_edit").show();  
        var est_fee;
        var received_fee;
        var prev_bal;
        var prev_extra;
        var discount;
        var cal_total_amount;
        var balance_amount =0;
        var extra_amount =0;
        var fee_date;
        var bill_book_no;
        var txn_no;
        var feee_info;
        
        
        
        
        est_fee=parseInt($("#estimate_fee").val());
        prev_bal=parseInt($("#prev_bal").val());
        prev_extra=parseInt($("#prev_extra").val());
        discount=parseInt($("#discount").val());
        received_fee=parseInt($("#received_fee").val());
        // alert(discount);
        // alert("Hi");
        // alert(received_fee);
        // cal_total_amount=$("#cal_total_amount").val();
        
        
        cal_total_amount=  (est_fee - (discount + prev_extra)) + parseInt(prev_bal);
        // alert(est_fee);
        // alert(cal_total_amount);
        if(cal_total_amount > received_fee){
            balance_amount=cal_total_amount - received_fee;
        }else if(received_fee > cal_total_amount){
            extra_amount=received_fee - cal_total_amount;
        }

        // alert(balance_amount);
        $("#cal_total_amount").val(cal_total_amount);
        $("#extra_amount").val(extra_amount);
        $("#balance_amount").val(balance_amount);
        
    });
    
    
    $("#submit_fee_edit").click(function(){
        // alert();
        var est_fee;
        var received_fee;
        var prev_bal;
        var prev_extra;
        var discount;
        var cal_total_amount;
        var balance_amount =0;
        var extra_amount =0;
        
        var fee_date;
        var bill_book_number ='';
        var txn_no ='';
        var fee_info ='';        
        var sid =$("#sid").val();
        var fee_no =$("#fee_no").val();
        
        
        
        est_fee=parseInt($("#estimate_fee").val());
        prev_bal=parseInt($("#prev_bal").val());
        prev_extra=parseInt($("#prev_extra").val());
        discount=parseInt($("#discount").val());
        received_fee=parseInt($("#received_fee").val());
        
        fee_date = $("#fee_date").val();
        bill_book_number = $("#bill_book_number").val();
        txn_no = $("#txn_no").val();
        fee_info = $("#fee_info").val();
        
        cal_total_amount=  parseInt((est_fee - (discount + prev_extra)) + parseInt(prev_bal));
        
        if(cal_total_amount > received_fee){
            balance_amount=cal_total_amount - received_fee;
        }else if(received_fee > cal_total_amount){
            extra_amount=received_fee - cal_total_amount;
        }
        
        var course_id = "<?php echo $course_id;?>";
        
        $.ajax({
            url: '<?php echo base_url() ?>Fee/update_fee_invoice/',
            type: 'POST', 
            data: {est_fee:est_fee,received_fee:received_fee,prev_bal:prev_bal,prev_extra:prev_extra,discount:discount,cal_total_amount:cal_total_amount,balance_amount:balance_amount,extra_amount:extra_amount,fee_date:fee_date,bill_book_number:bill_book_number,txn_no:txn_no,fee_info:fee_info,sid:sid,fee_no:fee_no,course_id:course_id},
            success: function(data){
                // $("#list_invoice").html(data);
                alert(data);
            }
        });
        
        
    });
    
});
</script>


<?php
    }
}
?>