<?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)
    ) {
        $orderId = $data->orderId;
        $serviceId = $data->serviceId;
        $orderDataId = $data->orderDataId;
        $isExistQuery = "SELECT * FROM Orders WHERE orderId = '$orderId' ";
        $deleteQuery = "DELETE FROM `Orders` WHERE orderId = '$orderId'";
        if (mysqli_num_rows(mysqli_query($con, $isExistQuery)) != 0) {
            if($serviceId == 1 || $serviceId == 2){
                $deleteOrderDataQuery = "DELETE FROM `NameOrders` WHERE nameOrderId = '$orderDataId'";
            }else if($serviceId == 4 || $serviceId == 5 || $serviceId == 11){
                $deleteOrderDataQuery = "DELETE FROM `BrandingOrders` WHERE brandOrderId = '$orderDataId'";
            }else if($serviceId == 6 || $serviceId == 7){
                $deleteOrderDataQuery = "DELETE FROM `HoroscopeOrders` WHERE horoscopeOrderId = '$orderDataId'";
            }else if($serviceId == 8){
                $deleteOrderDataQuery = "DELETE FROM `AuspiciousOrders` WHERE auspiciousOrderId = '$orderDataId'";
            }else if($serviceId == 9){
                $deleteOrderDataQuery = "DELETE FROM `PromisesOrders` WHERE promiseOrderId = '$orderDataId'";
            }
            if (mysqli_query($con, $deleteQuery)) {
                if (mysqli_affected_rows($con) == 1) {
                    if (mysqli_query($con, $deleteOrderDataQuery)) {
                        if (mysqli_affected_rows($con) == 1) {
                            echo json_encode([
                                'status' => 1,
                                'message' => 'Data Deleted successfully.',
                            ]);
                            http_response_code(200);
                        }else{
                            echo json_encode([
                                'status' => 0,
                                'message' => mysqli_error($con),
                            ]);
                            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,
    ]);
    die();
}

?>