<ul class="list-group">
<li class="list-group-item">
	<form method="get">
		<div class="row mini-labels">
			<div class="col-0 col-md-10">
			</div>
			<div class="col-12 col-md-2">
				<label>Agrupar por:</label>
				<select name ="tipo" class="form-control select-id-tipo" onchange="this.form.submit()">
					<option value="0" <?php echo ($tipo == 0) ? 'selected="selected"' : '';?>>Por producto</option>
					<option value="1" <?php echo ($tipo == 1) ? 'selected' : '';?>>Por fecha</option>
				</select>
			</div>
		</div>
	</form>
</li>
<?php
if ($tipo == 0) {
	if(!empty($ps_productos)) {
		?>
		<li class="list-group-item">
		<h4>Total de Alertas: <?php echo $total_alertas;?><h4>
	</li>
		<?php
		foreach($ps_productos as $k => $p) {
			?>
			<li class="list-group-item">
				<p>
					<span class="badge badge-pill badge-primary ps-id-product ver-ps" data-ps-id="<?php echo $k;?>"><?php echo $k;?></span> 
					<a target="_blank" href="/productos/detalle/<?php echo $p['id'];?>"><span class="nombre"><?php echo $p['name'];?></span></a> 
					<span class="badge badge-pill badge-success clickable" data-toggle="collapse" href="#collapse<?php echo $k;?>" title="Total Esperando"><?php echo count($p['mails']);?></span>
				</p>
				<div class="collapse" id="collapse<?php echo $k; ?>">
					<table class="table">
						<tr>
							<th>Email</th>
							<th>Nombre</th>
							<th>Fecha</th>
						</tr>
					<?php
					foreach($p['mails'] as $m) {
						echo '<tr>';
						echo '<td>' . $m['mail'] . '</td><td>' . $m['nombre'] . '</td><td>' . fecha($m['fecha']) . '</td>';
						echo '</tr>';
					}
					?>
					</table>
				</div>
			</li>
			<?php
		}
	}
	else {
		?>
		<li class="list-group-item">A&uacute;n no hay productos esperados</li>
		<?php
	}
}
if ($tipo == 1) {
	if(!empty($alertas)) {
		?>
		<li class="list-group-item">
			<table class="table">
				<tr>
					<th>Producto</th>
					<th>Email</th>
					<th>Nombre</th>
					<th>Fecha</th>
				</tr>
			<?php
			foreach($alertas as $alerta) {
				?>
				<tr>
					<td>
						<span class="badge badge-pill badge-primary ps-id-product ver-ps" data-ps-id="<?php echo $alerta['id_product'];?>"><?php echo $alerta['id_product'];?></span> 
						<a target="_blank" href="/productos/detalle/<?php echo $alerta['id'];?>"><span class="nombre"><?php echo $alerta['name'];?></span></a>
					</td>
					<?php
				echo '<td>' . $alerta['mail'] . '</td>';
				echo '<td>' . $alerta['nombre'] . '</td>';
				echo '<td>' . fechaHora($alerta['fecha']) . '</td>';
				echo '</tr>';
			}
			?>
			</table>
		</li>
	<?php
	}
}
?>
</ul>

<script>
$(document).ready(function() {
	$(document).on('click', '.ps-id-product', function(){
	//al hacer click en badge ps id debe abrir una nueva pestaña con la url de ps
		var ps_id = $(this).data('ps-id');
		var product_token = $('#psProductToken').val();
		var url = 'https://dementegames.cl/admin161m0dq8h/index.php/product/form/' + ps_id + '?_token=' + product_token;
		window.open(url, '_blank');
	});
});
</script>