<?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');
$method = $_SERVER['REQUEST_METHOD'];
if ($method == 'OPTIONS') {
    header('Access-Control-Allow-Origin: *');
    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('HTTP/1.1 200 OK');
    die();
}

include '../../../../config/db.php';
$data = [];
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    $query = 'SELECT Services.*, Categories.Category AS categoryName, Subcategories.Subcategory AS subcategoryName FROM Services INNER JOIN Categories ON Services.category = Categories.CategoryId
    INNER JOIN Subcategories ON Subcategories.SubcategoryId = Services.subcategory WHERE Services.status <> -1';
    if ($result = mysqli_query($con, $query)) {
        $i = 0;
        while ($rows = mysqli_fetch_assoc($result)) {
            $data[$i]['serviceId'] = $rows['serviceId'];
            $data[$i]['title'] = $rows['title'];
            $data[$i]['slug'] = $rows['slug'];
            $data[$i]['categoryName'] = $rows['categoryName'];
            $data[$i]['category'] = $rows['category'];
            $data[$i]['subcategoryName'] = $rows['subcategoryName'];
            $data[$i]['subcategory'] = $rows['subcategory'];
            $data[$i]['price'] = $rows['price'];
            $data[$i]['description'] = $rows['description'];
            $data[$i]['shortDescription'] = $rows['shortDescription'];
            $data[$i]['status'] = $rows['status'];
            $data[$i]['dateCreated'] = $rows['dateCreated'];
            $data[$i]['dateModified'] = $rows['dateModified'];
            $i++;
        }
        $response = array(
            'status' => 'success',
            'services' => $data
          );
        echo json_encode($response);
    }
} else {
    echo json_encode([
        'status' => -1,
    ]);
    die();
}
