<ul class="list-group">
	<li class="list-group-item">
	<ul class="nav nav-pills alt-pills float-right mini-labels">
			<form method="get">

				<li class="nav-item float-left mr-3">
					<label>Ordenar por:</label>
					<select name ="t" class="form-control select-tipo-ranking" onchange="this.form.submit()">
						<?php
							if ($t == 1) {
								$t1 = ' selected="selected"';
								$t2 = '';
							}
							if ($t == 2) {
								$t1 = '';
								$t2 = ' selected="selected"';
							}

						?>
						<option value="1" <?php echo $t1; ?>>Unidades</option>
						<option value="2" <?php echo $t2; ?>>Monto</option>
					</select>
				</li>

				<li class="nav-item float-left mr-3">
					<label>Proveedor</label>
					<select name ="p" class="form-control select-id-proveedor" onchange="this.form.submit()">
					<option value="0">Todos</option>
						<?php
							foreach($distribuidores as $k => $v) {
								$selected = '';
								if (!empty($p)) {
									if ($p == $k) {
										$selected =' selected="selected"';
									}
								}
								echo '<option value="' . $k . '"' . $selected . '>' . $v . '</option>';
							}
						?>
					</select>
				</li>

				<li class="nav-item float-left mr-3">
					<label>Categoría</label>
					<select name ="ct" class="form-control select-id-categoria" onchange="this.form.submit()">
					<option value="0">Todas</option>
						<?php
							foreach($categorias as $k => $v) {
								$selected = '';
								if (!empty($ct)) {
									if ($ct == $k) {
										$selected =' selected="selected"';
									}
								}
								echo '<option value="' . $k . '"' . $selected . '>' . $v . '</option>';
							}
						?>
					</select>
				</li>

				<li class="nav-item float-left mr-3">
					<label>Canal de venta</label>
					<select name ="c" class="form-control select-canal" onchange="this.form.submit()">
						<?php
							foreach ($canales as $k => $v) {
								$selected = '';
								if (!empty($c)) {
									if ($c == $k) {
										$selected =' selected="selected"';
									}
								}
								echo '<option value="' . $k . '"' . $selected . '>' . $v . '</option>';
							}
						?>
					</select>
				</li>

				<li class="nav-item float-left mr-3">
					<label>Periodo</label>
					<select name ="m" class="form-control select-meses-ventas" onchange="this.form.submit()">
						<?php
							foreach ($meses_ventas as $k => $v) {
								$selected = '';
								if (!empty($m)) {
									if ($m == $v) {
										$selected =' selected="selected"';
									}
								}
								echo '<option value="' . $v . '"' . $selected . '>' . mtt($v) . '</option>';
							}
						?>
					</select>
				</li>

			</form>
		</ul>
	</li>
