@extends("admin.layout.base")
@extends("admin.layout.header")

@section("title")
    Vaskemaskine Reservationer - Vis
@endsection

@section("path")
    <a href="{{ route('washing-reservations.index') }}" class="text-white">Vis Vaskemaskine Reservationer</a> /
@endsection

@section("content")
    <?php
    date_default_timezone_set('Europe/Copenhagen');
    ?>
    <style>
        .letterSpaceTable{
            letter-spacing: 1.2px;
        }
    </style>
    <table class="tbl mt-0 letterSpaceTable fixOverflow" id="table_id">
        <thead>
            <th>Lokation</th>
            <th>Vaskemaskine</th>
            <th>Tidspunkt</th>
            <th>Bruger</th>
            @if(auth()->user()->can('washing.machine.reservation.delete'))
                <th class="w-1em"><img class="w-100" src="{{ asset('/images/icons/trashcan.svg') }}" alt="Delete"></th>
            @endif
        </thead>
        <tbody>
            @foreach($reservations as $reservation)
                @if(date('Y-m-d H:i:s', strtotime('-1 hour')) < $reservation->time)
                    <tr id="row_{{ $reservation->id }}">
                        <td>{{ \App\Location::query()->find(\App\WashingMachine::query()->find($reservation->machine_id)->location_id)->name }}</td>
                        <td>{{ \App\WashingMachine::query()->find($reservation->machine_id)->name }}</td>
                        <td>{{ \Illuminate\Support\Facades\Date::createFromTimeStamp(strtotime($reservation->time))->format('d/m/Y \k\l\. H:i') }}</td>
                        <td>{{ ucfirst(\App\User::query()->find($reservation->user_id)->name_first) }} {{ ucfirst(\App\User::query()->find($reservation->user_id)->name_last) }}</td>
                        @if(auth()->user()->can('washing.machine.reservation.delete'))
                            <td>
                                @csrf
                                <a class="w-100 nostyle" onclick="delete_reservation({{ $reservation->id }})"><img class="w-100 cursor-pointer" src="{{ asset('/images/icons/trashcan-dark.svg') }}" alt="Delete"></a>
                            </td>
                        @endif
                    </tr>
                @endif
            @endforeach
        </tbody>
    </table>
@endsection
@section('scripts')
    <script>
        $(document).ready( function () {
            $('#table_id').DataTable({
                columnDefs: [
                    { orderable: false, targets: -1 }
                ]
            });
        });

        function delete_reservation(id) {
            var token = $("input[name='_token']").val();

            Swal.fire({
                title: 'Er du sikker?',
                text: "Dette kan ikke blive ændret tilbage!",
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                confirmButtonText: 'Slet Reservationen',
                cancelButtonText: 'Annuller'
            }).then((result) => {
                if (result.isConfirmed) {
                    $.ajax({
                        type: "POST",
                        url: "/washing-reservations/"+id,
                        data:{'_token':token, _method: 'DELETE'},
                        success: function () {
                            $('#table_id').DataTable().row($('#row_'+id)[0]).remove().draw();

                            Swal.fire(
                                'Reservationen er slettet!',
                                '',
                                'success'
                            )
                        },
                        error:function (data) {
                            console.log(data);
                        }
                    });
                }
            })
        }
    </script>
@endsection