<?php

session_start();

require_once 'includes.php';

include 'dbconfig4.php';

require_once("conn.php");

if(isset($_POST['add_attendence'])){

    date_default_timezone_set("Asia/Colombo");
	
    $student=mysqli_real_escape_string($conn,$_POST['userID']);

    $type  = $_GET['type'];
    $class  = $_POST['class'];    

        //------------------------------
        $sql = "INSERT INTO investattendence (student, class, type, status) VALUES ('$student','$class','$type','1')";

        if (mysqli_query($conn, $sql)){

			$successMSG = "Student Attendence Marked Successfully.";

			header("refresh:2;student_attendence.php?type=$type&clear"); // redirects image view page after 5 seconds.
			

        }else{
				$errMSG = "error while inserting....";
			}

}

if(isset($_GET['rvid'])){

    $aid=mysqli_real_escape_string($conn,$_GET['rvid']);
	
	$ctype=mysqli_real_escape_string($conn,$_GET['type']);

    if(mysqli_query($conn,"DELETE FROM investattendence WHERE aid='$aid'")){
		
	$successMSG = "Student Attendence Delete Successfully.";

   echo "<script>window.location='student_attendence.php?type=$ctype&clear';</script>";

    }

}

?>


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

<head>
	
	<meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1">
	
<?php	
if (isset($_GET['type']))
{
?>
    <title><?php echo $_GET['type']; ?> - Student Attendence | investsics | Yasith Dharshana | investsicsedu.lk | Dashbaord</title>
<?php } ?>	

    <?php
	require_once 'headercss.php';
	?>

</head>

<body>

    <!--**********************************
        Main wrapper start
    ***********************************-->
    <div id="main-wrapper">
	
<?php

require_once 'navbarheader.php';

require_once 'sidebarmenu.php';
	
?>
		
        <!--**********************************
            Content body start
        ***********************************-->
        <div class="content-body">
            <!-- row -->
            <div class="container-fluid">
				    
                <div class="row page-titles mx-0">
                    <div class="col-sm-6 p-md-0">
                        <div class="welcome-text">
<?php 
	
if (isset($_GET['type']))

{
	
?>
                            <h4><?php echo $_GET['type']; ?> - All Student Attendence <br> <?php
$stmt = $DB_con->prepare('SELECT COUNT(*) AS register_count1 FROM investclass_schinvestle where classtype="'. $_GET["type"] .'"');
$stmt->execute();
$result = $stmt->fetch();
$total_register1 = $result['register_count1'];?> [Total Classes - <?php echo $total_register1 ?>] - <?php
$stmt = $DB_con->prepare('SELECT COUNT(*) AS register_count2 FROM investattendence where type="'. $_GET["type"] .'"');
$stmt->execute();
$result = $stmt->fetch();
$total_register2 = $result['register_count2'];?> [Total Student Attend - <?php echo $total_register2 ?>]</h4>
<?php

}

?>
                        </div>
                    </div>
                    <div class="col-sm-6 p-md-0 justify-content-sm-end mt-2 mt-sm-0 d-flex">
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="home.php">Home</a></li>
                            <li class="breadcrumb-item active"><a href="javascript:void(0);">Student Attendence</a></li>
                            <li class="breadcrumb-item active"><a href="javascript:void(0);">All Student Attendence</a></li>
                        </ol>
                    </div>
                </div>
				
				<div class="row">
					<div class="col-lg-12">
						<ul class="nav nav-pills mb-3">
							<li class="nav-item"><a href="#list-view" data-toggle="tab" class="nav-link btn-primary mr-1 show active">List View</a></li>
						</ul>
					</div>
					<div class="col-lg-12">

<form method="post" class="m-3" autocomplete="off">

<strong>Add New Student Attendence</strong>
<hr>
<div class="row">
<div class="col-lg-4">
<div class="form-group">

<select type="text" name="userID" id="userID" required class="form-control" placeholder="Select Student" list="stu_list" onChange="JavaScrip:dis_load(this.value);" onBlur="JavaScrip:dis_load();">

<?php

$select_stu=mysqli_query($conn,"SELECT * FROM investregister ORDER BY fullname");

while($stu_resalt=mysqli_fetch_array($select_stu)){

?>

<option value="<?php echo $stu_resalt['reid']; ?>"><?php echo $stu_resalt['fullname']; ?> - <?php echo "0".(int)$stu_resalt['contactnumber']; ?></option>   

<?php

}

?>

</select>

</div>
</div>
<div class="col-lg-8">
<div class="form-group">

<select name="class" class="form-control">

<?php
if (isset($_GET['type']))
{

$tec_qury=mysqli_query($conn,"SELECT cs.classid,cs.level,cs.subject,cs.lesson,cs.classdate,cs.classtype,cs.add_date,sb.sid,sb.name,sb.status FROM investclass_schinvestle cs INNER JOIN investsubject sb ON cs.subject=sb.sid where classtype='".$_GET["type"]."' and sb.status='Completed' ORDER BY add_date");

while($tec_resalt=mysqli_fetch_array($tec_qury)){

?>

<option value="<?php echo $tec_resalt['classid']; ?>"><?php

						$id = $tec_resalt['level'];

						$query = $DB_con->prepare('SELECT name FROM investclass WHERE cid='.$id);

						$query->execute();

						$result = $query->fetch();

						echo $result['name'];

						?> - [ <?php

						$id = $tec_resalt['subject'];

						$query = $DB_con->prepare('SELECT name FROM investsubject WHERE sid='.$id);

						$query->execute();

						$result = $query->fetch();

						echo $result['name'];

						?> ] - <?php echo $tec_resalt['lesson']; ?> - <?php echo $tec_resalt['classdate']; ?></option>    

<?php

}
}

