<?php
  //Query parametros perfil
$queryperfil = mysqli_query($mysqli, "SELECT * FROM perfil")
                                or die('error'.mysqli_error($mysqli));
$rows  = mysqli_num_rows($queryperfil);
$data_perfil  = mysqli_fetch_assoc($queryperfil);
$folio_perfil = $data_perfil['folio'];
$folio_rem_perfil = $data_perfil['folio2'];
$nombre_perfil = $data_perfil['nombre_empresa'];
$direccion_perfil = $data_perfil['direccion'];
$direccion_perfil = $data_perfil['direccion'];
$email_perfil = $data_perfil['email'];
$almacen_perfil = $data_perfil['almacen'];
$almacenista_perfil = $data_perfil['almacenista'];
  
  
?>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title></title>
	<!-- El CSS de DataTables -->
	<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/jszip-2.5.0/pdfmake-0.1.18/dt-1.10.12/af-2.1.2/b-1.2.2/b-colvis-1.2.2/b-flash-1.2.2/b-html5-1.2.2/b-print-1.2.2/cr-1.3.2/fc-3.2.2/fh-3.1.2/kt-2.1.3/r-2.1.0/rr-1.1.2/sc-1.4.2/se-1.2.0/datatables.min.css"/>
	<!-- El CSS de Bootstrap -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
	<!--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">

  
    <link rel="stylesheet" type="text/css" href="datatables/Responsive-2.2.7/css/responsive.bootstrap4.min.css"/>-->
  

</head>



<section class="content-header">
  <h1>
    <i class="fa fa-sign-in icon-title"></i> Consulta salidas de almacen <?php echo $almacen_perfil ?>;
</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>";
    }
    elseif ($_GET['alert'] == 2) {
      echo "<div class='alert alert-danger alert-dismissable'>
              <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
              <h4>  <i class='icon fa fa-warning'></i> Error!</h4>
              Favor de seleccionar registros.
            </div>";
    }     
    elseif ($_GET['alert'] == 3) {
      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>
              Registros cancelados correctamente.
            </div>";
    }
    elseif ($_GET['alert'] == 4) {
      $folio = '';
      if (!empty($_GET['folio']))
        $folio = $_GET['folio'];
      echo "<div class='alert alert-danger alert-dismissable'>
              <button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>
              <h4>  <i class='icon fa fa-warning'></i> Error!</h4>
              El folio ya se encuentra cancelado: $folio
            </div>";
			
    }   
