<?php
session_start();
include '../db/db.php';
file_put_contents(__DIR__ . '/log_update.txt', print_r($_POST, true), FILE_APPEND);

header('Content-Type: application/json');

$id   = isset($_POST['id'])             ? intval($_POST['id'])             : 0;
$newC = isset($_POST['idCaracteristica']) ? intval($_POST['idCaracteristica']) : 0;
$newV = isset($_POST['valor'])          ? trim($_POST['valor'])            : '';

if (!$id || !$newC) {
    echo json_encode(['ok' => false, 'error' => 'Faltan parámetros (id o idCaracteristica)']);
    exit;
}

$stmt = $conn->prepare("
    UPDATE tb_caracteristicas_productos
    SET idCaracteristica = ?, valor = ?
    WHERE id = ?
");
if (!$stmt) {
    echo json_encode(['ok' => false, 'error' => 'Prepare falló: ' . $conn->error]);
    exit;
}

$stmt->bind_param('isi', $newC, $newV, $id);
$res = $stmt->execute();
if (!$res) {
    echo json_encode(['ok' => false, 'error' => 'Execute falló: ' . $stmt->error]);
    $stmt->close();
    exit;
}

$stmt->close();
echo json_encode(['ok' => true]);
