<?php
header('Content-Type: application/json; charset=UTF-8');
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

try {
    $host = 'localhost';
    $dbname = 'CORREOS';
    $user = 'root';
    $pass = 'root';

    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $user, $pass, [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ]);

    $id = $_POST['id'] ?? null;
    $destacado = $_POST['destacado'] ?? "0";

    if (!$id) {
        echo json_encode(["success" => false, "message" => "Falta el ID del correo"]);
        exit;
    }
    $stmt = $pdo->prepare("UPDATE CORREO SET DESTACADO = ? WHERE ID_CORREO = ?");
    $ok = $stmt->execute([$destacado, $id]);


    if (!$ok) {
        echo json_encode(["success" => false, "sqlError" => $stmt->errorInfo()]);
        exit;
    }


    echo json_encode(["success" => $ok]);
} catch (Exception $e) {
    echo json_encode(["success" => false, "message" => $e->getMessage()]);
}
