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

require_once('db_connection.php');

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

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

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

        // Obtener la lista de ciudades
        $stmtCities = $pdo->prepare("SELECT city_name FROM cms_coverage_cities WHERE coverage_id = ? ORDER BY id ASC");
        $stmtCities->execute([$coverageId]);
        $data['cities'] = $stmtCities->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()]);
}
?>