@extends(Auth::check() && Auth::user()->role->layout == 1 ? 'layouts.admin' : 'layouts.employee')

@section('content')
    <div class="content-wrapper">
        <section class="content-header">
            <h1>
                {{trans('general.reservations')}}
            </h1>
        </section>

        <section class="content">
            <div class="row">

                @if(!$valid)

                    @if ($holiday > 0)

                    <div class="callout callout-danger">
                        <h4>Festivos</h4>
                        <p>El sistema sólo permite reservar 2 días festivo por socio. Para cualquier duda, contacte con el administrador, gracias.</p>
                    </div>

                    @elseif ($message != '')

                    <div class="callout callout-danger">
                        <h4>El sistema no permite realizar la reserva</h4>
                        <p>{{ $message }} Para cualquier duda, contacte con el administrador, gracias.</p>
                    </div>

                    @else

                    <div class="callout callout-danger">
                        <h4>No dispone de salidas sufientes</h4>
                        <p>Ya ha agotado el número de salidas asignadas. Para cualquier duda, contacte con el administrador, gracias.</p>
                    </div>

                    <p class="text-center"><a href="{{ asset('/credits/' . Auth::user()->customer_id . '/listado' ) }}" class="btn btn-success btn-flat">Ver créditos</a></p>

                    @endif

                @else

                    <div class="col-md-12">
                        <div class="box box-success">
                            <div class="box-header">
                                <div class="user-block">
                                    <span class="username no-margin"></span>
                                    <span class="description no-margin">{{trans('general.reservation')}} </span>
                                </div>
                            </div>
                            <div class="box-body">

                        {!! Form::open(['route' => 'storecalendar', 'method' => 'post']) !!}
                          <div class="col-md-8 col-md-offset-2">
                            {!! Form::hidden('user_id', $request->user_id , ['class' => 'form-control']) !!}
	                          {!! Form::hidden('customer_id', $customer->id, ['class' => 'form-control']) !!}
                            {!! Form::hidden('boat_id', $request->boat_id , ['class' => 'form-control']) !!}
                            {!! Form::hidden('date_start_', $date_start , ['class' => 'form-control']) !!}
                            {!! Form::hidden('date_start', $date_start , ['class' => 'form-control']) !!}
	                          {!! Form::hidden('schedule_id', $request->schedule, ['class' => 'form-control']) !!}
                            @if ($request->status == '4')
                                {!! Form::hidden('status_id', 4, ['class' => 'form-control']) !!}
                            @else
                                {!! Form::hidden('status_id', 2, ['class' => 'form-control']) !!}
                            @endif
                            <p>Nombre: {{Auth::user()->first_name }} {{Auth::user()->last_name }}</p>
		                        <p>Cliente: {{ $customer->fiscal_reason }}</p>
                            <p>Embarcación: {{$boat->name }}</p>
                            <p>Fecha: {{$date_start }}</p>
		                        <p>Horario: {{ $schedule->name}}</p>

                            <div class="form-group{{ $errors->has('reference_number') ? ' has-error' : '' }}">
                                {!! Form::label('reference_number',trans('general.reservationsnumber') ) !!}
                                @if(isset($reference_number))
                                    {{--$reference_number --}}
                                @else {{$reference_number = null}}
                                @endif

                                {!! Form::text('reference_number',$reference_number, ['class' => 'form-control','readonly']) !!}
                                @if ($errors->has('reference_number'))
                                    <span class="help-block">
                                            <strong>{{ $errors->first('reference_number') }}</strong>
                                        </span>
                                @endif
                            </div>
                            <div class="form-group{{ $errors->has('comments') ? ' has-error' : '' }}">
                                {!! Form::label('comment', trans('general.comment')) !!}
                                {!! Form::textarea('comments', null, ['class' => 'form-control','rows' => '3']) !!}
                                @if ($errors->has('comments'))
                                    <span class="help-block">
                                <strong>{{ $errors->first('comments') }}</strong>
                            </span>
                                @endif
                            </div>

                            <div id="errorDiv"></div>

                            {!! Form::submit(trans('general.reservationsconfirm'), ['class' => 'form-control btn btn-success btn-flat', 'id' => 'submitButton']) !!}
                          </div>
                        {!! Form::close() !!}


                        @if (Session::has('sweet_alert.alert'))
                            <script>
                                swal({
                                    title: "CONFIRMAR PRE-RESERVA",
                                    text: "{{ $message }}<br/><br/>Escriba <b>CANCEL</b> para cancelar esta reserva",
                                    type: "input",
                                    html: true,
                                    cancelButtonText: 'Pre-Reservar',
                                    confirmButtonText: 'Cancelar',
                                    confirmButtonColor: "#ff1a1a",
                                    showCancelButton: true,
                                    closeOnConfirm: false,
                                    closeOnCancel: true,
                                    animation: "slide-from-top",
                                    inputPlaceholder: "CANCEL",
                                    showConfirmButton: "CANCEL",
                                    //confirmButtonText: "{!! Session::get('sweet_alert.confirmButtonText') !!}",
                                    // confirmButtonColor: "#AEDEF4"

                                    // more options
                                }, function(inputValue) {
                                    if (inputValue == "CANCEL") {
                                        window.location.replace('/public/reservations');
                                    } else {
                                        swal.showInputError("Es neceserario que escriba CANCEL!");
                                        return false;
                                    }
                                });

                            </script>
                        @endif
                    </div>

                @endif

            </div>
        </section>
    </div>
@endsection


@section('foot')

    @php

        $fecha = explode('/',$date_start);
        $fecha2 = DateTime::createFromFormat('j/n/Y', $date_start);
        $weekDay = $fecha2->format('w');

    @endphp

    <script type="text/javascript">
        const url_to_request_get = "https://skipper.komunica.es/checkCredit.php";
        $('#schedule_id').on('change', function(){
            $('#errorDiv').html('');
            $('#submitButton').attr('disabled');
            $.ajax({
                url: url_to_request_get,
                method: "GET",
                data: { u : {{ $request->user_id }} , y : {{ $fecha[2] }}, m : {{ $fecha[1] }}, d : {{ $fecha[0] }}, t : $('#schedule_id').val() },
                dataType: "html"
            }).done(function(data) {
                if ( data == 'false'){
                    $('#errorDiv').html('<span class="help-block"><strong>No puedes crear esta reserva porque no tienes créditos</strong></span>');
                }else{
                    $('#submitButton').removeAttr('disabled');
                }
            });

        });

    </script>

@endsection
