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

?>

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

<head>
    <meta charset="UTF-8">
    <?php include "includes/scripts.php"; ?>
    <title>CRM - COPERMIN MEDIC | SIS - COTIZACIONES</title>
    <link rel="icon" href="../img/logo_favicon_vigitronic.ico">
    <script type="text/javascript" src="jquery-1.11.1.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>

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

        <center>
            <h1><i class="far fa-newspaper fa-lg"></i> CRM - COPERMIN MEDIC</h1>
        </center>
        <form action="buscar_cotizacion.php" method="get" class="form_search">
            <input type="text" name="busqueda" id="busqueda" placeholder="(Ej busqueda: 01)">
            <button type="submit" class="btn_search"><i class="fas fa-search"></i></button>
        </form>

        <div>
            <h5>Buscar por Fecha</h5>
            <form action="buscar_cotizacion.php" method="get" class="form_search_date">
                <label>De: </label>
                <input type="date" name="fecha_de" id="fecha_de" required>
                <label>A</label>
                <input type="date" name="fecha_a" id="fecha_a" required>
                <button type="submit" class="btn_view"><i class="fas fa-search"></i></button>
            </form>
        </div>

        <form action="excel_cotizaciones.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_cotizaciones.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>Nro. Cotizacion</th>
                    <th>Fecha - Hora</th>
                    <th>Cliente</th>
                    <th>Vendedor</th>
                    <th class="textright">Total Cotizacion</th>
                    <th>Estado</th>
                    <th class="textright">Acciones</th>
                </tr>
                <?php
                //Paginador
                $sql_registe = mysqli_query($conection, "SELECT COUNT(*) as total_registro FROM factura WHERE estatus != 10 ");
                $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 f.nofactura,f.fecha,f.totalfactura,f.codcliente,f.estatus,
                                                     u.nombre as vendedor,
                                                     cl.nombre as cliente
                                              FROM factura f
                                              INNER JOIN usuario u
                                              ON f.usuario = u.idusuario
                                              INNER JOIN cliente cl
                                              ON f.codcliente = cl.idcliente
                                              WHERE f.estatus != 10
                                              ORDER BY f.fecha ASC LIMIT $desde,$por_pagina");



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

                    while ($data = mysqli_fetch_array($query)) {
                        if ($data["estatus"] == 1) {
                            $estado = '<span class="pagada">Cotizacion enviada</span>';
                        } else {
                            $estado = '<span class="anulada">Se perdio la venta</span>';
                        }
                ?>

                        <tr id="row_<?php echo $data["nofactura"]; ?>">
                            <td>No 2024-0<?php echo $data["nofactura"]; ?></td>
                            <td><?php echo $data["fecha"]; ?></td>
                            <td><?php echo $data["cliente"]; ?></td>
                            <td><?php echo $data["vendedor"]; ?></td>
                            <td class=""><span>S/.</span><?php echo $data["totalfactura"]; ?></td>
                            <td class="estado"><?php echo $estado; ?></td>


                            <!-- <td class="estado"><?php echo $estado; ?></td> -->
                            <!-- <td><?php echo $data["estado"]; ?></td> -->
                            <!-- <td class="textright totalfactura"><span>$/.</span><?php echo round($data["totalfactura"] + $data["totalfactura"] * 0.18, 2); ?></td> -->

                            <td>
                                <div class="div_acciones">
                                    <div>
                                        <button title="Visualizar Cotizacion" class="btn_view view_factura" type="button" cl="<?php echo $data["codcliente"]; ?>" f="<?php echo $data['nofactura']; ?>">
                                            <i class="fas fa-eye"></i></button>
                                    </div>

                                    <?php if ($_SESSION['rol'] == 1 || $_SESSION['rol'] == 2 || $_SESSION['rol'] == 3) {
                                        if ($data["estatus"] == 1) {
                                    ?>
                                            <div class="div_factura">
                                                <button title="Cambiar estado" class="btn_anular anular_factura" fac="<?php echo $data["nofactura"]; ?>"><i class="fas fa-sync-alt"></i></button>
                                            </div>
                                        <?php } else { ?>

                                            <div class="div_factura">
                                                <button type="button" class="btn_anular inactive"><i class="fas fa-ban"></i></button>
                                            </div>
                                    <?php       }
                                    } ?>
                                </div>
                            </td>
                        </tr>
                        </form>

                <?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>