<?php $r="../";
require_once($r."inc/funciones.php");
//require_once($r."inc/d-error.php");
$titlepage = 'Historico de abonos';
validNotSession($r);

$spe=$_SESSION['perfil'];
$mysqli = connectDB($r);
roleaccess(1427);
$iduser=$_SESSION['userid'];
$row1 = $mysqli->query("SELECT * FROM z_abonos GROUP BY idabono");
?>
<!DOCTYPE html>
<html lang="es">
  <?php include $r."inc/head.php"; ?>
  
 
<body class="fixed-nav sidenav-toggled sticky-footer bg-dark" id="page-top">
  <?php include $r."inc/nav.php"; ?>
  <div class="content-wrapper">
    <div class="container-fluid">
      <div class="card mb-3">
        <div class="card-header">
          <i class="fa fa-table"></i> MIS PEDIDOS</div>
        <div class="card-body">
          <div class="table-responsive">
            <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
              <thead>
                <tr>
                  <th>Usuario</th>
                  <th>Codigo</th>
                  <th>Cliente</th>
                  <th>Fecha de abono</th>
                  <th>Valor abonado</th>
                  <th></th>
                </tr>
              </thead>
              <tfoot>
                <tr>
                  <th>Usuario</th>
                  <th>Codigo</th>
                  <th>Cliente</th>
                  <th>Fecha de abono</th>
                  <th>Valor abonado</th>
                  <th></th>
                </tr>
              </tfoot>
              <tbody>
                    <?php
                    while($row2 = $row1->fetch_assoc()) {
                    ?>
                      <tr>
                        <td><?php $usuario = suser($row2["iduser"]); echo $usuario['nombre']; ?></td>
                        <td><?php echo $row2["idabono"]; ?></td>
                        <td><?php $sql3 = $mysqli->query("SELECT * FROM `z_pedidos` WHERE `codigopedido` = {$row2['codigopedido']}"); $row3 = $sql3->fetch_assoc(); echo sinfo('z_clientes',$row3['idcliente'])['nombre'];  ?></td>
                        <td><?php echo $row2["fecha"]; ?></td>
                        <td><?php echo $row2["total"] ?></td>
                        <td width="75px"><a class="btn btn-primary text-secondary h5" onclick="habonos(<?php echo $row2["idabono"]; ?>)"><i class="text-white  fa fa-history"></i></a><a class="btn btn-danger text-secondary h5" onclick="undoabonos(<?php echo $row2["idabono"]; ?>, this)"><i class="text-white fa fa-undo"></i></a></td>
                        </tr>
                    <?php
                    }
                    ?>
              <tbody>
            </table>
          </div>
        </div>
      </div><a class="btn btn-primary btn-block" href="index.php" title="Nuevo Pedido">Nuevo Pedido</a>
    </div>
  </div>
  <?php include $r."inc/foot.php"; ?>
  
  <script>
    $('#dataTable').DataTable( {
        "order": [[ 3, "desc" ]],
        "lengthMenu": [[25, 50, 100, 500, -1], [25, 50, 100, 500, "All"]]
    } );
    
    function habonos(idcodeabono){
        $.post( "acciones/infocodeabonos.php", { code: idcodeabono })
          .done(function( data ) {
          swal({ html:true, title:'#<i>'+idcodeabono+' </i>', text:'<div>'+data+'</div>'});
          });
        
    }
    function undoabonos(idcodeabono, element) {
        swal({
            title: "¿Estás seguro?",
            text: "Este movimiento será revertido.",
            type: "warning",
            showCancelButton: true,
            confirmButtonText: "Sí, revertir",
            cancelButtonText: "No, cancelar",
            closeOnConfirm: false,
            closeOnCancel: true
        }, 
        function(isConfirm) {
            if (isConfirm) {
                // Ejecutar POST con jQuery para revertir el movimiento
                $.post("acciones/revertirabono.php", { code: idcodeabono })
                .done(function(response) {
                    swal("Revertido", "El movimiento ha sido revertido.", "success");
                    // Eliminar la fila padre del elemento que disparó la acción
                    $(element).closest('tr').remove();
                })
                .fail(function() {
                    swal("Error", "No se pudo revertir el movimiento.", "error");
                });
            }
        });
    }


  </script>
</body>
</html>    
    