<?php 
    include("../db.php");
    if($_FILES){
        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/gallery/".$lnewfile)){
    		    $_POST['image'] = $lnewfile;
    		    $_POST['status'] = 1;
    		}
    	}
        if($_POST['image']!=""){
            $db->insertAry("gallery", $_POST);
        }

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

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

?>

<div class="main">
    
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <div class="content-div">
                    <h2>GALLERY IMAGES</h2>
                    
                    <form action="" method="post" enctype="multipart/form-data">
                        <div class="form-row row">
                            <div class="col-md-2"><label>Image</label></div>
                            <div class="col-md-8"><input type="file" name="image" class="form-control" /></div>
                            <div class="col-md-2"><input type="submit" class="btn btn-primary" value="Save"></div>
                        </div>
                    </form><br>
                    
                    <table class="table table-striped table-hovered">
                        <thead class="table-dark">
                            <tr>
                                <th>S.No.</th>
                                <th>Image</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach($data as $img){ $i++; ?>
                                <tr>
                                    <td><?php echo $i; ?></td>
                                    <td><img src="../uploads/gallery/<?php echo $img['image']; ?>" style="width:250px;" /></td>
                                    <td><a href="gallery?action=delete&id=<?php echo $img['id']; ?>&name=<?php echo $img['image']; ?>" class="btn btn-danger"><i class="fa fa-trash" aria-hidden="true"></i></a></td>
                                </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                    
                </div>

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