<?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;
    }
}

$hoje = date('Y-m-d');
$filtro_tipo = isset($_GET['tipo']) ? $_GET['tipo'] : 'TUDO';

// 3. BUSCA DE EVENTOS
$eventos_hoje = [];
$eventos_atrasados = [];
$eventos_futuros = [];

if ($config_encontrado && isset($conn)) {
    $where_tipo = ($filtro_tipo != 'TUDO') ? " AND e.tipo = '$filtro_tipo' " : "";
    
    $sql = "SELECT e.*, a.nome as nome_animal 
            FROM animais_eventos e 
            JOIN animais_cadastros a ON e.animal_id = a.id 
            WHERE e.realizado = 0 $where_tipo
            ORDER BY e.data_evento ASC";
    
    $res = $conn->query($sql);
    if($res) {
        while($row = $res->fetch_assoc()) {
            if($row['data_evento'] == $hoje) {
                $eventos_hoje[] = $row;
            } elseif ($row['data_evento'] < $hoje) {
                $eventos_atrasados[] = $row;
            } else {
                $eventos_futuros[] = $row;
            }
        }
    }
}

// Função para retornar o ícone do serviço
function getIcone($tipo) {
    switch($tipo) {
        case 'BANHO': return '🛁';
        case 'TOSA': return '✂️';
        case 'CONSULTA': return '🩺';
        case 'VACINA': return '💉';
        default: return '📅';
    }
}
?>

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

<style>
    .container-agenda { padding: 10px; }
    .filtros-agenda { display: flex; gap: 5px; margin-bottom: 15px; overflow-x: auto; padding-bottom: 5px; }
    .btn-filtro { padding: 5px 10px; border-radius: 20px; border: 1px solid #ccc; background: #fff; font-size: 10px; font-weight: bold; text-decoration: none; color: #666; white-space: nowrap; }
    .btn-filtro.active { background: #333; color: #fff; border-color: #333; }

    .grid-agenda { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 15px; }
    .card-agenda { background: #fff; border-radius: 5px; border-top: 5px solid #333; box-shadow: 0 2px 8px rgba(0,0,0,0.1); padding: 12px; }
    .card-hoje { border-top-color: #2e7d32; background: #f1f8e9; }
    .card-atrasado { border-top-color: #d32f2f; }
    .card-futuro { border-top-color: #00569e; }

    .titulo-card { font-weight: 900; font-size: 11px; margin-bottom: 10px; display: flex; justify-content: space-between; border-bottom: 1px solid rgba(0,0,0,0.05); padding-bottom: 5px; }
    
    .item-agenda { border-bottom: 1px solid #eee; padding: 10px 0; display: flex; justify-content: space-between; align-items: flex-start; }
    .item-agenda:last-child { border-bottom: none; }
    
    .info-pet { font-size: 11px; flex: 1; }
    .info-pet strong { color: #333; font-size: 12px; }
    .info-pet .servico-tag { font-size: 9px; font-weight: bold; color: #2e7d32; margin-top: 2px; }
    .info-pet .obs-texto { display: block; font-size: 10px; color: #d32f2f; font-style: italic; margin-top: 3px; background: rgba(255,255,255,0.5); padding: 2px 5px; border-radius: 3px; border-left: 2px solid #d32f2f; }
    
    .btn-check { background: #2e7d32; color: #fff; border: none; padding: 5px 10px; font-size: 10px; font-weight: bold; border-radius: 4px; cursor: pointer; text-decoration: none; margin-left: 10px; }
</style>

<div class="container-agenda">
    
    <div style="margin-bottom: 10px; display: flex; justify-content: space-between; align-items: center;">
        <h5 style="margin:0; font-weight: bold; color: #333;">📅 AGENDA PET</h5>
        <a href="vacinas.php" class="btn btn-sm btn-warning" style="font-size: 10px; font-weight: bold; color: #fff;">+ NOVO</a>
    </div>

    <div class="filtros-agenda">
        <a href="agenda.php?tipo=TUDO" class="btn-filtro <?php echo $filtro_tipo == 'TUDO' ? 'active' : ''; ?>">MOSTRAR TUDO</a>
        <a href="agenda.php?tipo=BANHO" class="btn-filtro <?php echo $filtro_tipo == 'BANHO' ? 'active' : ''; ?>">🛁 BANHOS</a>
        <a href="agenda.php?tipo=TOSA" class="btn-filtro <?php echo $filtro_tipo == 'TOSA' ? 'active' : ''; ?>">✂️ TOSAS</a>
        <a href="agenda.php?tipo=CONSULTA" class="btn-filtro <?php echo $filtro_tipo == 'CONSULTA' ? 'active' : ''; ?>">🩺 CONSULTAS</a>
        <a href="agenda.php?tipo=VACINA" class="btn-filtro <?php echo $filtro_tipo == 'VACINA' ? 'active' : ''; ?>">💉 VACINAS</a>
    </div>

    <div class="grid-agenda">
        
        <div class="card-agenda card-hoje">
            <div class="titulo-card" style="color: #2e7d32;">🌟 HOJE <span><?php echo count($eventos_hoje); ?></span></div>
            <?php foreach($eventos_hoje as $e): ?>
            <div class="item-agenda">
                <div class="info-pet">
                    <strong><?php echo getIcone($e['tipo']); ?> <?php echo $e['nome_animal']; ?></strong>
                    <div class="servico-tag"><?php echo $e['tipo']; ?></div>
                    <?php if(!empty($e['descricao'])): ?>
                        <span class="obs-texto">⚠️ <?php echo $e['descricao']; ?></span>
                    <?php endif; ?>
                </div>
                <a href="vacinas.php?baixa=<?php echo $e['id']; ?>" class="btn-check">CONCLUIR</a>
            </div>
            <?php endforeach; ?>
        </div>

        <div class="card-agenda card-atrasado">
            <div class="titulo-card" style="color: #d32f2f;">⚠️ ATRASADOS <span><?php echo count($eventos_atrasados); ?></span></div>
            <?php foreach($eventos_atrasados as $e): ?>
            <div class="item-agenda">
                <div class="info-pet">
                    <strong><?php echo getIcone($e['tipo']); ?> <?php echo $e['nome_animal']; ?></strong>
                    <div class="servico-tag" style="color: #d32f2f;"><?php echo date('d/m', strtotime($e['data_evento'])); ?> - <?php echo $e['tipo']; ?></div>
                    <?php if(!empty($e['descricao'])): ?>
                        <span class="obs-texto"><?php echo $e['descricao']; ?></span>
                    <?php endif; ?>
                </div>
                <a href="vacinas.php?baixa=<?php echo $e['id']; ?>" class="btn-check">OK</a>
            </div>
            <?php endforeach; ?>
        </div>

        <div class="card-agenda card-futuro">
            <div class="titulo-card" style="color: #00569e;">🗓️ PRÓXIMOS DIAS <span><?php echo count($eventos_futuros); ?></span></div>
            <?php foreach($eventos_futuros as $e): ?>
            <div class="item-agenda">
                <div class="info-pet">
                    <strong><?php echo getIcone($e['tipo']); ?> <?php echo $e['nome_animal']; ?></strong>
                    <div class="servico-tag" style="color: #00569e;"><?php echo date('d/m', strtotime($e['data_evento'])); ?> - <?php echo $e['tipo']; ?></div>
                    <?php if(!empty($e['descricao'])): ?>
                        <span class="obs-texto"><?php echo $e['descricao']; ?></span>
                    <?php endif; ?>
                </div>
            </div>
            <?php endforeach; ?>
        </div>

    </div>
</div>

</body>
</html>