<?php
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php');
if (isset($_POST['cliente'])){$cliente=$_POST['cliente']; $cliente;}
if (isset($_POST['ven_id'])){$ven_id=$_POST['ven_id']; $ven_id;}

if ($ven_id>0)
{
    $registros=mysqli_query($con,"SELECT * FROM ventas where ven_id='$ven_id' and empresa='$empresa' and ven_estado='sin pagar'") or die(mysqli_error($con));
    $reg=mysqli_fetch_array($registros);
    $ven_id=$reg['ven_id'];
    $ven_estado=$reg['ven_estado'];
    $ven_forma_de_pago=$reg['ven_forma_de_pago'];
    $Cliente_clie_id=$reg['Cliente_clie_id'];
    if ($reg<1)
    { 
?>
        <div class="alert alert-danger alert-dismissable">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
            <strong>No existe factura con ese N o ya esta pagada!</strong> <a href="#" class="alert-link"></a>
        </div>
<?php   
    }
    else 
    {
        include('../php_pos/saldodeventas.php');
        $sql2=mysqli_query($con, "SELECT * from cliente where clie_id='$Cliente_clie_id' and empresa='$empresa' ");
        $sql2=mysqli_fetch_array($sql2);
?>
        <script>
            $('#ventanacxc').hide(); 
        </script>
  
        <form method="post" action="" id="form_cxc" name="form_cxc">
            <div class="col-md-12">
                <div class="form-group">
                    <div class="alert alert-info alert-dismissable">
                        <strong>Abono de la factura N <?php  echo $reg['ven_n_venta']; ?></strong> de <?php echo $sql2['clie_nombre']; ?>
                    </div>
                    <pre style="font-size:24px;text-align:center"><strong class="text-danger">$<?php echo number_format($saldo,0); ?></strong></pre>
                    <input type="hidden" name="clie_id" value="<?php echo $Cliente_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" required 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" autocomplete="off" required readonly >
                    <div class="middle" id="ventana2">
                        <label>
                            <input type="radio" name="forma_pago" id="forma_pago" value="efectivo" checked onChange="pago(this)"/>
                            <div class="front-end box"><span>EFECTIVO</span></div>
                        </label>
                        <label>
                            <input type="radio" name="forma_pago" id="forma_pago" value="transferencia" onChange="pago(this)"/>
                            <div class="back-end box"><span>TRANSFERENCIA</span></div>
                        </label>
                        <label>
                            <input type="radio" name="forma_pago" id="forma_pago" value="tarjeta" onChange="pago(this)"/>
                            <div class="back-end box"><span>TARJETA</span></div>
                        </label>
                    </div> 
                    <div id="div_efectiv" style="display: "> </div>

                    <div id="div_not" style="display: none">
                        <label>Nota Credito Disponible</label>
                        <input type="text" name="nota_credito" id="nota_credito" value="" class="form-control name_list" autocomplete="off" readonly >
                    </div>

                    <div id="div_medio_pag" style="display: none">
                        <label>PLATAFORMA DE PAGO</label>
                        <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>
                    </div>
                    <label>ABONO </label>
                    <input type="number" name="valor" value="" class="form-control name_list" autocomplete="off" required max="<?php echo $saldo; ?>">
                </div>
            </div> <!-- /.form-group -->
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                <button type="submit" class="btn btn-info" name="btn_cxc" value="btn_cxc" id="btn_cxc">Abonar</button>
            </div>
        </form>
        <script>
        function pago(sel) {
            if (sel.value=="efectivo")
            {
                divC = document.getElementById("div_not");
                divC.style.display = "none";
                divT = document.getElementById("div_medio_pag");
                divT.style.display = "none";
                divT = document.getElementById("div_efectiv");
                divT.style.display = "block";
            } 
            else if (sel.value=="nota credito")
            {
                divC = document.getElementById("div_not");
                divC.style.display = "";
                divT = document.getElementById("div_medio_pag");
                divT.style.display = "none";
                divT = document.getElementById("div_efectiv");
                divT.style.display = "none";
            } 
            else 
            {
                divC = document.getElementById("div_not");
                divC.style.display="none";
                divT = document.getElementById("div_medio_pag");
                divT.style.display = "";
                divT = document.getElementById("div_efectiv");
                divT.style.display = "none";
            }
        }
        </script>       

        <script>
        $("#form_cxc" ).submit(function( event ) {
        $('#btn_cxc').attr("disabled", true);

        var parametros = $(this).serialize();
            $.ajax({
              type: "POST",
              url: "php_movimientos/registrar_cxc.php",
              data: parametros,
                beforeSend: function(objeto){
                 $('#resultados').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
                },
              success: function(datos){
              $("#resultados").html(datos);
              $('#btn_cxc').attr("disabled", false);
              $('#buscarcxc').modal('hide');
              $('body').removeClass('modal-open');
              $('.modal-backdrop').remove();
              document.getElementById("form_cxc").reset();
              load(1);
              loadmodal();
              
              }
          });
            event.preventDefault();
        })
        </script>
<?php   }  } 
else if ($cliente>0)
{ 
    $regi=mysqli_query($con,"SELECT * FROM ventas where Cliente_clie_id='$cliente' and ven_estado='sin pagar' and empresa='$empresa'") or die(mysqli_error($con));
    $reg1=mysqli_fetch_array($regi);
    if ($reg1<1)
    {   
?>
        <div class="alert alert-danger alert-dismissable">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
            <strong>El cliente no tiene facturar por pagar</strong> <a href="#" class="alert-link"></a>
        </div>
<?php  
    }
    else 
    { 
?>
    <script>
        $('#ventanacxc').hide(); 
    </script>        

    <table class="table table-bordered table-hover table-striped">
        <thead>
            <tr>
                <th>N°</th>
                <th>FECHA</th>
                <th>TOTAL</th>
                <th>DEVOLUCION</th>
                <th>SALDO</th>
                <th></th>
            </tr>
        </thead>
        <?php 
        $totalpagar=0;
        $registros=mysqli_query($con,"SELECT * FROM ventas where Cliente_clie_id='$cliente' and ven_estado='sin pagar' and empresa='$empresa'") or die(mysqli_error($con));
        while ($reg=mysqli_fetch_array($registros))
        {
            $ven_id=$reg['ven_id'];
            $ven_estado=$reg['ven_estado'];
            $ven_forma_de_pago=$reg['ven_forma_de_pago'];
            $Cliente_clie_id=$reg['Cliente_clie_id'];
            include('../php_pos/saldodeventas.php');
            $totalpagar+=$saldo;
        ?>
            <tr>
                <td><?php echo $reg['ven_n_venta']; ?></td>
                <td><?php echo $reg['ven_fecha']; ?></td>
                <td><p class="pull-right"><?php echo number_format($sumador_total,0); ?></p></td>
                <td><p class="pull-right"><?php echo number_format($devo_total,0);?></p></td>
                <td><p class="pull-right"><?php echo number_format($saldo,0); ?></p></td>
                <td>
                   <a href="#" class="btn btn-xs  btn-info" onclick="factura('<?php echo $ven_id ?>')"><i class="btn-icon-only icon-plus"></i></a>
                </td>
            </tr>
        <?php } ?> 
            <tr>
                <td colspan="4">TOTAL</td>
                <td><p class="pull-right"><?php echo number_format($totalpagar,0); ?></p></td>
                <td></td>
            </tr> 
    </table>
    <?php if($totalpagar>0){ ?>

    <div class="modal-footer" style="padding-right:0px;">
        <form method="post" action="" id="form_pago_cxc_todo" name="form_pago_cxc_todo">
            <input type="hidden" name="cliente" id="cliente" value="<?php echo $Cliente_clie_id; ?>" required readonly>
            <button type="submit" class="btn btn-success" name="pago_cxc_todo" id="pago_cxc_todo">PAGAR TODO</button>
            <a href="#" class="btn btn-info" onclick="abonototal()">ABONAR</a>
        </form>
    </div>

    <script>
    $("#form_pago_cxc_todo" ).submit(function( event ) {
    $('#pago_cxc_todo').attr("disabled", true);
     var parametros = $(this).serialize();
       $.ajax({
          type: "POST",
          url: "php_movimientos/forms_cxc_todo.php",
          data: parametros,
            beforeSend: function(objeto){
             $('#datoscxc').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
            },
          success: function(datos){
          $("#datoscxc").html(datos);
          $('#pago_cxc_todo').attr("disabled", false);
          // load(1);
          }
      });
    event.preventDefault();
    })
    </script>
    
<?php } ?> 
<?php } }?>
<script>
function factura(ven_id)
{
    $.ajax({
    type: "POST",
    url: "php_movimientos/forms_cxc.php",
    data: "ven_id="+ven_id,
    beforeSend: function(objeto){
    $("#datoscxc").html("Mensaje: Cargando...");
    },
    success: function(datos){
    $("#datoscxc").html(datos);
    }
    });
}


function abonototal()
{
    var cliente= $("#cliente").val();
    $.ajax({
    type: "POST",
    url: "php_movimientos/forms_cxc_abonar.php",
    data: "cliente="+cliente,
    beforeSend: function(objeto){
    $("#datoscxc").html("Mensaje: Cargando...");
    },
    success: function(datos){
    $("#datoscxc").html(datos);
    }
    });
}
</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>