<?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->femaleName) &&
        !empty($data->maleName) &&
        !empty($data->femaleCountry) &&
        !empty($data->femaleCity) &&
        !empty($data->maleCountry) &&
        !empty($data->maleCity) &&
        !empty($data->femaleYear) &&
        !empty($data->femaleMonth) &&
        !empty($data->femaleDate) &&
        !empty($data->femaleTime) &&
        !empty($data->maleYear) &&
        !empty($data->maleMonth) &&
        !empty($data->maleDate) &&
        !empty($data->maleTime) &&
        !empty($data->sendingMethod) &&
        !empty($data->email) &&
        !empty($data->phone)
    ) {
        $orderId = mysqli_real_escape_string($con, $data->orderId);
        $femaleName = mysqli_real_escape_string($con, $data->femaleName);
        $maleName = mysqli_real_escape_string($con, $data->maleName);
        $femaleBirthCountry = mysqli_real_escape_string($con, $data->femaleCountry);
        $femaleBirthPlace = mysqli_real_escape_string($con, $data->femaleCity);
        $maleBirthCountry = mysqli_real_escape_string($con, $data->maleCountry);
        $maleBirthPlace = mysqli_real_escape_string($con, $data->maleCity);
        $femaleYear = mysqli_real_escape_string($con, $data->femaleYear);
        $femaleMonth = mysqli_real_escape_string($con, $data->femaleMonth);
        $femaleDate = mysqli_real_escape_string($con, $data->femaleDate);
        $femaleTime = mysqli_real_escape_string($con, $data->femaleTime);
        $maleYear = mysqli_real_escape_string($con, $data->maleYear);
        $maleMonth = mysqli_real_escape_string($con, $data->maleMonth);
        $maleDate = mysqli_real_escape_string($con, $data->maleDate);
        $maleTime = mysqli_real_escape_string($con, $data->maleTime);
        $sendingMethod = mysqli_real_escape_string($con, $data->sendingMethod);
        $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 `PromisesOrders`(`promiseOrderId`, `orderId`, `femaleName`, `maleName`, `femaleBirthCountry`, `maleBirthCountry`, `femaleBirthPlace`, `maleBirthPlace`, `femaleBirthTime`, `maleBirthTime`, `femaleYear`, `femaleMonth`, `femaleDate`, `maleYear`, `maleMonth`, `maleDate`, `sendingMethod`, `email`, `phone`, `status`, `dateCreated`, `dateModified`)
        VALUES (null,$orderId,'$femaleName','$maleName','$femaleBirthCountry','$maleBirthCountry','$femaleBirthPlace','$maleBirthPlace','$femaleTime','$maleTime','$femaleYear','$femaleMonth','$femaleDate','$maleYear','$maleMonth','$maleDate','$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.',
                ]);
                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);
}
