<?php
require_once(__DIR__ . '/../db/conexion.php');
header('Content-Type: application/json');

$sql = "SELECT ID, Sector, Tiendas FROM sectoresdPDVs ORDER BY Sector";
$result = $conn->query($sql);

$sectores = [];

while ($row = $result->fetch_assoc()) {
    $tiendasRaw = explode(',', $row['Tiendas']);
    $tiendas = array_map('trim', $tiendasRaw);
    $sectores[] = [
        'id' => $row['ID'],
        'sector' => $row['Sector'],
        'tiendas' => $tiendas
    ];
}

echo json_encode(['success' => true, 'sectores' => $sectores]);
