<?php
    include ("header.php");
     setlocale(LC_MONETARY, 'en_US');
            if (!empty($_POST))
            {
             $id = $mysqli->real_escape_string($_POST['id']);
             $ven_id = $mysqli->real_escape_string($_POST['ven_id']);
             $pago = $mysqli->real_escape_string($_POST['pago']);
             $total = $mysqli->real_escape_string($_POST['total']);
             $tipopago = $mysqli->real_escape_string($_POST['tipopago']);
             $cambio=$pago-$total;
             
               
             if($tipopago=='efectivo')
             {
              $tp=1;
              echo '<div class="content-wrapper">';
                           
              ?>
  <div class="col-sm-3">
    <div class="card">
      <div class="card-body">
        <h1 class="card-title"><b>Cambio:</b></h1>
        Total a pagar:
        <p class="card-text" align="right"><b><?echo money_format('%(#10.2n', $total) . "\n";?></b></p>
        Pago con:
        <p class="card-text" align="right"><b><?echo money_format('%(#10.2n', $pago) . "\n";?></b></p>
        Cambio:
        <p class="card-text" align="right"><b><?echo money_format('%(#10.2n', $cambio) . "\n";?></b></p>
        
        
        
      </div>
    </div>
  </div>
              <?
             
             
             
                   
                         echo '<div class="alert alert-success " role="alert">';
                               echo 'Registro guardado con exito...';
                                             
                          
            }
            else
            {
             $tp=2;
             
             
                   echo '<div class="content-wrapper">
                           <div class="alert alert-success " role="alert">';
                         
                               echo 'Pago generado con tarjeta <br>Registro guardado con exito...';
                                             
                           
            }
            
            echo '</div>
                           <a href="ventas.php" class="btn btn-primary" role="button" data-bs-toggle="button">Regresar</a>
                           <div class="page-content fade-in-up">
                           <a href="iiticket.php?ven_id='.$ven_id.'" class="btn btn-primary" role="button" target="_blank" data-bs-toggle="button">Imprimir Tiket</a>
                           <div class="page-content fade-in-up">';
                           
 $consulta="UPDATE `venta` SET `ven_total`='$total',`ven_status`='0',`ven_tipopago`='$tp' WHERE ven_id='$ven_id'";
 //echo $consulta;
                    $stmt = $mysqli->prepare($consulta);
                    if ($stmt->execute()){
                        $guardado=1;
                    }
                           
                           


                           
                    include ("footer.php");
                       exit;

            }
            
            
            if (!empty($_GET))
            {
                $bandera=0;
                $id = $_GET['id'];
                $titulo="Modificar Paciente";
                $tituloBoton="Modificar";
                $sql="SELECT * FROM `pacientes` WHERE pac_id = $id";
                $result = $mysqli->query($sql); 
                $row = $result->fetch_assoc();
                $nombre=$row['pac_nombre'];
                $nacimiento=$row['pac_nacimiento'];
                $sexo=$row['pac_sexo'];
                


                
                
                
            }
