
<?php
session_start();
session_regenerate_id(true);
require_once '../config.php';
require_once '../functions/enDecpt.php';


// $users_name=  $_SESSION['current_session']['user']['firstname'];
$id = $_GET['id'];
$title = $_POST['title'];
$body = $_POST['body'];
$status = $_POST['status'];

$name = $_FILES['file']['name'];
$size = $_FILES['file']['size'];
$type = $_FILES['file']['type'];
$temp = $_FILES['file']['tmp_name'];
$fname = date("YmdHis") . '_' . $name;


$dbHandler = DbHandler();

if (isset($_SESSION['admin'])) {

    $sqlNotEmptyUpload = $dbHandler->prepare("UPDATE tbl_blog SET name = ?, fname = ?, a_title = ?,
        a_body = ?, status = ? WHERE id = ?");

    $updateAllWithoutUpload = $dbHandler->prepare("UPDATE tbl_blog SET a_title = ?,
        a_body = ?, status = ? WHERE id = ?");

    if (isset($_POST['submit']) != "") {
        //if user didnt upload new picture, that is only made changes to the text
        //update just the text
        // echo "<script>alert('we dey here')</script>";
        if ($_FILES['file']['error'] > UPLOAD_ERR_OK) {
            // echo "<script>alert('File is empty');</script>";
            $updateArticleTable = $updateAllWithoutUpload->execute([$title, $body, $status, base64_url_decode($id)]);
            if ($updateArticleTable) {

                $_SESSION['success'] = "Article has been updated";
                // echo "<script>alert('upload without picture in progress!')</script>";
                echo "<script>window.location='../Blog/blog-manager.php'</script>";
            } else {
                $_SESSION['error'] = "Record does not exist!";
                // $errors[] = "Record does not exist!";
            }
            // exit();
        } else {
            //if user upload new picture
            //update the text and delete previous image from upload folder
            $fetchdoc = $dbHandler->prepare('SELECT fname FROM tbl_blog WHERE id=?');
            $fetchdoc->execute([base64_url_decode($id)]);

            // $record = $fetchdoc->fetch();
            $record = $fetchdoc->fetch(PDO::FETCH_ASSOC);
            $imageUrl = '../Blog/upload/' . $record['fname'];
            // echo "<script>console.log('$imageUrl')</script>";

            if (($record['fname']) == 0) {
                unlink($imageUrl); //first image upload
                // echo "<script>alert('we dey here32552')</script>";

                if ($_FILES['file']['name']) {
                    //move the updated image to upload folder
                    $move =  move_uploaded_file($temp, "../Blog/upload/" . $fname);

                    if ($move) {
                        $updateArticleTable = $sqlNotEmptyUpload->execute([$name, $fname, $title, $body, $status, base64_url_decode($id)]);
                        if ($updateArticleTable) {
                            $_SESSION['success'] = "Blog has been updated";
                            echo "<script>window.location='../Blog/blog-manager.php</script>";
                        }
                    }
                } else {
                    echo "<script>alert('Record does not exist!')</script>";
                }
            } else {
                //susequent uploads, check if image exists
                if (file_exists($imageUrl)) {
                    //delete the image
                    unlink($imageUrl);
                    if ($_FILES['file']['name']) {
                        //move the updated image to upload folder
                        $move =  move_uploaded_file($temp, "../Blog/upload/" . $fname);
                        if ($move) {
                            $updateArticleTable = $sqlNotEmptyUpload->execute([$name, $fname, $title, $body, $status, base64_url_decode($id)]);
                            if ($updateArticleTable) {
                                $_SESSION['success'] = "Blog has been updated";
                                echo "<script>window.location='../Blog/blog-manager.php'</script>";
                            }
                            // exit();
                        }
                    } else {
                        echo "<script>alert('Record does not exist!')</script>";
                    }
                }
            }
        }
    }
} else {
    echo "<script> window.location.assign('./logout.php'); </script>";
}
?>