<?php
include_once 'header.php';
$obj=new Database();
?>

<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">Dashboard</h4>
              <div class="ms-auto text-end">
                <nav aria-label="breadcrumb">
                  <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="#">Dashboard</a></li>
                  </ol>
                </nav>
              </div>
            </div>
          </div>
        </div>
        <!-- ============================================================== -->
        <!-- End Bread crumb and right sidebar toggle -->
<div class="container-fluid">    
        <div class="row">
                  <div class="col-lg-4">
                      <div class="row">
                        <div class="col-6">
                          <div class="card-hover bg-cyan p-10 text-white text-center">
                            <i class="mdi mdi-account fs-3 mb-1 font-16"></i>
                            <h5 class="mb-0 mt-1"><?php 
                             
                                 $obj->select('students','*',null,null,null,null);
                                 $result=$obj->getResult(); 
                                 $c=0;                  
                                 foreach($result as list("status"=>$status,"date"=>$date))
                                 {                              
                                     $c=$c+1;                               
                                 }
                                echo $c;
                                                          
                            ?></h5>
                            <small class="font-light">All Students</small>
                          </div>
                        </div>
                        <div class="col-6">
                          <div class="card-hover bg-success p-10 text-white text-center">
                            <i class="mdi mdi-plus fs-3 font-16"></i>
                            <h5 class="mb-0 mt-1"><?php 
                             
                             $id="status='Active'";
                             $obj->select('students','*',null,$id,null,null);
                             $result=$obj->getResult(); 
                             $c=0;                  
                             foreach($result as list("status"=>$status,"date"=>$date))
                             {                              
                                 $c=$c+1;                               
                             }
                            echo $c;
                                                      
                        ?></h5>
                            <small class="font-light">Active Students</small>
                          </div>
                        </div>
                        <div class="col-6 mt-3">
                          <div class="card-hover bg-warning p-10 text-white text-center">
                            <i class="mdi mdi-cart fs-3 mb-1 font-16"></i>
                            <h5 class="mb-0 mt-1"><?php 
                             
                             $id="status='Active'";
                             $obj->select('course','*',null,$id,null,null);
                             $result=$obj->getResult(); 
                             $c=0;                  
                             foreach($result as list("status"=>$status))
                             {                              
                                 $c=$c+1;                               
                             }
                            echo $c;
                                                      
                        ?></h5>
                            <small class="font-light">All Course</small>
                          </div>
                        </div>

                        <div class="col-6 mt-3">
                          <div class="card-hover bg-info p-10 text-white text-center">
                            <i class="mdi mdi-web fs-3 mb-1 font-16"></i>
                            <h5 class="mb-0 mt-1"><?php 
                             
                             $id="status='Active'";
                             $obj->select('shift','*',null,$id,null,null);
                             $result=$obj->getResult(); 
                             $c=0;                  
                             foreach($result as list("status"=>$status))
                             {                              
                                 $c=$c+1;                               
                             }
                            echo $c;
                                                      
                        ?></h5>
                            <small class="font-light">All Shift</small>
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="col-lg-4">
                      <div class="row">
                        <div class="col-6">
                          <div class="card-hover bg-info p-10 text-white text-center">
                            <i class="mdi mdi-account fs-3 mb-1 font-16"></i>
                            <h5 class="mb-0 mt-1"><?php                            
                             $id="status='Active'";
                             $obj->select('user','*',null,$id,null,null);
                             $result=$obj->getResult(); 
                             $c=0;                  
                             foreach($result as list("status"=>$status,"date"=>$date))
                             {                              
                                 $c=$c+1;                               
                             }
                            echo $c;
                                                      
                        ?></h5>
                            <small class="font-light">Total Users</small>
                          </div>
                        </div>
                        <div class="col-6">
                          <div class="card-hover bg-danger p-10 text-white text-center">
                            <i class="mdi mdi-plus fs-3 font-16"></i>
                            <h5 class="mb-0 mt-1"><?php 
                                 $obj->select('students_course','*',null,null,null,null);
                                 $result=$obj->getResult(); 
                                 $c=0;
                                                               
                                 foreach($result as list("f_id"=>$id,"student_name"=>$s_name,"batch_no"=>$batch_no,"course_fee"=>$pay_mode))
                                 {
                                   $id="batch_no='".$batch_no."'";
                                   $paid=$obj->Amount_Count('txn_details',$id,'credit');
                                   $fee=$obj->Amount_Count('txn_details',$id,'debit');
                                   $fee=$fee-$paid;
                                   
                                   if($fee>0)
                                   {
                                     $c=$c+$fee;
                                   
                                   }
                                }
                                echo $c;
                                                          
                            ?></h5>
                            <small class="font-light">Pending Payment</small>
                          </div>
                        </div>
                        <div class="col-6 mt-3">
                          <div class="card-hover bg-warning p-10 text-white text-center">
                            <i class="mdi mdi-cart fs-3 mb-1 font-16"></i>
                            <h5 class="mb-0 mt-1"><?php 
                                 $obj->select('students_course','*',null,null,null,null);
                                 $result=$obj->getResult(); 
                                 $c=0;
                                 $month='%'.date('M-Y');                            
                                 foreach($result as list("f_id"=>$id,"student_name"=>$s_name,"batch_no"=>$batch_no,"course_fee"=>$pay_mode))
                                 {
                                   $id="batch_no='".$batch_no."' and date LIKE '".$month."'";
                                   $paid=$obj->Amount_Count('txn_details',$id,'credit');                           
                                   if($fee>0)
                                   {
                                     $c=$c+$paid;
                                   
                                   }
                                }
                                echo $c;
                                                          
                            ?></h5>
                            <small class="font-light">This Month Recieved </small>
                          </div>
                        </div>

                        <div class="col-6 mt-3">
                          <div class="card-hover bg-info p-10 text-white text-center">
                            <i class="mdi mdi-web fs-3 mb-1 font-16"></i>
                            <h5 class="mb-0 mt-1"><?php 
                                 $obj->select('inventory','*',null,null,null,null);
                                 $result=$obj->getResult(); 
                                 $c=0;
                                 $month='%'.date('M-Y');                            
                                 foreach($result as list("amount"=>$amount,"date"=>$date))
                                 {
                                   $id="student_id='' and date LIKE '".$month."'";
                                   $inventory=$obj->Amount_Count('txn_details',$id,'debit');                           
                                   
                                     $c=$c+$inventory;
                                  
                                }
                                echo $c;
                                                          
                            ?></h5>
                            <small class="font-light">This Month Inventory</small>
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="col-lg-4">
                      <div class="row">
                        <div class="col-6">
                          <div class="card-hover bg-primary p-10 text-white text-center">
                            <i class="mdi mdi-account fs-3 mb-1 font-16"></i>
                            <h5 class="mb-0 mt-1"><?php 
                                 $month='%'.date('M-Y'); 
                                 $id="student_id!='' and date LIKE '".$month."'";
                                 $obj->select('txn_details','*',null,$id,null,null);
                                 $result=$obj->getResult(); 
                                 $c=0;                  
                                 foreach($result as list("debit"=>$debit,"credit"=>$credit,"date"=>$date))
                                 {   
                                    if($credit=='')  
                                    {
                                          $c=$c+1; 
                                    }                                                                                               
                                  }
                                echo $c;
                                                          
                            ?></h5>
                            <small class="font-light">This Month Assign Course</small>
                          </div>
                        </div>
                        <div class="col-6">
                          <div class="card-hover bg-dark p-10 text-white text-center">
                            <i class="mdi mdi-plus fs-3 font-16"></i>
                            <h5 class="mb-0 mt-1"><?php 
                             
                             $id="result_status=''";
                             $obj->select('student_exam','*',null,$id,null,null);
                             $result=$obj->getResult(); 
                             $c=0;                  
                             foreach($result as list("status"=>$status))
                             {                              
                                 $c=$c+1;                               
                             }
                            echo $c;
                                                      
                        ?></h5>
                            <small class="font-light">Pending Results</small>
                          </div>
                        </div>
                        <div class="col-6 mt-3">
                          <div class="card-hover bg-danger p-10 text-white text-center">
                            <i class="mdi mdi-cart fs-3 mb-1 font-16"></i>
                            <h5 class="mb-0 mt-1">
                            <?php 

                             $obj->select('student_exam','*',null,null,null,null);
                             $result=$obj->getResult(); 
                             $c=0;                  
                             foreach($result as list("status"=>$status,"exam_date"=>$date))
                             {                              
                                 if(date('Y',strtotime($date))>=date('Y'))   
                                 {
                                  if(date('m',strtotime($date))>=date('m'))   
                                  {
                                    if(date('d',strtotime($date))>=date('d'))   
                                    {
                                        $c=$c+1;
                                    }
                                  }
                                  
                                 }                          
                             }
                                 echo $c;
                                                      
                        ?>

                            </h5>
                            <small class="font-light">Pending Exam</small>
                          </div>
                        </div>

                        <div class="col-6 mt-3">
                          <div class="card-hover bg-info p-10 text-white text-center">
                            <i class="mdi mdi-web fs-3 mb-1 font-16"></i>
                            <h5 class="mb-0 mt-1">
                            <?php 
                                 $month=date('d')."%"; 
                                 $id="status='Present' and attendence_date LIKE '".$month."'";
                                 $obj->select('students_attendence','*',null,$id,null,null);
                                 $result=$obj->getResult(); 
                                 $c=0;                  
                                 foreach($result as list("student_id"=>$student_id))
                                  {                           
                                       $c=$c+1;                             
                                  }
                                echo $c;
                                                          
                            ?>
                            </h5>
                            <small class="font-light">Today Attendence</small>
                          </div>
                        </div>
                      </div>
                    </div>
        </div>
        <br>
        <div class="row">
        <div class="col-md-6">
              <div class="card">
                <div class="card-body">
                  <h5 class="card-title mb-0">Last Payment</h5>
                </div>
                <table class="table">
                  <thead>
                    <tr>
                      <th scope="col">Batch No</th>
                      <th scope="col">Amount</th>
                      <th scope="col">Date</th>
                    </tr>
                  </thead>
                  <tbody>
                  <?php
                                
                                $obj->select('students_fee','*',null,null,'id DESC',4);
                                $result=$obj->getResult(); 
                                $c=0;                                                             
                                foreach($result as list("batch_no"=>$b_no,"amount"=>$dob,"payment_date"=>$date))
                                {                                 
                                  
                                  echo "<tr><td>$b_no</td><td>$dob</td><td>$date</td></tr>";
                                }   
                                                          
                ?>            
                  </tbody>
                </table>
              </div>
            </div>
                       <!-- card new -->

            <div class="col-md-6">
              <div class="card">
                <div class="comment-widgets scrollable">
                <div class="card-body">
                  <h5 class="card-title mb-0">Last Students</h5>
                </div>
                <?php
                           
                              $obj->select('students_course','*',null,null,'id DESC',2);
                              $result=$obj->getResult(); 
                              $c=0;                                                             
                              foreach($result as list("batch_no"=>$b_no,"course_name"=>$c_name,"start_date"=>$s_date,"end_date"=>$e_date,"student_name"=>$student_name,"date"=>$date))
                              {  
                                    ?>                              
                                    <div class="d-flex flex-row comment-row">
                                    <div class="p-2">
                                        <img src="../assets/images/users/3.jpg" alt="user" width="50" class="rounded-circle" />
                                    </div>
                                        <div class="comment-text w-100">
                                        <h6 class="font-medium"><?php echo " ".$b_no." - ".$student_name; ?></h6>
                                        <span class="mb-3 d-block"><?php echo $c_name." ( ".$s_date."-".$e_date." )"; ?></span>
                                        <div class="comment-footer">
                                            <span class="text-muted float-end"><?php echo $date; ?></span>
                                            <button type="button" class="btn btn-cyan btn-sm text-white"> View</button>&nbsp;&nbsp;                        
                                            <button type="button" class="btn btn-danger btn-sm text-white">Print</button>
                                        </div>
                                        </div>
                                    </div>

                                  <?php
                                }   
                                    ?>     
                 
                            



                </div>
              </div>
         
