<?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->ownerName1) &&
        !empty($data->country1) &&
        !empty($data->city1) &&
        !empty($data->year1) &&
        !empty($data->month1) &&
        !empty($data->date1) &&
        !empty($data->time1) &&
        !empty($data->nameType) &&
        !empty($data->natureOfName) &&
        !empty($data->natureOfBusiness) &&
        !empty($data->sendingMethod) &&
        !empty($data->phone)
    ) {
        $orderId = mysqli_real_escape_string($con, $data->orderId);
        $ownerName1 = mysqli_real_escape_string($con, $data->ownerName1);
        $ownerName2 = mysqli_real_escape_string($con, $data->ownerName2);
        $country1 = mysqli_real_escape_string($con, $data->country1);
        $country2 = mysqli_real_escape_string($con, $data->country2);
        $city1 = mysqli_real_escape_string($con, $data->city1);
        $city2 = mysqli_real_escape_string($con, $data->city2);
        $year1 = mysqli_real_escape_string($con, $data->year1);
        $month1 = mysqli_real_escape_string($con, $data->month1);
        $date1 = mysqli_real_escape_string($con, $data->date1);
        $time1 = mysqli_real_escape_string($con, $data->time1);
        $year2 = mysqli_real_escape_string($con, $data->year2);
        $month2 = mysqli_real_escape_string($con, $data->month2);
        $date2 = mysqli_real_escape_string($con, $data->date2);
        $time2 = mysqli_real_escape_string($con, $data->time2);
        $nameType = mysqli_real_escape_string($con, $data->nameType);
        $natureOfName = mysqli_real_escape_string($con, $data->natureOfName);
        $natureOfBusiness = mysqli_real_escape_string($con, $data->natureOfBusiness);
        $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 `BusinessNameOrders`(`nameOrderId`, `orderId`, `orderData`, `ownerNameF`, `ownerNameS`, `countryF`, `countryS`, `cityF`, `cityS`, `yearF`, `monthF`, `dateF`, `timeF`, `yearS`, `monthS`, `dateS`, `timeS`, `nameType`, `natureOfName`, `natureOfBusiness`, `sendingMethod`, `email`, `phone`, `status`, `dateCreated`, `dateModified`)
        VALUES (null,$orderId,'','$ownerName1','$ownerName2','$country1','$country2','$city1','$city2','$year1','$month1','$date1','$time1','$year2','$month2','$date2','$time2',$nameType,'$natureOfName','$natureOfBusiness','$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);
}
