<?php
// assets/includes/loadBusinessCategories.php
header('Content-Type: text/html; charset=utf-8');
header('Cache-Control: no-store');

require_once('../config/dbConnection.php');

$db   = DatabaseConnection::getInstance();
$conn = $db->getConnection();

// Validate and sanitize the type_id
if (!isset($_GET['type_id']) || !ctype_digit($_GET['type_id'])) {
    echo "<option value=''>-- Invalid or Missing Type --</option>";
    exit;
}

$type_id = (int) $_GET['type_id'];

// Use a prepared statement to prevent SQL injection
$sql = "SELECT id, name 
        FROM smartsync_businesscategories 
        WHERE business_type_id = ?
        ORDER BY name ASC";

$stmt = $conn->prepare($sql);
if (!$stmt) {
    echo "<option value=''>-- Database Error --</option>";
    exit;
}

$stmt->bind_param('i', $type_id);
$stmt->execute();
$result = $stmt->get_result();

echo "<option value=''>-- Select Category --</option>";

if ($result->num_rows > 0) {
    while ($c = $result->fetch_assoc()) {
        $id   = htmlspecialchars($c['id']);
        $name = htmlspecialchars($c['name']);
        echo "<option value='{$id}'>{$name}</option>";
    }
} else {
    echo "<option value=''>-- No Categories Found --</option>";
}

$stmt->close();
