<ul class="list-group">
	<li class="list-group-item">
		<form method="get">
			<div class="row mini-labels">
			
				<div class="col-12 col-md-6">
					<br />
					<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#modalProducto" data-id="0">
						<i class="fas fa-plus"></i> Agregar
					</button>
				</div>

				<div class="col-12 col-md-2">
					<label>Categoría</label>
					<select name ="categoria" class="form-control select-id-categoria" onchange="this.form.submit()">
					<?php
						foreach ($filtrosCategorias as $k => $v) {
							$selected = '';
							if ($categoria == $k) {
								$selected =' selected="selected"';
							}
							echo '<option value="' . $k . '"' . $selected . '>' . $filtrosCategorias[$k] . '</option>';
						}
					?>
					</select>
				</div>

				<div class="col-12 col-md-2">
					<label>Proveedor</label>
					<select name ="proveedor" class="form-control select-id-proveedor" onchange="this.form.submit()">
					<option value="0">Todos</option>
						<?php
							foreach($distribuidores as $k => $v) {
								$selected = '';
								if (!empty($proveedor)) {
									if ($proveedor == $k) {
										$selected =' selected="selected"';
									}
								}
								echo '<option value="' . $k . '"' . $selected . '>' . $v . '</option>';
							}
						?>
					</select>
				</div>
				
				<div class="col-12 col-md-2">
					<label>Disponibildad</label>
					<select name ="estado" class="form-control select-estado" onchange="this.form.submit()">
					<option value="0">Todos</option>
						<?php
							foreach($estados as $k => $v) {
								$selected = '';
								if (!empty($estado)) {
									if ($estado == $k) {
										$selected =' selected="selected"';
									}
								}
								echo '<option value="' . $k . '"' . $selected . '>' . $v . '</option>';
							}
						?>
					</select>
				</div>
			</div>
		</form>
	</li>
<?php
if(!empty($productos)) {
	?>
	<li class="list-group-item">
		<h4 class="char"><?php echo count($productos) . ' productos encontrados'; ?></h4>
		<div class="table-responsive">
			<table class="table table-slim">
				<thead>
					<tr>
						<th class="text-center">Id PS</th>
						<th class="text-center">VDM</th>
						<th>Producto</th>
						<th>Categoría</th>
						<th>Proveedor</th>
						<th class="text-center">Ranking</th>
						<th class="text-center">Estado</th>
						<th class="text-center">Última Venta</th>
						<th class="text-center">Comp.</th>
						<th class="text-center">Vend.</th>
						<th class="text-center">Disp.</th>
						<th class="text-center">Bodega</th>
						<th class="text-center">1601</th>
						<th class="text-center">LD</th>
						<th class="text-center">DS</th>
						<th class="text-center">PS</th>
						<?php
							if (!empty($marketplaces_detalles)) {
								foreach($marketplaces_detalles as $mp) {
									echo '<th class="text-center">' . $mp['abreviacion'] . '</th>';
								}
							}
						?>
						<th class="text-center"></th>
					</tr>
				</thead>
				<tbody>
					<?php
					foreach($productos as $p) {
						?>
						<tr>
							<td class="text-center">
								<?php
									if ($p['ps_id'] > 0) {
										?>
										<span class="badge badge-pill badge-primary ps-id-product ver-ps" data-ps-id="<?php echo $p['ps_id']; ?>"><?php echo $p['ps_id']; ?></span>
									<?php
									}
								?>
							</td>
							<td class="text-center">
								<a target="_blank" href="/productos/detalle/<?php echo $p['id']; ?>"><span class="badge badge-pill badge-primary"><?php echo $p['id']; ?></span></a>
							</td>

							<td>
								<?php
								if (!empty($p['ps_imagen'])) {
									echo ps_image($p['ps_imagen']);
								} 
								?>
								<a target="_blank" href="/productos/detalle/<?php echo $p['id']; ?>">
									<span class="nombre"><?php echo $p['nombre']; ?></span>
								</a>
							</td>
							<td><?php echo $categorias[$p['id_categoria']]; ?></td>
							<td><?php echo $proveedores[$p['id_proveedor']]; ?></td>
							<td class="text-center stock"><?php echo $rankings[$p['id_ranking']]; ?></td>
							<td class="text-center disponibilidad border-right">
								<?php
									if ($p['disponibles'] > 0) {
										$estado = 'Disponible';
									}
									elseif ($p['available_now'] != '') {
										$estado = 'Preventa';
									}
									else {
										$estado = 'Agotado';
									}
									echo '<span class="badge badge-pill badge-' . $estado .'">' . $estado .'</span>';
								?>
							</td>
							<td class="text-center stock"><?php echo fecha($p['ultima_venta']); ?></td>
							<td class="text-center stock"><span class="badge badge-pill badge-info" data-toggle="tooltip" title="Comprados"><?php echo $p['comprados']; ?></span></td>
							<td class="text-center stock"><span class="badge badge-pill badge-primary border-right" data-toggle="tooltip" title="Vendidos"><?php echo $p['vendidos']; ?></span></td>

							<?php 
							$titleGeneral = 'Disponibles: ' . $p['disponibles'] . '<br />'
											. 'Reservados: ' .  $p['reservados'] . '<br />'
											. 'Stock Físico: ' .  $p['stock'] . '<br />';
							?>

							<td class="text-center stock tt" title="<?php echo $titleGeneral; ?>"><span class="badge badge-pill badge-success"><?php echo $p['disponibles']; ?></span></td>
							
							<?php
							$mostrados = [1, 5, 8, 12];
							foreach($mostrados as $idAlmacen) {
								$title = $almacenes_activos[$idAlmacen] . '<br />'
										. 'Disponibles: ' . $info_productos[$p['id']][$idAlmacen]['disponibles'] . '<br />'
										. 'Reservados: ' .  $info_productos[$p['id']][$idAlmacen]['reservados'] . '<br />'
										. 'Stock Físico: ' .  $info_productos[$p['id']][$idAlmacen]['stock'] . '<br />';
								echo '<td class="text-center bodega">';
								echo '<span class="tt" title="'.$title.'">' . $info_productos[$p['id']][$idAlmacen]['disponibles'] . '</span></td>';
							}
							?>

							<td class="text-center stock-ps">
								<span class="badge badge-pill badge-success" data-toggle="tooltip" title="Disponibles"><?php echo $p['ps_disponibles']; ?></span>
							</td>
							<?php
								if (!empty($marketplaces_detalles)) {
									foreach($marketplaces_detalles as $mp) {
										if (isset($p['marketplaces'][$mp['id']]['stock'])) {
											echo '<td class="text-center stock-ml border-right">' . danger_badge($p['marketplaces'][$mp['id']]['stock'], 'mayor', $p['disponibles']) . '</td>';
										}
										else {
											echo '<td class="text-center stock-ml border-right"></td>';
										}
									}
								}
							?>
							<td class="text-center">
								<button type="button" class="btn btn-primary btn-xs d-lg-inline" data-toggle="modal" data-target="#modalProducto" data-id="<?php echo $p['id']; ?>">
									<i class="fas fa-pencil-alt"></i>
								</button>
							</td>
						</tr>
					<?php
					}
					?>
				</tbody>
			</table>
		</div>
	</li>
	<?php
}
else {
	?>
	<li class="list-group-item"><strong>Sin productos para los filtros seleccionados</strong></li>
	<?php
}
echo '</ul>';
?>

<?php $this->load->view('productos/modal'); ?>
<script src="<?php echo js('productos.js'); ?>"></script>
