"; echo "Test Menús - WhatsApp Bot"; echo ""; echo ""; echo "

🔧 Test de Funcionalidad de Menús

"; try { $db = Database::getInstance(); echo "
✅ Conexión a base de datos: EXITOSA
"; // Verificar tabla menus echo "

📋 Verificando tabla 'menus'

"; try { $result = $db->fetchAll("SHOW TABLES LIKE 'menus'"); if (count($result) > 0) { echo "
✅ Tabla 'menus' existe
"; // Mostrar estructura $structure = $db->fetchAll("DESCRIBE menus"); echo "

Estructura de tabla 'menus':

"; echo "
";
            foreach ($structure as $column) {
                echo "{$column['Field']} - {$column['Type']} - {$column['Null']} - {$column['Default']}\n";
            }
            echo "
"; } else { echo "
❌ Tabla 'menus' NO existe
"; echo "
💡 Creando tabla 'menus'...
"; $createMenusTable = " CREATE TABLE IF NOT EXISTS menus ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, menu_key VARCHAR(100) NOT NULL, description TEXT, welcome_message TEXT, status ENUM('active', 'inactive') DEFAULT 'active', options_count INT DEFAULT 0, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, INDEX idx_menu_key (menu_key), INDEX idx_status (status) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"; $db->execute($createMenusTable); echo "
✅ Tabla 'menus' creada
"; } } catch (Exception $e) { echo "
❌ Error verificando tabla 'menus': " . $e->getMessage() . "
"; } // Verificar tabla menu_options echo "

📋 Verificando tabla 'menu_options'

"; try { $result = $db->fetchAll("SHOW TABLES LIKE 'menu_options'"); if (count($result) > 0) { echo "
✅ Tabla 'menu_options' existe
"; // Mostrar estructura $structure = $db->fetchAll("DESCRIBE menu_options"); echo "

Estructura de tabla 'menu_options':

"; echo "
";
            foreach ($structure as $column) {
                echo "{$column['Field']} - {$column['Type']} - {$column['Null']} - {$column['Default']}\n";
            }
            echo "
"; } else { echo "
❌ Tabla 'menu_options' NO existe
"; echo "
💡 Creando tabla 'menu_options'...
"; $createMenuOptionsTable = " CREATE TABLE IF NOT EXISTS menu_options ( id INT AUTO_INCREMENT PRIMARY KEY, menu_id INT NOT NULL, option_key VARCHAR(10) NOT NULL, option_text VARCHAR(255) NOT NULL, option_action ENUM('message', 'submenu', 'template', 'function') DEFAULT 'message', option_value TEXT, order_index INT DEFAULT 0, is_active BOOLEAN DEFAULT true, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (menu_id) REFERENCES menus(id) ON DELETE CASCADE, INDEX idx_menu_id (menu_id), INDEX idx_option_key (option_key), INDEX idx_order (order_index) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"; $db->execute($createMenuOptionsTable); echo "
✅ Tabla 'menu_options' creada
"; } } catch (Exception $e) { echo "
❌ Error verificando tabla 'menu_options': " . $e->getMessage() . "
"; } // Verificar menús existentes echo "

📱 Menús existentes

"; try { $menus = $db->fetchAll("SELECT * FROM menus ORDER BY created_at DESC"); if (count($menus) > 0) { echo "
📋 Se encontraron " . count($menus) . " menús:
"; echo "
";
            foreach ($menus as $menu) {
                echo "ID: {$menu['id']} | Nombre: {$menu['name']} | Clave: {$menu['menu_key']} | Estado: {$menu['status']}\n";
            }
            echo "
"; } else { echo "
📋 No hay menús configurados aún
"; // Crear menú de ejemplo echo "
💡 Creando menú de ejemplo...
"; $menuData = [ 'name' => 'Menú Principal', 'menu_key' => 'main', 'description' => 'Menú principal del bot', 'welcome_message' => '¡Hola! Bienvenido a nuestro servicio. Por favor selecciona una opción:', 'status' => 'active', 'options_count' => 0 ]; $menuId = $db->insert('menus', $menuData); if ($menuId) { echo "
✅ Menú de ejemplo creado (ID: {$menuId})
"; // Crear opciones de ejemplo $options = [ ['menu_id' => $menuId, 'option_key' => '1', 'option_text' => 'Información', 'option_action' => 'message', 'option_value' => 'Aquí tienes información sobre nuestros servicios', 'order_index' => 1], ['menu_id' => $menuId, 'option_key' => '2', 'option_text' => 'Soporte', 'option_action' => 'message', 'option_value' => 'Nuestro equipo de soporte te ayudará en breve', 'order_index' => 2], ['menu_id' => $menuId, 'option_key' => '0', 'option_text' => 'Salir', 'option_action' => 'message', 'option_value' => 'Gracias por contactarnos', 'order_index' => 3] ]; foreach ($options as $option) { $db->insert('menu_options', $option); } // Actualizar contador de opciones $db->execute("UPDATE menus SET options_count = 3 WHERE id = :id", ['id' => $menuId]); echo "
✅ Opciones de menú creadas
"; } } } catch (Exception $e) { echo "
❌ Error verificando menús: " . $e->getMessage() . "
"; } // Test de API echo "

🔌 Test de API

"; try { $apiUrl = 'http://localhost:8000/api/get_menus.php'; echo "
📞 Probando API: {$apiUrl}
"; $context = stream_context_create([ 'http' => [ 'method' => 'GET', 'header' => 'Content-Type: application/json', 'timeout' => 10 ] ]); $response = @file_get_contents($apiUrl, false, $context); if ($response !== false) { $data = json_decode($response, true); if ($data !== null) { echo "
✅ API responde correctamente
"; echo "
" . json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "
"; } else { echo "
❌ API responde pero JSON inválido
"; echo "
" . htmlspecialchars($response) . "
"; } } else { echo "
❌ No se pudo conectar a la API
"; } } catch (Exception $e) { echo "
❌ Error probando API: " . $e->getMessage() . "
"; } } catch (Exception $e) { echo "
❌ Error de conexión: " . $e->getMessage() . "
"; } echo "
"; echo "

Estado del servidor: El servidor PHP está corriendo en http://localhost:8000

"; echo "

Aplicación principal: WhatsApp Bot Manager

"; echo "

Fecha de prueba: " . date('Y-m-d H:i:s') . "

"; echo ""; ?>