//Query parametros perfil
				$queryperfil = mysqli_query($mysqli, "SELECT * FROM perfil")
												or die('error'.mysqli_error($mysqli));
				$rows  = mysqli_num_rows($queryperfil);
				$data_perfil  = mysqli_fetch_assoc($queryperfil);
				$folio_perfil = $data_perfil['folio'];
				$folio_rem_perfil = $data_perfil['folio2'];
				$nombre_perfil = $data_perfil['nombre_empresa'];
				$direccion_perfil = $data_perfil['direccion'];
				$direccion_perfil = $data_perfil['direccion'];
				$email_perfil = $data_perfil['email'];
				$almacen_perfil = $data_perfil['almacen'];
				$almacenista_perfil = $data_perfil['almacenista'];	
    ?>
	<div id="alert-msj">
	
	</div>
	

      <div class="box box-primary">
        <div class="box-body">
           <div class="box-body">    
          <div class="table-responsive">        
                       <table id="dataTables" class="table table-bordered table-striped table-hover">          
            <thead>
              <tr>      
			           <th class="center"style="color: #ffffff; background-color: #4682B4">ID</th>
						<th class="center"style="color: #ffffff; background-color: #4682B4">FOLIO</th> 
						<th class="center"style="color: #ffffff; background-color: #4682B4">REMISION</th>						
						<th class="center"style="color: #ffffff; background-color: #4682B4">GENERA</t>
                        <th class="center"style="color: #ffffff; background-color: #4682B4">RECIBE</th>
						 <th class="center"style="color: #ffffff; background-color: #4682B4">TIPO</th>
						<th class="center"style="color: #ffffff; background-color: #4682B4">MOTIVO SALIDA</th> 						
				       <th class="center"style="color: #ffffff; background-color: #4682B4">FECHA</th>
						<th class="center"style="color: #ffffff; background-color: #4682B4">MOTIVO CANCELACION</th>
						 <th class="center"style="color: #ffffff; background-color: #4682B4">STATUS</th>
						<th class="center"style="color: #ffffff; background-color: #4682B4">ACCIONES</th>
              </tr>
            </thead>         
            <tbody>
            <?php  
				$query = mysqli_query($mysqli, "
							SELECT
                                    MAX(a.id) AS id,
                                    MAX(a.folio) AS folio,
                                    MAX(d.folio) AS remision,
                                    MAX(a.created_date) AS created_date,
                                    MAX(u.name_user) AS name_user,
                                    CASE
                                        WHEN MAX(b.tipo_transaccion) = 'Salida' THEN 'SALIDA'
                                        WHEN MAX(b.tipo_transaccion) = 'Entrada' THEN 'ENTRADA'
                                        ELSE ''
                                    END AS Tipo,
                                    CASE
                                        WHEN MAX(a.status) = 'C' THEN 'CANCELADA'
                                        WHEN MAX(a.status) = 'A' THEN 'ACTIVA'
                                        ELSE 'SURTIDA'
                                    END AS status,
                                    MAX(a.txtCorreo) AS txtCorreo,
                                    MAX(a.txtCancel) AS txtCancel,
                                    MAX(a.txtMotivo) AS txtMotivo,
                                    MAX(c.nombre) AS nombre
                                FROM
                                    transaccion_medicamentos_enc AS a
                                JOIN
                                    transaccion_medicamentos AS b ON b.idenc = a.id
                                JOIN
                                    usuarios AS u ON b.created_user = u.id_user
                                JOIN
                                    correos AS c ON c.tipo = a.txtCorreo
                                JOIN
                                    remision_encabezado AS d ON d.idSalida = a.id
                                WHERE
                                    tipo_transaccion = 'Salida'
                                GROUP BY
                                    a.folio
                                ORDER BY
                                    MAX(a.id) DESC;")
                or die('error '.mysqli_error($mysqli));

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

              echo "<tr>
                      <td width='10' class='center'>$data[id]</td>                 
                      <td width='10' class='center'>$folio_perfil-$data[folio]</td>
					   <td width='10' class='center'>$folio_rem_perfil-$data[remision]</td>
					  <td width='10' class='center'>$data[name_user]</td>
                      <td width='80' class='center'>$data[nombre] $data[txtCorreo]</td>					  
					  <td width='80' class='center'>$data[Tipo]</td>
					  <td width='80' class='center'>$data[txtMotivo]</td>
                      <td width='30' class='center'>$fecha</td>
					  <td width='80' class='center'>$data[txtCancel]</td>
					   <td width='20' class='center'>$sta</td>
					  <td width='30' class='center'>
                      <a href='modules/trade/medicines_consulta_enc/print.php?id=$data[id]' class='btn-circle' target='_blank'><i class='glyphicon glyphicon-print' data-toggle='tooltip' title='Reimprimir'></i></a>
                      <!--<a href='?module=remision_consulta&id=$data[id]' class='btn-circle'><i class='glyphicon glyphicon-remove' data-toggle='tooltip' title='Cancelar' onclick='return cancelar(\"$data[status]\");'></i></a>
					  <a href='modules/trade/remision/surtir.php?id=$data[id]' target='_blank' class='btn-circle'><i class='glyphicon glyphicon-ok' data-toggle='tooltip' title='Surtir'  onclick='return surtir(\"$data[status]\");'></i></a>  -->   

						<button id='fwdbtn' style='color:red' type='button' class='btn-circle' id='en_cancelar' class='btn-circle'><i class='glyphicon glyphicon-remove' data-toggle='tooltip' title='Cancelar' onclick='cancelarAjax(\"$data[status]\",$data[id]);'></i></button>
                      <!--<a href='?module=remision_consulta&id=$data[id]' class='btn-circle'><i class='glyphicon glyphicon-remove' data-toggle='tooltip' title='Cancelar' onclick='return cancelar(\"$data[status]\");'></i></a>-->
					  <!--<a  class='btn-circle'><i class='glyphicon glyphicon-remove' data-toggle='tooltip' title='Cancelar' onclick='return cancelarAjax(\"$data[status]\");'></i></a>-->
                      					  
                      </td> 
					  
                    </tr>";
            }
            ?>
			
			<!-- <td width='20' class='center'>$data[serie]$data[folio]</td> 
			         <td width='80' class='center'>$data[name_user]</td>--> <!-- Este es el codigo con la columna de serie y folio que estaba arriba -->
            </tbody>
          </table>
          </div><!-- /.box-body -->
        </div><!-- /.box-body -->
      </div><!-- /.box -->
    </div><!--/.col -->
  </div>   <!-- /.row -->
