<?php
include_once 'header.php';
if(isset($_GET['msg']))
{
  ?> 
    <?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();
                                $obj->select('students_attendence','*',null,null,'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='id1[]' data-id=$id id='id1'>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">
                <!-- 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 with Multiple 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-body">
                                                                    <div class="form-group row">
                                                                            <label for="s_date" class="col-sm-3 text-end control-label col-form-label">Attend. 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>                                                                                              
                                                    </div>                                                 
                                                 <div class="col-md-6">
                                                                <div class="card-body">                                                                 
                                                                    <div class="form-group row">
                                                                        <label for="shift" class="col-sm-3 text-end control-label col-form-label">Shift Time</label>
                                                                        <div class="col-sm-9">
                                                                            <select class="form-control" id="shift">
                                                                                <option></option>
                                                                            <?php
                                                                                          
                                                                                          $obj->select('shift','*',null,null,null,null);
                                                                                          $result=$obj->getResult(); 
                                                                                                                  
                                                                                          foreach($result as list("shift_time"=>$shift_time))
                                                                                          {
                                                                                              echo "<option value='{$shift_time}'>$shift_time</option>";                                    
                                                                                          }
                                                                            ?>
                                          
                                                                            </select>
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                    </div>

                                                    <div class="col-md-12">         
                                                        <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 ID</th>
                                                                    <th scope="col">Student Name</th>                                                               
                                                                    <th scope="col">Status</th>
                                                                </tr>
                                                                </thead>
                                                                <tbody id="tbody">                       
                                                                    
                                                                </tbody>                  
                                                            </table>
                                                        </div> 
                                                        </td></tr>    
                                                       </table>   
                                                    </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();
        /****************************************
       *       Model                  *
       ****************************************/
                $('#add').click(function(){
                    $("#myModal").modal('show');
                    
                });
                $("table").on("click","#id1",function(){
                    var id = $(this).data("id"); 
                    if (confirm('Do you really want to Delete Attendence!',"")) {                                     
                    $.ajax({
                                url:"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='./multi-attendence.php';}, 2000);                                   
                                  }
                                }
                            });   
                          }

                    });
         /****************************************
       *       shift Clicker                *
       ****************************************/    
                $('#shift').change(function(){
                            var name = $("#shift").val();
                            var s_date ='multi';
                            var date =$("#s_date").val();
                        
                            $.ajax({
                                url:"core/fetch_attendence.php",
                                method:"post",
                                data: {
                                    name:name, 
                                    s_date:s_date,
                                    date:date,                              
                                },
                                success: function(data){
                                  var c=0;var td=null;
                                  $('#tbody').empty();
                                  $.each(data,function(key,value){
                                      c=c+1; 
                                      if(value['present']=='1')
                                      {
                                        td='<td><input type="checkbox" value="'+value['batch_no']+'" name="status[]" id="status" checked/></td>';
                                      } 
                                      else
                                      {
                                        td='<td><input type="checkbox" value="'+value['batch_no']+'" name="status[]" id="status" data-id='+value['student_id']+'></td>';
                                      }                                    
                                     if(c<=5)
                                      {
                                        $('#tbody').append('<tr><td>'+c+'</td><td>'+value['batch_no']+'</td><td>'+value['student_id']+'</td><td>'+value['student_name']+'</td>'+td+'</tr>/');                                                                        
                                      }
                                  })
                                }
                            }); 
                }); 
                $(".id").click(function() {
                  var id = $(this).closest('tr').find('td.id').text();
                  var date = $(this).closest('tr').find('td.date').text();             
               
                            $.ajax({
                                url:"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');
               });
        /****************************************
       *       save Data                   *
       ****************************************/
                $('#save_data').click(function(){                
                        var option_id = [];
                        var option_id1 = [];
                        var date=$('#s_date').val();
                        var shift=$('#shift').val();

                                    $('input[type=checkbox]').each(function (i) {
                                                if (this.checked) {
                                                    option_id[i] = $(this).val();
                                                }
                                                else
                                                {
                                                    option_id1[i] = $(this).val();
                                                }
                                        });
                                            if(option_id.length === 0) {
                                              showAlert('Please Select atleast One!','warning');return 0;
                                            }
                                    $.ajax({
                                        url:"core/multi-attendence.php",
                                        method:"post",
                                        data: {
                                            option_id:option_id,date:date,option_id1:option_id1,                                                    
                                        },
                                        success: function(data){  
                                                        
                                        if(data==1)
                                        {
                                            //$("#myModal").modal('toggle');
                                            showAlert('Attendence Successfully Updated!','success');
                                            setTimeout(function(){window.location='multi-attendence.php';}, 2000); 
                                        }
                                        else{
                                            showAlert('Attendence Successfully Updated!','success');
                                            setTimeout(function(){window.location='multi-attendence.php';}, 2000); 
                                        }
                                        }
                                    });   
	            });
    });
    </script>

  </body>
</html>