<?php
include '../config.php'; 
include '../header.php'; 

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['adicionar'])) {
    $responsavel = $_POST['responsavel'];
    $produto = $_POST['produto'];
    $classificacao = $_POST['classificacao'];
    $link = $_POST['link'];
    $preco = $_POST['preco'];
    $destino = $_POST['destino'];

    $stmt = $conn->prepare("INSERT INTO lista_desejos (responsavel, item, classificacao, link, valor, destino, status) VALUES (?, ?, ?, ?, ?, ?, 'Pendente')");
    $stmt->bind_param("ssssds", $responsavel, $produto, $classificacao, $link, $preco, $destino);
    $stmt->execute();
    echo "<script>alert('Desejo de $responsavel gravado!'); window.location.href='pedidos.php';</script>";
}

$pessoas = ['Alexandre', 'Fran', 'Tulio', 'Julia', 'Outros'];

// Busca a contagem de pedidos pendentes para cada pessoa
$contagem = [];
foreach ($pessoas as $p) {
    $res = $conn->query("SELECT COUNT(*) as total FROM lista_desejos WHERE responsavel = '$p' AND status = 'Pendente'");
    $row = $res->fetch_assoc();
    $contagem[$p] = $row['total'];
}
?>

<div class="container mt-2" style="max-width: 500px;">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h6>NOVO DESEJO</h6>
        <a href="pedidos.php" class="btn btn-dark btn-sm">VER TODOS PEDIDOS</a>
    </div>

    <ul class="nav nav-pills nav-justified mb-3 shadow-sm" id="pills-tab" role="tablist" style="font-size: 9px;">
        <?php foreach ($pessoas as $index => $p): ?>
            <li class="nav-item">
                <a class="nav-link <?php echo $index === 0 ? 'active' : ''; ?> p-2" data-toggle="pill" href="#form-<?php echo $p; ?>">
                    <?php echo $p; ?> (<?php echo $contagem[$p]; ?>)
                </a>
            </li>
        <?php endforeach; ?>
    </ul>

    <div class="tab-content">
        <?php foreach ($pessoas as $index => $p): ?>
        <div class="tab-pane fade <?php echo $index === 0 ? 'show active' : ''; ?>" id="form-<?php echo $p; ?>">
            <form method="POST" class="p-3 border rounded bg-white shadow-sm">
                <input type="hidden" name="responsavel" value="<?php echo $p; ?>">
                <label class="small font-weight-bold">PRODUTO PARA <?php echo strtoupper($p); ?></label>
                <input type="text" name="produto" class="form-control mb-2" required>
                
                <label class="small font-weight-bold">CLASSIFICAÇÃO</label>
                <select name="classificacao" class="form-control mb-2" required>
                    <option value="Muito Urgente">MUITO URGENTE (VERMELHO)</option>
                    <option value="Urgente">URGENTE (AMARELO)</option>
                    <option value="Não Urgente">NÃO URGENTE (VERDE)</option>
                </select>

                <div class="row no-gutters mb-2">
                    <div class="col-7 pr-1"><input type="url" name="link" class="form-control" placeholder="LINK"></div>
                    <div class="col-5"><input type="number" step="0.01" name="preco" class="form-control" placeholder="PREÇO"></div>
                </div>

                <label class="small font-weight-bold">DESTINO</label>
                <select name="destino" class="form-control mb-3">
                    <option value="Para Mim">PARA MIM</option>
                    <option value="Presente">PRESENTE</option>
                </select>

                <button type="submit" name="adicionar" class="btn btn-success btn-block">GRAVAR DESEJO</button>
            </form>
        </div>
        <?php endforeach; ?>
    </div>
</div>

<style>
    .nav-pills .nav-link.active { background-color: #ff9800 !important; }
    .nav-pills .nav-link { color: #333; border: 1px solid #ddd; margin: 1px; }
</style>