?>

</select>

</div>
</div>
<div class="col-lg-12">
<button name="add_attendence" type="submit" class="btn btn-info btn-block ml-2">Add Attendence</button>
</div>
</div>
<datalist id="stu_list">

</datalist>

</form>

<script>

function dis_load() {

    var userID=document.getElementById('userID').value;

  var xhttp = new XMLHttpRequest();

  xhttp.onreadystatechange = function() {

    if (this.readyState == 4 && this.status == 200) {

     document.getElementById("details_dis").innerHTML = this.responseText;

    }

  };

  xhttp.open("GET", "ajax_details_dis2.php?userID="+userID, true);

  xhttp.send();

}

</script>

<div id="details_dis" class="m-3"></div>

 </div>

					<div class="col-lg-12">
						<div class="row tab-content">
							<div id="list-view" class="tab-pane fade active show col-lg-12">
								<div class="card">
									<div class="card-header">
<?php
	
if (isset($_GET['type']))
{
?>
										<h4 class="card-title">All Student Attendence - <?php echo $_GET['type']; ?></h4>
<?php } ?>	
									</div>
									<div class="card-body">
									<?php

									if(isset($errMSG)){

									?>
			
									<div class="alert alert-danger alert-dismissible alert-alt solid fade show">
									<button type="button" class="close h-100" data-dismiss="alert" aria-label="Close"><span><i class="mdi mdi-close"></i></span></button>
									<strong>Error!</strong> <?php echo $errMSG; ?>
									</div>

									<?php

									}
	
									else if(isset($successMSG)){

									?>
			
									<div class="alert alert-success alert-dismissible alert-alt solid fade show">
									<button type="button" class="close h-100" data-dismiss="alert" aria-label="Close"><span><i class="mdi mdi-close"></i></span></button>
									<strong>Success!</strong> <?php echo $successMSG; ?>.
									</div>

									<?php

									}

									?>
										<div class="table-responsive">
											<table id="example3" class="table table-bordered">
												<thead>
													<tr>
<th>#</th>
<th>Action</th>
<th>Status</th>
<th>Student</th>
<th>Class</th>
<th>Added Date</th>
													</tr>
												</thead>
												<tbody>
<?php
	
if (isset($_GET['type']))
{
$count=0;
$attendence_qury=mysqli_query($conn,"SELECT * FROM investattendence WHERE type='".$_GET['type']."' and status='1' ORDER BY add_date DESC");
while($attendence_resalt=mysqli_fetch_array($attendence_qury)){
$count++;
	
$user_qury=mysqli_query($conn,"SELECT * FROM investregister WHERE reid='$attendence_resalt[student]'");
$user_resalt=mysqli_fetch_array($user_qury);
?>
<tr>
<td><?php echo number_format($count,0); ?></td>
<td>	

<a href="student_attendence.php?rvid=<?php echo $attendence_resalt['aid']; ?>&type=<?php echo $_GET['type']; ?>" title="Delete Attendence" onClick="JavaScript:return confirm('Are your sure change this attendence status?');" class="badge badge-danger"><i class="la la-trash-o"></i> Delete</a>

</td>
<td>
<?php if($attendence_resalt['status']==0){ ?>
<span class="badge badge-warning">Not Attended</span>
<?php } ?>
<?php if($attendence_resalt['status']==1){ ?>
<span class="badge badge-success">Attended</span>
<?php } ?>	
</td>
<td>Name : <?php echo $user_resalt['fullname']; ?><br>Contact No : <?php echo "0".(int)$user_resalt['contactnumber']; ?></td>
<td>
<span class="badge badge-secondary">Class : <?php

						$id = $attendence_resalt['class'];

						$query = $DB_con->prepare('SELECT level FROM investclass_schinvestle WHERE classid='.$id);

						$query->execute();

						$result = $query->fetch();

						$query = $DB_con->prepare('SELECT name FROM investclass WHERE cid='.$result['level']);

						$query->execute();

						$result = $query->fetch();

						echo $result['name'];

						?> - [ <?php
						
						$id = $attendence_resalt['class'];

						$query = $DB_con->prepare('SELECT subject FROM investclass_schinvestle WHERE classid='.$id);

						$query->execute();

						$result = $query->fetch();

						$query = $DB_con->prepare('SELECT name FROM investsubject WHERE sid='.$result['subject']);

						$query->execute();

						$result = $query->fetch();

						echo $result['name'];

						?> ] - <?php

						$id = $attendence_resalt['class'];

						$query = $DB_con->prepare('SELECT lesson FROM investclass_schinvestle WHERE classid='.$id);

						$query->execute();

						$result = $query->fetch();

						echo $result['lesson'];

						?> - [ <?php

						$id = $attendence_resalt['class'];

						$query = $DB_con->prepare('SELECT classtype FROM investclass_schinvestle WHERE classid='.$id);

						$query->execute();

						$result = $query->fetch();

						echo $result['classtype'];

						?> ] - <?php

						$id = $attendence_resalt['class'];

						$query = $DB_con->prepare('SELECT classdate FROM investclass_schinvestle WHERE classid='.$id);

						$query->execute();

						$result = $query->fetch();

						echo $result['classdate'];

						?></span>
</td>
<td><?php echo date_format(date_create($attendence_resalt['add_date']),"M d, Y - h:i:s A"); ?></td>
</tr>
<?php
}
}
?>
												</tbody>
											</table>
										</div>
									</div>
                                </div>
                            </div>
						</div>
					</div>
				</div>
				
            </div>
        </div>
        <!--**********************************
            Content body end
        ***********************************-->

<?php
 
require_once 'footer.php';

require_once 'footerjs.php';

?>   
	
</body>
</html>