<?php
include_once 'header.php';

?>
<div class="page-wrapper">
        <!-- ============================================================== -->
        <!-- Bread crumb and right sidebar toggle -->
        <!-- ============================================================== -->
        <div class="page-breadcrumb">
          <div class="row">
            <div class="col-12 d-flex no-block align-items-center">
              <h4 class="page-title">All Fee List</h4>
              <div class="ms-auto text-end">
                <nav aria-label="breadcrumb">
                  <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="#">Dashboard</a></li>
                    <li class="breadcrumb-item active" aria-current="page">
                     Student Fee
                    </li>
                    <li class="breadcrumb-item"><a id="add" href="#">Add Fee</a></li>
                  </ol>
                </nav>
              </div>
            </div>
          </div>
        </div>
        <!-- ============================================================== -->
        <!-- End Bread crumb and right sidebar toggle -->
    
        <div class="container-fluid row">
            <div class="col-12">  
              <div class="card">
                <div class="card-body">
                  <h5 class="card-title"> </h5>
                  <div class="table-responsive">
                    <table id="zero_config" class="table table-striped table-bordered">
                      <thead>
                        <tr>
                          <th>S.No</th>
                          <th>Student ID</th>
                          <th>Student Name</th>
                          <th>Payment Mode</th>
                          <th>Amount</th>
                          <th>Action</th>
                        </tr>
                      </thead>
                      <tbody>
                        
                              <?php
                                
                                $obj=new Database();
                                $id='';
                                if(isset($_POST['s_id']))
                                {
                                    $id=" student_id=".$_POST['s_id'];                                 
                                   
                                }
                                else{
                                   $id=null;
                                }
                                $obj->select('students_fee','*',null,$id,'id DESC',null);
                                $result=$obj->getResult(); 
                                $c=0;
                                                              
                                foreach($result as list("f_id"=>$id,"student_id"=>$s_id,"student_name"=>$s_name,"payment_mode"=>$pay_mode,"amount"=>$amount))
                                {
                            
                                  $c=$c+1;
                                  echo "<tr><td>$c</td><td>$s_id</td><td>$s_name</td><td>$pay_mode</td><td>$amount</td><td><a href='../admin/core/feeReciept.php?f_id=".$id."' class='badge btn-info btn-view' data-id=".$id."><span class='mdi-delete'></span></a>&nbsp;&nbsp;</td></tr>";
                                }
                            
                                ?>
                      </tbody>
                  
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <!-- ============================================================== -->
          <!-- End PAge Content -->
          <!-- ============================================================== -->
          <!-- ============================================================== -->
