<?php
echo 'test';
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');
$method = $_SERVER['REQUEST_METHOD'];
if ($method == "OPTIONS") {
    header('Access-Control-Allow-Origin: *');
    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("HTTP/1.1 200 OK");
    die();
}


include '../../../../config/db.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = json_decode(file_get_contents('php://input'));
    if (
        !empty($data->serviceId) &&
        !empty($data->title) &&
        !empty($data->slug) &&
        !empty($data->category) &&
        !empty($data->subcategory) &&
        !empty($data->price) &&
        !empty($data->description)
    ) {
        $serviceId = mysqli_real_escape_string($con, $data->serviceId);
        $title = mysqli_real_escape_string($con, $data->title);
        $slug = mysqli_real_escape_string($con, $data->slug);
        $category = mysqli_real_escape_string($con, $data->category);
        $subcategory = mysqli_real_escape_string($con, $data->subcategory);
        $price = mysqli_real_escape_string($con, $data->price);
        $shortDescription = mysqli_real_escape_string($con, $data->shortDescription);
        $description = mysqli_real_escape_string($con, $data->description);
        $status = mysqli_real_escape_string($con, $data->status);

        $isExistQuery = "SELECT * FROM Services WHERE serviceId = '$serviceId' ";
        if (mysqli_num_rows(mysqli_query($con, $isExistQuery)) != 0) {

            $query = "UPDATE Services SET `title` = '$title', `category` = '$category', `subcategory` = '$subcategory', `price` = '$price', `shortDescription` = '$shortDescription', `description` = '$description', `status` = $status, dateModified = NOW() WHERE serviceId = '$serviceId'";

            if (mysqli_query($con, $query)) {
                if (mysqli_affected_rows($con) == 1) {
                    echo json_encode([
                        'status' => 1,
                        'message' => 'Data updated 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);
}
