<ul class="list-group">
	<li class="list-group-item">
		<div class="row">
			<div class="col-12 col-md-4">
				<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#modalTraspasoForm" data-id="0"><i class="fas fa-plus"></i> Nuevo Traspaso</h4>
			</div>
			<div class="col-12 col-md-8">
				<ul class="nav nav-pills alt-pills float-right mini-labels">
					<form method="get">
						<li class="nav-item float-left mr-3">
							<label>Estado</label>
							<select name ="estado" class="form-control select-estado" onchange="this.form.submit()">
								<option value="todos"<?php echo ($estado == 'todos') ? ' selected="selected"' :'';?>>Todos</option>
								<option value="0"<?php echo ($estado == '0') ? ' selected="selected"' :'';?>>Borrador</option>
								<option value="1"<?php echo ($estado == '1') ? ' selected="selected"' :'';?>>Solicitado</option>
								<option value="2"<?php echo ($estado == '2') ? ' selected="selected"' :'';?>>En Preparación</option>
								<option value="3"<?php echo ($estado == '3') ? ' selected="selected"' :'';?>>Preparado</option>
								<option value="4"<?php echo ($estado == '4') ? ' selected="selected"' :'';?>>Completado</option>
							</select>
						</li>
						<li class="nav-item float-left mr-3">
							<label>Origen</label>
							<select name ="origen" class="form-control select-origen" onchange="this.form.submit()">
								<option value="0">Todos</option>
								<?php
									foreach ($almacenes as $k => $v) {
										$selected = '';
										if (!empty($origen)) {
											if ($origen == $k) {
												$selected =' selected="selected"';
											}
										}
										echo '<option value="' . $k . '"' . $selected . '>' . $v . '</option>';
									}
								?>
							</select>
						</li>
						<li class="nav-item float-left mr-3">
							<label>Destino</label>
							<select name ="destino" class="form-control select-destino" onchange="this.form.submit()">
								<option value="0">Todos</option>
								<?php
									foreach ($almacenes as $k => $v) {
										$selected = '';
										if (!empty($destino)) {
											if ($destino == $k) {
												$selected =' selected="selected"';
											}
										}
										echo '<option value="' . $k . '"' . $selected . '>' . $v . '</option>';
									}
								?>
							</select>
						</li>
						<li class="nav-item float-left mr-3">
							<label>Solicitante</label>
							<select name ="solicitante" class="form-control select-solicitante" onchange="this.form.submit()">
								<option value="0">Todos</option>
								<?php
									foreach ($usuariosActivos as $k => $v) {
										$selected = '';
										if (!empty($solicitante)) {
											if ($solicitante == $k) {
												$selected =' selected="selected"';
											}
										}
										echo '<option value="' . $k . '"' . $selected . '>' . $v . '</option>';
									}
								?>
							</select>
						</li>
					</form>
				</ul>
			</div>
		</div>
	</li>
	<li class="list-group-item">
		<div class="table-responsive">
			<table class="table table-striped lista-traspasos">
				<thead>
					<tr>
						<th>ID</th>
						<th>Descripción</th>
						<?php /*<th>Fecha Solicitud</th> */ ?>
						<th>Origen</th>
						<th>Destino</th>
						<th class="text-center">Productos</th>
						<th>Responsables</th>
						<th>Fechas</th>
						<th>Estado</th>
						<th style="min-width: 130px;">Acciones</th>
					</tr>
				</thead>
				<tbody>
					<?php
					if (!empty($traspasos)) {
						foreach($traspasos as $t) {
							?>
							<tr id="traspaso<?php echo $t['id']; ?>">
								<td><?php echo $t['id']; ?></td>
								<td class="descripcion"><?php echo $t['descripcion']; ?></td>
								<?php /* <td><?php echo fechaHora($t['created']); ?></td> */ ?>
								<td class='nombre-origen'><?php echo $almacenes[$t['id_origen']]; ?></td>
								<td class='nombre-destino'><?php echo $almacenes[$t['id_destino']]; ?></td>
								<td class="text-center">
									<?php echo $t['productos']; ?>
									<?php 
									if (in_array($_SESSION['usuario']['perfil'], [1, 2]) && $t['total_venta'] > 0) { ?>
										<br /><?php echo clp($t['total_venta']); ?>
									<?php
									}
									?>
								</td>
								<td class="traspaso-responsables">
									<span class="tt" title="Solicitante"><span class="badge badge-pill badge-info"><i class="fas fa-list"></i></span> <strong><?php echo $usuarios[$t['id_solicitante']]; ?></strong></span>
									<?php
										if (!empty($t['id_preparador'])) {
											echo '<br /><span class="tt badge-preparador" title="Preparador"><span class="badge badge-pill badge-primary"><i class="fas fa-box"></i></span> <strong>' . $usuarios[$t['id_preparador']] . '</strong></span>';
										}
										if (!empty($t['id_receptor'])) {
											echo '<br /><span class="tt" title="Receptor"><span class="badge badge-pill badge-success"><i class="fas fa-box-open"></i></span> <strong>' . $usuarios[$t['id_receptor']] . '</strong></span>';
										}
									?>
								</td>
								<td class="traspaso-fechas">
									<?php
										if (isDate($t['fecha_solicitud'])) {
											echo '<span class="tt" title="Solicitado"><span class="badge badge-pill badge-info"><i class="fas fa-list"></i></span> <strong>' . fechaHora($t['fecha_solicitud']) . '</strong></span>';
										}
										if (isDate($t['fecha_preparacion'])) {
											echo '<br /><span class="tt" title="Preparado"><span class="badge badge-pill badge-primary"><i class="fas fa-box"></i></span> <strong>' . fechaHora($t['fecha_preparacion']) . '</strong></span>';
										}
										if (isDate($t['fecha_completado'])) {
											echo '<br /><span class="tt" title="Completado"><span class="badge badge-pill badge-success"><i class="fas fa-box-open"></i></span> <strong>' . fechaHora($t['fecha_completado']) . '</strong></span>';
										}
									?>
								</td>
								<td><span class="badge badge-pill badge-status traspaso-status-<?php echo $t['estado_traspaso']; ?>"><?php echo $estadosTraspasos[$t['estado_traspaso']]; ?></span>
									<?php
										if ($t['diferencias_recepcion'] > 0) {
											echo '<br /><span class="badge badge-pill badge-danger">' . $t['diferencias_recepcion'] . ' diferencias</span>';
										}
									?>
								</td>
								<td>
									<button type="button" class="btn btn-info btn-sm tt mb-1" title="Ver" data-toggle="modal" data-target="#modalTraspasoForm" data-id="<?php echo $t['id']; ?>" data-accion="ver">
										<i class="fas fa-eye"></i>
									</button>
									<?php
									// En estado en borrador solo permite seguir creando al solicitante
									if ($t['estado_traspaso'] == 0 && $t['id_solicitante'] == $_SESSION['usuario']['id']) {
										?>
										<button type="button" class="btn btn-primary btn-sm tt mb-1 btn-editar" title="Preparar" data-toggle="modal" data-target="#modalTraspasoForm" data-id="<?php echo $t['id']; ?>" data-accion="editar">
											<i class="fas fa-pencil-alt"></i>
										</button>
									<?php
									}
									if ($t['estado_traspaso'] == 1) {
										?>
										<button type="button" class="btn btn-primary iniciar-preparacion btn-sm tt mb-1 btn-editar" title="Empezar a Preparar"  data-id="<?php echo $t['id']; ?>" data-accion="editar">
											<i class="fas fa-box"></i>
										</button>
									<?php
									}
									// En estado en preparación solo permite seguir preparando al preparador
									if ($t['estado_traspaso'] == 2 && $t['id_preparador'] == $_SESSION['usuario']['id']) {
										?>
										<button type="button" class="btn btn-primary btn-sm tt mb-1 btn-editar" title="Continuar Preparación" data-toggle="modal" data-target="#modalTraspasoForm" data-id="<?php echo $t['id']; ?>" data-accion="editar">
											<i class="fas fa-box"></i>
										</button>
										<button type="button" class="btn btn-primary btn-sm tt mb-1 btn-liberar" title="Liberar Preparación" data-id="<?php echo $t['id']; ?>" data-accion="liberar">
											<i class="fas fa-undo"></i>
										</button>
									<?php
									}

									if ($t['estado_traspaso'] == 3) {
										?>
										<button type="button" class="btn btn-primary btn-sm tt mb-1 btn-editar" title="Empezar a Validar" data-toggle="modal" data-target="#modalTraspasoForm" data-id="<?php echo $t['id']; ?>" data-accion="editar">
											<i class="fas fa-check"></i>
										</button>
									<?php
									}

									if ($t['estado_traspaso'] != 4 && $t['estado_traspaso'] != 5) {
										if ($t['id_solicitante'] == $_SESSION['usuario']['id'] || in_array($_SESSION['usuario']['perfil'], [1, 2, 3])) {
										?>
											<button type="button" class="btn btn-danger btn-sm tt mb-1 btn-cancelar" title="Cancelar" data-id="<?php echo $t['id']; ?>" data-accion="cancelar">
												<i class="fas fa-times"></i>
											</button>
										<?php
										}
									}

									if (
										$t['estado_traspaso'] != 4 &&
										$t['estado_traspaso'] != 5 &&
										in_array($_SESSION['usuario']['perfil'], [1, 2])
									) {
									?>
										<button 
											type="button" 
											class="btn btn-warning btn-sm tt mb-1 btn-cambiar-destino" 
											title="Cambiar Destino"
											data-id="<?php echo $t['id']; ?>"
											data-destino="<?php echo $t['id_destino']; ?>"
										>
											<i class="fas fa-exchange-alt"></i>
										</button>
									<?php
									}
									?>
									<a href="/traspasos/imprimir/<?php echo $t['id']; ?>" target="_blank" class="btn btn-outline-secondary btn-sm tt mb-1" title="imprimir"><i class="fas fa-print"></i></a>
								</td>
							</tr>
						<?php
						}
					}
					else {
						echo '<tr><td colspan="9" class="text-center">No hay traspasos</td></tr>';
					}
					?>
				</tbody>
			</table>
		</div>
		<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>
	</li>
</ul>
<?php $this->load->view('traspasos/modal_form'); ?>
<div class="modal fade" id="modalCambiarDestino" tabindex="-1">
	<div class="modal-dialog">
		<div class="modal-content">

			<div class="modal-header">
				<h5 class="modal-title">Cambiar destino</h5>
				<button type="button" class="close" data-dismiss="modal">
					<span>&times;</span>
				</button>
			</div>

			<div class="modal-body">
				<div class="alert alert-danger div-error-destino" style="display:none;"></div>
				<input type="hidden" id="cambiarDestinoIdTraspaso">

				<label>Destino</label>

				<select id="cambiarDestinoSelect" class="form-control">
					<?php
					foreach ($almacenes as $k => $v) {
						echo '<option value="' . $k . '">' . $v . '</option>';
					}
					?>
				</select>

			</div>

			<div class="modal-footer">
				<button type="button" class="btn btn-secondary" data-dismiss="modal">
					Cerrar
				</button>

				<button type="button" class="btn btn-primary btn-guardar-cambio-destino">
					Guardar
				</button>
			</div>

		</div>
	</div>
</div>
<script src="<?php echo js('traspasos.js'); ?>"></script>