@extends(Auth::check() && Auth::user()->role->layout == 1 ? 'layouts.admin' : 'layouts.employee')
@section('head')
	<link rel="stylesheet" href="{{ asset('/plugins/datepicker/bootstrap-datepicker.css') }}">
	<style type="text/css">
		@media screen and (max-width: 767px){
			.table-responsive>.table>tbody>tr>td,
			.table-responsive>.table>tbody>tr>th,
			.table-responsive>.table>tfoot>tr>td,
			.table-responsive>.table>tfoot>tr>th,
			.table-responsive>.table>thead>tr>td,
			.table-responsive>.table>thead>tr>th { font-size:11.5px; }

			.table-responsive>.table>thead>tr>td.statusCol {padding-left:0; padding-right:0;}
		}
	</style>
@endsection

@section('content')
	<div class="content-wrapper">
		<section class="content-header">
			<h1>
				{{trans('general.reservations')}}
			</h1>
		</section>
		@if(Auth::user()->role->role_permission('delete_reservation'))
			<div class="row no-margin" >
				{!! Form::open(['route' => 'busqueda', 'method' => 'GET']) !!}
				<div class="col-md-2">
					<div class="form-group{{ $errors->has('boat_id') || $errors->has('boat_id') ? ' has-error' : '' }}">
						{!! Form::label('boat_id',  trans('general.boats')) !!}<br />
						{!! Form::select('boat_id', $barcos, null, ['class' => 'form-control']) !!}

						@if ($errors->has('boat_id'))
							<span class="help-block">
						        <strong>{{ $errors->first('boat_id') }}</strong>
						    </span>
						@endif
					</div>
				</div>
				<div class="col-md-2">
					<input type="hidden" name="_token" value="{{ csrf_token() }}">

					<div class="form-group{{ $errors->has('date_start') ? ' has-error' : '' }}">
						{!! Form::label('date_start', trans('general.date_start')) !!}
						<div class="input-group">
							<div class="input-group-addon">
								<i class="fa fa-calendar"></i>
							</div>
							{!! Form::text('date_start', null, ['class' => 'form-control date_start']) !!}

						</div>
						@if ($errors->has('date_start'))
							<span class="help-block">
								<strong>{{ $errors->first('date_start') }}</strong>
							</span>
						@endif
					</div>
				</div>
				<div class="col-md-2">
					<div class="form-group{{ $errors->has('schedule_id') || $errors->has('schedule_id') ? ' has-error' : '' }}">
						{!! Form::label('schedule_id',  trans('general.schedule')) !!}<br />
						{!! Form::select('schedule_id', $turno, null, ['class' => 'form-control']) !!}

						@if ($errors->has('schedule_id'))
							<span class="help-block">
                                <strong>{{ $errors->first('schedule_id') }}</strong>
                            </span>
						@endif
					</div>
				</div>
				<div class="col-md-2">
					<div class="form-group{{ $errors->has('status_id') || $errors->has('status_id') ? ' has-error' : '' }}">
						{!! Form::label('status_id',  trans('general.estado')) !!}<br />
						{!! Form::select('status_id', $estado, null, ['class' => 'form-control']) !!}

						@if ($errors->has('status_id'))
							<span class="help-block">
                                <strong>{{ $errors->first('status_id') }}</strong>
                            </span>
						@endif
					</div>
				</div>
				<div class="col-md-2">
					<div class="form-group mt-lg">
						{!! Form::label('boat',  trans('general.buscador')) !!}<br />
						{!! Form::submit(trans('general.buscar'), ['class' => 'form-control btn btn-info']) !!}
					</div>
					{!! Form::close() !!}
				</div>
			</div>
		@endif
		<section class="content">
			<div class="row">
				<div class="col-md-12">
					@include('alert.success')
					@include('alert.errorlist')
					@if(Auth::user()->role->role_permission('create_reservation') && Auth::user()->role->layout == 1 )
						<a href="{{ asset('/reservations/create') }}" type="button" class="btn btn-success btn-flat margin">	{{trans('general.nueva')}}  {{trans('general.reservation')}} </a>
					@endif
					@if(Auth::user()->role->layout == 1)
					<!-- a href="{{ asset('/credits/' . Auth::user()->id . '/listado' ) }}" type="button" class="btn btn-info btn-flat margin">Transferencia de créditos</a -->
					@endif
					<div class="box box-success">
						<div class="box-header">
							<h3 class="box-title">{{trans('general.lista')}} {{trans('general.reservations')}} </h3>
							<div class="box-tools">
								{!! Form::open(['url' => '/reservations/search', 'method' => 'get']) !!}
								<div class="input-group input-group-sm" style="width: 150px;">
									{!! Form::text('term', null, ['class' => 'form-control pull-right', 'placeholder' => 'Search']) !!}
									<div class="input-group-btn">
										<button type="submit" class="btn btn-default"><i class="fa fa-search"></i></button>
									</div>
								</div>
								{!! Form::close() !!}
							</div>
						</div>

						<div class="contenedorReservas" id="vMovil" style="display:none;">
							@foreach($reservations as $reservation)
								<div class="col-md-6">
									<div class="box box-default">
										<div class="box-header with-border">
											<h4 class="box-title">{{ date('d/m/Y', strtotime($reservation->date_start))}} - {{ $reservation->boat->name}}</h4>
										</div>
										<div class="box-body">
											@if (Auth::user()->role->layout == 1)
												<p>{{ $reservation->first_name }} {{ $reservation->last_name }}</p>
												<p>{{ $reservation->email }}</p>
											@endif
											<p>{{ $reservation->schedule->name }}</p>
											<p>
												@if($reservation->status_id == 1)
													<span class="label label-success">{{$reservation->status->name}}</span>
												@elseif($reservation->status_id == 2)
													<span class="label label-warning">{{$reservation->status->name}}</span>
												@elseif($reservation->status_id == 3)
													<span class="label label-danger">{{$reservation->status->name}}</span>
												@elseif($reservation->status_id == 4)
													<span class="label label-info">{{$reservation->status->name}}</span>
												@endif
											</p>
											<p>
												@if(Auth::user()->role->role_permission('edit_reservation') || Auth::user()->role->role_permission('delete_reservation'))
													<div class="btn-group">
														@if(Auth::user()->role->role_permission('edit_reservation'))
															<a href="{{ asset('reservations/'.$reservation->id.'/edit') }}" title="Edit" class="btn btn-default btn-flat btn-sm"><i class="fa fa-pencil"></i></a>
														@endif
														@if(false)
															Auth::user()->role->role_permission('delete_reservation')
															<a type="button" class="btn btn-default btn-flat btn-sm" title="Delete" data-toggle="modal" data-target="#deleteModal{{ $reservation->id }}"><i class="fa fa-trash"></i></a>
															<div id="deleteModal{{ $reservation->id }}" class="modal fade" role="dialog">
																<div class="modal-dialog">
																	<div class="modal-content">
																		<div class="modal-header">
																			<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
																			<h4 class="modal-title">{{trans('general.deleteconfirm')}}</h4>
																		</div>
																		<div class="modal-body">
											<p>{{trans('general.delete')}}</p>
										</div>
										<div class="modal-footer">
											<button type="button" class="btn btn-default btn-flat pull-left" data-dismiss="modal">{{trans('general.cancel')}}</button>
											{!! Form::open(['url' => ['reservations/'.$reservation->id], 'method' => 'delete']) !!}
											{!! Form::submit(trans('general.si'), ['class' => 'btn btn-success btn-flat']) !!}
											{!! Form::close() !!}
										</div>
									</div>
								</div>
						</div>
						@endif
					</div>
					@endif
					@if($reservation->status_id == 1)
						<a href="{{  asset('reservations/'. $reservation->id .'/checkin/') }}" class="btn btn-flat btn-sm btn-default" title="checkin"><i class="fa fa-hand-o-right"></i></a>
						<a href="{{  asset('reservations/'. $reservation->id .'/checkout/') }}" class="btn btn-flat btn-sm btn-default" title="checkout"><i class="fa fa-hand-o-left"></i></a>
					@endif
					@php
						$ahora = \Carbon\Carbon::now();
													$inicio = \Carbon\Carbon::createFromFormat('d/m/Y H:i:s', $reservation->date_start. ' '. $reservation->schedule->end_hour);

						$diff_in_days = $inicio->diffInDays($ahora);
					@endphp

					@if ($diff_in_days < 3)
						@if ($reservation->status_id == 2)
							<div style="display:block">
								{!! Form::open(['url' => ['reservations/confirmation'], 'method' => 'post']) !!}
								{!! Form::hidden("reservation_id", $reservation->id) !!}
								{!! Form::submit('Confirmar', ['class' => 'line-188 btn btn-success btn-flat']) !!}
								{!! Form::close() !!}
							</div>
						@endif
					@endif
					@if ($reservation->status_id == 2 || $reservation->status_id == 4)
						<div style="display:block">
							{!! Form::open(['url' => ['reservations/cancelation'], 'method' => 'post']) !!}
							{!! Form::hidden("reservation_id", $reservation->id) !!}
							{!! Form::submit('Cancelar', ['class' => 'line-188 btn btn-cancel btn-flat']) !!}
							{!! Form::close() !!}
						</div>
						@endif
						</p>
				</div>
			</div>
	</div>


	@endforeach
	</div>

	<div class="box-body table-responsive no-padding" id="vEscritorio" style="display:none;">
		<table class="table table-hover">
			<tr>
				@if (Auth::user()->role->layout == 1)
					<th>{{trans('general.nombre')}} </th>
					<th>{{trans('general.apellido')}} </th>
					<th>Email</th>
				@endif
				<th>{{trans('general.fecha')}}</th>
				<th>{{trans('general.boat')}} </th>
				<th>Horario</th>
				<th>{{trans('general.reservationsstatus')}}</th>

				@if(Auth::user()->role->role_permission('edit_reservation') || Auth::user()->role->role_permission('delete_reservation'))
					<th>{{trans('general.accion')}}</th>
				@endif
			</tr>
			@foreach($reservations as $reservation)
				<tr>
					@if (Auth::user()->role->layout == 1)
						<td>{{ $reservation->first_name }}</td>
						<td>{{ $reservation->last_name }}</td>
						<td>{{ $reservation->email }}</td>
					@endif
					<td>{{ $reservation->date_start}}</td>
					<td>{{ $reservation->boat->name}}</td>
					<td>{{ $reservation->schedule->name }}</td>
					<td class="statusCol">
						@if($reservation->status_id == 1)
							<span class="label label-success">{{$reservation->status->name}}</span>
						@elseif($reservation->status_id == 2)
							<span class="label label-warning">{{$reservation->status->name}}</span>
						@elseif($reservation->status_id == 3)
							<span class="label label-danger">{{$reservation->status->name}}</span>
						@elseif($reservation->status_id == 4)
							<span class="label label-info">{{$reservation->status->name}}</span>
						@endif
					</td>
					<td>
						@if(Auth::user()->role->role_permission('edit_reservation') || Auth::user()->role->role_permission('delete_reservation'))
							<div class="btn-group">
								@if(Auth::user()->role->role_permission('edit_reservation'))
									<a href="{{ asset('reservations/'.$reservation->id.'/edit') }}" title="Edit" class="btn btn-default btn-flat btn-sm"><i class="fa fa-pencil"></i></a>
								@endif
								@if(false)
									Auth::user()->role->role_permission('delete_reservation')
									<a type="button" class="btn btn-default btn-flat btn-sm" title="Delete" data-toggle="modal" data-target="#deleteModal{{ $reservation->id }}"><i class="fa fa-trash"></i></a>
									<div id="deleteModal{{ $reservation->id }}" class="modal fade" role="dialog">
										<div class="modal-dialog">
											<div class="modal-content">
												<div class="modal-header">
													<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
													<h4 class="modal-title">{{trans('general.deleteconfirm')}}</h4>
												</div>
												<div class="modal-body">
													<p>{{trans('general.delete')}}</p>
												</div>
												<div class="modal-footer">
													<button type="button" class="btn btn-default btn-flat pull-left" data-dismiss="modal">{{trans('general.cancel')}}</button>
													{!! Form::open(['url' => ['reservations/'.$reservation->id], 'method' => 'delete']) !!}
													{!! Form::submit(trans('general.si'), ['class' => 'btn btn-success btn-flat']) !!}
													{!! Form::close() !!}
												</div>
											</div>
										</div>
									</div>
								@endif
							</div>
						@endif
						@if($reservation->status_id == 1)
							<a href="{{  asset('reservations/'. $reservation->id .'/checkin/') }}" class="btn btn-flat btn-sm btn-default" title="checkin"><i class="fa fa-hand-o-right"></i></a>
							<a href="{{  asset('reservations/'. $reservation->id .'/checkout/') }}" class="btn btn-flat btn-sm btn-default" title="checkout"><i class="fa fa-hand-o-left"></i></a>
						@endif
						@php
							$ahora = \Carbon\Carbon::now();
							$inicio = \Carbon\Carbon::createFromFormat('d/m/Y H:i:s', $reservation->date_start. ' '. $reservation->schedule->end_hour);
							//$diff_in_days = $inicio->diffInHours($ahora, false);
							$diff_in_days = $ahora->diffInHours($inicio, false);
							if ($reservation->schedule_id == 2 || $reservation->schedule_id == 4) {
								$diff_in_days = $diff_in_days +24;
							}
						@endphp

						@if ($diff_in_days < 72 && $diff_in_days > 0 ) <!--4 DAYS TO CONFIRM =  96 HOURS-->
						@if ($reservation->status_id == 2)
							<div style="display:inline-block">
								{!! Form::open(['url' => ['reservations/confirmation'], 'method' => 'post']) !!}
								{!! Form::hidden("reservation_id", $reservation->id) !!}
								{!! Form::submit('Confirmar', ['class' => 'btn btn-success btn-flat']) !!}
								{!! Form::close() !!}
							</div>
						@endif
						@endif
						@if ($reservation->status_id == 2 || $reservation->status_id == 4)
							<div style="display:inline-block">
								{!! Form::open(['url' => ['reservations/cancelation'], 'method' => 'post']) !!}
								{!! Form::hidden("reservation_id", $reservation->id) !!}
								{!! Form::submit('Cancelar', ['class' => 'btn btn-cancel btn-flat']) !!}
								{!! Form::close() !!}
							</div>
						@endif
					</td>
				</tr>
			@endforeach
		</table>
		@if(Auth::user()->role->layout == 0)
			<table class="table table-hover">
				<tr>
					@if (Auth::user()->role->layout == 1)
						<th>{{trans('general.nombre')}} </th>
						<th>{{trans('general.apellido')}} </th>
						<th>Email</th>
					@endif
					<th>{{trans('general.fecha')}}</th>
					<th>{{trans('general.boat')}} </th>
					<th>Horario</th>
					<th>Números de referencia</th>
					<th>{{trans('general.reservationsstatus')}}</th>

					@if(Auth::user()->role->role_permission('edit_reservation') || Auth::user()->role->role_permission('delete_reservation'))
						<th>{{trans('general.accion')}}</th>
					@endif
				</tr>
				@foreach($array_of_grouped_def as $reservation)
					<tr>
						@if (Auth::user()->role->layout == 1)
							<td>{{ $reservation['first_name'] }}</td>
							<td>{{ $reservation['last_name'] }}</td>
							<td>{{ $reservation['email'] }}</td>
						@endif
						<td>{{ \Carbon\Carbon::createFromFormat('d/m/Y H:i:s',$reservation['date_start'])->format('d/m/Y') }} hasta: {{ \Carbon\Carbon::createFromFormat('d/m/Y H:i:s',$reservation['date_end_end'])->format('d/m/Y')}}</td>
						<td>{{ $reservation['boat']->name}}</td>
						<td>{{ \Carbon\Carbon::createFromFormat('d/m/Y H:i:s',$reservation['date_start'])->format('H:i')}} hasta: {{\Carbon\Carbon::createFromFormat('d/m/Y H:i:s',$reservation['date_end_end'])->format('H:i')}}</td>
						<td>
							<?php foreach ($reservation['reference_numbers'] as $key => $value): ?>
							{{$value}}
							<br/>
							<?php endforeach; ?>
						</td>
						<td class="statusCol">
							@if($reservation['status_id'] == 1)
								<span class="label label-success">{{$reservation['status']->name}}</span>
							@elseif($reservation['status_id'] == 2)
								<span class="label label-warning">{{$reservation['status']->name}}</span>
							@elseif($reservation['status_id'] == 3)
								<span class="label label-danger">{{$reservation['status']->name}}</span>
							@elseif($reservation['status_id'] == 4)
								<span class="label label-info">{{$reservation['status']->name}}</span>
							@endif
						</td>
						<td>
							@if(Auth::user()->role->role_permission('edit_reservation') || Auth::user()->role->role_permission('delete_reservation'))
								<div class="btn-group">
									@if(Auth::user()->role->role_permission('edit_reservation'))
										<a href="{{ asset('reservations/'.$reservation['id'].'/edit') }}" title="Edit" class="btn btn-default btn-flat btn-sm"><i class="fa fa-pencil"></i></a>
									@endif
									@if(false)
										Auth::user()->role->role_permission('delete_reservation')
										<a type="button" class="btn btn-default btn-flat btn-sm" title="Delete" data-toggle="modal" data-target="#deleteModal{{ $reservation['id'] }}"><i class="fa fa-trash"></i></a>
										<div id="deleteModal{{ $reservation['id'] }}" class="modal fade" role="dialog">
											<div class="modal-dialog">
												<div class="modal-content">
													<div class="modal-header">
														<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
														<h4 class="modal-title">{{trans('general.deleteconfirm')}}</h4>
													</div>
													<div class="modal-body">
														<p>{{trans('general.delete')}}</p>
													</div>
													<div class="modal-footer">
														<button type="button" class="btn btn-default btn-flat pull-left" data-dismiss="modal">{{trans('general.cancel')}}</button>
														{!! Form::open(['url' => ['reservations/'.$reservation['id']], 'method' => 'delete']) !!}
														{!! Form::submit(trans('general.si'), ['class' => 'btn btn-success btn-flat']) !!}
														{!! Form::close() !!}
													</div>
												</div>
											</div>
										</div>
									@endif
								</div>
							@endif
							@if($reservation['status_id'] == 1)
								@php
									$id_group = implode('-', $reservation['ids']);
								@endphp
								<a href="{{  asset('reservations/'. $id_group .'/checkin/group') }}" class="btn btn-flat btn-sm btn-default" title="checkin"><i class="fa fa-hand-o-right"></i></a>
								<a href="{{  asset('reservations/'. $id_group .'/checkout/group') }}" class="btn btn-flat btn-sm btn-default" title="checkout"><i class="fa fa-hand-o-left"></i></a>
							@endif
							@php
								$ahora = \Carbon\Carbon::now();
								$inicio = \Carbon\Carbon::createFromFormat('d/m/Y H:i:s', $reservation['date_start']);
								//$diff_in_days = $inicio->diffInHours($ahora, false);
								$diff_in_days = $ahora->diffInHours($inicio, false);
								if ($reservation->schedule_id == 2 || $reservation->schedule_id == 4) {
									$diff_in_days = $diff_in_days +24;
								}
							@endphp

							@if ($diff_in_days < 72 && $diff_in_days > 0 ) <!--4 DAYS TO CONFIRM =  96 HOURS-->
							@if ($reservation['status_id'] == 2)
								<div style="display:inline-block">
									{!! Form::open(['url' => ['reservations/confirm/group'], 'method' => 'post']) !!}
									<?php foreach ($reservation['ids'] as $key => $value): ?>
									{!! Form::hidden("reservation_id_".$key, $value) !!}
									<?php endforeach; ?>
									{!! Form::submit('Confirmar', ['class' => 'line-419 btn btn-success btn-flat']) !!}
									{!! Form::close() !!}
								</div>
							@endif
							@endif
							@if ($reservation['status_id'] == 2 || $reservation['status_id'] == 4)
								<div style="display:inline-block">
								{!! Form::open(['url' => ['reservations/cancel/group'], 'method' => 'post']) !!}
								<!-- {!! Form::hidden("reservation_id", $reservation['id']) !!} -->
									<?php foreach ($reservation['ids'] as $key => $value): ?>
									{!! Form::hidden("reservation_id_".$key, $value) !!}
									<?php endforeach; ?>
									{!! Form::submit('Cancelar', ['class' => 'line-419 btn btn-cancel btn-flat']) !!}
									{!! Form::close() !!}
								</div>
							@endif
						</td>
					</tr>
				@endforeach
			</table>
		@endif
	</div>

	<div class="box-footer clearfix">
		<div class="row">
			<div class="col-xs-12">
				{{trans('general.mostrando')}} {{ $reservations->firstItem() }} {{trans('general.de')}} {{ $reservations->lastItem() }} {{trans('general.de')}} {{ $reservations->total() }} {{trans('general.registros')}}
				<div class="pull-right">
					{!! $reservations->links() !!}
				</div>
			</div>
		</div>
	</div>
	</div>
	</div>
	</div>
	</section>
	</div>
