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

@section("title")
    Bruger - Vis
@endsection

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

@section("content")
    <style>
        .showUsers {
            position: relative;
            display: inline-block;
            overflow: visible;
        }
        .showUsers .showUserImages {
            visibility: hidden;
            width: 250px;
            max-height: 500px;
            max-width: 300px;
            object-fit: cover;
            background-color: transparent;
            color: #fff;
            text-align: center;
            border-radius: 6px;
            padding: 5px 0;
            position: absolute;
            z-index: 10;
            top: -5px;
            right: 105%;
            display: block;
        }
        .showUsers:hover .showUserImages {
            visibility: visible;
            z-index: 100;
        }
        .noImages{
            cursor: not-allowed;
        }

        .letterSpaceTable{
            letter-spacing: 1.2px;
        }
    </style>
    <style>
        /* The switch - the box around the slider */
        .switch {
            position: relative;
            display: inline-block;
            width: 60px;
            height: 28px;
        }

        /* Hide default HTML checkbox */
        .switch input {
            opacity: 0;
            width: 0;
            height: 0;
        }

        /* The slider */
        .slider {
            position: absolute;
            cursor: pointer;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: #ccc;
            transition: .4s;
        }

        .slider:before {
            position: absolute;
            content: "";
            height: 20px;
            width: 20px;
            left: 4px;
            bottom: 4px;
            background-color: white;
            transition: .4s;
        }

        input:checked + .slider {
            background-color: #00788A;
        }

        input:focus + .slider {
            box-shadow: 0 0 1px #00788A;
        }

        input:checked + .slider:before {
            transform: translateX(32px);
        }

        /* Rounded sliders */
        .slider.round {
            border-radius: 34px;
        }

        .slider.round:before {
            border-radius: 50%;
        }
    </style>
    <div class="row align-items-center">
        @if(auth()->user()->can('user.create'))
            <a class="btn btn-inline btn-sde-blue mb-0" href="{{ route('users.create') }}"><img src="{{ asset('/images/icons/plus.svg') }}" alt="Create">Opret Bruger</a>
        @endif
    </div>
    <div class="pb-4 mt-1">
        <label>Åben for registrering:</label>
        <label class="switch">
            <input type="checkbox" onclick="AllowActionRegistering(this)" @if(\App\AllowAction::query()->where('action', '=', 'Registering')->first()->allow == true) checked @endif>
            <span class="slider round"></span>
        </label>
    </div>
    <table class="tbl mt-2 letterSpaceTable" id="table_id">
        <thead>
            <th>Fornavn</th>
            <th>Efternavn</th>
            <th>Mail</th>
            <th>Telefon</th>
            <th>Rolle(r)</th>
            <th class="w-1em"><img class="w-100" src="{{ asset('/images/icons/eye.svg') }}" alt="ShowImage"></th>
            @if(auth()->user()->can('user.edit'))
                <th class="w-1em"><img class="w-100" src="{{ asset('/images/icons/pencil.svg') }}" alt="Update"></th>
            @endif
            @if(auth()->user()->can('user.delete'))
                <th class="w-1em"><img class="w-100" src="{{ asset('/images/icons/trashcan.svg') }}" alt="Delete"></th>
            @endif
        </thead>
        <tbody>
            @foreach($users as $user)
                <tr id="row_{{ $user->id }}">
                    <td>{{ $user->name_first }}</td>
                    <td>{{ $user->name_last }}</td>
                    <td>{{ $user->email }}</td>
                    <td>{{ $user->phone }}</td>
                    <td>
                        @for($i = 0; $i < count($user->roles); $i++)
                            @if(count($user->roles)-1 != $i)
                                {{$user->roles[$i]->name}},
                            @else
                                {{$user->roles[$i]->name}}
                            @endif
                        @endfor
                    </td>
                    @if($user->resource_id !== null)
                        <td style="overflow: visible"><a class="showUsers"><img src="{{ asset('/images/icons/eye-dark.svg') }}"><img src="{{ asset(\App\Resource::query()->where("id", "=", $user->resource_id)->first()->filename) }}" class="showUserImages"></a></td>
                    @else
                        <td style="overflow: visible"><a class="showUsers noImages"><img src="{{ asset('/images/icons/eye-dark.svg') }}"><img src="" class="showUserImages"></a></td>
                    @endif
                    @if(auth()->user()->can('user.edit'))
                        <td><a href="{{ route("users.edit", [ "user" => $user->id ]) }}"><img class="w-100" src="{{ asset('/images/icons/pencil-dark.svg') }}" alt="Update"></a></td>
                    @endif
                    @if(auth()->user()->can('user.delete'))
                        <td>
                            @csrf
                            <a class="w-100 nostyle" onclick="delete_user({{ $user->id }})"><img class="w-100 cursor-pointer" src="{{ asset('/images/icons/trashcan-dark.svg') }}" alt="Delete"></a>
                        </td>
                    @endif
                </tr>
            @endforeach
        </tbody>
    </table>
@endsection

@section('scripts')
    <script>
        $(document).ready( function () {
            $('#table_id').DataTable({
                columnDefs: [
                    { orderable: false, targets: [-1, -2, -3] }
                ]
            });
        });

        function delete_user(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 Bruger',
                cancelButtonText: 'Annuller'
            }).then((result) => {
                if (result.isConfirmed) {
                    $.ajax({
                        type: "POST",
                        url: "users/"+id,
                        data:{'_token':token, _method: 'DELETE'},
                        success: function () {
                            if (id == 1) {
                                Swal.fire(
                                    'Administrator brugeren kan ikke slettes',
                                    '',
                                    'warning'
                                )
                            } else {
                                $('#table_id').DataTable().row($('#row_'+id)[0]).remove().draw();

                                Swal.fire(
                                    'Brugeren er slettet!',
                                    '',
                                    'success'
                                )
                            }
                        },
                        error:function (data) {
                            console.log(data);
                        }
                    });
                }
            })
        }

        function AllowActionRegistering(el) {
            if (el.hasAttribute('checked')) {
                axios.post("{{ route("allowaction.allowactionregistering") }}", null, { params: { id: {{ \App\AllowAction::query()->where('action', '=', 'Registering')->first()->id }}, allow: '0' }}
                ).then(function (response) {
                    el.removeAttribute('checked');

                    Swal.fire(
                        'Der er nu lukket for registrering af brugere!',
                        '',
                        'success'
                    )
                }).catch(function (error) {
                    console.log(error);
                });
            } else {
                axios.post("{{ route("allowaction.allowactionregistering") }}", null, { params: { id: {{ \App\AllowAction::query()->where('action', '=', 'Registering')->first()->id }}, allow: '1' }}
                ).then(function (response) {
                    el.setAttribute('checked', 'checked');

                    Swal.fire(
                        'Der er nu åbnet for registrering af brugere!',
                        '',
                        'success'
                    )
                }).catch(function (error) {
                    console.log(error);
                });
            }
        }
    </script>
@endsection