<?php
// require_once '../functions/config.php';
require_once '../functions/enDecpt.php';
$dbHandler = DbHandler();
$id = $_GET['id'];

if (isset($_SESSION['admin'])) {
    $query = $dbHandler->prepare("SELECT * FROM tbl_blog WHERE id = ?");
    $query->execute([base64_url_decode($id)]);
    while ($row = $query->fetch()) {
        $id = $row['id'];
        $upload = $row['fname'];
        $title = $row['a_title'];
        $body = $row['a_body'];
        $status = $row['status'];
        $createdWhen = $row['created_at'];
        $status = $row['status'];
?>
        <form class="p-3" enctype="multipart/form-data" action="../functions/updateBlog.php?id=<?php echo base64_url_encode($id) ?>" name="form" method="post">
            <div class="row">

                <div class="col-12">
                    <div class="img-preview m-auto">
                        <?php
                        $img_path = "./upload/$upload";
                        ?>
                        <img src="<?php echo $img_path ?>" alt="blog-post" class="rounded-circle img-fluid" style="width: 150px;height: 150px">
                    </div>

                    <div class="form-group my-1">
                        <div id="emailHelp" class="form-text"><span class="text-danger"><Strong>*</strong></span>change profile picture, image size 1 MB max</div>
                        <!--default html file upload button-->
                        <input type="file" id="file" name="file" hidden />

                        <!--our custom file upload button-->
                        <label for="file">Choose File</label>

                        <!-- name of file chosen -->
                        <span id="file-chosen">No file chosen</span>
                    </div>
                </div>
                <div class="col-12">
                    <div class="form-group my-4">
                        <div id="emailHelp" class="form-text"><b>Title</b></div>
                        <input type="text" class="form-control" name="title" placeholder="Title" rows="3" value="<?php echo $title ?>" />
                    </div>
                </div>
                <div class="col-12">
                    <div class="form-group my-4">
                        <div id="emailHelp" class="form-text"><b>Status</b></div>
                        <select class="form-select" aria-label="Default select example" id="status" name="status" value="<?php echo $status ?>">
                            <option value="pending">pending</option>
                            <option value="published">published</option>
                        </select>
                    </div>
                </div>
                <div class="col-12">
                    <!-- <div class="form-group">
                        <div id="emailHelp" class="form-text"><b>Body</b></div>
                        <textarea class="form-control" name="body" placeholder="Body" rows="3" style="  width: 100%;height: 150px;padding: 12px 20px;box-sizing: border-box;border: 2px solid #ccc;border-radius: 4px; background-color: #f8f8f8;font-size: 16px;"><?php echo $body ?></textarea>
                    </div> -->

                    
						<!-- <div id="trumbowyg-demo"> -->
							<textarea class="my-editor" id="trumbowyg-demo" name="body" placeholder="Body"><?php echo $body ?></textarea>
						<!-- </div> -->
                </div>
            </div>
            <div class="col-12">
                <input class="btn btn-dark my-1" type="submit" name="submit"></input>
            </div>
        </form>
<?php
    }
} else {
    echo "<script>window.location='../logout.php'</script>";
}
?>