</div>
          <!-- ============================================================== -->
          <!-- End PAge Content -->
          <!-- ============================================================== -->
          <!-- ============================================================== -->

</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();
                    $("#modelForm").get(0).reset();                  
                    setTimeout(function(){$("#myModal").modal('show');}, 100);
                });
              $('#save-data').click(function(){
                   var user_name = $("#user_name").val();
                   var user_id = $("#user_id").val();
                   var password = $("#password").val();
                   if($("#radio1").is(':checked')){
                       var level='Admin';
                    } else {
                       var level='Staff';
                    }   
                    
                    if((user_name.length===0)||(user_id.length===0)||(password.length===0))
                    {
                      alert("some field is empty!");
                      return 0;
                    }                  
                         $.ajax({
                             url:"core/user.php",
                             method:"post",
                             data: {
                                 user_name:user_name,
                                 user_id:user_id,
                                 password:password,
                                 level:level,                          
                             },
                             success: function(data){                         
                               if(data==1)
                               {
                                $("#myModal").modal('toggle');
                                showAlert('User Successfully Added!','success');
                                setTimeout(function(){window.location='./user.php';}, 2000); 
                               
                               }
                             }
                         });                
               });
               $("#zero_config").on("click", "#id", function() {              
                var id = $(this).data("id");                      
                    $.ajax({
                                url:"core/delete.php",
                                method:"post",
                                data: {
                                   id:id,
                                   state:'usr',                          
                                },
                                success: function(data){                        
                                  if(data.length===1)
                                  {
                                    //$("#id").closest('tr').remove();
                                    showAlert('User Successfully Deleted!','success');
                                    setTimeout(function(){window.location='./user.php';}, 2000); 
                                    
                                  }
                                }
                            });     
              });
         
        });
    
</script>

  </body>
</html>