<?php 
    include("../db.php");
    
   if($_FILES){
        if(isset($_FILES["photo"]["name"]) && !empty($_FILES["photo"]["name"])){
    		$lfilename = basename($_FILES['photo']['name']);
    		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
    		$lnewfile=md5(microtime()).".".$lext;
    		echo "$lnewfile";
    		if(move_uploaded_file($_FILES['photo']['tmp_name'],"../uploads/toppers/".$lnewfile)){
    		    $_POST['photo'] = $lnewfile;
    		}
    	}
        if($_POST['photo']!=""){
          
    
            
            $db->insertAry("toppers", $_POST);
            
        }

        header("Location: toopers");
    }
    //end
if($_GET['action']=="delete" && isset($_GET['id']) && trim($_GET['id'])!='' && isset($_GET['name']) && trim($_GET['name'])!=''){
    
    $img = $_GET["name"];
    unlink("../uploads/toppers/$img");
 	$res=$db->delete("toppers","where id='".$_GET['id']."' ");
     header("Location: toopers");
}
    
   
?>

<!DOCTYPE html>
<html>
<head>
      <title>Admin Dashboard</title>
    <?php include("meta.php"); ?>

</head>
<body> 
 
<?php include("header.php"); ?>

<?php include("sidebar.php"); 

    $data = $db->getRows("select * from toppers ");

?>

<div class="main">
    
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="content-div">
                    <h2>TOPPERS</h2>
                    <div>
                    <button  style="background: blue;color: white;font-size: 25px;" data-toggle="modal" data-target="#exampleModal">+Add New</button>
                    </div>
                    <table class="table table-hover">
  <thead class="bg-black text-white font-weight-bold ml-3" style="font-size: 15px;">
    <tr>
        <th scope="col">S.No</th>
            <th scope="col">Name</th>

      <th scope="col">Class</th>
      <th scope="col">Percentage</th>
      <th scope="col">Session</th>
       <th scope="col">Photo</th>
        <th scope="col">Action</th>
    </tr>
  </thead>
  <tbody>
      <?php 
      $i=0;
      foreach($data as $res){$i++
          
          ?>
           <tr >
      <th  style="font-weight: 600; color: green;padding: 0.75rem;" ><?php echo $i ; ?></th>
      <th  style="font-weight: 600; color: green;padding: 0.75rem;" ><?php echo $res['name'];?></th>
      <th style="padding: 0.75rem;" ><?php echo $res['class'];?></th>
       <th style="padding: 0.75rem;" ><?php echo $res['percentage'];?></th>
        <th style="padding: 0.75rem;" ><?php echo $res['session'];?></th>
      <td style="padding: 0.75rem;">
          <img src="../uploads/toppers/<?php echo $res["photo"]; ?>" alt="Service image" style="height: 100px;">
          </td>
     <td><a href="toopers?action=delete&id=<?php echo $res['id']; ?>&name=<?php echo $res['photo']; ?>" class="btn btn-danger"><i class="fa fa-trash" aria-hidden="true"></i></a></td>
    </tr>
    <?php  }?>
   
  
  </tbody>
</table>



<!-- Modal add -->
<div class="modal fade " id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Add Toopers</h5>
        
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form action="" method="post" enctype="multipart/form-data" >
    <div class="form-group">
    <label for="exampleInputEmail1">Name</label>
    <input type="text" class="form-control" name="name" required  placeholder="Enter student name">
    
  </div>
  <div class="form-group mt-3">
    <label for="exampleInputPassword1">Class</label>
    <input type="text" class="form-control" name="class" required placeholder="class">
  </div>
  <div class="form-group mt-3">
    <label for="exampleInputPassword1">Percentage</label>
    <input type="text" class="form-control" name="percentage" required placeholder="percentage">
  </div>
  <div class="form-group mt-3">
    <label for="exampleInputPassword1">Session</label>
    <input type="text" class="form-control" name="session" required placeholder="session">
  </div>
<div class="form-group mt-3">
    <label for="exampleInputPassword1">Photo</label>
    <input type="file" class="form-control" required name="photo" >
  </div>
  <button type="submit"  class="btn btn-success mt-4">Save</button>
</form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
       
      </div>
    </div>
  </div>
</div>
              
             
                </div>

            </div>   
        </div>
    </div>
    
</div>
     
</body>
</html> 
