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

// Define a data de hoje para o filtro
$hoje = date('Y-m-d');

// SQL 1: VENDAS (ENTRADA)
$sql_vendas = "SELECT 
            PRODUTO_NOME, 
            SUM(QUANTIDADE) as TOTAL_QTD, 
            SUM(VALOR_TOTAL) as VALOR_ACUMULADO 
        FROM movimentacoes 
        WHERE DATE(DATA_MOVIMENTACAO) = '$hoje' 
        AND TIPO = 'ENTRADA' 
        GROUP BY PRODUTO_NOME 
        ORDER BY TOTAL_QTD DESC";
$res_vendas = $conn->query($sql_vendas);

// SQL 2: SAÍDAS (SAIDA)
$sql_saidas = "SELECT 
            PRODUTO_NOME, 
            SUM(QUANTIDADE) as TOTAL_QTD, 
            SUM(VALOR_TOTAL) as VALOR_ACUMULADO 
        FROM movimentacoes 
        WHERE DATE(DATA_MOVIMENTACAO) = '$hoje' 
        AND TIPO = 'SAIDA' 
        GROUP BY PRODUTO_NOME 
        ORDER BY TOTAL_QTD DESC";
$res_saidas = $conn->query($sql_saidas);

// Totais para os Cards
$total_vendas_res = $conn->query("SELECT SUM(VALOR_TOTAL) as TOTAL FROM movimentacoes WHERE DATE(DATA_MOVIMENTACAO) = '$hoje' AND TIPO = 'ENTRADA'")->fetch_assoc();
$total_saidas_res = $conn->query("SELECT SUM(VALOR_TOTAL) as TOTAL FROM movimentacoes WHERE DATE(DATA_MOVIMENTACAO) = '$hoje' AND TIPO = 'SAIDA'")->fetch_assoc();
?>

