<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$results_per_page = 5;
include '../includes/header3.php';
error_reporting(0);

if (isset($_GET['view'])) {
    $page = $_GET['view'];
} else {
     if($_SESSION['aviso_reseller'] == 0) {    
        $page = 1;
        $file_path = '../a/revenda_mensagem.json';
        $file_content = file_get_contents($file_path);
        $data_array = json_decode($file_content, true);
        $titulo = $data_array['title'];
        $mensagem = $data_array['content'];
        $_SESSION['aviso_reseller'] = 1;
    } 
}

$seller_name = $_SESSION['reseller_name'];
$start_from = ($page - 1) * $results_per_page;
$db1 = new SQLite3('../a/.eggziedb.db');

// Search functionality
$searchTerm = isset($_GET['search']) ? $_GET['search'] : '';
$searchQuery = "WHERE seller = '$seller_name'";
if (!empty($searchTerm)) {
    $searchQuery = "WHERE (mac_address LIKE '%$searchTerm%' OR title LIKE '%$searchTerm%' OR username LIKE '%$searchTerm%') AND seller = '$seller_name'";
}

$result = $db1->query("SELECT COUNT(id) AS total FROM ibo $searchQuery");
$rownew = $result->fetchArray();
$totaleview = $rownew['total'];
$total_pages = ceil($rownew['total'] / $results_per_page);
$res1 = $db1->query("SELECT * FROM ibo $searchQuery ORDER BY id ASC LIMIT $start_from, $results_per_page");

if (isset($_GET['delete'])) {
    $db1->exec('DELETE FROM ibo WHERE id=' . $_GET['delete']);
    header('Location: index.php');
}
?>

<style>
:root {
    --primary: #FF6B9D;
    --primary-dark: #D94A7E;
    --secondary: #00D4FF;
    --accent: #FFD166;
    --surface: #0A0F1C;
    --surface-light: #1A1F2C;
    --surface-lighter: #2A2F3C;
    --text-primary: #FFFFFF;
    --text-secondary: #B0B7C3;
    --text-muted: #6B7280;
    --border: rgba(255,255,255,0.1);
    --glow: 0 0 20px rgba(255, 107, 157, 0.3);
    --glow-secondary: 0 0 20px rgba(0, 212, 255, 0.3);
}


/* === DESIGN FUTURISTA PARA A PÁGINA === */
body {
    background: var(--surface) !important;
    color: var(--text-primary);
    font-family: 'Inter', sans-serif;
}

.container-fluid {
    padding: 20px;
}

.card {
    background: var(--surface-light);
    border-radius: 16px;
    border: 1px solid var(--border);
    margin-bottom: 20px;
    padding: 0;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
    transition: all 0.3s ease;
}

.card:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
    border-color: var(--primary);
}

.card-header {
    background: var(--surface-lighter);
    padding: 20px;
    border-bottom: 1px solid var(--border);
    border-radius: 16px 16px 0 0;
    position: relative;
}

.card-header::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 1px;
    background: linear-gradient(90deg, transparent, var(--primary), transparent);
}

.card-body {
    padding: 30px;
}

/* === BOTÕES FUTURISTAS === */
.btn-primary {
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    border: none;
    border-radius: 10px;
    color: white;
    padding: 12px 24px;
    font-weight: 600;
    transition: all 0.3s ease;
    box-shadow: var(--glow);
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 0 30px rgba(0, 212, 255, 0.5);
    color: white;
}

