<?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">-->
</head>

<section class="content-header">
  <h1>
    <i class="fa fa-sign-in icon-title"></i> Consulta Remisiones <?php echo $almacen_perfil ?>;
</section>

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

    <!--<?php
    $alerta = 0;
    if (isset($_GET['id'])) {
      $id = $_GET['id'];
      $queryDel = mysqli_query($mysqli, "UPDATE remision_encabezado SET status = 'C' WHERE id = $id")
                or die('error '.mysqli_error($mysqli));
      if($queryDel)
        $alerta = 1;
    }  

    if ($alerta == 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>
              Remision cancelada correctamente.
            </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="dataTables1" 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">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 REMISION</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.serie) AS serie,
                                    MAX(a.folio) AS folio,
                                    MAX(a.created_date) AS created_date,
                                    MAX(u.name_user) AS name_user,
                                    MAX(b.tipo_transaccion) 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
                                    remision_encabezado AS a
                                JOIN
                                    remision_detalle 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
                                WHERE
                                    tipo_transaccion = 'Remision'
                                GROUP BY
                                    a.folio  -- Agregado a.folio a la cláusula GROUP BY
                                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_rem_perfil-$data[folio]</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/remision/reprint.php?id=$data[id]' class='btn-circle' target='_blank'><i class='glyphicon glyphicon-print' data-toggle='tooltip' title='Reimprimir'></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>
                        					  
                      </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 src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
	<script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.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 remision?') == true) {
       var comentario = prompt("Cual es el motivo de la cancelacion", "");
        
				if(val == true && confirm('Procedemos a cancelar la remision con tu comentario') == true) {
				return true;
				} else {
				return false;
			} 
			
		}
         		
	   
	}
	
	function cancelarPrueba() {
 
	 if(confirm('Estas seguro de cancelar esta remision?'))
	 {
		 return true;
	 }
	 else
	 {
		 return false;
	 }

         	
	}
	
	function validarCancelacion()
	{
		
	}

function cancelarAjax(estatus,id)
{
	
	
	
	if(!validar(estatus))
	{
		if(estatus == 'CANCELADA'){
			alert("La remision ya fue cancelada");
		}else if(estatus = 'SURTIDA'){
			alert("La remision 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/remision/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=remision_consulta";
}	

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

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

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

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

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