<?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'];
$nombre_perfil = $data_perfil['nombre_empresa'];
$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>

  <!-- Content Header (Page header) -->
  <section class="content-header">
    <h4 class="page-header">
                        OPCIONES PRINCIPALES INVENTARIO  <?php echo $data_perfil['almacen'];?><small>seleccione la categoría <code>!seleccione correctamente¡</code><code></code></small>
                   </h4>
    
  </section>
  
  <!-- Main content -->
  <section class="content">
    <div class="row">
      <div class="col-lg-12 col-xs-12">
        <div class="alert alert-info alert-dismissable">
          <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
          <p style="font-size:15px">
            <i class="icon fa fa-user"></i> Bienvenido <strong><?php echo $_SESSION['name_user']; ?></strong> a la aplicación de inventario de productos  <?php echo $data_perfil['almacen'];?>.
          </p>        
        </div>
      </div>  
    </div>
   
   <!--     Perdmisos para administrar MERCADOTECNIA    -->
   
   
    <!-- Small boxes (Stat box) 
	
	
	<div class="small-box bg-info">
  <div class="inner">
    <h3>150</h3>
    <p>New Orders</p>
  </div>
  <div class="icon">
    <i class="fas fa-shopping-cart"></i>
  </div>
  <a href="#" class="small-box-footer">
    More info <i class="fas fa-arrow-circle-right"></i>
  </a>
</div>-->
	
	
	
	
	
	
	
    <div class="row">
      <div class="col-lg-3 col-xs-6">
        <!-- small box -->
        <div style="background-color:#00c0ef;color:#fff" class="small-box bg-info">
          <div class="inner">
         <h3>Salidas</h3>
          <p>REIMPRIME</p>
          </div>
          <div class="icon">
          <i ></i>
          </div>
                <a href="?module=salida_consulta_almacen" class="small-box-footer" title="Imprimir" data-toggle="tooltip"><i class="fa fa-print"></i></a>
                <a class="small-box-footer"><i class="fa"></i> MAS INFORMACION <i class="fa fa-arrow-circle-right"></i></a>   
          </div>
          </div><!-- ./col -->
		

     <div class="row">
      <div class="col-lg-3 col-xs-6">
        <!-- small box -->
        <div style="background-color:#FF5233;color:#ffff" class="small-box">
          <div class="inner">
          <h3>Salidas</h3>
          <p>PDF&EXCEL</p>
          </div>
          <div class="icon">
          <i ></i>
          </div>
                <a href="?module=stock_report_folio" class="small-box-footer" title="Imprimir" data-toggle="tooltip"><i class="fa fa-print"></i></a>
                <a class="small-box-footer"><i class="fa"></i> MAS INFORMACION <i class="fa fa-arrow-circle-right"></i></a>   
          </div>
          </div><!-- ./col -->  
 		
		
	 <div class="row">
      <div class="col-lg-3 col-xs-6">
        <!-- small box -->
        <div style="background-color:#00a65a;color:#fff" class="small-box">
          <div class="inner">
        <h3>Entradas	</h3>
          <p>PDF&EXCEL</p>
          </div>
          <div class="icon">
          <i ></i>
          </div>
                <a href="?module=stock_report" class="small-box-footer" title="Imprimir" data-toggle="tooltip"><i class="fa fa-print"></i></a>
                <a class="small-box-footer"><i class="fa"></i> MAS INFORMACION <i class="fa fa-arrow-circle-right"></i></a>   
          </div>
          </div><!-- ./col -->
		  
		  <!-- Small boxes (Stat box) -->
		  
		  
		  <div class="row">
      <div class="col-lg-3 col-xs-6">
        <!-- small box -->
        <div style="background-color:#FFC133;color:#ffff" class="small-box">
          <div class="inner">
          <h3>Kardex</h3>
          <p>CONSULTA</p>
          </div>
          <div class="icon">
          <i ></i>
          </div>
                <a href="?module=view_kardex" class="small-box-footer" title="Imprimir" data-toggle="tooltip"><i class="fa fa-print"></i></a>
                <a class="small-box-footer"><i class="fa"></i> MAS INFORMACION <i class="fa fa-arrow-circle-right"></i></a>   
          </div>
          </div><!-- ./col -->
		  
		</div><!-- ./col -->  
	 </div><!-- ./col -->	  
    </div><!-- /.row -->
    </div><!-- /.row -->
  </section><!-- /.content -->
  
   <!--     Fin Perdmisos para administrar Exp    -->
   <!-- Main content -->
<section class="content">
  <div class="row">
    <div class="col-md-20">
	<h3 class="box-title center" >ALMACEN DE TRANSITO:</h3>
      <div class="box box-primary">
        <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">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 
                                a.id, 
                                a.serie, 
                                a.folio, 
                                a.created_date, 
                                u.name_user, 
                                b.tipo_transaccion AS Tipo,
                                CASE
                                    WHEN a.status = 'C' THEN 'CANCELADA'
                                    WHEN a.status = 'A' THEN 'ACTIVA'
                                    ELSE 'SURTIDA'
                                END AS status,
                                a.txtCorreo,
                                a.txtCancel,
                                a.txtMotivo,
                                c.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'
                                AND a.status = 'A'
                            GROUP BY 
                                a.id, a.serie, a.folio, a.created_date, u.name_user, b.tipo_transaccion,
                                a.txtCorreo, a.txtCancel, a.txtMotivo, c.nombre
                            ORDER BY 
                                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>";
            }
            ?>
            </tbody>
          </table>
		  
        </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) {
    if(status == 'C') {
      alert("La remision ya fue cancelada");
      return false;
    }
    if(status == 'S') {
      alert("La remision ya fue surtida");
      return false;
    }  
    return true;
	
  }
  
    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(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 comentario;
				} else {
				return '';
			} 
			
		}
         		
	   
	}

function cancelarAjax(estatus,id)
{
	
	//console.log(estatus+' :: '+id); return;
	let texto = cancelarPrueba(estatus);
	//console.log(texto); 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){
			//console.log(response); return;
			if(response != ''){
				$('#alert-msj').html(response);
				setTimeout(refresh, 1500); 
				
				//alert('Cancelado correctamente!');
			}else{
				alert('Error!');
			}
		},
		error: function(err){
			console.log(err);
		}
	})
}

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

  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>   
   
 