<?php
include '../url_amigable.php';
// include 'sesion.php';
include 'concha.php';

function contarPendientes($conn, $tabla)
{
    $total = 0;
    $sql = "SELECT COUNT(*) AS total FROM {$tabla} WHERE ver = 'no'";
    if ($consulta = $conn->query($sql)) {
        $row = $consulta->fetch_assoc();
        $total = isset($row['total']) ? (int)$row['total'] : 0;
        $consulta->free_result();
    }
    return $total;
}

function armarUrlPaginacion($pagina, $page, $porPagina, $tour = 0)
{
    $page = (int)$page;
    if ($page < 1) {
        $page = 1;
    }

    $offset = ($page - 1) * $porPagina;
    $params = array(
        'i' => $offset,
        'p' => $page
    );

    if ((int)$tour === 1) {
        $params['tour'] = 1;
    }

    return $pagina . '?' . http_build_query($params);
}

$m_mensajes    = contarPendientes($conn, 'chat');
$m_comentarios = contarPendientes($conn, 'comentarios');
$m_fotos       = contarPendientes($conn, 'fotosoyentes');
$m_avisos      = contarPendientes($conn, 'avisos');
$m_tot         = $m_mensajes + $m_comentarios + $m_fotos + $m_avisos;

$tour = isset($_GET['tour']) ? (int)$_GET['tour'] : 0;

// PAGINACION
$pagina = "notas.php";
$tabla  = "notas";
$c      = 15;

$p = isset($_GET['p']) ? (int)$_GET['p'] : 1;
$i = isset($_GET['i']) ? (int)$_GET['i'] : 0;

if ($p < 1) {
    $p = 1;
}
if ($i < 0) {
    $i = 0;
}

// Si vino solo offset, recalculo página
if (isset($_GET['i']) && !isset($_GET['p'])) {
    $p = floor($i / $c) + 1;
}

// Total de registros
$total_registros = 0;
$sqlTotal = "SELECT COUNT(*) AS total FROM {$tabla}";
if ($consultaTotal = $conn->query($sqlTotal)) {
    $rowTotal = $consultaTotal->fetch_assoc();
    $total_registros = isset($rowTotal['total']) ? (int)$rowTotal['total'] : 0;
    $consultaTotal->free_result();
}

$cant = ($total_registros > 0) ? (int)ceil($total_registros / $c) : 1;

if ($p > $cant) {
    $p = $cant;
}

$i = ($p - 1) * $c;

$desde = ($total_registros > 0) ? ($i + 1) : 0;
$hasta = ($total_registros > 0) ? min($i + $c, $total_registros) : 0;

$pagina_inicio = max(1, $p - 2);
$pagina_fin    = min($cant, $p + 2);