.btn-success {
    background: linear-gradient(135deg, #10B981, #059669);
    border: none;
    border-radius: 10px;
    color: white;
    padding: 12px 24px;
    font-weight: 600;
    transition: all 0.3s ease;
    box-shadow: 0 0 20px rgba(16, 185, 129, 0.3);
}

.btn-success:hover {
    transform: translateY(-2px);
    box-shadow: 0 0 30px rgba(16, 185, 129, 0.5);
    color: white;
}

.btn-danger {
    background: linear-gradient(135deg, #EF4444, #DC2626);
    border: none;
    border-radius: 10px;
    color: white;
    padding: 8px 16px;
    font-weight: 600;
    transition: all 0.3s ease;
}

.btn-danger:hover {
    transform: translateY(-2px);
    box-shadow: 0 0 20px rgba(239, 68, 68, 0.4);
    color: white;
}

/* === FORMULÁRIOS FUTURISTAS === */
.form-control {
    background: var(--surface-lighter);
    border: 2px solid var(--border);
    border-radius: 10px;
    color: var(--text-primary);
    padding: 12px 16px;
    width: 100%;
    transition: all 0.3s ease;
}

.form-control:focus {
    border-color: var(--primary);
    outline: none;
    box-shadow: 0 0 0 3px rgba(0, 212, 255, 0.1);
}

.form-group label {
    color: var(--text-primary);
    font-weight: 600;
    margin-bottom: 8px;
}

/* === TABELA FUTURISTA === */
.table-responsive {
    border-radius: 12px;
    overflow: hidden;
}

.table {
    background: var(--surface-lighter);
    border-radius: 12px;
    color: var(--text-primary);
    margin-bottom: 0;
}

.table thead {
    background: linear-gradient(135deg, var(--surface-light), var(--surface-lighter));
}

.table thead th {
    border-bottom: 2px solid var(--primary);
    color: var(--text-primary);
    font-weight: 600;
    padding: 16px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-size: 0.85rem;
}

.table tbody tr {
    transition: all 0.3s ease;
    border-bottom: 1px solid var(--border);
}

.table tbody tr:hover {
    background: rgba(0, 212, 255, 0.05);
    transform: translateX(4px);
}

.table tbody td {
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
    color: var(--text-secondary);
    vertical-align: middle;
}

/* === PAGINAÇÃO FUTURISTA === */
.pagination {
    display: flex;
    gap: 8px;
    justify-content: center;
    margin-top: 20px;
    flex-wrap: wrap;
}

.pagination-gap {
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: white;
    padding: 10px 16px;
    border-radius: 10px;
    text-decoration: none;
    font-weight: 600;
    transition: all 0.3s ease;
    border: none;
    box-shadow: var(--glow);
}

.pagination-gap:hover {
    transform: translateY(-2px);
    box-shadow: 0 0 20px rgba(0, 212, 255, 0.5);
    color: white;
    text-decoration: none;
}

.pagination-red {
    background: linear-gradient(135deg, #EF4444, #DC2626);
    color: white;
    padding: 10px 16px;
    border-radius: 10px;
    text-decoration: none;
    font-weight: 600;
    transition: all 0.3s ease;
    border: none;
    box-shadow: 0 0 20px rgba(239, 68, 68, 0.3);
}

.pagination-red:hover {
    transform: translateY(-2px);
    box-shadow: 0 0 25px rgba(239, 68, 68, 0.5);
    color: white;
    text-decoration: none;
}

/* === MODAL FUTURISTA === */
.modal-content {
    background: var(--surface-light);
    border-radius: 16px;
    border: 1px solid var(--border);
    color: var(--text-primary);
}

.modal-header {
    background: var(--surface-lighter);
    border-bottom: 1px solid var(--border);
    border-radius: 16px 16px 0 0;
    padding: 20px;
}

.modal-header h2 {
    color: var(--primary);
    margin: 0;
    font-weight: 700;
}

.modal-body {
    padding: 20px;
    color: var(--text-secondary);
}

.modal-footer {
    border-top: 1px solid var(--border);
    padding: 20px;
}

/* === BADGES E ÍCONES === */
.btn-icon {
    background: transparent;
    border: none;
    padding: 8px;
    border-radius: 8px;
    transition: all 0.3s ease;
}

.btn-icon:hover {
    background: rgba(0, 212, 255, 0.1);
    transform: scale(1.1);
}

.text-primary2 {
    color: var(--primary) !important;
    font-weight: 700;
}

/* === RESPONSIVIDADE === */
@media (max-width: 768px) {
    .container-fluid {
        padding: 10px;
    }
    
    .card-body {
        padding: 20px;
    }
    
    .table thead th,
    .table tbody td {
        padding: 10px 8px;
        font-size: 0.8rem;
    }
    
    .pagination {
        gap: 4px;
    }
    
    .pagination-gap,
    .pagination-red {
        padding: 8px 12px;
        font-size: 0.8rem;
    }
}
</style>

<!-- Modal de Confirmação de Exclusão -->
<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h2>Confirmar Exclusão</h2>
            </div>
            <div class="modal-body">
                <i class="fas fa-exclamation-triangle text-warning mr-2"></i>
                Você realmente deseja excluir este usuário?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancelar</button>
                <a class="btn btn-danger btn-ok">Deletar</a>
            </div>
        </div>
    </div>
</div>

<!-- Modal de Mensagem da Revenda -->
<?php if(isset($titulo)) {
    echo '
    <div class="modal fade vertical" id="mensagem_revenda" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h2><i class="fas fa-info-circle text-primary mr-2"></i>'.$titulo.'</h2>
                </div>
                <div class="modal-body">
                    '.$mensagem.'
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" data-dismiss="modal">Fechar</button>
                </div>
            </div>
        </div>
    </div>';
}
?>

<main role="main" class="col-15 pt-4 px-5">
    <div class="container-fluid">
        <!-- Content Row -->
        <div class="row">
            <!-- First Column -->
            <div class="col-lg-12">
                <!-- Card Principal -->
                <div class="card shadow mb-4">
                    <div class="card-header py-3">
                        <h6 class="m-0 font-weight-bold text-primary2">
                            <i class="fas fa-fw fa-users"></i> Gerenciar Usuários
                        </h6>
                    </div>
                    <div class="card-body">
                        <!-- Formulário de Pesquisa -->
                        <form method="get" class="mb-4">
                            <div class="form-group">
                                <div class="input-group">
                                    <input type="text" name="search" class="form-control" 
                                           placeholder="🔍 Pesquise por MAC, Título, Username..." 
                                           value="<?php echo htmlspecialchars($searchTerm); ?>">
                                    <div class="input-group-append">
                                        <button type="submit" class="btn btn-primary">
                                            <i class="fas fa-search mr-2"></i>Pesquisar
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </form>

                        <!-- Botão Criar Usuário -->
                        <div class="form-group mb-4">
                            <a href="./users_create.php" class="btn btn-success">
                                <i class="fas fa-plus-circle mr-2"></i>
                                <span class="text">Criar Novo Usuário</span>
                            </a>
                        </div>

                        <!-- Tabela de Usuários -->
                        <div class="table-responsive">
                            <table class="table table-striped table-sm">
                                <thead>
                                    <tr>
                                        <th>Título</th>
                                        <th>Mac Address</th>
                                        <th>Username</th>
                                        <th>Seller</th>
                                        <th>Validade</th>
                                        <th>DNS</th>
                                        <th>Extra</th>
                                        <th>Ações</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php while ($row1 = $res1->fetchArray()) { ?>
                                        <tr>
                                            <?php
                                            $users_id = $row1['id'];
                                            $user_mac = $row1['mac_address'];
                                            $user_username = $row1['username'];
                                            $user_seller = $row1['seller'];
                                            $user_expire_date = $row1['expire_date'];
                                            $user_url = $row1['url'];
                                            $user_title = $row1['title'];
                                            ?>
                                            <td><strong><?php echo htmlspecialchars($user_title); ?></strong></td>
                                            <td><code><?php echo htmlspecialchars($user_mac); ?></code></td>
                                            <td><?php echo htmlspecialchars($user_username); ?></td>
                                            <td><?php echo htmlspecialchars($user_seller); ?></td>
                                            <td><?php echo htmlspecialchars($user_expire_date); ?></td>
                                            <td><?php echo htmlspecialchars($user_url); ?></td>
                                            <td><?php echo htmlspecialchars($row1['extra']); ?></td>
                                            <td>
                                                <div class="btn-group" role="group">
                                                    <a class="btn btn-icon" href="./users_update.php?update=<?php echo $users_id; ?>" 
                                                       title="Editar Usuário">
                                                        <i class="fas fa-edit" style="color: var(--primary); font-size: 16px;"></i>
                                                    </a>
                                                    <a class="btn btn-icon delete-btn" href="#" 
                                                       data-href="./index.php?delete=<?php echo $users_id; ?>" 
                                                       data-toggle="modal" data-target="#confirm-delete"
                                                       title="Excluir Usuário">
                                                        <i class="fas fa-trash" style="color: #EF4444; font-size: 16px;"></i>
                                                    </a>
                                                </div>
                                            </td>
                                        </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        </div>

                        <!-- Paginação -->
                        <?php if ($results_per_page < $totaleview) { ?>
                            <div class="pagination">
                                <?php if ($page > 1) { ?>
                                    <a class="pagination-gap" href='index.php?view=<?php echo ($page - 1); ?><?php echo !empty($searchTerm) ? "&search=" . urlencode($searchTerm) : ""; ?>'>
                                        <i class="fas fa-chevron-left mr-1"></i>Anterior
                                    </a>
                                <?php } ?>
                                
                                <?php for ($i = 1; $i <= $total_pages; $i++) { ?>
                                    <?php if ($i == $page) { ?>
                                        <a class="pagination-red" href='index.php?view=<?php echo $i; ?><?php echo !empty($searchTerm) ? "&search=" . urlencode($searchTerm) : ""; ?>'>
                                            <?php echo $i; ?>
                                        </a>
                                    <?php } else { ?>
                                        <a class="pagination-gap" href='index.php?view=<?php echo $i; ?><?php echo !empty($searchTerm) ? "&search=" . urlencode($searchTerm) : ""; ?>'>
                                            <?php echo $i; ?>
                                        </a>
                                    <?php } ?>
                                <?php } ?>
                                
                                <?php if ($page < $total_pages) { ?>
                                    <a class="pagination-gap" href='index.php?view=<?php echo ($page + 1); ?><?php echo !empty($searchTerm) ? "&search=" . urlencode($searchTerm) : ""; ?>'>
                                        Próximo<i class="fas fa-chevron-right ml-1"></i>
                                    </a>
                                <?php } ?>
                            </div>
                        <?php } ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</main>

<script>
// Script para o modal de confirmação de exclusão
$(document).ready(function() {
    $('#confirm-delete').on('show.bs.modal', function(e) {
        $(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
    });

    // Mostrar modal de mensagem se existir
    <?php if(isset($titulo)) { ?>
        $('#mensagem_revenda').modal('show');
    <?php } ?>
});
</script>

<?php 
include '../includes/footer3.php';
require '../includes/egz3.php';
?>