<?php
session_start();
require_once('../conexion.php');
require_once('../include/sesiones.php'); 
$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
if($action == 'ajax')
{
    $buscar=mysqli_real_escape_string($con,(strip_tags($_REQUEST['buscar'], ENT_QUOTES)));
    $tipo_doc=mysqli_real_escape_string($con,(strip_tags($_REQUEST['tipo_doc'], 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;
    if ($tipo_doc=='9'){
        $count_query=mysqli_query($con, "SELECT count(*) AS numrows FROM dian_nomina where nom_tipo_doc='$tipo_doc' ");
        $row= mysqli_fetch_array($count_query);
        $numrows=$row['numrows'];
        $total_pages = ceil($numrows/$per_page);
        $reload='./P_pos.php';
        $sql="SELECT * FROM dian_nomina where  empresa='$empresa' and nom_tipo_doc='$tipo_doc' and nom_numero like '%$buscar%' order by nom_id desc LIMIT $offset,$per_page";
        $query=mysqli_query($con, $sql);
        if ($numrows>0) {
?>
    
<div class="table-responsive">
    <table class="table table-bordered table-hover table-striped" id="datos" style="font-size: 9pt;">
        <thead>
            <tr>
                <th width="10%">N° NOMINA</th>
                <th width="10%">FECHA</th>
                <th>EMPLEADO</th>
                <th width="15%">TOTAL</th>
                <th width="2%">ESTADO</th>
                <th width="12%"></th>
            </tr>
        </thead>
        <tbody  class="buscar">
        <?php 
        while ($reg=mysqli_fetch_array($query))
            {
                $nom_id=$reg["nom_id"];
                $nom_prefijo=$reg['nom_prefijo'];
                $nom_numero=$reg['nom_numero'];
                $nom_fecha=$reg['nom_fecha'];
                $nom_emp_id=$reg['nom_emp_id'];

                $can=mysqli_query($con,"SELECT * FROM empleados where emp_id='$nom_emp_id'") or die(mysqli_error($con)); 
                $dato1=mysqli_fetch_array($can);
        ?>
            <tr>
                <td><?php echo $nom_prefijo; echo $nom_numero;?></td>
                <td><?php echo $nom_fecha;?></td>
                <td><?php echo $dato1['emp_nombre']; echo ' '; echo $dato1['emp_primer_apellido']; echo ' '; echo $dato1['emp_segundo_apellido'];?></td>
                <td><?php //echo utf8_encode($clie_nombre); ?></td>
                <td>
                    <p class="pull-right"> <?php if ($reg['nom_estado']=="Aprobada") { ?>
                    <p class="pull-right"><span class="badgeazul" > <i class="btn-icon-only icon-ok"> <?php //echo $reg['estado_dian']; ?></i></span></p></td>
                    <?php }  else { ?>
                    <p class="pull-right"><span class="badgerojo" > <i class="btn-icon-only icon-remove"> <?php //echo $reg['estado_dian']; ?></i></span></p></td>
                    <?php } ?></p></td>
                <td>
                    <a href="libraries/tcpdf/documentos/nomina.php?nom_id=<?php echo $nom_id;?>" target="_blank" class="btn btn-success btn-xs" ><span class="btn-icon-only icon-print"></span></a>
                    <?php 
                    if ($reg['nom_estado']=="Aprobada") 
                        { ?>
                    <a href="#" onclick="eliminar('<?php echo $reg["nom_id"];?>')" title="Eliminar" class="btn btn-xs btn-danger"> <i class="btn-icon-only icon-remove"></i></a>
                     <?php } ?>
                    <?php  
                        if($reg['nom_estado']<>"Aprobada"){ ?>
                         
                    <?php } else { ?>
                        <a data-id="<?php echo $reg['nom_id'];?>" class="btn btn-xs btn-default btn-correo" title='Enviar por email' ><i class="btn-icon-only icon-envelope"></i></a>
                        <a href="V_nomina_ajuste_registrar.php?nom_id=<?php echo $nom_id;?>" class="btn btn-xs btn-warning" title='Nomina de Ajuste' ><i class="btn-icon-only fa fa-reply-all"></i></a>
                  <?php } ?>
                </td>
            </tr>
           <?php }?>
            <tr>
                <td colspan=12>
                    <span class="pull-right">
                        <?php echo paginate($reload, $page, $total_pages, $adjacents); ?>
                    </span>
                </td>
            </tr>
        </table>
    </div>

<?php } } elseif ($tipo_doc=='10')

    $count_query=mysqli_query($con,"SELECT count(*) AS numrows FROM dian_nomina where nom_tipo_doc='$tipo_doc'");
    $row= mysqli_fetch_array($count_query);
    $numrows=isset($row['numrows']);
    $total_pages=ceil($numrows/$per_page);
    $reload = './P_pos.php';
    $sql="SELECT * FROM dian_nomina where nom_tipo_doc='$tipo_doc' and nom_numero like '%$buscar%' order by nom_id desc LIMIT $offset,$per_page";
    $query=mysqli_query($con, $sql);
    if ($numrows>0) { ?>
    <div class="table-responsive">
        <table class="table table-bordered table-hover table-striped" id="datos" style="font-size: 9pt;">
            <thead>
                <tr>
                    <th width="10%">N° AJUSTE</th>
                    <th width="10%">FECHA</th>
                    <th>EMPLEADO</th>
                    <th width="15%">TOTAL</th>
                    <th width="2%">ESTADO</th>
                    <th width="10%"></th>
                </tr>
            </thead>
            <tbody  class="buscar">
            <?php 
                while ($reg=mysqli_fetch_array($query))
                {
                    $nom_id=$reg["nom_id"];
                    $nom_prefijo=$reg['nom_prefijo'];
                    $nom_numero=$reg['nom_numero'];
                    $nom_fecha=$reg['nom_fecha'];
                    $nom_emp_id=$reg['nom_emp_id'];
                    $can=mysqli_query($con,"SELECT * FROM empleados where emp_id='$nom_emp_id'") or die(mysqli_error($con)); 
                    $dato1=mysqli_fetch_array($can);
            ?>
                <tr>
                    <td><?php echo $nom_prefijo; echo $nom_numero;?></td>
                    <td><?php echo $nom_fecha;?></td>
                    <td><?php echo $dato1['emp_nombre']; echo ' '; echo $dato1['emp_primer_apellido']; echo ' '; echo $dato1['emp_segundo_apellido'];?></td>
                    <td><?php //echo utf8_encode($clie_nombre); ?></td>
                    <td><p class="pull-right"> <?php if ($reg['nom_estado']=="Aprobada") { ?>
                        <p class="pull-right"><span class="badgeazul" > <i class="btn-icon-only icon-ok"> <?php //echo $reg['estado_dian']; ?></i></span></p></td>
                        <?php }  else { ?>
                        <p class="pull-right"><span class="badgerojo" > <i class="btn-icon-only icon-remove"> <?php //echo $reg['estado_dian']; ?></i></span></p></td>
                        <?php } ?></p></td>
                    <td>
                        <a href="libraries/tcpdf/documentos/nomina.php?nom_id=<?php echo $nom_id;?>" target="_blank" class="btn btn-success btn-xs" ><span class="btn-icon-only icon-print"></span></a>

                          <!--<a href="#" onclick="eliminar('<?php //echo $reg["nom_id"];?>')" title="Eliminar" class="btn btn-xs btn-danger"> <i class="btn-icon-only icon-remove"></i></a>-->
                        <?php  
                            if($reg['nom_estado']<>"Aprobada"){ ?>
                             <a data-id="<?php echo $reg['nom_id'];?>" title="Renviar" class="btn btn-xs btn-warning btn-dian" data-toggle="modal"><i class="btn-icon-only fa fa-upload"></i></a> 
                        <?php } if($reg['nom_estado']=="Aprobada"){ ?>
                            <a data-id="<?php echo $reg['nom_id'];?>" class="btn btn-xs btn-default btn-correo" title='Enviar por email'><i class="btn-icon-only icon-envelope"></i></a>
                        <?php } ?>
                    </td>
                </tr>
               <?php } ?>
                <tr>
                    <td colspan=12>
                        <span class="pull-right">
                            <?php echo paginate($reload, $page, $total_pages, $adjacents); ?>
                        </span>
                    </td>
                </tr>
            </table>
        </div>
<?php  }  }  ?>

<script>
    $(".btn-dian").click(function(){
        id = $(this).data("id");
        $.get("./php_doc_nomina/forms_dian_renviar.php","id="+id,function(data){
            $("#form-dian").html(data);
        });
        $('#dian').modal('show');
    });
</script>
  
 <div class="modal fade" id="dian" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Enviar Nomina</h4>
            </div>
            <div class="modal-body">
                <div id="form-dian"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


<script>
    $(".btn-correo").click(function(){
        id = $(this).data("id");
        $.get("./php_doc_nomina/forms_envio_correo.php","id="+id,function(data){
            $("#form-correo").html(data);
        });
        $('#correo').modal('show');
    });
</script>
  
 <div class="modal fade" id="correo" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Enviar Correo </h4>
            </div>
            <div class="modal-body">
                <div id="form-correo"></div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


<script type="text/javascript">
function eliminar (nom_id)
{
    if (confirm("Realmente deseas eliminar la nomina")){ 
    $.ajax({
        type: "GET",
        url: "php_doc_nomina/eliminar_nomina.php",
        data: "nom_id="+nom_id,
        beforeSend: function(objeto){
        $('#loader1').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
        },
        success: function(datos){
        $("#loader1").html(datos);
        nomina(1);
        load(1);
        }
        });
    }
} 
</script>