<?php
// cms/differentiators-read.php
header('Content-Type: application/json');

require_once('db_connection.php');

try {
    $data = [
        'general' => [],
        'items' => []
    ];

    // Obtener datos generales de la sección
    $stmt = $pdo->query("SELECT * FROM cms_differentiators ORDER BY updated_at DESC LIMIT 1");
    $generalData = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($generalData) {
        $data['general'] = $generalData;
        $differentiatorsId = $generalData['id'];

        // Obtener la lista de diferenciadores
        $stmtItems = $pdo->prepare("SELECT item_icon, item_title, item_description FROM cms_differentiators_items WHERE differentiators_id = ? ORDER BY id ASC");
        $stmtItems->execute([$differentiatorsId]);
        $data['items'] = $stmtItems->fetchAll(PDO::FETCH_ASSOC);
    } 

    echo json_encode(['success' => true, 'data' => $data]);
} catch (PDOException $e) {
    http_response_code(500);
    echo json_encode(['success' => false, 'error' => 'Error Al Cargar Los Datos' . $e->getMessage()]);
}
?>