<?php
/**
 * Script para testar a estrutura das tabelas
 * Verifica se as colunas estão corretas
 */

header('Content-Type: text/html; charset=utf-8');

// Conectar ao banco
function conectarBanco() {
    $host = 'localhost';
    $dbname = 'pinheir3_controle_frotasV1.1';
    $username = 'pinheir3_admin';
    $password = '080880Gelboy';
    
    try {
        $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8mb4", $username, $password, [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        ]);
        return $pdo;
    } catch (PDOException $e) {
        throw new Exception('Erro de conexão: ' . $e->getMessage());
    }
}

try {
    $pdo = conectarBanco();
    echo "<h1>🔍 Teste de Estrutura do Banco</h1>";
    echo "<style>
        .success { color: green; font-weight: bold; }
        .error { color: red; font-weight: bold; }
        .info { color: blue; }
        table { border-collapse: collapse; width: 100%; margin: 10px 0; }
        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
        th { background-color: #f2f2f2; }
    </style>";
    
    // Testar tabela motoristas
    echo "<h2>📋 Tabela Motoristas</h2>";
    $stmt = $pdo->query("DESCRIBE motoristas");
    $colunas = $stmt->fetchAll();
    
    echo "<table>";
    echo "<tr><th>Campo</th><th>Tipo</th><th>Nulo</th><th>Chave</th><th>Padrão</th><th>Extra</th></tr>";
    foreach ($colunas as $coluna) {
        echo "<tr>";
        echo "<td>{$coluna['Field']}</td>";
        echo "<td>{$coluna['Type']}</td>";
        echo "<td>{$coluna['Null']}</td>";
        echo "<td>{$coluna['Key']}</td>";
        echo "<td>{$coluna['Default']}</td>";
        echo "<td>{$coluna['Extra']}</td>";
        echo "</tr>";
    }
    echo "</table>";
    
    // Testar tabela veículos
    echo "<h2>🚗 Tabela Veículos</h2>";
    $stmt = $pdo->query("DESCRIBE veiculos");
    $colunas = $stmt->fetchAll();
    
    echo "<table>";
    echo "<tr><th>Campo</th><th>Tipo</th><th>Nulo</th><th>Chave</th><th>Padrão</th><th>Extra</th></tr>";
    foreach ($colunas as $coluna) {
        echo "<tr>";
        echo "<td>{$coluna['Field']}</td>";
        echo "<td>{$coluna['Type']}</td>";
        echo "<td>{$coluna['Null']}</td>";
        echo "<td>{$coluna['Key']}</td>";
        echo "<td>{$coluna['Default']}</td>";
        echo "<td>{$coluna['Extra']}</td>";
        echo "</tr>";
    }
    echo "</table>";
    
    // Testar tabela clientes
    echo "<h2>👥 Tabela Clientes</h2>";
    $stmt = $pdo->query("DESCRIBE clientes");
    $colunas = $stmt->fetchAll();
    
    echo "<table>";
    echo "<tr><th>Campo</th><th>Tipo</th><th>Nulo</th><th>Chave</th><th>Padrão</th><th>Extra</th></tr>";
    foreach ($colunas as $coluna) {
        echo "<tr>";
        echo "<td>{$coluna['Field']}</td>";
        echo "<td>{$coluna['Type']}</td>";
        echo "<td>{$coluna['Null']}</td>";
        echo "<td>{$coluna['Key']}</td>";
        echo "<td>{$coluna['Default']}</td>";
        echo "<td>{$coluna['Extra']}</td>";
        echo "</tr>";
    }
    echo "</table>";
    
    // Verificar dados existentes
    echo "<h2>📊 Dados Existentes</h2>";
    
    $stmt = $pdo->query("SELECT COUNT(*) as total FROM motoristas");
    $motoristas = $stmt->fetch()['total'];
    echo "<p><strong>Motoristas:</strong> $motoristas registros</p>";
    
    $stmt = $pdo->query("SELECT COUNT(*) as total FROM veiculos");
    $veiculos = $stmt->fetch()['total'];
    echo "<p><strong>Veículos:</strong> $veiculos registros</p>";
    
    $stmt = $pdo->query("SELECT COUNT(*) as total FROM clientes");
    $clientes = $stmt->fetch()['total'];
    echo "<p><strong>Clientes:</strong> $clientes registros</p>";
    
    echo "<h2>✅ Teste Concluído</h2>";
    echo "<p class='success'>Estrutura do banco verificada com sucesso!</p>";
    
} catch (Exception $e) {
    echo "<p class='error'>❌ Erro: " . $e->getMessage() . "</p>";
}
?> 
