<?php
	include_once('includes/connect_database.php'); 
	include_once('functions.php'); 
?>
	<?php 
//error_reporting(1);
	$sql_query = "SELECT Value
					FROM tbl_setting WHERE Variable = 'otp'";
		$stmt2 = $connect->stmt_init();
		if($stmt2->prepare($sql_query)) {	
			$stmt2->execute();
			// store result 
			$stmt2->store_result();
			$stmt2->bind_result($checked);
							$stmt2->fetch();

	$stmt2->close();

}		
			$export=false;
		// create object of functions class
		$function = new functions;
		
		// create array variable to store data from database
		$data = array();

		if(isset($_SESSION['keyword']) && $_SESSION['keyword']!=''){	
			// check value of keyword variable
			$keyword = $function->sanitize($_SESSION['keyword']);
			$bind_keyword = "%".$keyword."%";
		}else{
			$keyword = "";
			$bind_keyword = $keyword;
		}
			
		if(empty($keyword)){
			$sql_query = "SELECT id,student_name,student_dob,mobile,email,guardian,mobile_guardian,email_guardian,address,city,ten_board,created
					FROM tbl_student
					ORDER BY id DESC";
		}else{
			$sql_query = "SELECT id,student_name,student_dob,mobile,email,guardian,mobile_guardian,email_guardian,address,city,ten_board,created
					FROM tbl_student
					WHERE (student_name LIKE ? OR email LIKE ? OR mobile LIKE ? OR city LIKE ? OR address LIKE ? OR email_guardian LIKE ? OR guardian LIKE ?)
					ORDER BY id DESC";
		}
		$stmt = $connect->stmt_init();
		if($stmt->prepare($sql_query)) {	
			// Bind your variables to replace the ?s
			if(!empty($keyword)){

				$stmt->bind_param('sssssss', $bind_keyword,$bind_keyword,$bind_keyword,$bind_keyword,$bind_keyword,$bind_keyword,$bind_keyword);
			}
			// Execute query
			$stmt->execute();
			// store result 
			$stmt->store_result();
			$stmt->bind_result($data['id'], 
					$data['student_name'],
					$data['student_dob'],
										$data['mobile'],$data['email'],
										$data['guardian'],$data['mobile_guardian'],
										$data['email_guardian'],$data['address'],
										$data['city'],$data['ten_board'],$data['created']

					);
			// get total records
			$total_records = $stmt->num_rows;
		}
				
		// check page parameter
		if(isset($_GET['page'])){
			$page = $_GET['page'];
		}else{
			$page = 1;
		}
						
		// number of data that will be display per page		
		$offset = 10;
						
		//lets calculate the LIMIT for SQL, and save it $from
		if ($page){
			$from 	= ($page * $offset) - $offset;
		}else{
			//if nothing was given in page request, lets load the first page
			$from = 0;	
		}	
		
		if(empty($keyword)){
			$sql_query = "SELECT id,student_name,student_dob,mobile,email,guardian,mobile_guardian,email_guardian,address,city,ten_board,created
					FROM tbl_student
					 ORDER BY id DESC LIMIT ?, ?";
		}else{
			$sql_query = "SELECT id,student_name,student_dob,mobile,email,guardian,mobile_guardian,email_guardian,address,city,ten_board,created
					FROM tbl_student
					WHERE (student_name LIKE ? OR email LIKE ? OR mobile LIKE ? OR city LIKE ? OR address LIKE ? OR email_guardian LIKE ? OR guardian LIKE ?	)
									ORDER BY id DESC LIMIT ?, ?";
		}
		
		$stmt_paging = $connect->stmt_init();
		if($stmt_paging ->prepare($sql_query)) {
			// Bind your variables to replace the ?s
			if(empty($keyword)){
				$stmt_paging ->bind_param('ss', $from, $offset);
			}else{
				$stmt_paging ->bind_param('sssssssss', $bind_keyword,$bind_keyword,$bind_keyword,$bind_keyword,$bind_keyword,$bind_keyword,$bind_keyword, $from, $offset);
			}
			// Execute query
			$stmt_paging ->execute();
			// store result 
			$stmt_paging ->store_result();
			$stmt_paging->bind_result($data['id'], 
					$data['student_name'],
					$data['student_dob'],
										$data['mobile'],$data['email'],
										$data['guardian'],$data['mobile_guardian'],
										$data['email_guardian'],$data['address'],$data['city'],$data['ten_board'],$data['created']

					);
			// for paging purpose
			$total_records_paging = $total_records; 
		}
