<?php 

require "top.php"; 
require "dbconnect.php";

if(isset($_GET['type']) && $_GET['type']!=''){
    $type = $_GET['type'];

    if($type=='delete'){ 
        $id = $_GET['id'];
        $delete_sql = "delete from management where id='$id'";
        mysqli_query($con,$delete_sql);
    }
}

if(isset($_POST['submit'])){
    $title = $_POST['title'];
    $content = $_POST['content'];
    $image = $_FILES['image']['name'];
    $target_img = "upload/".$image;
    move_uploaded_file($_FILES['image']['tmp_name'],$target_img);
    
    $sql = "insert into management(image,title,content) values('$image','$title','$content')";
    $res = mysqli_query($con,$sql);
}

?>

<div class="container gap">
    <h4 class="mt-3">Management Form</h4>
    <form method="post" enctype="multipart/form-data">
        <div class="form-group">
            <label>Image</label>
            <input type="file" name="image" class="form-control">
        </div>
        <div class="form-group">
            <label>Title</label>
            <input type="text" name="title" class="form-control">
        </div>
        <div class="form-group">
            <label>Description</label>
            <textarea name="content" rows="5" class=form-control></textarea>
        </div>
        <div class="form-group">
            <button class="btn btn-primary" name="submit">Submit</button>
        </div>
    </form>
    <div class="row">
        <table class="table table-bordered">
            <thead class="bg-info">
                <tr>
                    <th>Image</th>
                    <th>Title</th>
                    <th>Description</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $res = mysqli_query($con,"select * from management");
               while($row = mysqli_fetch_assoc($res)){
                ?>
                <tr>
                    <td><img src="upload/<?php echo $row['image'] ?>" height="150" width="200"></td>
                    <td><?php echo $row['title'] ?></td>
                    <td><?php echo $row['content'] ?></td>
                    <td><?php 
                    echo "<a class='btn btn-danger' href='?type=delete&id=".$row['id']."'>Delete</a>";
                    ?></td>
                </tr>
                <?php } ?>
            </tbody>
        </table>
    </div>
</div>

<?php require "footer.php"; ?>