if (($pagina_fin - $pagina_inicio) < 4) {
    if ($pagina_inicio == 1) {
        $pagina_fin = min($cant, 5);
    } elseif ($pagina_fin == $cant) {
        $pagina_inicio = max(1, $cant - 4);
    }
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="description" content="Panel de Administracion">
<meta name="author" content="<?php echo $nombresitio; ?>">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title><?php echo $nombresitio; ?> - Panel de Administracion</title>

<link href="https://fonts.googleapis.com/css?family=Montserrat:100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900&display=swap" rel="stylesheet">
<link rel="shortcut icon" href="" type="image/x-icon">
<link rel="apple-touch-icon" href="">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" crossorigin="anonymous">
<link rel="stylesheet" href="">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" crossorigin="anonymous"></script>

<style>
    .carousel-cell {
        width: 66%;
        margin-right: 5px;
    }

    .display-flex {
        display: flex;
        justify-content: center;
        align-items: center;
    }

    * {
        border-radius: 0 !important;
    }

    body {
        background-image: url(../fondeux.jpg);
        background-position: top center;
        background-repeat: repeat-x;
        background-color: #EBEBEB;
        height: 100%;
    }

    .btn, a, h1, h2, h3, h4, h5, h6, b, span, p, label, select, input {
        font-family: 'Montserrat', sans-serif;
    }

    .btn, h1, h2, h3, h4, h5, h6 {
        font-weight: 600;
    }

    .navbar {
        position: relative;
        min-height: 50px;
        padding-bottom: 20px;
    }

    .btn-primary {
        color: #fff !important;
        background-color: #0066FF;
        border-color: #0066FF;
    }

    .btn-primary:hover {
        background-color: #0052cc;
        border-color: #0052cc;
    }

    .page-header {
        margin: 0;
        padding: 1em;
        background-color: #0066FF;
        border: 0;
        text-align: center;
    }

    .page-header h2 {
        color: #ffffff;
        font-weight: bold;
        margin: 0;
    }

    .bloqueAzul {
        text-align: center;
    }

    .bloqueAzul .div {
        border: 1px solid #e4e4e4;
        background-color: #fff;
    }

    ul {
        list-style: none;
        padding: 0;
    }

    .far {
        color: #0066FF;
    }

    .faso {
        color: #fff;
    }

    .colorAzul h3 {
        color: #0066FF;
    }

    .bloque-paginacion {
        margin-top: 25px;
        padding: 20px;
        background: linear-gradient(180deg, #f8faff 0%, #eef4ff 100%);
        border: 1px solid #dbe7ff;
    }

    .bloque-paginacion-top {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 15px;
        flex-wrap: wrap;
        margin-bottom: 15px;
    }

    .bloque-paginacion-top h6 {
        margin: 0;
        color: #1e2a3b;
        font-size: 14px;
        font-weight: 700;
    }

    .bloque-paginacion-top .resumen {
        color: #5b6573;
        font-size: 13px;
        margin: 0;
    }

    .pagination-modern {
        display: flex;
        align-items: center;
        flex-wrap: wrap;
        gap: 8px;
        margin: 0;
        padding: 0;
    }

    .pagination-modern a,
    .pagination-modern span {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        min-width: 42px;
        height: 42px;
        padding: 0 14px;
        text-decoration: none;
        background: #ffffff;
        border: 1px solid #d8e3fb;
        color: #24457a;
        font-size: 14px;
        font-weight: 700;
        transition: all .2s ease;
    }

    .pagination-modern a:hover {
        background: #0066FF;
        color: #fff;
        border-color: #0066FF;
    }

    .pagination-modern .active {
        background: #0066FF;
        color: #fff;
        border-color: #0066FF;
        box-shadow: 0 6px 18px rgba(0, 102, 255, 0.20);
    }

    .pagination-modern .disabled {
        background: #f0f3f8;
        color: #a0a8b5;
        border-color: #e2e7ef;
        cursor: default;
    }

    .bloque-ir-pagina {
        margin-top: 18px;
        display: flex;
        gap: 10px;
        align-items: center;
        flex-wrap: wrap;
    }

    .bloque-ir-pagina label {
        margin: 0;
        font-size: 13px;
        font-weight: 700;
        color: #1e2a3b;
    }

    .bloque-ir-pagina .form-control,
    .bloque-ir-pagina .btn {
        height: 42px;
    }

    .bloque-ir-pagina .form-control {
        max-width: 220px;
        border: 1px solid #d8e3fb;
    }

    .bloque-ir-pagina .btn {
        min-width: 150px;
    }

    .linea-separadora {
        border-top: 1px solid #e7edf7;
        margin: 18px 0;
    }

    @media (max-width: 767px) {
        .bloque-paginacion-top {
            flex-direction: column;
            align-items: flex-start;
        }

        .pagination-modern {
            gap: 6px;
        }

        .pagination-modern a,
        .pagination-modern span {
            min-width: 38px;
            height: 38px;
            padding: 0 10px;
            font-size: 13px;
        }

        .bloque-ir-pagina {
            flex-direction: column;
            align-items: stretch;
        }

        .bloque-ir-pagina .form-control,
        .bloque-ir-pagina .btn {
            max-width: 100%;
            width: 100%;
        }
    }
</style>

<script language="javascript">
function ventanaSecundaria(URL){
    window.open(URL,"ventana1","width=550,height=450,scrollbars=NO");
}
</script>
</head>

<body>

<?php if ($tour != 1) { ?>
<div class="container" style="background-color:#F1F2ED;">
    <?php include 'menu.php'; ?>
</div>
<?php } ?>

<div role="main" style="background-color:#fff;" class="container pt-3">

    <div class="page-header" style="text-align:center;">
        <h2><?php echo $idioma_100265; ?></h2>
    </div>

    <div class="row mt-3">
        <div class="col-12 col-md-6">
            <h3><?php echo $idioma_100266; ?></h3>
            <form method="GET" action="buscar_noticias.php">
                <input name="busqueda" type="text" class="form-control mb-2" id="busqueda">
                <input name="button" type="submit" class="btn btn-primary" id="button" value="<?php echo $idioma_100267; ?>">
            </form>
        </div>

        <div class="col-12 col-md-6">
            <h3><?php echo $idioma_100268; ?></h3>
            <form method="GET" action="filtrar_noticias.php">
                <select name="ID_cat" class="form-control mb-2" id="cat5">
                    <option value="0" selected="selected"><?php echo $idioma_100269; ?></option>
                    <?php
                    $sql = "SELECT * FROM categorias ORDER BY nombre";
                    $consulta = $conn->query($sql);
                    while ($row = $consulta->fetch_assoc()) {
                        $ID = $row["ID"];
                        $nombre = $row["nombre"];
                    ?>
                        <option value="<?php echo $ID; ?>"><?php echo $nombre; ?></option>
                    <?php
                    }
                    $consulta->free_result();
                    ?>
                </select>

                <input name="button3" type="submit" class="btn btn-primary" id="button3" value="<?php echo $idioma_100270; ?>">
            </form>
        </div>
    </div>

    <hr>

    <div class="mt-3">
        <div class="bloque-paginacion">
            <div class="bloque-paginacion-top">
                <div>
                    <h6>Listado de noticias</h6>
                    <p class="resumen">Mostrando <strong><?php echo $desde; ?></strong> a <strong><?php echo $hasta; ?></strong> de <strong><?php echo $total_registros; ?></strong> registros</p>
                </div>
                <div>
                    <p class="resumen">Página <strong><?php echo $p; ?></strong> de <strong><?php echo $cant; ?></strong></p>
                </div>
            </div>

            <div class="pagination-modern">
                <?php if ($p > 1) { ?>
                    <a href="<?php echo armarUrlPaginacion($pagina, 1, $c, $tour); ?>">Inicio</a>
                    <a href="<?php echo armarUrlPaginacion($pagina, $p - 1, $c, $tour); ?>"><?php echo isset($idioma_100271) ? $idioma_100271 : 'Anterior'; ?></a>
                <?php } else { ?>
                    <span class="disabled">Inicio</span>
                    <span class="disabled"><?php echo isset($idioma_100271) ? $idioma_100271 : 'Anterior'; ?></span>
                <?php } ?>

                <?php for ($x = $pagina_inicio; $x <= $pagina_fin; $x++) { ?>
                    <?php if ($x == $p) { ?>
                        <span class="active"><?php echo $x; ?></span>
                    <?php } else { ?>
                        <a href="<?php echo armarUrlPaginacion($pagina, $x, $c, $tour); ?>"><?php echo $x; ?></a>
                    <?php } ?>
                <?php } ?>

                <?php if ($p < $cant) { ?>
                    <a href="<?php echo armarUrlPaginacion($pagina, $p + 1, $c, $tour); ?>"><?php echo isset($idioma_siguiente) ? $idioma_siguiente : 'Siguiente'; ?></a>
                    <a href="<?php echo armarUrlPaginacion($pagina, $cant, $c, $tour); ?>">Última</a>
                <?php } else { ?>
                    <span class="disabled"><?php echo isset($idioma_siguiente) ? $idioma_siguiente : 'Siguiente'; ?></span>
                    <span class="disabled">Última</span>
                <?php } ?>
            </div>

            <div class="linea-separadora"></div>

            <form action="notas.php" method="get" class="bloque-ir-pagina">
                <label for="pagina_select">Ir a página</label>
                <select name="p" id="pagina_select" class="form-control">
                    <?php for ($r = 1; $r <= $cant; $r++) { ?>
                        <option value="<?php echo $r; ?>" <?php echo ($r == $p) ? 'selected="selected"' : ''; ?>>
                            Página <?php echo $r; ?>
                        </option>
                    <?php } ?>
                </select>

                <?php if ($tour == 1) { ?>
                    <input type="hidden" name="tour" value="1">
                <?php } ?>

                <button type="submit" class="btn btn-primary">Cambiar página</button>
            </form>
        </div>

        <div class="mt-4">
            <?php
            $sql = "SELECT * FROM {$tabla} ORDER BY ID DESC LIMIT {$i},{$c}";
            $consulta = $conn->query($sql);

            if ($consulta && $consulta->num_rows > 0) {
                while ($row = $consulta->fetch_assoc()) {
                    $i_d              = 0;
                    $ID               = $row["ID"];
                    $visitas          = $row["visitas"];
                    $impresiones      = $row["impresiones"];
                    $titulo           = $row["titulo"];
                    $color            = $row["color"];
                    $carpetafoto      = $row["carpetafoto"];
                    $fechapub         = $row["fechapub"];
                    $foto             = $row["foto"];
                    $destacado        = $row["destacado"];
                    $audio            = $row["audio"];
                    $youtube          = $row["youtube"];
                    $d                = $row["d"];
                    $m                = $row["m"];
                    $ver              = $row["ver"];
                    $a                = $row["a"];
                    $fecha            = $d . " - " . $m . " - " . $a;
                    $ext_img          = $row["ext_img"];
                    $editor           = $row["editor"];
                    $des_cat          = $row["des_cat"];
                    $cat              = $row["cat"];
                    $user_carga_nota  = $row["user"];
                    $pie_foto         = $row["pie_foto"];
                    $altfoto          = $row["altfoto"];
                    $titulofoto       = $row["titulofoto"];

                    include 'mostrar_notisnew.php';
                }

                $consulta->free_result();
            } else {
            ?>
                <div class="alert alert-light border">
                    No se encontraron noticias para mostrar.
                </div>
            <?php } ?>
        </div>

        <div class="bloque-paginacion mt-4 mb-4">
            <div class="bloque-paginacion-top">
                <div>
                    <h6>Navegación rápida</h6>
                    <p class="resumen">Podés moverte entre páginas sin usar el selector inferior del navegador.</p>
                </div>
                <div>
                    <p class="resumen">Total de páginas: <strong><?php echo $cant; ?></strong></p>
                </div>
            </div>

            <div class="pagination-modern">
                <?php if ($p > 1) { ?>
                    <a href="<?php echo armarUrlPaginacion($pagina, 1, $c, $tour); ?>">Inicio</a>
                    <a href="<?php echo armarUrlPaginacion($pagina, $p - 1, $c, $tour); ?>"><?php echo isset($idioma_100271) ? $idioma_100271 : 'Anterior'; ?></a>
                <?php } else { ?>
                    <span class="disabled">Inicio</span>
                    <span class="disabled"><?php echo isset($idioma_100271) ? $idioma_100271 : 'Anterior'; ?></span>
                <?php } ?>

                <?php for ($x = $pagina_inicio; $x <= $pagina_fin; $x++) { ?>
                    <?php if ($x == $p) { ?>
                        <span class="active"><?php echo $x; ?></span>
                    <?php } else { ?>
                        <a href="<?php echo armarUrlPaginacion($pagina, $x, $c, $tour); ?>"><?php echo $x; ?></a>
                    <?php } ?>
                <?php } ?>

                <?php if ($p < $cant) { ?>
                    <a href="<?php echo armarUrlPaginacion($pagina, $p + 1, $c, $tour); ?>"><?php echo isset($idioma_siguiente) ? $idioma_siguiente : 'Siguiente'; ?></a>
                    <a href="<?php echo armarUrlPaginacion($pagina, $cant, $c, $tour); ?>">Última</a>
                <?php } else { ?>
                    <span class="disabled"><?php echo isset($idioma_siguiente) ? $idioma_siguiente : 'Siguiente'; ?></span>
                    <span class="disabled">Última</span>
                <?php } ?>
            </div>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" crossorigin="anonymous"></script>
<script src="https://unpkg.com/isotope-layout@3/dist/isotope.pkgd.js"></script>
<script type="text/javascript">
window.setInterval(function(){
    $('.grid').isotope({
        itemSelector: '.grid-item'
    });
}, 500);
</script>
<script defer src="https://use.fontawesome.com/releases/v5.8.1/js/all.js" crossorigin="anonymous"></script>
</body>
</html>