<?php
header('Access-Control-Allow-Origin: *');
header(
    'Access-Control-Allow-Methods: HEAD, GET, POST, PUT, PATCH, DELETE, OPTIONS'
);
header(
    'Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method,Access-Control-Request-Headers, Authorization'
);
header('Content-Type: application/json');

$shortDescription = "";
include '../../../../config/db.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = json_decode(file_get_contents('php://input'));
    if (
        !empty($data->blogId) &&
        !empty($data->title) &&
        !empty($data->slug) &&
        !empty($data->author) &&
        !empty($data->article)
    ) {
        $blogId = mysqli_real_escape_string($con, $data->blogId);
        $title = mysqli_real_escape_string($con, $data->title);
        $slug = mysqli_real_escape_string($con, $data->slug);
        $author = mysqli_real_escape_string($con, $data->author);
        $isDonation = mysqli_real_escape_string($con, $data->isDonation);
        $isSiteGuide = mysqli_real_escape_string($con, $data->isSiteGuide);
        $donationVideo = mysqli_real_escape_string($con, $data->donationVideo);
        $shortDescription= mysqli_real_escape_string($con, $data->shortDescription);
        $article = mysqli_real_escape_string($con, $data->article);
        $isImageUpdated = mysqli_real_escape_string($con, $data->isImageUpdated);
        $image = $data->image;
        $status = 1;

        // $dateTime = date('Y-m-d H:i:s');
        $isExistQuery = "SELECT * FROM Blog WHERE blogId = '$blogId' ";
        if (mysqli_num_rows(mysqli_query($con, $isExistQuery)) != 0) {
            $query = "UPDATE `Blog` SET `title`= '$title',`author`='$author',`isDonation`= '$isDonation', `isSiteGuide` = '$isSiteGuide' ,`donationVideo`= '$donationVideo',`shortDescription`= '$shortDescription',`article`= '$article',`status`='$status' WHERE blogId= '$blogId'";
            if (mysqli_query($con, $query)) {
                if (mysqli_affected_rows($con) == 1 || $isImageUpdated == true) {
                    if($isImageUpdated == 1){
                        $folderPath = "../../../../public/blog/";
                        $image_parts = explode(";base64,", $image);
                        $image_type_aux = explode("image/", $image_parts[0]);
                        $image_base64 = base64_decode($image_parts[1]);
                        $imageName =  uniqid() . '.png';
                        $file = $folderPath . $imageName;
                        // $res = file_put_contents($file, $image_base64);
                        if (file_put_contents($file, $image_base64)) {
                            $query = "UPDATE `Blog` SET `image`= '$imageName' WHERE blogId= '$blogId'";
                            if (mysqli_query($con, $query)) {
                                if (mysqli_affected_rows($con) == 1) {
                                    echo json_encode([
                                        'status' => 1,
                                        'message' => 'Data added successfully.',
                                    ]);
                                    http_response_code(200);
                                }
                            }else{
                                echo json_encode([
                                    'status' => -3,
                                    'message' => 'Image name update failed.',
                                ]);
                                http_response_code(200);
                            }
                        }else{
                            echo json_encode([
                                'status' => 2,
                                'message' => 'Data added successfully. But image save failed.',
                            ]);
                            http_response_code(200);
                        }
                    }else{
                        echo json_encode([
                            'status' => 1,
                            'message' => 'Data added successfully.',
                        ]);
                        http_response_code(200);
                    }
                } else {
                    echo json_encode([
                        'status' => 0,
                        'message' => 'Could not insert data.',
                    ]);
                    http_response_code(200);
                }
            } else {
                echo json_encode([
                    'status' => 0,
                    'message' => mysqli_error($con),
                ]);
                http_response_code(200);
            }
        } else {
            echo json_encode([
                'status' => -2,
                'message' => 'Data not exists',
            ]);
            http_response_code(200);
        }
    } else {
        echo json_encode([
            'status' => -1,
            'message' => 'Required fields are missing.',
        ]);
        http_response_code(200);
    }
} else {
    echo json_encode([
        'status' => -1,
    ]);
    http_response_code(200);
}

?>