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

error_reporting(0);
if (isset($_GET['view'])) {
    $page = $_GET['view'];
} else {
    
    if($_SESSION['aviso_seller'] == 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_seller'] = 1;
    } 
}

$seller_name = $_SESSION['seller_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>
  .pagination-gap {
  margin-left: 2px; /* Adjust the desired left margin */
  margin-right: 2px; /* Adjust the desired right margin */
  background-color: green; /* Set the button background color to red */
  color: white; /* Set the button text color to white */
  text-align: center; /* Center the button text */
  padding: 5px 10px;
}

.pagination-red {
  margin-left: 2px; /* Adjust the desired left margin */
  margin-right: 2px; /* Adjust the desired right margin */
  background-color: royalblue; /* Set the button background color to red */
  color: white; /* Set the button text color to white */
  text-align: center; /* Center the button text */
  padding: 5px 10px;
}

</style>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<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</h2>
            </div>
            <div class="modal-body">
                Você realmente deseja excluir este usuário?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                <a class="btn btn-danger btn-ok">Deletar</a>
            </div>
        </div>
    </div>
</div>

<?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" style="color: red;">
                        <h2>'.$titulo.'</h2>
            </div>
            <div class="modal-body" style="color: black;">
                '.$mensagem.'
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" style="display: inline-block;
    font-weight: 400;
    text-align: center;
    white-space: nowrap;
    vertical-align: middle;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    border: 1px solid transparent;
    padding: 0.375rem 0.75rem;
    font-size: 1rem;
    line-height: 1.5;
    border-radius: 0.25rem;
    transition: color .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out; color: #212529;
    background-color: #ffc107;
    border-color: #ffc107;" data-dismiss="modal">Fechar</button>
              
            </div>
        </div>
    </div>
</div>';

}
?>


<main role="main" class="col-15 pt-4 px-5">
    <div class="">
        <div class="chartjs-size-monitor" style="position:absolute ; left: 0px; top: 0px; right: 0px; bottom: 0px; overflow: hidden; pointer-events: none; visibility: hidden; z-index: -1;">
            <div class="chartjs-size-monitor-expand" style="position:absolute;left:0;top:0;right:0;bottom:0;overflow:hidden;pointer-events:none;visibility:hidden;z-index:-1;">
                <div style="position:absolute;width:1000000px;height:1000000px;left:0;top:0"></div>
            </div>
            <div class="chartjs-size-monitor-shrink" style="position:absolute;left:0;top:0;right:0;bottom:0;overflow:hidden;pointer-events:none;visibility:hidden;z-index:-1;">
                <div style="position:absolute;width:200%;height:200%;left:0; top:0"></div>
            </div>
        </div>
        <div id="main">
    <!-- Content Row -->
    <div class="row">
        <!-- First Column -->
        <div class="col-lg-12">
            <!-- Custom codes -->
            <div class="card border-left-primary shadow h-100 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-user-plus"></i> Usuários</h6>
                </div>
                <div class="card-body">
                    <form method="get" class="mb-3">
                        <div class="form-group">
                            <div class="input-group">
                                 <input type="text" name="search" class="form-control" placeholder="Pesquise por MAC, Titulo, Username....">
                                <div class="input-group-append">
                                    <button type="submit" class="btn btn-primary">Pesquisar</button>
                                </div>
                            </div>
                        </div>
                    </form>
                    <div class="form-group">
                        <div class="input-group">
                            <a href="./users_create.php" class="btn btn-primary btn-icon-split">
                                <span class="icon text-white-50"><i class="fas fa-check"></i></span>
                                <span class="text">Criar</span>
                            </a>
                        </div>
                    </div>
                    <br>
                    <div class="table-responsive">
                        <table class="table table-striped table-sm">
                            <thead class="text-primary">
                                <tr>
                                    <th>Título</th>
                                    <th>Mac Address</th>
                                    <th>Username</th>
                                    <th>Vendedor</th>
                                    <th>Validade</th>
                                    <th>DNS</th>
                                    <th>Extra</th>
                                    <th>Editar</th>
                                    <th>Deletar</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><?php echo $user_title; ?></td>
                                        <td><?php echo $user_mac; ?></td>
                                        <td><?php echo $user_username; ?></td>
                                         <td><?php echo $user_seller; ?></td>
                                        <td><?php echo $user_expire_date; ?></td>
                                        <td><?php echo $user_url; ?></td>
                                         <td><?php echo $row1['extra']; ?></td>
                                        <td>
                                            <a class="btn btn-icon" href="./users_update.php?update=<?php echo $users_id; ?>">
                                                <span class="icon text-white-50"><i class="fa fa-pencil-square" style="font-size:24px;color:blue"></i></span>
                                            </a>
                                        </td>
                                        <td>
                                            <a class="btn btn-icon delete-btn" href="#" data-href="./index.php?delete=<?php echo $users_id; ?>" data-toggle="modal" data-target="#confirm-delete">
                                                    <span class="icon text-white-50"><i class="fa fa-ban" style="font-size:24px;color:red"></i></span>
                                                </a>
                                        </td>
                                    </tr>
                                <?php } 
                                
                                ?>
                            </tbody>
                        </table>
                    </div>
                    <?php if ($results_per_page < $totaleview) { ?>
    <div class="pagination">
        <?php if ($page > 1) { ?>
            <a class="pagination pagination-gap" href='index.php?view=<?php echo ($page - 1); ?>'>&lt; Anterior</a>
        <?php } ?>
        <?php for ($i = 1; $i <= $total_pages; $i++) { ?>
            <?php if ($i == $page) { ?>
                <a class="active pagination pagination-red" href='index.php?view=<?php echo $i; ?>'>[<?php echo $i; ?>]</a>
            <?php } else { ?>
                <a class="pagination pagination-gap" href='index.php?view=<?php echo $i; ?>'><?php echo $i; ?></a>
            <?php } ?>
        <?php } ?>
        <?php if ($page < $total_pages) { ?>
            <a class="pagination pagination-gap" href='index.php?view=<?php echo ($page + 1); ?>'>Próximo &gt;</a>
        <?php } 
        
        ?>
    </div>
<?php

   
}


include '../includes/footer2.php';
require '../includes/egz2.php';

 if(isset($titulo)) {
        echo '<script>$("#mensagem_revenda").modal("show");</script>';
}
?>








