<?php
session_start();
include "../conexion.php";

?>


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <?php include "includes/scripts.php"; ?>
    <title>Lista de productos | SIS - COTIZACIONES</title>
    <link rel="icon" href="../img/logo_favicon_vigitronic.ico">
</head>

<body>
    <?php include "includes/header.php"; ?>
    <section id="container">

        <center>
            <h1><i class="fas fa-cube fa-lg"></i> Lista de productos</h1>
        </center>
        <form action="buscar_productos.php" method="get" class="form_search">
            <input type="text" name="busqueda" id="busqueda" placeholder="Buscar">
            <button type="submit" class="btn_search"><i class="fas fa-search"></i></button>
        </form>

        <form action="excel_productos.php" method="POST" class="form_excel">
            <button title="Excel" type="submit" class="btn_excel"><i class="fas fa-file-excel fa-lg"></i></button>
        </form>

        <form action="pdf_productos.php" method="POST" class="form_pdf" target="_blank">
            <button title="PDF" type="submit" class="btn_pdf"><i class="fas fa-file-pdf fa-lg"></i></button>
        </form>

        <div class="containerTable">
            <table>
                <tr>
                    <th>Codigo</th>
                    <th>Descripcion</th>
                    <th>Precio</th>
                    <th>Cant.</th>
                    <th>

                        <?php

                        $query_proveedor = mysqli_query($conection, "SELECT idproveedor,proveedor FROM  proveedor
                    WHERE estatus = 1 ORDER BY proveedor ASC");

                        $result_proveedor = mysqli_num_rows($query_proveedor);

                        ?>

                        <select name="proveedor" id="search_proveedor">
                            <option value="" selected>PROVEEDORES</option>

                            <?php

                            if ($result_proveedor > 0) {
                                while ($proveedor = mysqli_fetch_array($query_proveedor)) {

                            ?>
                                    <option value="<?php echo $proveedor['idproveedor']; ?>"><?php echo $proveedor['proveedor']; ?> </option>

                            <?php

                                }
                            }

                            ?>

                        </select>

                    </th>
                    <th>Foto</th>
                    <th>Fecha de ingreso</th>
                    <?php if ($_SESSION['rol'] == 1 || $_SESSION['rol'] == 2) { ?>
                        <th>Acciones</th>
                    <?php } ?>

                </tr>
                <?php
                //Paginador
                $sql_registe = mysqli_query($conection, "SELECT COUNT(*) as total_registro FROM producto WHERE estatus = 1 ");
                $result_register = mysqli_fetch_array($sql_registe);
                $total_registro = $result_register['total_registro'];

                $por_pagina = 10;

                if (empty($_GET['pagina'])) {
                    $pagina = 1;
                } else {
                    $pagina = $_GET['pagina'];
                }

                $desde = ($pagina - 1) * $por_pagina;
                $total_paginas = ceil($total_registro / $por_pagina);

                $query = mysqli_query($conection, "SELECT p.idproducto,p.descripcion,p.precio,p.existencia,pr.proveedor,p.foto,
             p.date_add
             FROM producto p 
             INNER JOIN proveedor pr
             ON p.proveedor = pr.idproveedor                    
             WHERE p.estatus = 1 ORDER BY p.idproducto ASC LIMIT $desde,$por_pagina 
				");

                mysqli_close($conection);

                $result = mysqli_num_rows($query);
                if ($result > 0) {

                    while ($data = mysqli_fetch_array($query)) {
                        $formato = 'Y-m-d H:i:s';
                        $fecha = DateTime::createFromFormat($formato, $data["date_add"]);

                        if ($data['foto'] != 'img_producto.png') {
                            $foto = 'img/uploads/' . $data['foto'];
                        } else {
                            $foto = 'img/' . $data['foto'];
                        }

                ?>
                        <tr class="row<?php echo $data["idproducto"]; ?>">
                            <td><?php echo $data["idproducto"]; ?></td>
                            <td><?php echo $data["descripcion"]; ?></td>
                            <td class="celPrecio">S/. <?php echo $data["precio"]; ?></td>
                            <td class="celExistencia"><?php echo $data["existencia"]; ?></td>
                            <td><?php echo $data["proveedor"]; ?></td>
                            <td class="img_producto"><img src="<?php echo $foto; ?>" alt="<?php echo $data["descripcion"]; ?>" class="zoom"></td>
                            <td><?php echo $fecha->format('d-m-Y'); ?></td>

                            <td>
                                <?php if ($_SESSION['rol'] == 1 || $_SESSION['rol'] == 2) { ?>
                                    <a class="link_add add_product" product="<?php echo $data["idproducto"]; ?>" href="">
                                        <i class="fas fa-plus"></i> Agregar</a>
                                    |
                                    <a class="link_edit" href="editar_producto.php?id=<?php echo $data["idproducto"]; ?>">
                                        <i class="fas fa-edit"></i> Editar</a>
                                    |
                                    <a class="link_delete del_product" product="<?php echo $data["idproducto"]; ?>" href="">
                                        <i class="fas fa-trash"></i> Eliminar</a>

                                <?php } ?>
                            </td>

                        </tr>
                <?php
                    }
                }
                ?>

            </table>
        </div>

        <div class="paginador">
            <ul>
                <?php
                if ($pagina != 1) {
                ?>
                    <li><a href="?pagina=<?php echo 1; ?>"><i class="fas fa-fast-backward"></i></a></li>
                    <li><a href="?pagina=<?php echo $pagina - 1; ?>"><i class="fas fa-caret-left fa-lg"></i></a></li>
                <?php
                }
                for ($i = 1; $i <= $total_paginas; $i++) {

                    if ($i == $pagina) {
                        echo '<li class="pageSelected">' . $i . '</li>';
                    } else {
                        echo '<li><a href="?pagina=' . $i . '">' . $i . '</a></li>';
                    }
                }

                if ($pagina != $total_paginas) {
                ?>
                    <li><a href="?pagina=<?php echo $pagina + 1; ?>"><i class="fas fa-caret-right fa-lg"></i></a></li>
                    <li><a href="?pagina=<?php echo $total_paginas; ?> "><i class="fas fa-fast-forward"></i></a></li>
                <?php } ?>
            </ul>
        </div>

    </section>
    <?php include "includes/footer.php"; ?>
</body>

</html>