<?php
//error_reporting(0);

//Incluir Archivos Necesarios
require_once '../Clases/db.php';
require_once '../Clases/Venta.php';
require_once '../Clases/Cliente.php';

//Verificar si el usuario ha iniciado sesion 
session_start();
if (!isset($_SESSION['Username'])) { 
  header("Location: ../index.php");
  exit;
}

//############# TOASTS ############//
include "../ArchivosCSS/StyleToasts.php";
?>
<!DOCTYPE html>
<html lang="es">
<header>
    <title>Venta</title>
  
</header>
<body onload="onLoad()">
    <br><br>

    <!--======== Incluir Menu Lateral y Nav ========-->
    <?php  include 'menu_lateral.php'; ?>

    <!--======== Main ========-->
    <div class="main">
        <div class="interno" >
            <?php  error_reporting(0);
            require_once '../Clases/Tasa.php';
            $mostrar = new tasas ();
            $result = $mostrar->multiplicartasa(); 
            ?>
            <?php
            if (!isset($result)) {
                ?>
                <!--======== Area Toast ========-->
                <div class="contenedor-toasts-internos" id="contenedor-botones">
                    <?php
        //toast de error "Por favor, introduzca la tasa del día antes de realizar una venta.!!"
                    echo $errorBTNGVE1;
                    echo $errorToastGVE1;

                     //boton para registrar tasa
                    echo "<a href='tasadolar.php'>";
                    echo "<button type='button' class='btn btn-primary' data-bs-toggle='modal'>";
                    echo "<i class='bi bi-pencil-square'></i> Ir a Registrar Tasa!";
                    echo "</button>";
                    echo "</a>";
                    ?>
                </div>
                <!--======== Fin Area Toast ========-->
                <?php
            }else{ 
                ?> 
                <div class="breadcrumb-holder">                                             
                    <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#register1">
                        <i class="bi bi-person-plus"></i> Agregar Cliente
                        </button> <?php  include('./Modales/Ventas/agregarcliente.php'); ?>
                    </div><br><br>

                    <div class="breadcrumb-holder">                                             
                        <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#register">
                            <i class="bi bi-cart-plus"></i> Agregar Producto
                            </button> <?php  include('./Modales/Ventas/agregarproducto.php'); ?>
                        </div><br>
                    <?php }//Fin del ELSE ?> 

                    <!--======== Area Toast ========-->
                    <div class="contenedor-toasts-internos" id="contenedor-botones">
                        <?php
                        if(isset($_GET["m"])){
                            $m = $_GET["m"];

                            switch($m){
                                case "1":
                //toast de error "No hay la disponibilidad requerida del producto!! Consulte el stock del producto."
                                echo $errorBTNGVE2;
                                echo $errorToastGVE2;
                                break;
                                case "2":
                //toast de error "La cantidad debe ser un número positivo!"
                                echo $errorBTNGVE3;
                                echo $errorToastGVE3;
                                break;

                                case "3":
                //toast de venta exitosamente
                                echo $exitoVentaBTN;
                                echo $exitoVentaToast;
                                break;
                            }
                        } 
                        ?>
                    </div>
                    <!--======== Fin Area Toast ========-->

                    <table id="myTable1" class="table table-light table-striped">
                        <h4>Cliente</h4>
                        <tr>
                            <th>Nombre</th>
                            <th>Cedula</th>
                            <th>Eliminar</th>
                            <tr>
                                <?php 
                                require_once '../Clases/db.php';
                                $mostrar = new conn ();
                                $conn = $mostrar->crearconn(); 

                                $username = $_SESSION['Username'];
                                $sql = "SELECT ID FROM usuario WHERE Username = '{$username}'";
                                $r = $conn->query($sql);
                                $row = mysqli_fetch_assoc($r);
                                $user_id = $row['ID'];
                                $_SESSION["iduser"] = $user_id;
                                ?>
                                <?php 


                                if(isset($_SESSION["cliente"])){
                                   $_SESSION["iduser"];
                                   $cliente = $_SESSION["cliente"];
                                   foreach ($cliente as $c) {
                                     $e = 0;

                                     echo "<tr>";  
                                     echo "<td>".$c->nombre."</td>";
                                     echo "<td>".$c->cedula."</td>";
                                     echo "<td>";
                                     echo "<a class='btn btn-danger' href='./controladores/ventas/eliminarcli.php?inc=$e'>Eliminar</a>";
                                     echo "</td>";
                                     echo "</tr>"; 
                                 }

                                 ?>

                                 <table id="myTable" class="table table-light table-striped">
                                    <h4>Listado de compra</h4>
                                    <tr>
                                        <th>Codigo</th>
                                        <th>Nombre</th>
                                        <th>Precio $</th>
                                        <th>Precio Bs</th>
                                        <th>Stock actual</th>
                                        <th>Cantidad</th>
                                        <th>SubTotal $</th>
                                        <th>SubTotal Bs</th>
                                        <th>Eliminar</th>
                                        <tr>
                                          <?php
                                          if(isset($_SESSION["carrito"])){
                                            $carrito = $_SESSION["carrito"];

                                            $totaldolar = 0;
                                            $totalbs = 0;
                                            $i = 0;

                                            foreach ($carrito as $p) { 

                                             echo "<tr>";
                                             echo "<td>".$p->id."</td>";
                                             echo "<td>".$p->nombre."</td>";
                                             echo "<td>$".$p->precio."</td>";
                                             echo "<td>Bs ".$p->preciobs."</td>";
                                             echo "<td>".$p->stock."</td>";
                                             echo "<td>".$p->cantidad."</td>";
                                             echo "<td>$".$p->subTotal."</td>";
                                             echo "<td>Bs ".$p->subTotalBs."</td>";
                                             echo "<td>";
                                             echo "<a class='btn btn-danger' href='./controladores/ventas/eliminarPro.php?in=$i'>Eliminar</a>";
                                             echo "</td>";
                                             $totaldolar += $p->subTotal;
                                             $i++;
                                             $totalbs += $p->subTotalBs;

                                             echo "</tr>";
                                         }
                                         echo "<tr>";
                                         echo "<td colspan='6'><b>Total:</b></td>";
                                         echo "<td colspan='1'><b>$ $totaldolar</b></td>";
                                         echo "<td colspan='1'><b>Bs $totalbs</b></td>";
                                         echo "<td colspan='1'></td>";
                                         $_SESSION["totaldolar"] = $totaldolar;
                                         $_SESSION["totalbs"] = $totalbs;
                                         echo "</tr>";

                                         echo "<tr>";
                                         echo "<td colspan='9'>";
                                         echo "<form action='./controladores/ventas/generarVenta.php' method='post'>";
                                         echo "<input class='btn btn-danger botonv' type='submit' value='Vender'>";

                                         echo "</form>";

                                         echo "</td>";
                                         echo "</tr>";
                                     }
                                 }
                                 ?>
                             </table>
                         </div><!--##:: Fin Divinterno ::##-->
                     </div><!--##:: Fin DivMain ::##-->

                     <footer class="footer">
                        <p>&copy; Todos los derechos reservados - <?php echo date("Y"); ?> | La Emperadora Internet Y Mas</p>
                    </footer>
                    <!--##::Link boostrap JS offline::##-->
                    <script src="../bootstrap-5.3.1/js/bootstrap.bundle.min.js"></script>
                    <script src="../ArchivosJS/jquery-3.6.0.min.js"></script> 
                    <script src="../ArchivosJS/bootstrap.min.js"></script>
                    <script src="../DataTables/datatables.min.js"></script>

                    <script src="../ArchivosJS/toastr.min.js"></script>
<script src="../ArchivosJS/toasts.js"></script>
</body>
</html>