<!-- The Modal -->
<div class="modal" id="myModal">
  <div class="modal-dialog" style="max-width: 76%;">
    <div class="modal-content">

      <!-- Modal Header -->
        <div class="modal-header">
          <h4 class="modal-title">Add Student Fee</h4>
          <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
        </div>

      <!-- Modal body -->
      <div class="modal-body">
                              <div class="row">
                                                    <div class="col-md-6">
                                                        <div class="card">
                                                            <form class="form-horizontal" id="modelForm1">
                                                                <div class="card-body">
                                                                    <h4 class="card-title">Personal Info</h4>
                                                                    <div class="form-group row">
                                                                        <label for="s_id"
                                                                        class="col-sm-3 text-end control-label col-form-label">Student ID</label>
                                                                        <div class="col-sm-9">
                                                                        <?php
                                                                        if(isset($_POST['s_id']))
                                                                        {
                                                                            echo "<select class='form-control' id='s_id' name='s_id'>";
                                                                            echo "<option></option><option>".$_POST['s_id']."</option></select>";
                                                                           
                                                                        }
                                                                        else
                                                                        {
                                                                          echo  "<input type='text' class='form-control' id='s_id' placeholder='Student ID'/>";

                                                                        }

                                                                        ?>
                                                                        </div>
                                                                    </div>
                                                                    <div class="form-group row">
                                                                        <label for="name" class="col-sm-3 text-end control-label col-form-label">
                                                                        Student Name</label>
                                                                        <div class="col-sm-9">
                                                                            <input type="text" class="form-control" id="name"  readonly/>
                                                                        </div>
                                                                    </div>
                                                                    <div class="form-group row">
                                                                        <label for="fname" class="col-sm-3 text-end control-label col-form-label">
                                                                        Father Name</label>
                                                                        <div class="col-sm-9">
                                                                        <input type="text" class="form-control" id="fname"  readonly/>
                                                                        </div>
                                                                    </div>       
                                               
                                                                    <div class="form-group row">
                                                                        <label for="batch_no" class="col-sm-3 text-end control-label col-form-label">Batch No</label>
                                                                        <div class="col-sm-9">
                                                                            <select class="form-control" id="batch_no">
                                                                              
                                          
                                                                            </select>
                                                                        </div>
                                                                    </div>
                                                                    <div class="form-group row">
                                                                        <label for="c_name" class="col-sm-3 text-end control-label col-form-label">
                                                                        Course Name</label>
                                                                        <div class="col-sm-9">
                                                                        <input type="text" class="form-control" id="c_name"  readonly/>
                                                                        </div>
                                                                    </div> 
                                                                    <div class="form-group row">
                                                                        <label for="c_fee" class="col-sm-3 text-end control-label col-form-label">
                                                                        Course Fee</label>
                                                                        <div class="col-sm-9">
                                                                        <input type="text" class="form-control" id="c_fee"  readonly/>
                                                                        </div>
                                                                    </div>    
                                                                </div>
                                                            </form>
                                                        </div>
                                                    </div>
                                                    <div class="col-md-6">
                                                        <div class="card">
                                                            <form class="form-horizontal" id="modelForm2">
                                                                <div class="card-body">
                                                                    <h4 class="card-title">Payment Details</h4>
                                                                    <div class="form-group row">
                                                                        <label for="p_fee" class="col-sm-3 text-end control-label col-form-label">
                                                                        Paid Fee</label>
                                                                        <div class="col-sm-9">
                                                                        <input type="text" class="form-control" id="p_fee"  readonly/>
                                                                        </div>
                                                                    </div>
                                                                    <div class="form-group row">
                                                                        <label for="pp_fee" class="col-sm-3 text-end control-label col-form-label">
                                                                        Pending Fee</label>
                                                                        <div class="col-sm-9">
                                                                        <input type="text" class="form-control" id="pp_fee"  readonly/>
                                                                        </div>
                                                                    </div>
                                                                    <div class="form-group row">
                                                                        <label for="mode" class="col-sm-3 text-end control-label col-form-label">Payment Mode</label>
                                                                        <div class="col-sm-9">
                                                                            <select class="form-control" id="mode">
                                                                                <option> </option>
                                                                                <option>Cash Payment</option>
                                                                                <option>UPI Payment</option>
                                                                                <option>A/C Payment</option>
                                                                                <option>Cheque Payment</option>
                                                                            </select>
                                                                        </div>
                                                                </div>  
                                                                    <div class="form-group row">
                                                                        <label for="desc" class="col-sm-3 text-end control-label col-form-label"> Description</label>
                                                                        <div class="col-sm-9">
                                                                            <input type="text" class="form-control" id="desc"/>
                                                                        </div>
                                                                    </div> 
                                                                    <div class="form-group row">
                                                                        <label for="fee" class="col-sm-3 text-end control-label col-form-label"> Amount</label>
                                                                        <div class="col-sm-9">
                                                                            <input type="text"  class="form-control" id="fee"/>
                                                                        </div>
                                                                    </div> 
                                                                
                                                                        <div class="form-group row">
                                                                            <label for="p_date" class="col-sm-3 text-end control-label col-form-label">Payment Date</label>
                                                                            <div class="col-sm-9">
                                                                                <input type="date" class="form-control date-inputmask" id="p_date" value="<?php echo date('Y-m-d');?>"/>
                                                                            </div>
                                                                        </div>

                                                                </div>
                                                            </form>                                                                                  
                                                      </div>          
                                                    </div>
                              </div>                
      </div>
      <!-- Modal footer -->
        <div class="modal-footer">
          <button type="button" class="btn btn-danger" id="save_data">Save Record</button>
        </div>
    </div>
  </div>
