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

$ven_id=$_GET['id']; 
$sql=mysqli_query($con, "SELECT * from ventas where ven_id='$ven_id' and empresa='$empresa' ");
$reg=mysqli_fetch_array($sql);
$Cliente_clie_id=$reg['Cliente_clie_id'];
$cli=mysqli_query($con, "SELECT * from cliente where clie_id='$Cliente_clie_id' and empresa='$empresa' ");
$cli=mysqli_fetch_array($cli);
$clie_id=$cli['clie_id'];
$clie_nombre=$cli['clie_nombre'];



$sumador_combo=0;
$sumador_total=0;

$neto=0;
$total_descuento=0;
$total_iva=0;
$total_retencion=0;
$sumador_articulos=0;

$ret=mysqli_query($con, "SELECT * FROM retencion_venta where ventas_ven_id='$ven_id' ") or die(mysqli_error($con)); 
$rets=mysqli_fetch_array($ret);
if(isset($rets['porcentaje']))
{
    $ret_porcentaje=$rets['porcentaje'];
}
else
{
    $ret_porcentaje=0;
}

include('../include/total_venta.php');
$total_retencion+=($neto+$sumador_combo-$total_descuento)*($ret_porcentaje/100);
$sumador_total=$sumador_articulos+$sumador_combo-$total_retencion;

$sql=mysqli_query($con, "SELECT sum(veab_abono) as total from venta_abonos where ventas_ven_id='$ven_id' ");
$sql=mysqli_fetch_array($sql);
$total_abono=$sql['total'];

$devo_total=0;
$sql1=mysqli_query($con, "SELECT * from devolucion where empresa='$empresa' and ventas_ven_id='$ven_id'");
while ($row=mysqli_fetch_array($sql1))
{
    $dev_id=$row["dev_id"];
    $dev_referencia=$row["dev_referencia"];
    $dev_cantidad=$row['dev_cantidad'];
    $dev_precio=$row['dev_precio'];
    $devo_total+=$dev_precio; 
}
if($reg['ven_estado']=="sin pagar")
{ 
    $saldo_t=$sumador_total-$total_abono-$devo_total;
    if ($saldo_t<=0) 
    {
        $saldo='0';
    }
    else
    {
        $saldo=$saldo_t;
        number_format($saldo,0); 
    }
}
                 
?>
<form method="post" action="" id="abonar_fac" name="abonar_fac" onsubmit="return checkSubmit();">
    <div class="form-group">
        <input type="hidden" name="clie_id" value="<?php  echo $clie_id; ?>" class="form-control name_list" autocomplete="off" required readonly>
        <input type="hidden" name="ven_id" value="<?php  echo $reg['ven_id']; ?>" class="form-control name_list" autocomplete="off" required readonly>
        <input type="hidden" name="detalle" class="form-control"  value="Abono de la factura N <?php  echo $reg['ven_n_venta']; ?>">
        <input type="hidden" name="saldo" value="<?php  echo $saldo; ?>" class="form-control name_list">

        <div class="alert alert-info alert-dismissable">
            <strong>Abono de la factura N <?php  echo $reg['ven_n_venta']; ?></strong> de <?php echo $clie_nombre; ?>
        </div>

        <pre style="font-size:24px;text-align:center"><strong class="text-danger">$<?php echo number_format($saldo,0); ?></strong></pre>

        <div class="middle" id="ventana2">
            <label>
                <input type="radio" name="forma_pago" id="forma_pago" value="efectivo" checked onChange="medio_pago1(this)"/>
                <div class="front-end box"><span>EFECTIVO</span></div>
            </label>
            <label>
                <input type="radio" name="forma_pago" id="forma_pago" value="transferencia" onChange="medio_pago1(this)"/>
                <div class="back-end box"><span>TRANSFERENCIA</span></div>
            </label>
            <label>
                <input type="radio" name="forma_pago" id="forma_pago" value="tarjeta" onChange="medio_pago1(this)"/>
                <div class="back-end box"><span>TARJETA</span></div>
            </label>
            <?php if($cli['clie_nota_credito']>0){ ?>
            <label>
                <input type="radio" name="forma_pago" id="forma_pago" value="nota credito" onChange="medio_pago1(this)"/>
                <div class="back-end box"><span>CRUCE</span></div>
            </label>
             <?php } ?>
        </div> 


           
        <div id="div_efectivo" style="display: "> </div>

        <div id="div_nota" style="display: none">
            <label>Nota Credito Disponible</label>
            <input type="text" name="nota_credito" id="nota_credito" value="<?php echo number_format($cli['clie_nota_credito'],0); ?>" class="form-control name_list" autocomplete="off" readonly >
        </div>

        <div id="div_medio_pago" style="display: none">
            <label>PLATAFORMA DE PAGO</label>
            <section id="intro" >
                <select name="medio_pago" id="medio_pago" class="form-control name_list input-sm" required>
                    <?php
                        $datos = mysqli_query($con,"SELECT * FROM banco WHERE empresa='$empresa' order by ban_nombre asc") ;
                        while( $fila = mysqli_fetch_array( $datos ) )
                        {
                            echo' <option value="'.$fila['ban_nombre'].'">'.$fila['ban_nombre'].'</option>';
                        }
                    ?>
                </select>
            </section>
        </div>

        <label>Valor</label>
        <input type="number" name="valor" id="valor" class="form-control name_list" autocomplete="off" max="<?php echo round($saldo); ?>" required >
        
    </div>

    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
        <button type="submit" class="btn btn-success" name="btn_abonar" value="btn_abonar" id="btn_abonar">Abonar</button>
    </div>
