<?php
session_start();
require_once ("../conexion.php");
require_once('../include/sesiones.php');
$com_id=$_GET['id'];
$pagina=$_GET['pagina']; 

?>
<input type="hidden" name="pagina" id="pagina" value="<?php echo $pagina; ?>">
<form method="post" action="" name="form_pdf" id="form_pdf" enctype="multipart/form-data">
    <div class="form-group">
        <label for="factura">Adjuntar Factura ( PDF )</label>
        <input type="file" name="factura" id="factura">
        <input type="hidden" name="com_id" value="<?php  echo $com_id; ?>" required readonly>
    </div>
    <div class="modal-footer">
        <?php 
            $files = glob("../img/img_facturas/$com_id.pdf");
            if(count($files) > 0) 
            { 
                foreach($files as $file) 
                { 
                echo "<a href='img/img_facturas/$com_id.pdf' target='_blank' class='btn btn-info btn-s'><span class='btn-icon-only '>Ver</span></a>";
                } 
        ?>
            <button type="submit" class="btn btn-success" name="subir" value="subir" id="subir">Modificar</button>
        <?php  }  else { ?>
                  
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
        <button type="submit" class="btn btn-info" name="subir" value="subir" id="subir">Subir</button>

 <?php  }  ?>
    </div>    
</form>
          
<script type="text/javascript">
$( "#form_pdf" ).submit(function( event ) {
$('#subir').attr("disabled", true);
var pagina=$("#pagina").val();

 var formData = new FormData($("#form_pdf")[0]);
  var ruta = "php_compras/subir_pdf.php";
   $.ajax({
      type: "POST",
      url: ruta,
      data: formData,
      contentType: false,
      processData: false, 
        beforeSend: function(objeto){
         $('#resultados').html('<center><img src="./img/ajax-loader.gif"> CARGANDO...</center>');
        },
      success: function(datos){
      $("#resultados").html(datos);
      $('#subir').attr("disabled", false);
      $('#pdf').modal('hide');
      $('body').removeClass('modal-open');
      $('.modal-backdrop').remove();
      load(pagina);

      }
  });
  event.preventDefault();
})
</script> 
<style type="text/css">
input[type="file"]#factura {
 width: 0.1px;
 height: 0.1px;
 opacity: 0;
 overflow: hidden;
 position: absolute;
 z-index: -1;
 }

label[for="factura"] {
 font-size: 14px;
 font-weight: 600;
 color: #fff;
 background-color: #106BA0;
 display: inline-block;
 cursor: pointer;
 padding: 5px 40px ;
 width: fit-content;
 text-align: center;
 }
</style>