<?php

require_once '../../core/api_bootstrap.php';

$data = Request::get();

try {

    Validator::required($data,[
        'category_id'
    ]);

    $db = new Database();
    $conn = $db->connect();

    $category_id = (int)$data['category_id'];

    $query = "

        SELECT
            id,
            category_id,
            name,
            icon,
            image,
            slug,
            short_description,
            status,
            created_at
        FROM sub_categories
        WHERE category_id = :category_id
        AND status = 1
        ORDER BY name ASC

    ";

    $stmt = $conn->prepare($query);

    $stmt->execute([
        ':category_id'=>$category_id
    ]);

    $rows = $stmt->fetchAll();

    Response::success($rows);

}
catch(Exception $e){

    Logger::log("system.log",$e->getMessage());

    Response::error("Server error",500);

}