?>

        <div class="content-wrapper">
            <!-- START PAGE CONTENT-->
            <div class="page-heading">
                <h1 class="page-title">Nota de Venta</h1>
            </div>
            <div class="page-content fade-in-up">
                    <div class="ibox-body">
                    <div class="page-content fade-in-up">
                <div class="row">
                    <div class="col-md-12">
                        <div class="ibox">
                        <?php
                            if ($guardado == 1){?>
                        <div class="alert alert-success " role="alert">
                            Expediente guardado con exito... 
                            
                        </div>
                        <?php
                        }
                        if ($error == 1){?>
                            <div class="alert alert-danger " role="alert">
                                Error al guardar, verifique los datos...
                                
                            
                            <?php
                            }
                        ?>
                        <?
                        $fecha_nacimiento = $nacimiento;
                        $dia_actual = date("Y-m-d");
                        if($bandera==0)
                            $edad = date_diff(date_create($fecha_nacimiento), date_create($dia_actual));
                        ?>
                         <?
                                            
                                    $sql="SELECT COUNT(*) as num FROM `venta` WHERE `ven_pac_id` = $id and  `ven_status` = 1 LIMIT 1 ";
                                    $result = $mysqli->query($sql); 
                                    $row = $result->fetch_assoc();
                                    $num=$row['num'];
                                    
                                    if ($num==0)
                                    {
                                     $consulta="INSERT INTO `venta`(`ven_pac_id`, `ven_total`, `ven_fecha`, `ven_hora`, `ven_status`, `ven_tipopago`) VALUES ('".$id."','0','".date("Y-m-d")."','".date("H:i")."','1','1')";

                                        $stmt = $mysqli->prepare($consulta);
                                    if ($stmt->execute())
                                       $guardado=1;   
                                    }
                                    $sql="SELECT * FROM `venta` WHERE `ven_pac_id` = $id and  `ven_status` = 1 LIMIT 1 ";
                                    $result = $mysqli->query($sql); 
                                    $row = $result->fetch_assoc();
                                    $ven_id=$row['ven_id'];
                                    $ven_fehca=$row['ven_fecha'];
                                    $ven_hora=$row['ven_hora'];
                                    
                                    $sql="SELECT * FROM `venta_detalle`,productos WHERE `vdet_pro_id` = pro_id and vdet_ven_id = $ven_id";
                                    $resultado = mysqli_query($mysqli, $sql);
                                    
                            ?>
                            <div class="ibox-head">
                                <div class="ibox-title">Datos del Paciente</div>
                                <div class="ibox-tools">
                                    <a class="ibox-collapse"><i class="fa fa-minus"></i></a>
                                    <a class="fullscreen-link"><i class="fa fa-expand"></i></a>
                                </div>
                            </div>
                            <div class="ibox-body">
                                    <form id="signupform" class="form-horizontal" role="form" action="nventa.php" method="POST" autocomplete="off">
                                    <div class="row">
                                            <div class="col-sm-3 form-group">
                                                    <label>Nombre</label>
                                                    <input name="nombre" class="form-control" type="text" placeholder="Nombre Completo" value="<?php echo $nombre; ?>" readonly>
                                                </div>
                                            <div class="col-sm-3 form-group">
                                                    <label>Sexo</label>
                                                    <input name="sexo" class="form-control" type="text" placeholder="Nombre Completo" value="<?php echo $sexo; ?>" readonly>
                                                </div>
                                            <div class="col-sm-3 form-group">
                                                    <label>Edad en años</label>
                                                    <input name="edad" class="form-control" type="text" placeholder="Nombre Completo" value="<?php
                                                    if($bandera==0)
                                                        echo $edad->format('%y');
                                                    else
                                                        echo $edad; ?>" readonly>
                                                </div>
                                            <div class="col-sm-3 form-group">
                                                <label>Fecha de la venta</label><br>
                                                <input type="date" name="fechanota" step="1" min="2020-01-01" max="2025-12-31" value="<?php echo $ven_fehca;?>" readonly>
                                                
                                            </div>
                                            <div class="col-sm-3 form-group">
                                                    <label>Hora</label>
                                                    <input name="hora" class="form-control" type="text" placeholder="" value="<?php echo $ven_hora; ?>">
                                                </div>
                                    </div>
                                    
                                    
                                    
                        </div>
                            
                            
                            
                            
                    </div>
                            


                <div class="row">
                    <div class="col-xl-12">
                        <div class="ibox">
                            <div class="ibox-head">
                                <div class="ibox-title">ITEMS DE VENTA: 
                                <a href="aventa.php?pac_id=<?php echo $id;?>&ven_id=<?php echo $ven_id;?>" class="btn btn-success" role="button" data-bs-toggle="button">Agregar</a>
                                </div>
                            </div>
                            <div class="ibox-body">
                                <div class="col-sm-12 form-group">
                                    
                                <table class="table table-striped table-bordered table-hover" id="dataTablePacientes" cellspacing="0" width="100%">
                            <thead>
                                <tr>
                                       <th> TIPO </th>
                                       <th> NOMBRE </th>
                                       <th> DESCRIPCION </th>
                                       <th> PRECIO </th>
                                       <th></th>
                                 </tr>
                            </thead>
                                <tbody>          
                                    <?php $NUM=0;while($row = mysqli_fetch_array($resultado)) { $NUM=$NUM+1?>
                                    <tr>
                                        <td><?php echo $row['pro_tipo']; ?></td>
                                        <td><?php echo $row['pro_nombre']; ?></td>
                                        <td><?php echo $row['pro_descripcion']; ?></td>
                                        <td><?php echo money_format('%(#10.2n', $row['vdet_pro_venta']); ?></td>
                                         <td> <a href="#" data-href="edventa.php?id=<?php echo $id;?>&ven_id=<?php echo $ven_id;?>&vdet_id=<?php echo $row['vdet_id'];?>" data-toggle="modal" data-target="#confirm-deleteedventa" tabindex="-1" role="button" aria-disabled="true"><div class="icon-item"><i class="fa fa-trash"></i></div></a> </td>
                                                  </td>
                                       </tr>
                                    <?php } ?>
                                </tbody>
                        </table>
                                
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
 </form>               
                
                
 <form id="formventa" class="form-horizontal" role="form" action="nventa.php" method="POST" autocomplete="off">
<div class="row">
  <div class="col-sm-4">
    
  </div>
    <div class="col-sm-4">
    
  </div>
    
 <?

 $sql="SELECT sum(vdet_pro_venta) as total FROM `venta_detalle` WHERE vdet_ven_id = $ven_id";
 $result = $mysqli->query($sql); 
                $row = $result->fetch_assoc();
                $total=$row['total'];
 ?>   
 
                                    

  <div class="col-sm-4">
    <div class="card">
      <div class="card-body">
        <h1 class="card-title"><b>TOTAL:</b></h1>
        <p class="card-text" align="right"><b><?echo money_format('%(#10.2n', $total) . "\n";?></b></p>
        <div class="form-group col-md-12">
      <label for="inputState">Forma de pago</label>
      <select id="tipopago" name="tipopago" class="form-control">
        <option value="efectivo" selected>Efectivo</option>
        <option value="tarjeta">Tarjeta</option>
      </select>
    </div>
        <div class="col-sm-12 form-group">
           <label>¿Con cuanto paga el cliente?</label>
           <input name="pago" class="form-control" type="text" align="right" placeholder="Nombre Completo" value="0.00" >
         </div>
        <input id="ven_id" name="ven_id" type="hidden" value="<?echo $ven_id;?>">
        <input id="pac_id" name="pac_id" type="hidden" value="<?echo $id;?>">
        <input id="total" name="total" type="hidden" value="<?echo $total;?>">
        <a href="#" class="btn btn-info" onclick="document.getElementById('formventa').submit();">PAGAR</a>
        <a href="#" class="btn btn-danger">CANCELAR</a>
      </div>
    </div>
  </div>
</div>
</form>



                                    


                            
                            
            </div>                    
        </div>     
<?php 
    include ('footer.php');
?>