<?php

include '../db.php'; // Include your database connection



$categoryName = $_GET['category'] ?? '';



if ($categoryName) {

    // Fetch the category ID based on the selected category name

    $categoryQuery = "SELECT cid FROM category WHERE cname = ?";

    $stmt = $con->prepare($categoryQuery);

    $stmt->bind_param("s", $categoryName);

    $stmt->execute();

    $categoryResult = $stmt->get_result();

    $category = $categoryResult->fetch_assoc();

    $categoryId = $category['cid'] ?? null;



    if ($categoryId) {

        // Fetch services where data_subCategory matches the selected category ID

        $sql = "SELECT data_title FROM data WHERE data_up_by = '1' AND data_catagory = 'Service' AND data_recycle = '1' AND data_subCategory = ? ORDER BY data_title ASC";

        $stmt = $con->prepare($sql);

        $stmt->bind_param("i", $categoryId);

    }

} else {

    // No category selected; fetch all services

    $sql = "SELECT data_title FROM data WHERE data_up_by = '1' AND data_catagory = 'Service' AND data_recycle = '1' ORDER BY data_title ASC";

    $stmt = $con->prepare($sql);

}



$stmt->execute();

$result = $stmt->get_result();



$services = [];

while ($row = $result->fetch_assoc()) {

    $services[] = $row;

}



echo json_encode($services);

?>