<?php
header('Content-Type: application/json; charset=utf-8');

require_once __DIR__ . '/../../odbconexion/odbconexion.php';

try {
  $db = (new odbconexion())->openconection();
  $db->set_charset("utf8mb4");

  $sql = "SELECT Id_Catalogo_Cliente_Pertenece, Promotor
          FROM catalogo_cliente_pertenece
          ORDER BY Promotor ASC";

  $res = $db->query($sql);
  $out = [];

  while($row = $res->fetch_assoc()){
    $out[] = [
      "id" => (int)$row['Id_Catalogo_Cliente_Pertenece'],
      "text" => trim($row['Promotor'] ?? '')
    ];
  }

  echo json_encode(["ok"=>true, "data"=>$out], JSON_UNESCAPED_UNICODE);
} catch (Throwable $e){
  echo json_encode(["ok"=>false, "msg"=>"Error: ".$e->getMessage()], JSON_UNESCAPED_UNICODE);
}
