<?php
include 'db.php';
include 'auth.php';

header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode(['success' => false, 'message' => 'Invalid request method']);
    exit;
}

$sub_category_id = $_POST['sub_category_id'] ?? null;
$sub_category_name = trim($_POST['sub_category_name'] ?? '');

if (!$sub_category_id || empty($sub_category_name)) {
    echo json_encode(['success' => false, 'message' => 'Sub-category ID and name are required']);
    exit;
}

try {
    $stmt = $conn->prepare("UPDATE sub_categories SET name = ? WHERE id = ?");
    $stmt->bind_param("si", $sub_category_name, $sub_category_id);
    $stmt->execute();
    
    if ($stmt->affected_rows > 0) {
        echo json_encode(['success' => true, 'message' => 'Sub-category updated successfully']);
    } else {
        echo json_encode(['success' => false, 'message' => 'No changes made or sub-category not found']);
    }
} catch (Exception $e) {
    echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
}
?> 