</section><!-- /.content -->
<script language="javascript" src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
	<!-- El JavaScript de DataTables -->
	<script language="javascript" type="text/javascript" src="https://cdn.datatables.net/v/dt/jszip-2.5.0/pdfmake-0.1.18/dt-1.10.12/af-2.1.2/b-1.2.2/b-colvis-1.2.2/b-flash-1.2.2/b-html5-1.2.2/b-print-1.2.2/cr-1.3.2/fc-3.2.2/fh-3.1.2/kt-2.1.3/r-2.1.0/rr-1.1.2/sc-1.4.2/se-1.2.0/datatables.min.js"></script>
	<!-- El JavaScript de BootStrap -->
	<script language="javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
   

<script type="text/javascript">

  
  function validar(status) {
	let check = true;
	if(status == 'CANCELADA') {
	 
	  check = false;
	}
	if(status == 'SURTIDA') {
	 
	  check = false;
	}  
	return check;
  }
  
    function cancelar(status) {
     val = validar(status);
	 
	 if(val == true && confirm('Estas seguro de cancelar esta salida?') == true) {
       var comentario = prompt("Cual es el motivo de la cancelacion", "");
        
				if(val == true && confirm('Procedemos a cancelar la salida con tu comentario') == true) {
				return true;
				} else {
				return false;
			} 
			
		}
         		
	   
	}
	
	function cancelarPrueba() {
 
	 if(confirm('Estas seguro de cancelar esta salida?'))
	 {
		 return true;
	 }
	 else
	 {
		 return false;
	 }

         	
	}
	
	function validarCancelacion()
	{
		
	}

function cancelarAjax(estatus,id)
{
	
	
	
	if(!validar(estatus))
	{
		if(estatus == 'CANCELADA'){
			alert("La salida ya fue cancelada");
		}else if(estatus = 'SURTIDA'){
			alert("La salida ya fue surtida");
		}
		return;
	}
	
	
	if(!cancelarPrueba()){
		return;
	}
	
	var texto = prompt("Cual es el motivo de la cancelacion", "");
	
	if(texto == null)
	{
		return;
	}
	
	if(texto == ''){
		alert('El comentario esta vacio.');
		return;
	}
	
	$.ajax({
		url: 'modules/trade/medicines_consulta_enc/cancelar.php',
		data: 'comentario='+texto+'&id='+id,
		method: 'POST',
		success: function(response){
			
			if(response != ''){
				$('#alert-msj').html(response);
				setTimeout(refresh, 1500); 

			}else{
				alert('Error!');
			}
		},
		error: function(err){
			console.log(err);
		}
	})
}

function refresh(){
	window.location = "main.php?module=salida_consulta";
}	

  function surtir(status) {
    val = validar(status);
    if(val == true && confirm('Estas seguro de surtir esta salida?') == true) {
       setTimeout(retardo, 2000); 
       return true;
    } else {
      return false;
    } 
  }

  function retardo() {        
    window.location = "main.php?module=salida_consulta";
    document.location.reload();
  }

$(document).ready(function(){
  
  $("table").dataTable({"order": [[ 2, "desc" ]],
    
    rowCallback:function(row,data)
    {
      
      if(data[9] == "ACTIVA")
      {
        $($(row).find("td")[9]).css("background-color","Beige");
      }
      else if(data[9] == "SURTIDA"){
          $($(row).find("td")[9]).css("background-color","LightBlue");
      }
      else{
          $($(row).find("td")[9]).css("background-color","Pink");
      }
      
    }
	
    
  });
  
})

  </script>
<script >
$('#dataTables2').dataTable( 

  
{
	
    "drawCallback": function( settings ) {
         $('ul.pagination').addClass("pagination-sm");
		 
    }
	
	
});
	</script>