<?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->gender) &&
        !empty($data->country) &&
        !empty($data->city) &&
        !empty($data->year) &&
        !empty($data->month) &&
        !empty($data->date) &&
        !empty($data->time) &&
        !empty($data->natureOfName) &&
        !empty($data->motherName) &&
        !empty($data->fatherName) &&
        !empty($data->sendingMethod) &&
        !empty($data->phone)
    ) {
        $orderId = mysqli_real_escape_string($con, $data->orderId);
        $gender = mysqli_real_escape_string($con, $data->gender);
        $country = mysqli_real_escape_string($con, $data->country);
        $city = 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);
        $time = mysqli_real_escape_string($con, $data->time);
        $nameType = mysqli_real_escape_string($con, $data->natureOfName);
        $siblingNames = mysqli_real_escape_string($con, $data->siblingNames);
        $otherRequirements = mysqli_real_escape_string($con, $data->otherRquirements);
        $motherName = mysqli_real_escape_string($con, $data->motherName);
        $fatherName = mysqli_real_escape_string($con, $data->fatherName);
        $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 `NameOrders`(`nameOrderId`, `orderId`, `gender`, `country`, `city`, `year`, `month`, `date`, `time`, `nameType`, `siblingNames`, `otherRequirements`, `motherName`, `fatherName`, `sendingMethod`, `email`, `phone`, `status`, `dateCreated`, `dateModified`)
        VALUES (null,$orderId,'$gender','$country','$city','$year','$month','$date','$time','$nameType','$siblingNames','$otherRequirements','$motherName','$fatherName','$sendingMethod','$email','$phone',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.',
                    'error' => mysqli_error($con)
                ]);
                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,
        'message' => 'Method not allowed'
    ]);
    http_response_code(200);
}


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