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

require_once('db_connection.php');

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

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

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

        // Obtener la lista de beneficios
        $stmtBenefits = $pdo->prepare("SELECT benefit_title, benefit_description FROM cms_form_benefits WHERE form_id = ? ORDER BY id ASC");
        $stmtBenefits->execute([$formId]);
        $data['benefits'] = $stmtBenefits->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()]);
}
?>