<?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  Attendence List of Student </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
                    </li>
                    <li class="breadcrumb-item active" aria-current="page">
                    <a href="#" id="add">Add Attendence</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>Batch No</th>
                          <th>Student Name</th>
                          <th>Time</th>
                          <th>Date</th>
                          <th>Status</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_attendence','*',null,$id,'id DESC',null);
                                $result=$obj->getResult(); 
                                $c=0;                                                             
                                foreach($result as list("id"=>$id,"batch_no"=>$batch_no,"student_name"=>$s_name,"attendence_time"=>$time,"attendence_date"=>$date,"status"=>$status))
                                {                           
                                  $c=$c+1;
                                  echo "<tr><td>$c</td><td class='id'><a href='#'>$batch_no</a></td><td>$s_name</td><td>$time</td><td class='date'>$date</td><td>$status</td><td><button type='button' class='badge btn-danger' name='id[]' data-id=$id id='id'>X</button></td></tr>";
                                }                 
                                ?>
                      </tbody>                  
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <!-- ============================================================== -->
          <!-- End PAge Content -->
          <!-- ============================================================== -->
          <!-- ============================================================== -->
          <div class="modal" id="myModal1">
          <div class="modal-dialog" style="max-width: 72%;">
            <div class="modal-content">

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

              <!-- Modal body -->
              <div class="modal-body">
                                                  <table cellspacing="0" cellpadding="0" border="0" width="100%">
                                                        <tr><td>
                                                        <div class="table-responsive-sm" style="width:100%; height:200px; overflow:auto;">
                                                            <table id="" class="table">
                                                                <thead class="table-light">
                                                                <tr>
                                                                    <th scope="col">S.No</th>
                                                                    <th scope="col">Batch No</th>
                                                                    <th scope="col">Student Name</th>                                                               
                                                                    <th scope="col">Time</th>
                                                                    <th scope="col">Date</th>
                                                                </tr>
                                                                </thead>
                                                                <tbody id="tbody1">                       
                                                                    
                                                                </tbody>                  
                                                            </table>
                                                        </div> 
                                                        </td></tr>    
                                                       </table>   
              </div>
              <!-- Modal footer -->
              <div class="modal-footer">
                <button type="button" class="btn btn-danger" id="print">Print</button>
              </div>

            </div>
          </div>
        </div>