?>

<!-- Content Header (Page header) -->
                <section class="content-header">
               
<?php
if($_SESSION['delete']==1)
{
	?>
<br/>
<br/>
                   <div class="col-sm-12">
    
<div class="alert alert-success alert-dismissable">
                                        <i class="fa fa-check"></i>
                                        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                                        <b>Success!</b> Student Record Successfully Deleted.
                                    </div></div>
                                    <?php
                                }
                                ?>
                    <div class="col-sm-12">

                    	<h1>
                        Student List
                    	<!--<a href="add-country.php">
			<button class=" btn bg-navy btn-flat margin pull-right">Add New Country</button>
			<div class="clear"></div>
		</a>-->
		</h1></div>
	</section><section class="content">

	<div class="row"><div class="col-sm-12">
   <div class="box">
   	 <!-- <div class="box-header">
     <h3 class="box-title">Bordered Table</h3>
     </div><!-- /.box-header -->
     <div class="box-body">
                           
	<!-- search form -->
	<form class="list_header" method="post" action="search.php">
		<div class="col-md-12">
			<p class="pholder">Search by Name : </p>
		</div>

		<div class="col-md-3">
			<input type="text" value="<?php echo $keyword; ?>" class="form-control" name="keyword" />
		</div>

		<br>
		&nbsp;&nbsp;&nbsp;
		<input type="submit" class="btn btn-primary" name="btnSearch" value="Search" />
		<input type="submit" class="btn btn-primary" name="btnClear" value="Clear All" />
				<input type="submit" class="btn btn-primary" name="btnExport" value="Export" />
<input type="checkbox" name="action1" id="action1" title="otp verification" value="1"  <?php echo ($checked==1)?"checked":""; ?> /> OTP Verification

	</form>
	<!-- end of search form -->
	</div></div></div></div>

                 <!-- Main content -->
	<?php
	$_SESSION['delete']=0;
	unset($_SESSION['delete']);
		// if no data on database show "No Reservation is Available"
		if($total_records_paging == 0){
	
		?>
<div class="row"><div class="col-sm-12">
   <div class="box box-primary">
<div class="box-header"><h3 class="box-title">No Record Found.</h3></div>
   	</div>
   </div></div>
	<?php 
		// otherwise, show data
		}else{
			$row_number = $from + 1;
	?>
    <div class="row"><div class="col-sm-12"><div class="box box-primary"><div class="box-body table-responsive">

	<table class='table table-hover table-condensed table-bordered'>
		<tr class="success"><th>ID</th>
			<th>Name</th>
			<th>Email</th>
			<th>Date Of Birth</th>
			<th>Mobile</th><th>City</th>
			<th>Walk-In Date</th>
			<th>Action</th>
		</tr>
	<?php while ($stmt_paging->fetch()){ ?>
		<tr>
			<td><?php echo "FW202023".str_pad($data['id'],3,'0',STR_PAD_LEFT); ?></td>
			<td><?php echo $data['student_name'];?></td>
			<td><?php echo $data['email']; ?></td>
			<td><?php echo $data['student_dob']; ?></td>
			<td><?php echo $data['mobile']; ?></td><td><?php echo $data['city']; ?></td>
			<td><?php echo $data['created']; ?></td>
			<td width="25%">
				<a href="view-student.php?id=<?php echo $data['id'];?>">
				View
				</a>
				<a href="delete-std.php?id=<?php echo $data['id'];?>" onclick="return confirm('Are You sure want to delete Student data?');">Delete</a>
  <a href="print-pdf.php?id=<?php echo $data['id'];?>"  class="btn btn-success">
    <span class="glyphicon glyphicon-download-alt"></span> 
  </a>

			</td>
		</tr>
	<?php } }?>
	</table>
	</div></div></div></div>

	<div class="row"><div class="col-md-12">
	<h4>
	<?php 
		// for pagination purpose
		$function->doPages($offset, 'studentlist.php', '', $total_records, $keyword);?>
	</h4>
	</div></div>
	<div class="separator"> </div>

</section>
<?php 
	$stmt->close();
	include_once('includes/close_database.php'); ?>
					
				