</form>

<script>
function medio_pago1(sel) {
    if (sel.value=="efectivo")
    {
        divC = document.getElementById("div_nota");
        divC.style.display = "none";
        divT = document.getElementById("div_medio_pago");
        divT.style.display = "none";
        divT = document.getElementById("div_efectivo");
        divT.style.display = "block";
    } 
    else if (sel.value=="nota credito")
    {
        divC = document.getElementById("div_nota");
        divC.style.display = "";
        divT = document.getElementById("div_medio_pago");
        divT.style.display = "none";
        divT = document.getElementById("div_efectivo");
        divT.style.display = "none";
    } 
    else 
    {
        divC = document.getElementById("div_nota");
        divC.style.display="none";
        divT = document.getElementById("div_medio_pago");
        divT.style.display = "";
        divT = document.getElementById("div_efectivo");
        divT.style.display = "none";
    }
}
</script>

<script>
$( "#abonar_fac" ).submit(function( event ) {
$('#btn_abonar').attr("disabled", true);
var parametros = $(this).serialize();
   $.ajax({
      type: "POST",
      url: "php_pos/abonar.php",
      data: parametros,
        beforeSend: function(objeto){
         $('#resultados').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
        },
        success: function(datos){
        $("#resultados").html(datos);
        $('#btn_abonar').attr("disabled", false);

        $('#abonar').modal('hide');
        $('body').removeClass('modal-open');
        $('.modal-backdrop').remove();
        load(1);
      }
  });
  event.preventDefault();
})
</script>
<style>
.middle {
  width: 100%;
  text-align: center;
  /* Made by */
}
.middle h1 {
  font-family: "Dax", sans-serif;
  color: #ccc;
}
.middle input[type="radio"] {
  display: none;
}
.middle input[type="radio"]:checked + .box {
  background-color: #009e90;
}
.middle input[type="radio"]:checked + .box span {
  color: white;
  transform: translateY(70px);
}
.middle input[type="radio"]:checked + .box span:before {
  transform: translateY(0px);
  opacity: 1;
}
.middle .box {
  width: 131px;
  height: 60px;
  background-color: #ccc;
  transition: all 250ms ease;
  will-change: transition;
  display: inline-block;
  text-align: center;
  cursor: pointer;
  position: relative;
  font-family: "Dax", sans-serif;
  font-weight: 900;
  padding-top: 19px;
}
.middle .box span {
  left: 0;
  right: 0;
}
</style>