<style>
    .vendas-wrapper { display: flex; flex-direction: column; width: 100%; padding: 15px; box-sizing: border-box; text-transform: uppercase; font-family: 'Segoe UI', sans-serif; }
    
    .header-vendas { 
        display: flex; justify-content: space-between; align-items: center; 
        margin-bottom: 20px; border-bottom: 3px solid #28a745; padding-bottom: 10px; 
    }
    
    /* CARDS */
    .cards-container { display: flex; gap: 10px; margin-bottom: 20px; flex-wrap: wrap; }
    .card-resumo { color: #fff; padding: 15px; border-radius: 4px; flex: 1; min-width: 140px; text-align: center; box-sizing: border-box; }
    .card-vendas { background: #28a745; }
    .card-saidas { background: #d32f2f; }
    .card-resumo h3 { margin: 0; font-size: 9px; opacity: 0.9; }
    .card-resumo p { margin: 5px 0 0; font-size: 16px; font-weight: bold; font-family: monospace; }

    /* LADO A LADO CONFIG */
    .tabelas-lado-a-lado {
        display: flex;
        gap: 20px;
        align-items: flex-start;
    }

    .report-section { background-color: #fff; border: 1px solid #ccc; margin-bottom: 30px; flex: 1; overflow: hidden; }
    
    .report-title { 
        background-color: #333; color: #fff; padding: 10px; font-weight: bold; font-size: 11px; 
        text-align: center;
    }
    
    .table-container { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
    .table-vendas { width: 100%; border-collapse: collapse; font-size: 11px; }
    .table-vendas th { background-color: #f5f5f5; padding: 12px 10px; border-bottom: 2px solid #ddd; text-align: left; }
    .table-vendas td { padding: 12px 10px; border-bottom: 1px solid #eee; font-weight: bold; }
    
    .qtd-badge { background: #00569e; color: #fff; padding: 4px 8px; border-radius: 3px; font-family: monospace; }
    .valor-txt { color: #28a745; font-family: monospace; font-size: 13px; }
    .valor-txt-saida { color: #d32f2f; font-family: monospace; font-size: 13px; }

    /* --- AJUSTES PARA CELULAR --- */
    @media (max-width: 900px) {
        .tabelas-lado-a-lado { flex-direction: column; }
        .header-vendas { flex-direction: column; gap: 10px; text-align: center; }
        .cards-container { flex-direction: column; }
        .vendas-wrapper { padding: 10px; }
    }
</style>

<div class="vendas-wrapper">
    <div class="header-vendas">
        <div style="font-weight: bold; font-size: 18px; color: #28a745;">📈 LANÇAMENTOS DO DIA (<?php echo date('d/m/Y'); ?>)</div>
        <button type="button" onclick="window.location.reload()" style="cursor:pointer; padding: 8px 15px; font-weight:bold; font-size:10px;">🔄 ATUALIZAR</button>
    </div>

    <div class="cards-container">
        <div class="card-resumo card-vendas">
            <h3>TOTAL VENDAS (ENTRADAS)</h3>
            <p>R$ <?php echo number_format($total_vendas_res['TOTAL'] ?? 0, 2, ',', '.'); ?></p>
        </div>
        <div class="card-resumo card-saidas">
            <h3>TOTAL SAÍDAS (DESPESAS)</h3>
            <p>R$ <?php echo number_format($total_saidas_res['TOTAL'] ?? 0, 2, ',', '.'); ?></p>
        </div>
    </div>

    <div class="tabelas-lado-a-lado">
        <!-- TABELA DE VENDAS -->
        <div class="report-section">
            <div class="report-title" style="background: #28a745;">
                <span>📦 PRODUTOS VENDIDOS (ENTRADAS)</span>
            </div>
            <div class="table-container">
                <table class="table-vendas">
                    <thead>
                        <tr>
                            <th style="width: 30px;"></th>
                            <th>PRODUTO / SERVIÇO</th>
                            <th style="text-align: center; width: 80px;">QTD</th>
                            <th style="text-align: right; width: 120px;">VALOR TOTAL</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if($res_vendas && $res_vendas->num_rows > 0): 
                                while($v = $res_vendas->fetch_assoc()): ?>
                        <tr>
                            <td><input type="checkbox"></td>
                            <td><?php echo $v['PRODUTO_NOME']; ?></td>
                            <td style="text-align: center;"><span class="qtd-badge"><?php echo (int)$v['TOTAL_QTD']; ?></span></td>
                            <td style="text-align: right;" class="valor-txt">R$ <?php echo number_format($v['VALOR_ACUMULADO'], 2, ',', '.'); ?></td>
                        </tr>
                        <?php endwhile; else: ?>
                        <tr><td colspan="4" style="text-align:center; padding:20px;">NENHUMA VENDA HOJE.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- TABELA DE SAÍDAS -->
        <div class="report-section">
            <div class="report-title" style="background: #d32f2f;">
                <span>📤 SAÍDAS E RETIRADAS (DESPESAS)</span>
            </div>
            <div class="table-container">
                <table class="table-vendas">
                    <thead>
                        <tr>
                            <th style="width: 30px;"></th>
                            <th>DESCRIÇÃO DA SAÍDA</th>
                            <th style="text-align: center; width: 80px;">QTD</th>
                            <th style="text-align: right; width: 120px;">VALOR TOTAL</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if($res_saidas && $res_saidas->num_rows > 0): 
                                while($s = $res_saidas->fetch_assoc()): ?>
                        <tr>
                            <td><input type="checkbox"></td>
                            <td><?php echo $s['PRODUTO_NOME']; ?></td>
                            <td style="text-align: center;"><span class="qtd-badge" style="background:#666;"><?php echo (int)$s['TOTAL_QTD']; ?></span></td>
                            <td style="text-align: right;" class="valor-txt-saida">R$ <?php echo number_format($s['VALOR_ACUMULADO'], 2, ',', '.'); ?></td>
                        </tr>
                        <?php endwhile; else: ?>
                        <tr><td colspan="4" style="text-align:center; padding:20px;">NENHUMA SAÍDA REGISTRADA HOJE.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>