<?php 

include('includes/config.php');


?>


<?php
include('includes/user_data.php');

?>


<!doctype html>
<html lang="en">


<!-- Mirrored from codervent.com/rocker/demo/vertical/user-profile.html by HTTrack Website Copier/3.x [XR&CO'2014], Fri, 26 Aug 2022 06:47:36 GMT -->
<head>
	<!-- Required meta tags -->
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<!--favicon-->
	<link rel="icon" href="assets/images/favicon-32x32.png" type="image/png" />
	<!--plugins-->
	<link href="assets/plugins/simplebar/css/simplebar.css" rel="stylesheet" />
	<link href="assets/plugins/perfect-scrollbar/css/perfect-scrollbar.css" rel="stylesheet" />
	<link href="assets/plugins/metismenu/css/metisMenu.min.css" rel="stylesheet" />
	<!-- loader-->
	<link href="assets/css/pace.min.css" rel="stylesheet" />
	<script src="assets/js/pace.min.js"></script>
	<!-- Bootstrap CSS -->
	<link href="assets/css/bootstrap.min.css" rel="stylesheet">
	<link href="assets/css/bootstrap-extended.css" rel="stylesheet">
	<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500&amp;display=swap" rel="stylesheet">
	<link href="assets/css/app.css" rel="stylesheet">
	<link href="assets/css/icons.css" rel="stylesheet">
	<!-- Theme Style CSS -->
	<link rel="stylesheet" href="assets/css/dark-theme.css" />
	<link rel="stylesheet" href="assets/css/semi-dark.css" />
	<link rel="stylesheet" href="assets/css/header-colors.css" />
  <title>View Attendance</title>
  <script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
	<!--wrapper-->
	<div class="wrapper">
		<!--sidebar wrapper -->
		<?php include 'includes/side.php'; ?>
		<!--end sidebar wrapper -->
		<!--start header -->
		<?php include 'includes/head.php'; ?>
		<!--end header -->
		<!--start page wrapper -->
		<div class="page-wrapper">
			<div class="page-content">
				<!--breadcrumb-->
				<div class="page-breadcrumb d-none d-sm-flex align-items-center mb-3">
					<div class="breadcrumb-title pe-3"></div>
					<div class="ps-3">
						<nav aria-label="breadcrumb">
							<ol class="breadcrumb mb-0 p-0">
								<li class="breadcrumb-item"><a href="javascript:;"><i class="bx bx-home-alt"></i></a>
								</li>
								<li class="breadcrumb-item active" aria-current="page"></li>
							</ol>
						</nav>
					</div>
					
				</div>
				<!--end breadcrumb-->
				<div class="container">
					<div class="main-body">
						<div class="row">
						
							<div class="col-lg-12">
								<div class="card">
									<div class="card-body">
<?php

$_SESSION['teacher_id']=$teacher_id;

if (!isset($_SESSION['teacher_id'])) {
    header("Location: login.php");
    exit;
}

$teacher_id = $_SESSION['teacher_id'];
$teacher = $dbh->query("SELECT * FROM teachers WHERE id=$teacher_id")->fetch();
$class_id = $teacher['class'];

// Fetch class name
$classQuery = $dbh->prepare("SELECT * FROM classes WHERE id=?");
$classQuery->execute([$class_id]);
$class = $classQuery->fetch();

// Get session & term (latest active)
$session = $dbh->query("SELECT * FROM sessions ORDER BY id DESC LIMIT 1")->fetch();
$term = $dbh->query("SELECT * FROM terms ORDER BY id DESC LIMIT 1")->fetch();

$students = $dbh->query("SELECT * FROM students WHERE class_id=$class_id ORDER BY name ASC")->fetchAll();

$month = date('m');
$year = date('Y');
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
?>


  <div class="overflow-auto bg-white shadow-lg rounded-xl p-6">
    <h1 class="text-xl font-bold mb-2 text-center">Attendance Register - <?= date('F Y') ?></h1>
    <p class="mb-6 text-center text-gray-600">
      Class: <strong><?= $class['name'] ?></strong> | 
      Teacher: <strong><?= $teacher['name'] ?></strong> | 
      Session: <strong><?= $session['session_year'] ?></strong> 
    </p>

    <table class="border-collapse border border-gray-400 text-xs">
      <thead>
        <tr class="bg-gray-200">
          <th class="border px-2 py-1">#</th>
          <th class="border px-2 py-1">Student</th>
          <?php for($d=1; $d<=$daysInMonth; $d++): ?>
            <th class="border px-2 py-1 text-center"><?= $d ?></th>
          <?php endfor; ?>
          <th class="border px-2 py-1">Total</th>
        </tr>
      </thead>
      <tbody>
        <?php $sn=1; foreach($students as $st): ?>
        <tr>
          <td class="border px-2"><?= $sn++ ?></td>
          <td class="border px-2"><?= $st['name'] ?></td>
          <?php 
          $presentCount=0;
          for($d=1; $d<=$daysInMonth; $d++):
            $date="$year-$month-".str_pad($d,2,'0',STR_PAD_LEFT);
            $att = $dbh->query("SELECT status FROM attendance WHERE student_id={$st['id']} AND class_id=$class_id AND session_id={$session['id']} AND term_id={$term['id']} AND date='$date'")->fetch();
            $status="-";
            if($att){
              $status = $att['status'];
              if($status=="P") $presentCount++;
            }
          ?>
            <td class="border text-center"><?= $status ?></td>
          <?php endfor; ?>
          <td class="border text-center"><?= $presentCount ?>/<?= $daysInMonth ?></td>
        </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  </div>





								</div>
								
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<!--end page wrapper --> 
		<!--start overlay-->
		<div class="overlay toggle-icon"></div>
		<!--end overlay-->
		<!--Start Back To Top Button--> <a href="javaScript:;" class="back-to-top"><i class='bx bxs-up-arrow-alt'></i></a>
		<!--End Back To Top Button-->
		<footer class="page-footer">
			<p class="mb-0">Copyright © 2024. All right reserved.</p>
		</footer>
	</div>
	<!--end wrapper-->
	<!--start switcher-->
	<?php include 'includes/customiser.php'; ?>

	<!--end switcher-->
	<!-- Bootstrap JS -->
	<script src="assets/js/bootstrap.bundle.min.js"></script>
	<!--plugins-->
	<script src="assets/js/jquery.min.js"></script>
	<script src="assets/plugins/simplebar/js/simplebar.min.js"></script>
	<script src="assets/plugins/metismenu/js/metisMenu.min.js"></script>
	<script src="assets/plugins/perfect-scrollbar/js/perfect-scrollbar.js"></script>
	<!--app JS-->
	<script src="assets/js/app.js"></script>
</body>


</html>