</div>

          <footer class="footer text-center">
          All Rights Reserved by ECI. Designed and Developed by
          <a href="https://www.ntlogix.in">NTLogix</a>.
        </footer>
       
   <!-- All Jquery -->
    <!-- ============================================================== -->
    <script src="../assets/libs/jquery/dist/jquery.min.js"></script>
    <!-- Bootstrap tether Core JavaScript -->
    <script src="../assets/libs/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <!-- slimscrollbar scrollbar JavaScript -->
    <script src="../assets/libs/perfect-scrollbar/dist/perfect-scrollbar.jquery.min.js"></script>
    <script src="../assets/extra-libs/sparkline/sparkline.js"></script>
    <!--Wave Effects -->
    <script src="../dist/js/waves.js"></script>
    <!--Menu sidebar -->
    <script src="../dist/js/sidebarmenu.js"></script>
    <!--Custom JavaScript -->
    <script src="../dist/js/custom.min.js"></script>
    <!-- this page js -->
    <script src="../assets/extra-libs/multicheck/datatable-checkbox-init.js"></script>
    <script src="../assets/extra-libs/multicheck/jquery.multicheck.js"></script>
    <script src="../assets/extra-libs/DataTables/datatables.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <script>
      var msg;
      var icon;
      function showAlert(msg,icon) {
          Swal.fire({
              title: msg,
              icon: icon,
              confirmButtonText: 'Close'
          });
      }
    $(document).ready(function(){
                $("#zero_config").DataTable();
                $('#add').click(function(e){ 
                             e.preventDefault();
                            $("#modelForm1").get(0).reset();
                            $("#modelForm2").get(0).reset();              
                          setTimeout(function(){$("#myModal").modal('show');}, 100);
                });
                $("table").on("click",".btn-view",function(){
                    var id = $(this).data("id");                      
                  

                });
                $("table").on("click",".btn-delete",function(){
                    var id = $(this).data("id");                      
                    $.ajax({
                                url:"../admin/core/delete.php",
                                method:"post",
                                data: {
                                   id:id,
                                   state:'sFee',                          
                                },
                                success: function(data){                        
                                  if(data.length===1)
                                  {                            
                                    showAlert('Fee Txn Successfully Deleted!','success');
                                    setTimeout(function(){window.location='./fee-List.php';}, 2000);                                   
                                  }
                                }
                            });   
                  

                });
                $('#fee').keypress(function (e) {    
    
                      var charCode = (e.which) ? e.which : event.keyCode    

                      if (String.fromCharCode(charCode).match(/[^0-9]/g))    

                          return false;                        

                  });
                $('#batch_no').change(function(){
                                              var batch_no = $("#batch_no").val();                                             
                                              $.ajax({
                                                  url:"../admin/core/fetch_fee.php",
                                                  method:"post",
                                                  data: {
                                                      batch_no:batch_no,                          
                                                  },
                                                  success: function(data){
                                                    $('#c_name').val(data[0].course_name);
                                                    $('#c_fee').val(data[0].course_fee);
                                                    $('#p_fee').val(data[0].p_fee);
                                                    $('#pp_fee').val(data[0].pp_fee);
                                                  }
                                              }); 
                  }); 
                $('#s_id').change(function(){
                            var name = $("#s_id").val();
                            $.ajax({
                                url:"../admin/core/fetch_student.php",
                                method:"post",
                                data: {
                                    name:name,                          
                                },
                                success: function(data){
                  
                                    $('#name').val(data[0].name);
                                    $('#fname').val(data[0].father_name);                                                              
                                    if(data===0)
                                    {                                   
                                    $('#name').val('');
                                    $('#fname').val('');                                  
                                    $('#batch_no').val(null).trigger('change');                               
                                    }
                                }
                            }); 
                            var id = $("#s_id").val();
                            $.ajax({
                                url:"../admin/core/fetch_asignCourse.php",
                                method:"post",
                                data: {
                                    id:id,                          
                                },
                                success: function(data){                                
                                      if(data)
                                      {
                                        $("#batch_no").empty();
                                        $.each(data, function(key, value) {
                                                $('#batch_no')
                                                      .append($('<option>', { value : value })
                                                      .text(value));
                                            });
                                       }
                                          
                                }
                            });                 
                });
                $('#s_id').keypress(function (e) {    
    
                  var charCode = (e.which) ? e.which : event.keyCode    

                  if (String.fromCharCode(charCode).match(/[^0-9]/g))    

                      return false;                        

              });
              $('#save_data').click(function(){
                   var s_name = $("#name").val();
                   var s_id = $("#s_id").val();
                   var batch_no=$("#batch_no").val();
                   var payment_mode=$("#mode").val();
                   var description=$("#desc").val();
                   var course_name = $("#c_name").val();
                   var fee=$("#fee").val();    
                   var pp_fee=$("#pp_fee").val(); 
                   var p_fee=$("#p_fee").val();           
                   var p_date=$("#p_date").val();
                   if((fee.length===0)||(payment_mode.length===0)||(description.length===0)||(pp_fee.length===0))
                    {
                      showAlert('Some Fields Empty!','warning');
                      return 0;
                    } 
                    if(p_fee>=fee)
                    {
                      
                      showAlert('No Dues Pending !','warning');
                      return 0;
                    }
                         $.ajax({
                             url:"../admin/core/add-studentFee.php",
                             method:"post",
                             data: {
                                    s_name:s_name,
                                    s_id :s_id,
                                    batch_no:batch_no,
                                    payment_mode:payment_mode,
                                    description:description,
                                    course_name:course_name,
                                    fee:fee,                  
                                    p_date:p_date,
                                                        
                             },
                             success: function(data){
                               if(data==1)
                               {
                                $("#myModal").modal('toggle');
                                showAlert('Fee Successfully Added!','success');
                                setTimeout(function(){window.location='./student-fee.php';}, 2000); 
                               }
                             }
                         });
                    
               });
               
    }); 

</script>

  </body>
</html>