<?php
if(!empty($vendidos['data'])) {
	?>
	<li class="list-group-item">
		<h4 class="char"><?php echo mtt($m); ?><?php if (in_array($_SESSION['usuario']['perfil'], [1, 2, 3])) echo ' - Monto: ' . clp($vendidos['monto']) . ' - Margen: ' . $vendidos['margen'] . '%'; ?></h4>
		<?php 
			if ($_SESSION['usuario']['perfil'] == 1) {
				?>
				<table class="table table-striped table-slim">
				<thead>
					<tr>
						<th>Tipo</th>
						<th>Neto</th>
						<th>IVA</th>
						<th>Total</th>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td>Comprado</td>
						<td><?php echo clp($vendidos['costo_neto']); ?></td>
						<td><?php echo clp($vendidos['costo_iva']); ?></td>
						<td><?php echo clp($vendidos['costo_bruto']); ?></td>
					</tr>
					<tr>
						<td>Vendido</td>
						<td><?php echo clp($vendidos['monto_neto']); ?></td>
						<td><?php echo clp($vendidos['monto_iva']); ?></td>
						<td><?php echo clp($vendidos['monto']); ?></td>
					</tr>
					<tr>
						<td>Diferencia</td>
						<td><?php echo clp($vendidos['monto_neto'] - $vendidos['costo_neto']); ?></td>
						<td><?php echo clp($vendidos['monto_iva'] - $vendidos['costo_iva']); ?></td>
						<td><?php echo clp($vendidos['monto'] - $vendidos['costo_bruto']); ?></td>
					</tr>
				<?php 
			}
		?>
		<div class="table-responsive">
			<table class="table table-striped table-slim">
				<thead>
					<tr>
						<th class="text-center">Posición</th>
						<th class="text-center">Prestashop</th>
						<th>Producto</th>
						<th class="text-center">Ranking</th>
						<th class="text-center">Vendidos</th>
						<th class="text-center">Monto</th>
						<th class="text-center">Disponibilidad</th>
						<th class="text-center">Disp.</th>
						<th class="text-center">Bod.</th>
						<th class="text-center">Capi</th>
						<th class="text-center disponibles-1601">1601</th>
						<th class="text-center disponibles-ld">LD</th>
						<th class="text-center">BLD</th>
						<th class="text-center disponibles-ds">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">Full</th>
						<th class="text-center">Editar</th>
					</tr>
				</thead>
				<tbody>
					<?php
					$posicion = 0;
					foreach($vendidos['data'] as $p) {
						$posicion++;
						?>
						<tr>
							<td class="text-center"><strong><?php echo ($posicion + (($pagina - 1) * $cantidad)); ?></strong></td>
							<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>
								<?php
								if (!empty($ps_imagenes[$p['ps_id']])) {
									echo ps_image($ps_imagenes[$p['ps_id']]);
								} 
								?>
								<a target="_blank" href="/productos/detalle/<?php echo $p['id']; ?>">
									<span class="nombre"><?php echo $p['nombre']; ?></span>
								</a>
							</td>
							<td class="text-center"><span class="badge badge-pill badge-primary"><?php echo $rankings[$p['id_ranking']]?></span></td>
							<td class="text-center"><?php echo $p['cantidad']; ?></td>
							<td class="text-center"><?php echo clp($p['total']); ?></td>
							<td class="text-center">
								<?php
									if ($p['disponibles'] > 0) {
										echo '<span class="badge badge-pill badge-success">Disponible</span>';
										$badgeDisponibles = 'success';
									}
									else {
										if ($p['available_now'] != '') {
											echo '<span class="badge badge-pill badge-warning">Preventa</span>';
											$badgeDisponibles = 'warning';
										}
										else {
											echo '<span class="badge badge-pill badge-danger">Agotado</span>';
											$badgeDisponibles = 'danger';
										}
									}
								?>
							</td>
							<td class="text-center"><span class="badge badge-pill badge-<?php echo $badgeDisponibles;?>"><?php echo $p['disponibles']; ?></span></td>
							

							<td class="text-center"><span class="tt" title="Disponibles Bodega"><?php echo $info_productos[$p['id']]['disponibles_almacen'][1]; ?></span></td>
							<td class="text-center"><span class="tt" title="Disponibles Capi Games"><?php echo $info_productos[$p['id']]['disponibles_almacen'][10]; ?></span></td>

							<?php
								// Si disponibles en tienda está en 0 y hay productos disponibles se marca en rojo
								$disponibles1601 = $info_productos[$p['id']]['disponibles_almacen'][5];
								$disponiblesLD = $info_productos[$p['id']]['disponibles_almacen'][8];
								$disponiblesDS = $info_productos[$p['id']]['disponibles_almacen'][12];
								if ($p['disponibles'] > 0) {
									$disponibles1601 = danger_badge($disponibles1601, 'menor', 1);
									$disponiblesLD = danger_badge($disponiblesLD, 'menor', 1);
									$disponiblesDS = danger_badge($disponiblesDS, 'menor', 1);
								}
							?>

							<td class="text-center disponibles-1601"><span class="tt" title="Disponibles 1601"><?php echo $disponibles1601; ?></span></td>
							<td class="text-center disponibles-ld"><span class="tt" title="Disponibles Los Dominicos"><?php echo $disponiblesLD; ?></span></td>
							<td class="text-center"><span class="tt" title="Disponibles Bodega Los Dominicos"><?php echo $info_productos[$p['id']]['disponibles_almacen'][9]; ?></span></td>
							<td class="text-center disponibles-ds"><span class="tt" title="Disponibles Drugstore"><?php echo $disponiblesDS; ?></span></td>

							<td class="text-center"><span class="tt" title="Disponibles PS"><?php echo danger_badge($p['ps_disponibles'], 'mayor', $p['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"><span class="tt" title="Disponibles ' . $marketplaces_detalles[$mp['id']]['abreviacion'] . '">' . danger_badge($p['marketplaces'][$mp['id']]['stock'], 'mayor', $p['disponibles']) . '</td>';
										}
										else {
											echo '<td class="text-center"></td>';
										}
									}
								}
							?>
							<td class="text-center"><span class="tt" title="Disponibles Full"><?php echo $info_productos[$p['id']]['disponibles_almacen'][7]; ?></span></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>
								<button type="button" class="btn btn-primary btn-xs d-lg-inline tt" data-toggle="modal" data-target="#modalStockAlmacenes" data-id="<?php echo $p['id']; ?>" title="Editar Stocks">
									<i class="fas fa-list"></i>
								</button>
							</td>
						</tr>
					<?php
					}
					?>
				</tbody>
			</table>
			<div class="row paginacion">
				<div class="info col-12 col-md-6">
					<?php echo $paginacion['info']; ?>
				</div>
				<div class="col-12 col-md-6">
					<?php echo $paginacion['links']; ?>
				</div>
			</div>
		</div>
		
	</li>
	<?php
}
else {
	?>
	<li class="list-group-item"><strong>A&uacute;n no hay productos con ventas para este periodo</strong></li>
	<?php
} ?>
</ul>

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