<!-- The Modal -->
<div class="modal" id="myModal">
  <div class="modal-dialog" style="max-width: 72%;">
    <div class="modal-content">
      <!-- Modal Header -->
        <div class="modal-header">
          <h4 class="modal-title">Add Attendence Student</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="form">
                                                                <div class="card-body">
                                                                    <h4 class="card-title"></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="s_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="s_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="s_date" class="col-sm-3 text-end control-label col-form-label">Att. Date</label>
                                                                            <div class="col-sm-9">
                                                                                <input type="date" class="form-control date-inputmask" id="s_date" value="<?php echo date('Y-m-d');?>"/>
                                                                            </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="status" class="col-sm-3 text-end control-label col-form-label">Status</label>
                                                                        <div class="col-sm-9">
                                                                            <select class="form-control" id="status">
                                                                              <option></option>
                                                                              <option>Present</option>
                                                                                       
                                                                            </select>
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                            </form>
                                                        </div>
                                                    </div>
                                                    <div class="col-md-6">
                                                    <div class="card-body">
                                                    <h4 class="card-title"></h4> 
                                                    <div class="table-responsive-sm">    
                                                      <table id="" class="table">
                                                        <thead class="table-light">
                                                          <tr>
                                                            <th scope="col">S.No</th>
                                                            <th scope="col">Date</th>
                                                            
                                                            <th scope="col">Status</th>
                                                          </tr>
                                                        </thead>
                                                        <tbody id="tbody">                       
                                                              
                                                        </tbody>                  
                                                      </table>  
                                                      </div> 
                                                     </div>        
                                                    </div>
                              </div>                
      </div>
      <!-- Modal footer -->
        <div class="modal-footer">
          <button type="button" class="btn btn-danger" id="save_data">Save Attendence</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(){
        /****************************************
       *       Basic Table                   *
       ****************************************/
                  $("#zero_config").DataTable();
                  $('#add').click(function(){
                        $("#myModal").modal('show');
                  });
                  $("table").on("click","#id",function(){
                    var id = $(this).data("id"); 
                    if (confirm('Do you really want to Delete Attendence!',"")) {                                     
                      $.ajax({
                                  url:"../admin/core/delete.php",
                                  method:"post",
                                  data: {
                                    id:id,
                                    state:'atd',                          
                                  },
                                  success: function(data){                        
                                    if(data.length===1)
                                    {                            
                                      showAlert('Attendence Successfully Deleted!','success');
                                      setTimeout(function(){window.location='./attendence.php';}, 2000);                                   
                                    }
                                  }
                              });   
                            }

                    });
                    $(".id").click(function() {
                        var id = $(this).closest('tr').find('td.id').text();
                        var date = $(this).closest('tr').find('td.date').text();             
                    
                                  $.ajax({
                                      url:"../admin/core/fetch_attendence2.php",
                                      method:"post",
                                      data: {
                                          id:id,
                                          date:date,
                                          s_date:'month',                                
                                      },
                                      success: function(data){ 
                                      
                                        var c=0;
                                          $('#tbody1').empty();
                                          $.each(data,function(key,value){  
                                                c=c+1;                                  
                                                $('#tbody1').append('<tr><td>'+c+'</td><td>'+value['batch_no']+'</td><td>'+value['student_name']+'</td><td>'+value['attendence_time']+'</td><td>'+value['attendence_date']+'</td></tr>');                                                         
                                              })
                                      }
                                  });
                        $("#myModal1").modal('show');
                    });
                  $('#batch_no').change(function(){
                            var name = $("#batch_no").val();
                            var s_date='single';
                            var date=$("#s_date").val();
                            $("#status").prop("disabled",false);
                            $.ajax({
                                url:"../admin/core/fetch_attendence.php",
                                method:"post",
                                data: {
                                    name:name,
                                    s_date:s_date,
                                    date:date,
                                },
                                success: function(data){ 
                                  var c=0;
                                   
                                    $('#tbody').empty();
                                    $.each(data,function(key,value){  
                                          c=c+1;                                  
                                          $('#tbody').append('<tr><td>'+c+'</td><td>'+value['attendence_date']+'</td><td>'+value['status']+'</td></tr>');                                     
                                          $("#status").prop("disabled",true);
                                        })
                                }
                            }); 
                  }); 
                  $('#s_id').keypress(function (e) {    
    
                      var charCode = (e.which) ? e.which : event.keyCode    

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

                          return false;                        

                  });
                  $('#s_id').change(function(){
                            var name = $("#s_id").val();
                            $.ajax({
                                url:"../admin/core/fetch_student.php",
                                method:"post",
                                data: {
                                    name:name,                          
                                },
                                success: function(data){
                                  // $('#form')[0].reset();  
                                    //$('#add_data_Modal').modal('hide');  
                                    $('#s_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));
                                            });
                                       }
                                          
                                }
                            });                 
                });
                $('#save_data').click(function(){
                
                   var batch_no = $("#batch_no").val();
                   var student_id = $("#s_id").val();
                   var student_name = $("#s_name").val();                 
                   var s_date = $("#s_date").val();
                   var status = $("#status").val();
                   if((s_name.length === 0)||(batch_no.length === 0)||(status.length === 0)) {
                    showAlert('some field is empty!','warning');return 0;
                                            }                
                         $.ajax({
                             url:"../admin/core/add-attendence.php",
                             method:"post",
                             data: {
                                 batch_no:batch_no,
                                 student_id:student_id,
                                 student_name:student_name,                              
                                 s_date:s_date,
                                 status:status,                                                       
                             },
                             success: function(data){                          
                               if(data==1)
                               {
                                $("#myModal").modal('toggle');
                                showAlert('Attendence Successfully Added!','success');
                                setTimeout(function(){window.location='attendence.php';}, 2000); 
                               }
                               else
                               {
                                $("#myModal").modal('toggle');
                                showAlert('Attendence Not Added!','success');
                                setTimeout(function(){window.location='attendence.php';}, 2000); 
                               
                               }
                             }
                         });
                    
               });
     });

</script>

  </body>
</html>