@extends('template.'.\Setting::get('template', 'default').'.content.dashboard.app')

@section('content')

    <div class="admin-header">
        <div class="container">
            <h1><i class="fal fa-file-alt"></i> {{ __('admin-content.h1') }}</h1>
        </div>
    </div>

    <section id="ueber-uns">
        <div class="container">
            <div class="row">
                <div class="mx-auto">
                    <div class="btn-group btn-group-toggle mx-auto" data-toggle="buttons">
                        @foreach ($types as $type)
                            <label class="btn btn-lg {{ $type->id == $typeModel->id ? 'btn-primary' : 'btn-outline-primary' }}" onclick='window.location.href = "{{ url('/dashboard/') }}/{{ $type->slug }}";'>
                                <input type="radio" name="options" id="option1" {{ $type->id == $typeModel->id ? 'checked' : '' }}> <i class="fal fa-{{ $type->icon}}"></i> {{ $type->name }}
                            </label>
                        @endforeach
                    </div>
                </div>
            </div>

            <a href="{{ route('dashboard.viewAddNachrichten', [$typeModel->slug]) }}" class="float-right"><i class="fal fa-plus"></i>
                {{ __('admin-content.create-new-' . $typeModel->id) }}
            </a>

            <br><br>
        </div>
        <div class="table-responsive">
            <table class="table table-striped">
                <thead>
                <tr>
                    <th scope="col">@sortablelink('created_at', __('admin-content.date'))</th>
                    <th scope="col">@sortablelink('title', __('admin-content.title'))</th>
                    <th scope="col">@sortablelink('nachrichten', __('admin-content.message'))</th>
                    <th scope="col">@sortablelink('category_id', __('admin-content.category'))</th>
                    <th scope="col">{{ __('admin-content.extras') }}</th>
                    <th scope="col" class="text-center">{{ __('admin-content.actions') }}</th>
                    <th scope="col" class="text-right">@sortablelink('approved', 'Status')</th>
                </tr>
                </thead>

                <tbody>
                @foreach($nachrichten as $nach)
                    <tr id="nach{{$nach->id}}">

                        <td scope="col"><img src="{{ $nach->user->gravatar() }}" width="32" class="rounded-circle" title="{{ $nach->user->name }}"><a title="{{date('d.m.Y | H:i', strtotime($nach->created_at))}} Uhr">
                                {{ $nach->created_at->diffForHumans() }}
                            </a>
                        </td>

                        <td scope="col">
                            @if($nach->approved == 1)
                                @if($nach->type == 'news')
                                    <a href="{{ route('nachrichten.get', [$nach->category->slug, $nach->slug]) }}" target="_blank">{{Str::limit($nach->title, 30,'...')}} <i class="fal fa-external-link"></i> </a>

                                @else
                                    <a href="{{ route('nachrichten.get', [$nach->category->slug, $nach->slug]) }}" target="_blank">{{Str::limit($nach->title, 30,'...')}} <i class="fal fa-external-link"></i> </a>

                                @endif

                            @else()
                                {{Str::limit($nach->title, 30,'...')}}
                            @endif
                        </td>

                        <td scope="col"> <a title="{{strip_tags(html_entity_decode($nach->nachrichten))}}">
                                {{ Str::limit(strip_tags(html_entity_decode($nach->nachrichten)), 90, $end = '...') }}</a></td>

                        <td><span class="badge badge-pill badge-secondary">@if ($nach->category)
                                    {{ $nach->category->name }}
                                @else
                                    -
                                @endif</span>
                        </td>


                        <td scope="col">
                            @if(sizeof($nach->files) > 0)
                                <a href="" data-toggle="modal" data-target="#attachmentModal" onclick="getAttachments({{$nach->id}})">
                                    <i class="fal fa-paperclip"></i>
                                    <span class="badge badge-pill badge-dark">{{sizeof($nach->files)}}</span>
                                </a>
                            @endif

                            @if(sizeof($nach->comments) > 0)
                                <a href="{{url('/admin/reviewComments')}}">
                                    <i class="fal fa-comments"></i>
                                    <span class="badge badge-pill badge-dark">{{sizeof($nach->comments)}}</span>
                                </a>
                            @endif

                            @if ($type->id == 3)
                                @foreach ($nach->actions as $action)
                                    <span class="badge badge-primary" style="margin-left:5px;"><i class="fa fa-{{$action->icon}}"></i> <span style="vertical-align: text-top;">{{ $action->name }}</span></span>
                                @endforeach
                            @endif
                        </td>

                        <td scope="col" class="text-center">
                            <a href="{{ route('dashboard.viewEditNachrichten', [$nach->id]) }}">
                                <i class="fal fa-edit"></i>
                            </a>
                            |&nbsp;<i class="fad fa-trash" style="color: red;cursor:pointer;" onclick="removeNach({{$nach->id}})"></i>

                        </td>


                        <td scope="col" class="text-right">

                            @if ($nach->paid != 0 and (empty($nach->premium_until) or $nach->premium_until > date('Y-m-d')))

                                <i class="fal fa-gem ci-color" title="Premium | Amount: {{$nach->paid}} sats"></i>
                                |
                            @endif

                            @if($nach->approved != 1)
                                @if ($nach->approved == 2)
                                    <a>
                                        <i class="fas fa-spinner-third fa-spin" id="approved-{{$nach->id}}" style="color: orange"></i>
                                    </a>
                                @else
                                    <a>
                                        <i class="fas fa-calendar-alt" id="approved-{{$nach->id}}" style="color: red"></i>
                                    </a>
                                @endif
                            @else
                                <a>
                                    <i class="fas fa-calendar-alt" id="approved-{{$nach->id}}" style="color: green;" ></i>
                                </a>
                            @endif
                        </td>

                    </tr>
                @endforeach

                @if ($nachrichten->count() == 0)
                    <tr>
                        <td valign="top" colspan="7" class="text-center table-empty">No records found!</td>
                    </tr>
                @endif
                </tbody>
            </table>

        </div>
        <div>
    </section>

    <div class="modal fade" id="attachmentModal" role="dialog" aria-labelledby="attach_label_modal" aria-hidden="true">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title" id="attach_label_modal">Dateien</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <input type="hidden" id="nach_id"required="required" value="", name="nach_id">
                <div class="modal-body">
                    <h5><i class="fal fa-file"></i> Anhänge:</h5>
                    <div class="form-group" id="attachments_div"></div>
                </div>
                <div class="modal-footer">
                </div>
            </div>
        </div>
    </div>

    <script>
        $(document).on('focusin', function(e) {
            if ($(e.target).closest(".tox").length) {
                e.stopImmediatePropagation();
            }
        });
    </script>

    <script>
        function removeNach(id) {
            $.ajax({
                data:{
                    '_token':'{{csrf_token()}}',
                    'id': id,
                },
                type:"POST",
                async:true,
                url:"{{route('dashboard.removeMessage')}}",
                success:function(data) {
                    document.getElementById('nach'+data.response).remove();

                    toastr.options = {
                        "progressBar": true
                    };

                    var msg = toastr.success('Successfully removed!', 'Success');

                    $(msg).click(function() {
                        $.ajax({
                            type:"GET",
                            async:true,
                            url:"{{url('admin/restoreNach')}}/" + id,
                            success:function(data) {
                                location.reload();
                            },
                            error:function(data) {
                                console.log('error!!')
                            }
                        });
                    });
                },
                error:function(data) {
                    console.log('error!!')
                }
            });
        }

        function getAttachments(id) {
            $.ajax({
                data:{
                    '_token':'{{csrf_token()}}',
                    'id': id,
                },
                type:"POST",
                async:true,
                url:"{{route('dashboard.getAttachments')}}",
                success:function(data) {
                    console.log('success')
                    let attach_div = document.getElementById('attachments_div')
                    attach_div.innerHTML = data.response
                },
                error:function(data) {
                    console.log('error!!')
                }
            });
        }

        function removeAttach(id, child) {
            $.ajax({
                data:{
                    '_token':'{{csrf_token()}}',
                    'id': id,
                },
                type:"POST",
                async:true,
                url:"{{route('dashboard.removeFile')}}",
                success:function(data) {
                    console.log('success')
                    node = document.getElementById('file_' + id)
                    node.parentNode.removeChild(node);
                },
                error:function(data) {
                    console.log('error!!')
                }
            });
        }
    </script>
@stop
