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

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

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

@section("content")
    <style>
        .letterSpaceTable{
            letter-spacing: 1.2px;
        }
    </style>
    @if(auth()->user()->can('news.create'))
        <div class="row align-items-center">
            <a class="btn btn-inline btn-sde-blue mb-0" href="{{ route('news.create') }}"><img src="{{ asset('/images/icons/plus.svg') }}" alt="Create">Opret Nyheder</a>
        </div>
    @endif
    <table class="tbl letterSpaceTable fixOverflow" id="table_id">
        <thead>
            <th>Navn</th>
            <th>Nyheds type</th>
            <th>Udløbsdato</th>
            <th class="w-1em"><img class="w-100" src="{{ asset('/images/icons/preview.svg') }}" alt="preview"></th>
            @if(auth()->user()->can('news.edit'))
                <th class="w-1em"><img class="w-100" src="{{ asset('/images/icons/pencil.svg') }}" alt="Update"></th>
            @endif
            @if(auth()->user()->can('news.delete'))
                <th class="w-1em"><img class="w-100" src="{{ asset('/images/icons/trashcan.svg') }}" alt="Delete"></th>
            @endif
        </thead>
        <tbody>
            @foreach($news as $new)
                <tr id="row_{{ $new->id }}">
                    <td>{{$new->subname}}</td>
                    <td>{{$new->type}}</td>
                    @if($new->news_expiration_date !== null)
                        <td>{{ \Illuminate\Support\Facades\Date::createFromTimeStamp(strtotime($new->news_expiration_date))->format('d/m/Y \k\l\. H:i') }}</td>
                    @else
                        <td>Ingen udløbsdato</td>
                    @endif
                    <td><a id="preview" onclick="modalNewsContent({{$new->id}})" style="cursor: pointer" ><img class="w-100" src="{{ asset('/images/icons/preview-dark.svg') }}" alt="preview"></a></td>
                    @if(auth()->user()->can('news.edit'))
                        <td><a href="{{ route("news.edit", [ "news" => $new ]) }}"><img class="w-100" src="{{ asset('/images/icons/pencil-dark.svg') }}" alt="Update"></a></td>
                    @endif
                    @if(auth()->user()->can('news.delete'))
                        <td>
                            @csrf
                            <a class="w-100 nostyle" onclick="delete_news({{ $new->id }})"><img class="w-100 cursor-pointer" src="{{ asset('/images/icons/trashcan-dark.svg') }}" alt="Delete"></a>
                        </td>
                    @endif
                </tr>
            @endforeach
        </tbody>
    </table>

    <div id="newsModal" class="modal zindex-100">
        <div id="modal-content" class="modal-content text-black d-block w-50">
            <span class="close" onclick="closeModal()">&times;</span>
            <center>
                <h1 id="titleEvent"></h1>
                <p id="dateEvent"></p>
                <hr>
                <p id="descriptionEvent" style="white-space: pre-line"></p>

            </center>
        </div>
    </div>
@endsection
@section('scripts')
    <script>
        var modalNews = document.getElementById("newsModal");

        function modalNewsContent(id) {
            $.ajax({
                type: 'get',
                url: '{{route('news.preview')}}',
                data: {'preview':id},
                success:function (data) {
                    modalNews.style.display = "flex";
                    var l = JSON.parse(data);
                    $("#titleEvent").html(l.subname);
                    $("#dateEvent").html(l.news_expiration_date);
                    $("#descriptionEvent").html(l.content);
                },
                error:function (data) {
                    console.log(data);
                }
            });
        }

        function closeModal() {
            modalNews.style.display = "none";
            $("#titleEvent").html('');
            $("#dateEvent").html('');
            $("#descriptionEvent").html('');
        }

        window.onmousedown = function(event) {
            if (event.target == modalNews) {
                modalNews.style.display = "none";
                $("#titleEvent").html('');
                $("#dateEvent").html('');
                $("#descriptionEvent").html('');
            }
        }
    </script>
    <script>
        $(document).ready( function () {
            $('#table_id').DataTable({
                columnDefs: [
                    { orderable: false, targets: [-1, -2, -3, -4] }
                ]
            });
        });

        function delete_news(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 Nyheden',
                cancelButtonText: 'Annuller'
            }).then((result) => {
                if (result.isConfirmed) {
                    $.ajax({
                        type: "POST",
                        url: "news/"+id,
                        data:{'_token':token, _method: 'DELETE'},
                        success: function () {
                            $('#table_id').DataTable().row($('#row_'+id)[0]).remove().draw();

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