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

$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
if($action == 'ajax'){
    $mes = mysqli_real_escape_string($con,(strip_tags($_REQUEST['mes'], ENT_QUOTES)));
    $ano = mysqli_real_escape_string($con,(strip_tags($_REQUEST['ano'], ENT_QUOTES)));
    include '../include/pagination.php'; 
    $page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
    $per_page = 10; 
    $adjacents  = 4; 
    $offset = ($page - 1) * $per_page;

    $count_query=mysqli_query($con, "SELECT count(*) AS numrows FROM egresos where  MONTH(fecha)='$mes' AND YEAR(fecha)='$ano' and sucursal='$sucursal' and empresa='$empresa'");
    $row= mysqli_fetch_array($count_query);
    $numrows = $row['numrows'];
    $total_pages = ceil($numrows/$per_page);
    $reload = './U_usuarios.php';

    $sql = mysqli_query($con,"SELECT * FROM egresos where  MONTH(fecha)='$mes' AND YEAR(fecha)='$ano' and sucursal='$sucursal' and empresa='$empresa' order by fecha desc LIMIT $offset,$per_page");
    if ($numrows>0){ ?>

    <table class="table table-striped table-bordered">
        <thead>
            <tr>
                <th width="20%">USUARIO</th>
                <th width="20%">CONCEPTO</th>
                <th width="10%">BANCO</th>
                <th width="10%">PAGO</th>
                <th width="12%">TOTAL</th>
                <th width="12%">FECHA</th>
                <th width="10%"></th>
            </tr>
        </thead>
        <tbody class="buscara">
            <?php  
                while ($reg=mysqli_fetch_array($sql)){
                $id=$reg['id'];
            ?>
            <input type="hidden" value="<?php echo $reg['fecha'];?>" id="fecha1<?php echo $reg['id'];?>">
            <input type="hidden" value="<?php echo $reg['concepto'];?>" id="concepto1<?php echo $reg['id'];?>">
            <input type="hidden" value="<?php echo $reg['total'];?>" id="total1<?php echo $reg['id'];?>">

            <tr>
                <td>

                    <?php
                    $usua_id=$reg['usuario']; 
                     $usua = mysqli_query($con,"SELECT * FROM usuario where  usu_id='$usua_id' and empresa_emp_id='$empresa'");
                     $usu1=mysqli_fetch_array($usua);
                     echo $usu_nombre=$usu1['usu_nombre'];
                    ?>

                </td>
                <td><?php echo $reg['concepto']; ?></td>
                 <td><?php echo $reg['banco']; ?></td>
                 <td><?php echo $reg['metodo_pago']; ?></td>
                <td><p class="pull-right"><?php echo number_format($reg['total'],0); ?></p></td>
                <td><?php echo $reg['fecha']; ?></td>
                <td> 
                    <a onclick="obtener_datos('<?php echo $reg['id'];?>');" title="Modificar" class="btn btn-xs btn-info" data-toggle="modal" data-target="#modificar_egreso">
                    <i class="btn-icon-only icon-edit"></i></a>
                    <a onclick="eliminar('<?php echo $reg['id'];?>');" title="Eliminar" class="btn btn-xs btn-danger">
                    <i class="btn-icon-only icon-remove"></i></a>
                </td>         
            </tr>  
        <?php } ?>
            <tr>
                <td colspan=7><span class="pull-right">
                    <?php echo paginate($reload, $page, $total_pages, $adjacents); ?></span>
                    <input type="hidden" name="pagina" id="pagina" value="<?php echo $page; ?>">
                </td>
            </tr>
    </table>
<?php } } ?>
 <script>
function eliminar (id)
{
    var q= $("#q").val();
    if (confirm("Realmente deseas eliminar el Egreso")){ 
    $.ajax({
        type: "GET",
        url: "php_egresos/eliminar.php",
        data: "id="+id,"q":q,
     beforeSend: function(objeto){
         $('#resultados').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
      },
        success: function(datos){
    $("#resultados").html(datos);
    load(1);
    }
      });
    }
}

function obtener_datos(id)
{
    var fecha1 = $("#fecha1"+id).val();
    var concepto1 = $("#concepto1"+id).val();
    var total1 = $("#total1"+id).val();
    var pagina=$("#pagina").val();
    $("#id").val(id);
    $("#fecha1").val(fecha1);
    $("#concepto1").val(concepto1);
    $("#total1").val(total1);
    $("#pagina").val(pagina);
}
</script>