<?php 
    include("../db.php");
    
    //insert
   
    
    if($_FILES){
        if(isset($_FILES["tc"]["name"]) && !empty($_FILES["tc"]["name"])){
    		$lfilename = basename($_FILES['tc']['name']);
    		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
    		$lnewfile=md5(microtime()).".".$lext;
    		echo "$lnewfile";
    		       

    		if(move_uploaded_file($_FILES['tc']['tmp_name'],"../uploads/tc/".$lnewfile)){
    		    $_POST['tc'] = $lnewfile;
    		    $filetype=substr($lnewfile, strrpos($lnewfile, '.' )+1)."\n";

    		    
    		     $_POST['filetype'] = $filetype;
    		       $_POST['status'] = 1;
    		}
    	}
        if($_POST['tc']!=""){
          
    
            
            $db->insertAry("tc", $_POST);
            
        }

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

      if(isset($_FILES["image"]["name"]) && !empty($_FILES["image"]["name"])){
    		$lfilename = basename($_FILES['image']['name']);
    		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
    		$lnewfile=md5(microtime()).".".$lext;
    		echo "$lnewfile";
    		
    		if(move_uploaded_file($_FILES['image']['tmp_name'],"../uploads/tc/".$lnewfile)){
    		    $_POST['image'] = $lnewfile;
    		}
    	}
    

        $db->updateAry("tc", $_POST);
        
        header("Location: tc");
    }
    }
?>

<!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 tc");

?>

<div class="main">
    
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="content-div">
                    <h2>TC</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">Admission.No</th>

      <th scope="col">Tc  File</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="padding: 0.75rem;" ><?php echo $res['admissionno'];?></th>
      <td style="padding: 0.75rem;">
          <img src="../uploads/tc/<?php echo $res['tc']; ?>" style="height:100px;" />
          </td>
     <td><a href="https://bps.savarnacloudtech.com/uploads/tc/<?php echo $res['tc']; ?>" class="btn btn-primary" target="_blank" rel="noopener noreferrer"><i class="fa fa-eye"></i>View File</a><a href="tc?action=delete&id=<?php echo $res['id']; ?>&name=<?php echo $res['tc']; ?>" class="btn btn-danger"><i class="fa fa-trash" aria-hidden="true"></i></a></td>
    </tr>
    <?php  }?>
   
  
  </tbody>
</table>
<!-- Button trigger modal -->


<!-- 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 New Tc</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">Admission  No</label>
    <input type="text" class="form-control" name="admissionno" required  >
    
  </div>
  
 <div class="form-group mt-3">
    <label for="exampleInputPassword1">Select File</label>
    <input type="file" class="form-control" required name="tc"   >
    <small>Only pdf and img are allowed</small>
  </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> 
