<style type="text/css">
  .btn-primary-spacing {
    margin-right: 5px;    
  }
</style>


<section class="content-header">
  <h1>
    <i class="fa fa-sign-in icon-title"></i> Salidas de Almacen Trade
	
    <a class="btn btn-primary btn-social pull-right" href="modules/trade/medicines_consulta/print.php" title="Imprimir"
    data-toggle="tooltip" onclick="fnSelect()" id="lnkPrint" target="_blank" onclick="document.location.reload();">
      <i class="fa fa-print"></i>Guardar / Imprimir salida
    </a>    
    <a class="btn btn-primary btn-social pull-right btn-primary-spacing" href="?module=form_medicines_consulta&form=add" title="Agregar" data-toggle="tooltip">
      <i class="fa fa-plus"></i>Generar salidas
    </a>    
  </h1>
</section>

<section class="content">
  <div class="row">
    <div class="col-md-12">

    <?php  

    if (empty($_GET['alert'])) {
      echo "";
    } 

    elseif ($_GET['alert'] == 1) {
      echo "<div class='alert alert-success alert-dismissable'>
              <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
              <h4>  <i class='icon fa fa-check-circle'></i> Exito!</h4>
              Datos de productos han sido registrado correctamente.
            </div>";
    }
    ?>

      <div class="box box-primary">
        <div class="box-body">
          <form>
           <div class="form-group col-md-3">             
              <label for="fecha">Fecha</label>                
              <input type="text" class="form-control date-picker" data-date-format="dd-mm-yyyy" name="fecha" id="fecha" autocomplete="off" value="<?php echo date("Y-m-d"); ?>" readonly required>              
            </div>
            <div class="form-group col-md-3">             
              <label for="fecha">Usuario que genera salida </label>                
              <input type="text"  class="form-control" autocomplete="off" value="<?php echo $_SESSION['name_user']; ?>" readonly required>              
            </div>
			
			 
			 
			  <div class="form-group col-md-3">
				<?php 
					//Query Encabezado
				$queryfol = mysqli_query($mysqli, "
				select concat(serie,(select folio from transaccion_medicamentos_enc order by  folio desc  LIMIT 1 ) + 1) as folio 
				from  transaccion_medicamentos_enc 
				order by serie desc limit 1 ")
						or die('Error '.mysqli_error($mysqli));
				$rw_folio=mysqli_fetch_array($queryfol); 
			
				?>		  
              <label for="fecha">Folio de salida</label>                
              <input type="text"  class="form-control"  value="<?php echo $rw_folio['folio']; ?>" readonly required>              
            </div>
             <br>
<br>			 
            <div class="form-group row">			
							
			</div>

			
              <div class="form-group col-md-6">    
              <label for="observacion_enc">Observacion General:</label>                
              <textarea id="observacion_enc" name="observacion_enc" class="form-control" required> 	</textarea>				  
            </div>		
          </form>
          <div class="clearfix"></div>
          <hr>
        </div>
        <div class="box-body">         
          <table id="dataTables1" class="table table-bordered table-striped table-hover">
           
            <thead>
              <tr>
                <th class="center"></th>
                <th class="center">No.</th>
                <th class="center">Codigo de Transación</th>
                <th class="center">Fecha</th>
                <th class="center">Codigo de producto</th>
                <th class="center">Productos</th>
				        <th class="center">Tipo</th>
                <th class="center">Cant.</th>
                <th class="center">Unidad</th>			
                <th class="center">Observaciones</th>
              </tr>
            </thead>
         
            <tbody>
            <?php  
            $no = 1;
           
            $query = mysqli_query($mysqli, "
              SELECT a.tipo_transaccion, a.codigo_transaccion,a.fecha,a.codigo,a.numero,b.codigo,b.nombre,b.unidad,a.observaciones
              FROM transaccion_medicamentos as a 
							   INNER JOIN medicamentos as b ON a.codigo=b.codigo and a.tipo_transaccion='Salida' and a.idenc is NULL
              ORDER BY codigo_transaccion DESC")
                or die('error '.mysqli_error($mysqli));

           
            while ($data = mysqli_fetch_assoc($query)) { 
              $transacc = $data['codigo_transaccion'];  //  Adrian
              $fecha    = $data['fecha'];
              $exp      = explode('-',$fecha);
              $fecha2   = $exp[2]."-".$exp[1]."-".$exp[0];

             
              echo "<tr>
                      <td width='30' class='center'>
                        <input type='checkbox' name='chk_" . $transacc . "' />
                      </td>
                      <td width='30' class='center'>$no</td>
                      <td width='100' class='center'>$data[codigo_transaccion]</td>
                      <td width='80' class='center'>$fecha</td>
                      <td width='80' class='center'>$data[codigo]</td>
                      <td width='200'>$data[nombre]</td>
					            <td width='80' class='center'>$data[tipo_transaccion]</td>
                      <td width='100' align='right'>$data[numero]</td>
                      <td width='80' class='center'>$data[unidad]</td>					  
                      <td width='80' class='center'>$data[observaciones]</td>
                    </tr>";
              $no++;
            }
            ?>
            </tbody>
          </table>
        </div><!-- /.box-body -->
      </div><!-- /.box -->
    </div><!--/.col -->
  </div>   <!-- /.row -->
</section><!-- /.content -->

<script type="text/javascript">
  function fnSelect() {
    var fecha = $('#fecha').val();
    var nombre = $('#nombre').val();
	var observacion_enc = $('#observacion_enc').val();

    var sel = "";
    //get selected items
    $.each($("input[type=checkbox]:checked"), function(){
      sel += $(this).attr('name') + "|";
    });

    //remove last character
    if(sel != "")
      sel = sel.substring(0, sel.length - 1);
    
    //set URL default
    $('#lnkPrint').attr("href", "modules/trade/medicines_consulta/print.php");

    //send to print PDF
    var url = $('#lnkPrint').attr("href") + '?sel=' + sel;
    url = url + '&fecha=' + fecha + '&nombre=' + nombre + '&observacion_enc=' + observacion_enc;
    $('#lnkPrint').attr("href", url);

    window.location = "main.php?module=medicines_consulta";
  }
</script>
