@extends("app.layout.base")
@section("content")
    <style>
        iframe, .ql-video {
            width: 100%;
        }

        div.card {
            margin-top: 1rem;
            margin-bottom: 30px;
            width: auto;
            heigt: auto;
            box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
        }

        div.header {
            background-color: #00788a;
            color: white;
            padding: 8px;
            font-size: 10px;
            text-align: center;
        }

        div.container {
            padding: 8px;
            text-align: justify;
            line-height: 1.5;
        }

        ol {
            padding-right: 8px;
        }
    </style>
    <main style="justify-content: unset;">
        @foreach($news as $new)
                <div class="card">
                    @if($new->resource_id !== null)
                        <div class="header" style="background-size: cover; background-image: url('{{ asset(\App\Resource::query()->where("id", "=", $new->resource_id)->first()->filename) }}');">
                            <p style="text-shadow: 2px 2px 2px #00788A;"><b>{{ $new->name }}</b></p>
                            <h3 style="text-shadow: 2px 2px 2px #00788A;">{{ $new->subname }}</h3>
                        </div>
                    @else
                        <div class="header">
                            <p><b>{{ $new->name }}</b></p>
                            <h3>{{ $new->subname }}</h3>
                        </div>
                    @endif

                    <div class="container" style="margin-top: 8px;">
                        @if ($new->type_id == 1 || $new->type_id == 2) {{-- If it's a news or menu then display the whole message --}}
                            {!! $new->content !!}
                        @else {{-- If it's a menu, guide or event. Then display up to 300 characters of text --}}
                            {!! \App\Helpers::closetags(substr($new->content, 0, 300) ) !!}
                        @endif

                        @if ($new->type_id == 3) {{-- If it's displaying an event, then show `Tilmeld`, `Læs mere` & `Se deltagere` --}}
                            <div class="row" style="justify-content: space-between; margin-top: 16px;">
                                @if (count(\App\UserEvent::query()->where('event_id', '=', $new->arrangement_id)->where('user_id', '=', Auth::user()->id)->get()) > 0)
                                    <a style="margin: 0; padding: 0; font-weight: 700;" class="sde-blue text-center" href="javascript:void(0);" onclick="ajaxCall({{ $new->arrangement_id }}, this)" >{{__('msg.afmeld')}}</a>
                                @else {{-- ^ If you're already participating in the event, then show a ´cancel´ button - v Else show a ´participate´ button --}}
                                    <a style="margin: 0; padding: 0; font-weight: 700;" class="sde-blue text-center" href="javascript:void(0);" onclick="ajaxCall({{ $new->arrangement_id }}, this)" >{{__('msg.tilmeld')}}</a>
                                @endif
                                <a style="margin: 0; padding: 0; font-weight: 700;" class="sde-blue text-center" href="{{route("events.show", ["event" => $new->arrangement_id ])}}">{{__('msg.læsmere')}}</a>
                                <a style="margin: 0; padding: 0; font-weight: 700;" class="sde-blue text-center" href="{{route("events.accountsignups", ["event" => $new->arrangement_id ])}}">{{__('msg.sedeltagere')}}</a>
                            </div>
                        @elseif ($new->type_id == 4) {{-- Else if's displaying guides, then show `Læs mere` --}}
                            <div class="row" style="justify-content: center">
                                <a style="font-weight: 700;" href="{{route("guides.show", ["guide" => $new->arrangement_id])}}" class="sde-blue">{{__('msg.læsmere')}}</a>
                            </div>
                        @endif {{-- Else if it's a menu or news, then don't show a button at bottom --}}
                    </div>
                </div>
        @endforeach
        @if(count(\App\News::query()->orderBy("created_at", "desc")->get()) == 0)
            <p class="text-center">{{__('msg.ingennyheder')}}</p>
        @else
            {{ $news->links() }}
        @endif
            <div id="snackbar"></div>

    </main>
@endsection
@section("scripts")
    <script type="text/javascript">
        function snackbar(data) {
            var x = document.getElementById("snackbar");
            x.innerHTML = data;
            x.className = "show";

            setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);
        }

        function ajaxCall(event_id, el) {
            if(el.innerHTML === "{{__("msg.tilmeld")}}") {
                axios.post("{{ route("userevents.createajax") }}",
                    {
                        event_id: event_id
                    }
                ).then(function (response) {
                    var data = response.data;

                    snackbar(data);

                    el.innerHTML = "{{__("msg.afmeld")}}";
                }).catch(function (error) {
                    console.log(error);
                });
            } else if(el.innerHTML === "{{__("msg.afmeld")}}"){
                axios.post("{{ route("userevents.createajaxcancel") }}",
                    {
                        event_id: event_id
                    }
                ).then(function (response) {
                    var data = response.data;

                    snackbar(data);

                    el.innerHTML = "{{__("msg.tilmeld")}}";
                }).catch(function (error) {
                    console.log(error);
                });
            }
        }
    </script>
@endsection
