<?php
    include("../protecao_admin.php");
    include("../../conectar_db.php");

    // Consultas para alimentar a página
    $consulta_bairro_busca = "SELECT * FROM imoveis_bairros ORDER BY bairro ASC";
    $con_bairro_busca = mysqli_query($conn, $consulta_bairro_busca);

    $consulta_cidade_busca = "SELECT * FROM imoveis_cidades ORDER BY cidade ASC";
    $con_cidade_busca = mysqli_query($conn, $consulta_cidade_busca);
?>
<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Configurações - Bairros</title>
    
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" rel="stylesheet">

    <style>
        body { background-color: #f4f7f6; font-family: 'Inter', sans-serif; }
        .card-custom { border: none; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.05); background: #fff; }
        .btn-save { background-color: #0d6efd; color: white; border: none; font-weight: 600; padding: 12px; transition: 0.3s; }
        .btn-save:hover { background-color: #0b5ed7; transform: translateY(-2px); }
        
        .table thead th { background-color: #f8f9fa; color: #6c757d; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.5px; border-top: none; }
        .delete-btn { color: #dc3545; transition: 0.2s; padding: 5px 10px; }
        .delete-btn:hover { color: #a71d2a; transform: scale(1.1); }
        
        .header-title { background: #1a1a1a; color: white; padding: 25px 0; margin-bottom: 30px; }
    </style>
</head>
<body>

<header class="header-title shadow-sm">
    <div class="container d-flex justify-content-between align-items-center">
        <h4 class="m-0"><i class="fas fa-map-signs me-2"></i>Gerenciar Bairros</h4>
        <a href="tela_cadastro_imoveis.php" class="btn btn-outline-light btn-sm"><i class="fas fa-arrow-left me-1"></i> Voltar</a>
    </div>
</header>

<div class="container pb-5">
    <div class="row g-4">
        
        <div class="col-md-4">
            <div class="card card-custom p-4">
                <h5 class="fw-bold mb-4 text-primary">Novo Bairro</h5>
                <form action="cadastro_bairro_imoveis.php" method="post">
                    <div class="mb-3">
                        <label class="form-label small fw-bold text-muted">Nome do Bairro</label>
                        <input name="bairro" type="text" class="form-control" placeholder="Ex: Setor O" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label small fw-bold text-muted">Cidade</label>
                        <select name="cidade" class="form-select" required>
                            <option value="" selected disabled>Selecione a cidade...</option>
                            <?php while($row_c = mysqli_fetch_array($con_cidade_busca)): ?>
                                <option value="<?= $row_c['cidade'] ?>"><?= $row_c['cidade'] ?></option>
                            <?php endwhile; ?>
                        </select>
                    </div>
                    
                    <div class="mb-4">
                        <label class="form-label small fw-bold text-muted">Estado (UF)</label>
                        <select name="estado" class="form-select" required>
                            <option value="" selected disabled>UF...</option>
                            <?php 
                                $ufs = ["AC","AL","AP","AM","BA","CE","DF","ES","GO","MA","MT","MS","MG","PA","PB","PR","PE","PI","RJ","RN","RS","RO","RR","SC","SP","SE","TO"];
                                foreach($ufs as $uf) echo "<option value='$uf'>$uf</option>";
                            ?>
                        </select>
                    </div>

                    <button type="submit" class="btn btn-save w-100 shadow-sm">
                        <i class="fas fa-plus me-2"></i>Cadastrar Bairro
                    </button>
                </form>
            </div>
        </div>

        <div class="col-md-8">
            <div class="card card-custom p-4">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h5 class="fw-bold m-0">Bairros Cadastrados</h5>
                    <span class="badge bg-secondary rounded-pill"><?= mysqli_num_rows($con_bairro_busca) ?></span>
                </div>

                <div class="table-responsive">
                    <table class="table table-hover align-middle">
                        <thead>
                            <tr>
                                <th>Bairro</th>
                                <th>Cidade / UF</th>
                                <th class="text-end">Ações</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php if(mysqli_num_rows($con_bairro_busca) > 0): ?>
                                <?php while($row = mysqli_fetch_array($con_bairro_busca)): ?>
                                    <tr>
                                        <td class="fw-semibold text-dark"><?= $row['bairro'] ?></td>
                                        <td>
                                            <span class="text-muted small"><?= $row['cidade'] ?></span>
                                            <span class="badge bg-light text-dark border ms-1"><?= $row['estado'] ?></span>
                                        </td>
                                        <td class="text-end">
                                            <a href="delete_bairro_imoveis_cad.php?id=<?= $row['id'] ?>" 
                                               class="delete-btn" 
                                               title="Excluir"
                                               onclick="return confirm('Tem certeza que deseja excluir este bairro?')">
                                                <i class="fas fa-trash-alt"></i>
                                            </a>
                                        </td>
                                    </tr>
                                <?php endwhile; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="3" class="text-center py-5 text-muted">
                                        Nenhum bairro encontrado.
                                    </td>
                                </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
<?php mysqli_close($conn); ?>