<?php
$LINK 
= mysqli_connect(
    
"127.0.0.1",
    
"janya763",
    
"vOOxhXUhWhgCPWFC",
    
"janya763"
);

if (!isset(
$_GET["date"])) {
    echo 
"No date specified.";
    exit;
}

$editDate = $_GET["date"];

// Fetch entry by date
$stmt = $LINK->prepare("SELECT * FROM data WHERE date = ?");
$stmt->bind_param("s", $editDate);
$stmt->execute();
$result = $stmt->get_result();
$editData = $result->fetch_assoc();
$stmt->close();

if (!
$editData) {
    echo 
"No entry found for this date.";
    exit;
}

// Handle update
if (isset($_POST["update_entry"])) {
    
$title = $_POST["title_input"];
    
$paragraph = $_POST["get_page"];
    
$work = isset($_POST["isWork"]) ? 1 : 0;

    
// Handle image upload
    
$image = [];
    
$file = $_FILES["get_image"];
    for (
$i = 0; $i < count($file["name"]); $i++) {
        
$fileName = $file["name"][$i];
        
$fileTmpName = $file["tmp_name"][$i];
        
$fileError = $file["error"][$i];
        
$fileExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));

        
$allowed = ["jpg", "png", "jpeg", "svg", "heic"];
        if (
in_array($fileExt, $allowed) && $fileError === 0) {
            
$fileNameNew = uniqid("", true) . "." . $fileExt;
            
$fileDestination = "../Images/" . $fileNameNew;
            
$image[] = substr($fileDestination, 3); // remove "../"
            
move_uploaded_file($fileTmpName, $fileDestination);
        }
    }

    
// Keep old images if none uploaded
    
if (empty($image)) {
        
$image = json_decode($editData["images"]);
    }

    
// JSON encode
    
$title_json = mysqli_real_escape_string($LINK, json_encode($title));
    
$paragraph_json = mysqli_real_escape_string($LINK, json_encode($paragraph));
    
$image_json = mysqli_real_escape_string($LINK, json_encode($image));
    
$query = "UPDATE data SET title = '" . $title_json . "', text = '" . $paragraph_json . "', images = '"
     
. $image_json . "', work = " . $work . " WHERE date LIKE " . $_GET["date"] .";";
    
$result = mysqli_query($LINK, $query);
    echo 
"<p>Updated successfully!<a href='cms.php'>Go back</a></p>";
    exit;
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Edit Entry <?= htmlspecialchars($editDate) ?></title>
</head>
<body>
    <h1>Edit Entry: <?= htmlspecialchars($editDate) ?></h1>
    <form action="edit.php?date=<?= urlencode($editDate) ?>" method="POST" enctype="multipart/form-data">

        <h3>Titles</h3>
        <?php $titles = json_decode($editData["title"]); ?>
        <?php foreach ($titles as $i => $t) { ?>
            <input type="text" name="title_input[]" value="<?= htmlspecialchars($t) ?>" required><br>
        <?php } ?>

        <h3>Paragraphs</h3>
        <?php $paragraphs = json_decode($editData["text"]); ?>
        <?php foreach ($paragraphs as $i => $p) { ?>
            <textarea name="get_page[]" rows="4" cols="50" required><?= htmlspecialchars($p) ?></textarea><br>
        <?php } ?>

        <h3>Current Images</h3>
        <?php $images = json_decode($editData["images"]); ?>
        <?php foreach ($images as $img) { ?>
            <img src="../<?= $img ?>" style="max-height: 150px;"><br>
        <?php } ?>

        <h3>Upload New Images (optional)</h3>
        <input type="file" name="get_image[]" multiple><br><br>

        <label><input type="checkbox" name="isWork" <?= $editData["work"] ? "checked" : "" ?>> Work</label><br><br>

        <input type="submit" name="update_entry" value="Update Entry">
    </form>
</body>
</html>