@endsection

@section('foot')
	<script src="{{ asset('/plugins/select2/select2.min.js') }}"></script>
	<script src="{{ asset('plugins/jQueryUI/jquery-ui.min.js') }}"></script>

	<script src="{{ asset('/plugins/input-mask/jquery.inputmask.js') }}"></script>
	<script src="{{ asset('/plugins/input-mask/jquery.inputmask.date.extensions.js') }}"></script>
	<script src="{{ asset('/plugins/input-mask/jquery.inputmask.extensions.js') }}"></script>
	<script src="{{ asset('/plugins/iCheck/icheck.js') }}"></script>

	<script src="{{ asset('/plugins/mobile-detect/mobile-detect.js') }}"></script>

	<script src="{{ asset('/plugins/datepicker/bootstrap-datepicker.js') }}"></script>
	<script type="text/javascript">
		$(function () {
			$.fn.datepicker.dates['es'] = {
				days: ["Domingo", "Lunes", "Martes", "Miercoles", "jueves", "Viernes", "Sábado"],
				daysShort: ["Don", "Lun", "Mar", "Mie", "Jue", "Vie", "Sab"],
				daysMin: ["Do", "Lu", "Ma", "Mi", "Ju", "Vi", "Sa"],
				months: ["Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"],
				monthsShort: ["Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Sep", "Oct", "Nov", "Dic"],
				today: "Hoy",
				clear: "Borrar",

				weekStart: 0
			};


			$('.date_start').datepicker({
				language: "es",
				todayHighlight: true,
				weekStart: 1,
				autoclose: true,
				format: 'dd/mm/yyyy',
				maxDate: 'now',
				clearBtn: true,
				todayBtn: "linked",
				keyboardNavigation: false,

			});

		});

		var md = new MobileDetect(window.navigator.userAgent);

		if (md.mobile()){
			$('#vEscritorio').css('display','none');
			$('#vMovil').css('display','block');
		}else{
			$('#vEscritorio').css('display','block');
			$('#vMovil').css('display','none');
		}

	</script>
@endsection
