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

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

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

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

?>

<div class="main">
    
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="content-div">
                    <h2>DOWNLOADS CONTENT</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">Title</th>
      <th scope="col">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['title'];?></th>
      <td style="padding: 0.75rem;">
          <!--<img src="../uploads/downloads/<?php echo $res["file"]; ?>" alt="Service image" style="height: 100px;">-->
          <embed src="https://bps.savarnacloudtech.com/uploads/downloads/<?php echo $res['file']; ?>" width="129" height="150" 
 type="application/pdf">
          </td>
     <td><a class="btn btn-primary" href="https://bps.savarnacloudtech.com/uploads/downloads/<?php echo $res['file']; ?>" target="_blank" ><i class="fa fa-eye"></i>View</a><a href="downloads?action=delete&id=<?php echo $res['id']; ?>&name=<?php echo $res['file']; ?>" 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">Upload New File</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">Enter Title</label>
    <input type="text" class="form-control" name="title" required  placeholder="Enter title ">
    
  </div>
 
 <div class="form-group mt-3">
    <label for="exampleInputPassword1">File</label>
    <input type="file" class="form-control" required name="file" >
  </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> 
