<?php
include 'connect.php';
$categoryName = $_POST['categoryName'];
$imageName = $_FILES['categoryImage']['name'];
$categoryColor = $_POST['categoryColor'];

$imageTmp = $_FILES['categoryImage']['tmp_name'];
$imageExtension = pathinfo($imageName, PATHINFO_EXTENSION); // Get the file extension

$folderName = "../images/categories/";
$newImageName = uniqid() . '.' . $imageExtension;
$destination = $folderName . $newImageName;
$image = "images/categories/" . $newImageName;

// Check if the uploaded file is a PNG image
if (strtolower($imageExtension) != 'png') {
    echo "Only PNG images are accepted.";
    exit;
}

if (move_uploaded_file($imageTmp, $destination)) {
    $sql = "INSERT INTO a_categories (CategoryName, CategoryImage, CategoryColor) VALUES ('$categoryName', '$image', '$categoryColor')";
    $result = mysqli_query($con, $sql);
    if ($result) {
        echo "Done";
    } else {
        die(mysqli_error($con));
    }
} else {
    echo "Error uploading the file.";
}

?>