<?php
// 1. CONFIGURAÇÕES E ERROS
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
date_default_timezone_set('America/Sao_Paulo');

// 2. CONEXÃO
$config_encontrado = false;
$caminhos = ['../loja/config.php', '../config.php', 'config.php'];
foreach ($caminhos as $caminho) {
    if (file_exists($caminho)) {
        include_once($caminho);
        $config_encontrado = true;
        break;
    }
}

// 3. VARIÁVEIS DE DASHBOARD
$total_pets = 0;
$proximas_vacinas = [];
$ultimos_pets = [];
$hoje = date('Y-m-d');

if ($config_encontrado && isset($conn)) {
    // Total de Animais
    $res = $conn->query("SELECT COUNT(*) as total FROM animais_cadastros");
    if($res) $total_pets = $res->fetch_assoc()['total'];

    // Últimos cadastrados
    $sql_pets = "SELECT nome, especie, raca FROM animais_cadastros ORDER BY id DESC LIMIT 5";
    $res_pets = $conn->query($sql_pets);
    if($res_pets) {
        while($row = $res_pets->fetch_assoc()) $ultimos_pets[] = $row;
    }

    // Vacinas pendentes nos próximos 15 dias
    $data_limite = date('Y-m-d', strtotime('+15 days'));
    $sql_vacina = "SELECT v.data_evento, v.descricao, a.nome 
                   FROM animais_eventos v 
                   JOIN animais_cadastros a ON v.animal_id = a.id 
                   WHERE v.realizado = 0 AND v.data_evento BETWEEN '$hoje' AND '$data_limite' 
                   ORDER BY v.data_evento ASC";
    $res_vacina = $conn->query($sql_vacina);
    if($res_vacina) {
        while($row = $res_vacina->fetch_assoc()) {
            $row['data_br'] = date('d/m', strtotime($row['data_evento']));
            $proximas_vacinas[] = $row;
        }
    }
}
?>

<?php include_once('../header.php'); ?>

<style>
    .dashboard-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 15px; }
    .card-pet { background: #fff; border-radius: 8px; border-top: 5px solid #2e7d32; padding: 15px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }
    .card-pet h2 { font-size: 12px; margin-bottom: 10px; color: #2e7d32; display: flex; justify-content: space-between; }
    .pet-list { width: 100%; border-collapse: collapse; font-size: 10px; }
    .pet-list tr { border-bottom: 1px solid #eee; }
    .pet-list td { padding: 8px 4px; }
    .badge-vacina { background: #fff3e0; color: #e65100; padding: 2px 5px; border-radius: 4px; font-weight: bold; }
    .status-ok { background: #e8f5e9; color: #2e7d32; padding: 10px; border-radius: 5px; text-align: center; font-weight: bold; margin-top: 10px; }
</style>

<div class="dashboard-grid">

    <div class="card-pet">
        <h2>🐾 MEUS PETS <span>GERAL</span></h2>
        <table class="pet-list">
            <?php if (empty($ultimos_pets)): ?>
                <tr><td style="text-align:center">NENHUM PET CADASTRADO</td></tr>
            <?php else: ?>
                <?php foreach ($ultimos_pets as $p): ?>
                <tr>
                    <td><strong><?php echo $p['nome']; ?></strong></td>
                    <td style="color: #777; text-align: right;"><?php echo $p['especie']; ?> (<?php echo $p['raca']; ?>)</td>
                </tr>
                <?php endforeach; ?>
            <?php endif; ?>
        </table>
        <div class="status-ok">TOTAL DE PETS: <?php echo $total_pets; ?></div>
    </div>

    <div class="card-pet" style="border-top-color: #f57c00;">
        <h2 style="color: #f57c00;">💉 AGENDA DE VACINAS <span>15 DIAS</span></h2>
        <table class="pet-list">
            <?php if (empty($proximas_vacinas)): ?>
                <tr><td style="text-align:center; padding: 20px; color: #2e7d32;">✅ TUDO EM DIA POR ENQUANTO</td></tr>
            <?php else: ?>
                <?php foreach ($proximas_vacinas as $v): ?>
                <tr>
                    <td><span class="badge-vacina"><?php echo $v['data_br']; ?></span></td>
                    <td><strong><?php echo $v['nome']; ?></strong></td>
                    <td style="font-size: 9px;"><?php echo $v['descricao']; ?></td>
                </tr>
                <?php endforeach; ?>
            <?php endif; ?>
        </table>
    </div>

    <div class="card-pet" style="border-top-color: #333;">
        <h2 style="color: #333;">⚙️ AÇÕES RÁPIDAS</h2>
        <div style="display: flex; flex-direction: column; gap: 8px; margin-top: 10px;">
            <a href="cadastrar.php" class="btn btn-sm btn-outline-success">NOVO CADASTRO</a>
            <a href="vacinas.php" class="btn btn-sm btn-outline-warning">REGISTRAR VACINA</a>
            <a href="agenda.php" class="btn btn-sm btn-outline-dark">VER AGENDA COMPLETA</a>
        </div>
    </div>

</div>

</body>
</html>