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

$action = (isset($_REQUEST['action'])&& $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';

if($action == 'ajax'){
    $q = mysqli_real_escape_string($con,(strip_tags($_REQUEST['q'], ENT_QUOTES)));
    include '../include/pagination.php'; //include pagination file
    $page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
    $per_page = 20; //how much records you want to show
    $adjacents  = 4; //gap between pages after number of adjacents
    $offset = ($page - 1) * $per_page;

    
    $count_query   = mysqli_query($con, "SELECT count(*) AS numrows FROM dis_cotizacion where  distribuidor='$distribuidor'");
    $row= mysqli_fetch_array($count_query);
    $numrows = $row['numrows'];
    $total_pages = ceil($numrows/$per_page);
    $reload = './V_cotizaciones.php';
    $sql="SELECT * FROM  dis_cotizacion where distribuidor='$distribuidor'  order by cot_id desc LIMIT $offset,$per_page";
    $query = mysqli_query($con, $sql);
    
  
    if ($numrows>0){
      
      ?>

    <table class="table table-striped table-bordered">

        <thead>
            <tr>
                <th width="5%">Nº</th>
                <th width="10%">FECHA</th>
                <th>CLIENTE</th>
                <th>CONTACTOS</th>
                <th >TOTAL</th>
                <th width="18%"></th>
            </tr>
        </thead>
        <tbody  class="buscar">
<?php 

    while ($reg=mysqli_fetch_array($query))
    {
    $cot_id=$reg['cot_id'];
    $dis_cliente_clie_id= $reg['dis_cliente_clie_id'];

    $total=0;
    $descuentof=0;
    $descuento=0;
    $neto=0;
    $sumador_total=0;
    $iva_total=0;
    $sumador_total=0;
    $sql=mysqli_query($con, "SELECT * from  dis_cotizacion_detalle where dis_cotizacion_cot_id='$cot_id'  ");
    while ($row=mysqli_fetch_array($sql))
    {
    $id=$row["cotd_id"];
    $cotd_id=$row["cotd_id"];
    $cotd_referencia=$row['cotd_referencia'];
    $cotd_nombre=$row['cotd_nombre'];
    $cotd_cantidad=$row['cotd_cantidad'];
    $cotd_precio=$row['cotd_precio'];
    $cotd_iva=$row['cotd_iva'];
    $cotd_desc=$row['cotd_desc'];
 
    $ivaf=$cotd_iva/100;
    $precio_total=$cotd_precio*$cotd_cantidad;
    $sumador_total+=$precio_total;//Sumador
    $descuento=($cotd_desc/100)*$precio_total;
    $descuentof+=$descuento;

    $iva=$cotd_precio*$ivaf;
    $ivat=$iva*$cotd_cantidad;
    $sin_iva=($cotd_precio-$iva);
    $sin_ivat=($cotd_precio-$iva)*$cotd_cantidad;
    $iva_total+=$ivat;
    $neto+=$precio_total;
    $total=$sumador_total-$descuentof;
  }

  
    $can=mysqli_query($con,"SELECT * FROM dis_cliente where clie_id='$dis_cliente_clie_id' ") or die(mysqli_error($con)); 
    $dato=mysqli_fetch_array($can);
   
?>
            <tr>
                 
                <td><?php  echo $reg['cot_n_cotizacion']; ?><?php if ($clie_cc=='1091665283' || $clie_cc=='1091670119') {  ?>
                    <a href="libraries/tcpdf/documentos/factura_venta_extra.php?cot_id=<?php echo $cot_id;?>" target="_blank"   class="btn btn-icon-only btn-xs" style="color: white; border: solid 0px;background-color: transparent;color: transparent;" ><span class="btn-icon-only"></span></a>
                    <?php } ?></td>
                <td><?php  echo $reg['cot_fecha']; ?> / <?php echo $reg['cot_hora'];?></td>
                <td><?php echo $dato['clie_nombre']; ?></td>
                <td> <strong><?php //echo $dato['clie_correo']; ?> </strong><p><?php  echo $dato['clie_telefono'];  ?> </p>
                </td>
               
                <td><p  class="pull-right"> <?php echo number_format($total,0);?> </P></td>
                <td>
                    <div class="btn-group " >
                        <button type="button" class="btn btn-xs  btn-default dropdown-toggle" data-toggle="dropdown">Accion <span class="caret"></span></button>
                        <ul class="dropdown-menu" role="menu">
                           
                            <li><a href="" onclick="enviar_email('<?php echo $cot_id; ?>','<?php // echo $dato['clie_correo']; ?>')" data-id="<?php //echo $id;?>" class="btn-abonar">Enviar por email</a></li>
                            <li><a href="#cliente<?php echo $reg['cot_id']; ?>"   data-toggle="modal">Modificar Cliente</a></li>
                             <li class="divider"></li>
                            <li><a href="#verdetalle<?php echo $reg['cot_id']; ?>"   data-toggle="modal" >Detalles</a></li>
                            </ul>
                    </div>
                       
                    <?php include ('forms_detalle.php');
                         include ('forms_cliente.php');
                    ?>

                    
                     <a href="libraries/tcpdf/documentos/cotizacion5.php?cot_id=<?php echo $cot_id;?>" target="_blank" class="btn btn-success btn-xs" ><span class="btn-icon-only icon-print"></span></a>
                    <a href="php_cotizacion/sesion_modificar.php?cot_id=<?php echo $reg['cot_id'];?>&&page=<?php echo $page;?>" title="Editar Cotizacion"  class="btn btn-xs btn-info"  data-toggle="modal" ><i class="btn-icon-only icon-edit"></i></a>

                    <a href="#" class='btn btn-xs btn-danger' title='Eliminar Cotizacion' onclick="eliminar('<?php echo $reg['cot_id'];?>');" ><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>
                </td>
            </tr>
        </table>
    </div>
      <?php
    }
  }
?>
 <script>
    function eliminar (id)
    {
      var q= $("#q").val();
    if (confirm("Realmente deseas eliminar la Cotización")){ 
    $.ajax({
        type: "GET",
        url: "php_cotizaciones/eliminar_cotizacion.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);
    }
      });
    }
    }
</script>
  
