<?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();
}
require '../../../../config/db.php';
$data = [];
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    $catId = $_GET['id'];
    $query = "SELECT * FROM Subcategories sc INNER JOIN Categories c ON sc.CategoryId = c.CategoryId WHERE sc.CategoryId = '$catId'";
    if($result = mysqli_query($con,$query)){
        $i = 0;
        while($rows = mysqli_fetch_assoc($result)){
            $data[$i]['SubcategoryId'] = $rows['SubcategoryId'];
            $data[$i]['Subcategory'] = $rows['Subcategory'];
            $data[$i]['CategoryId'] = $rows['CategoryId'];
            $data[$i]['Category'] = $rows['Category'];
            $data[$i]['Status'] = $rows['Status'];
            $data[$i]['IsFeatured'] = $rows['IsFeatured'];
            $data[$i]['DateCreated'] = $rows['DateCreated'];
            $data[$i]['DateModified'] = $rows['DateModified'];
            $i++;
        }
        echo json_encode($data);
    }
} else {
    echo json_encode([
        'status' => -1
    ]);
    die();
}
