<ul class="list-group">
    <li class="list-group-item">
        <form method="get">
            <div class="row mini-labels">
                <div class="col-12 col-md-4">
                </div>
                <div class="col-12 col-md-2">
                    <label>Estado</label>
                    <select name ="estado" class="form-control select-id-estado" onchange="this.form.submit()">
                    <?php
                        $estados = [
                            1 => 'Publicados',
                            0 => 'No Publicados'
                        ];
                        foreach($estados as $k => $v) {
                            $selected = '';
                            if ($estado == $k) {
                                $selected =' selected="selected"';
                            }
                            echo '<option value="' . $k . '"' . $selected . '>' . $v . '</option>';
                        }
                    ?>
                    </select>
                </div>
                <div class="col-12 col-md-2">
                    <label>Stock</label>
                    <select name ="stock" class="form-control select-stock" onchange="this.form.submit()">
                    <?php
                        $stocks = [
                            0 => 'Todos',
                            1 => 'disponibles',
                            2 => 'Agotados'
                        ];
                        foreach($stocks as $k => $v) {
                            $selected = '';
                            if ($stock == $k) {
                                $selected =' selected="selected"';
                            }
                            echo '<option value="' . $k . '"' . $selected . '>' . $v . '</option>';
                        }
                    ?>
                    </select>
                </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()">
                    <option value="0">Todas</option>
                        <?php
                            foreach($categorias as $k => $v) {
                                $selected = '';
								if ($k != 7 && $k != 7) {
									if (!empty($categoria)) {
										if ($categoria == $k) {
											$selected =' selected="selected"';
										}
									}
									echo '<option value="' . $k . '"' . $selected . '>' . $v . '</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>
        </form>
    </li>
<?php
if(!empty($rows)) {
    ?>
    <li class="list-group-item">
        <h2>Productos <?= $linkActivo['section']; ?> (<?= count($rows); ?> productos)</h2>
        <div class="table-responsive">
            <table class="table table-striped">
                <thead>
                    <tr>
                    <th class="text-center">Prestashop</th>
                        <th>Producto</th>
                        <th class="text-center">Disponibilidad</th>
                        <th class="text-center">Disponibles</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">Editar</th>
                    </tr>
                </thead>
                <tbody>
                    <?php

                    foreach($rows 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>
                                <?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>
                                <?php /*<span class="badge badge-pill badge-primary"><?php echo $rankings[$p['id_ranking']]?></span>*/ ?>
                                
                            </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"><?php echo danger_badge($p['ps_disponibles'], 'mayor', $p['disponibles']); ?></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">
                                <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">
        <h2>Productos <?= $linkActivo['section']; ?> (0 productos)</h2>
        <p><strong>No se encontraron productos</strong></p>
    </li>
    <?php
}
echo '</ul>';
?>

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