<?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');

include '../../../../config/db.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = json_decode(file_get_contents('php://input'));
    if (
        !empty($data->orderId) &&
        !empty($data->firstName) &&
        !empty($data->lastName) &&
        !empty($data->country) &&
        !empty($data->city) &&
        !empty($data->year) &&
        !empty($data->month) &&
        !empty($data->date) &&
        !empty($data->time) &&
        !empty($data->sendingMethod) &&
        !empty($data->phone)
    ) {
        $orderId = mysqli_real_escape_string($con, $data->orderId);
        $fName = mysqli_real_escape_string($con, $data->firstName);
        $lName = mysqli_real_escape_string($con, $data->lastName);
        $birthCountry = mysqli_real_escape_string($con, $data->country);
        $birthPlace = mysqli_real_escape_string($con, $data->city);
        $year = mysqli_real_escape_string($con, $data->year);
        $month = mysqli_real_escape_string($con, $data->month);
        $date = mysqli_real_escape_string($con, $data->date);
        $birthTime = mysqli_real_escape_string($con, $data->time);
        $remarks = mysqli_real_escape_string($con, $data->remarks);
        $isSpeech = mysqli_real_escape_string($con, $data->isSpeech);
        $sendingMethod = mysqli_real_escape_string($con, $data->sendingMethod);
        $email = (isset($data->email)) ? mysqli_real_escape_string($con, $data->email) : "";
        $phone = mysqli_real_escape_string($con, $data->phone);
        $dateTime = date("Y-m-d H:i:s");

        $query = "INSERT INTO `HoroscopeOrders`(`horoscopeOrderId`, `orderId`, `fName`, `lName`, `birthCountry`, `birthPlace`, `birthTime`, `year`, `month`, `date`, `isSpeech`, `sendingMethod`, `email`, `phone`, `remarks`, `status`, `dateCreated`, `dateModified`)
        VALUES (null,$orderId,'$fName','$lName','$birthCountry','$birthPlace','$birthTime','$year','$month','$date','$isSpeech','$sendingMethod','$email','$phone','$remarks',1,'$dateTime','$dateTime')";


        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 {
                deleteOrder($con, $orderId);
                echo json_encode([
                    'status' => 0,
                    'message' => 'Could not insert data.',
                ]);
                http_response_code(400);
            }
        } else {
            deleteOrder($con, $orderId);
            echo json_encode([
                'status' => 0,
                'message' => mysqli_error($con),
            ]);
            http_response_code(400);
        }
    } else {
        deleteOrder($con, $data->orderId);
        echo json_encode([
            'status' => -1,
            'message' => 'Required fields are missing',
        ]);
        http_response_code(400);
    }
} else {
    echo json_encode([
        'status' => 0,
    ]);
    die();
}


function deleteOrder($con, $id)
{
    $delete_query = "DELETE FROM Orders WHERE OrderId= $id";
    mysqli_query($con, $delete_query);
}
