<x-layout>
    <x-slot:title>Guestbook</x-slot:title>
    <table class="gb-entry-form-container" role="presentation">
        <tr>
            <td>
                <form method="POST" action="/guestbook">
                    @csrf
                    @if(!isLegacy())<x-honeypot/>@endif
                    <table class="form" role="presentation">
                        <tr>
                            <td>
                                <label for="name"><strong>Name:</strong></label>
                            </td>
                            <td>
                                <input name="name" type="text" id="name">
                            </td>
                            <td>
                                <span class="text-danger">{{ $errors->first('name') }}</span>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <label for="message"><strong>Message:</strong></label>
                            </td>
                            <td>
                                <textarea name="message" id="message" rows="3"></textarea>
                            </td>
                            <td>
                                <span class="text-danger">{{ $errors->first('message') }}</span>
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td>
                                @if(isLegacy())
                                <input type="submit" name="Submit" VALUE="Submit">
                                @else
                                <button type="submit">Submit</button>
                                @endif
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td>
                            </td>
                        </tr>
                    </table>
                </form>
            </td>
            <td>
                <p><i>A few things to note:</i></p>
                <ul>
                    <li>You can submit an entry <u>once every hour</u>.</li>
                    <li>Your IP address is logged but <u>not</u> publicly displayed.</li>
                    <li>Any entries that appear to be spam <u>will</u> be removed.</li>
                    <li>Entries submitted on this site are flagged for manual review.</li>
                    @if (isLegacy())
                        <li>This form is broken in super-old browsers.</li>
                    @endif
                </ul>
            </td>
        </tr>
    </table>

    @if (isLegacy())
    <font size="+2"><b>Entries ({{ count($entries) }} total)</b></font>
    <table cellspacing="10" cellpadding="0" border="0">
        @foreach ($entries as $entry)
            @php
                $user_agent = $parser->parse($entry->agent);
            @endphp
        <tr>
            <td>
                <table cellspacing="2" cellpadding="5" border="1" width="100%">
                    <tr><td>
                            Submitted by <b>{{ $entry->name }}</b>
                            on <b>{{ $entry->created_at->format('Y-m-d') }}</b>
                            at <b>{{ $entry->created_at->format('h:i:s A (e)') }}</b>
                        </td></tr>
                    <tr><td>{{ $entry->message }}</td></tr>
                    <tr><td>
                            @if($entry->agent === "Agent Unavailable")
                                <i>Agent unavailable</i>
                            @else
                                @if ($user_agent->ua->toString() == "Other" || $user_agent->os->toString() == "Other")
                                    <i>Posted using <b>{{ $entry->agent }}</b></i>
                                @else
                                <i>Posted using <b>{{ $user_agent->ua->toString() }}</b>
                                    on <b>{{ $user_agent->os->toString() }}</b></i>
                                @endif
                            @endif
                        </td></tr>
                </table>
            </td>
        </tr>
        @endforeach

    </table>
    @else
    <hr>
    <h2>Entries <small>({{ count($entries) }} total)</small></h2>
    @foreach ($entries as $entry)
        @php
            $user_agent = $parser->parse($entry->agent);
        @endphp
        <div class="gb-entry{{ $entry->flagged ? " gb-entry-flagged" : "" }}">
            Submitted by <strong>{{ $entry->name }}</strong>
            on <strong>{{ $entry->created_at->format('Y-m-d') }}</strong>
            at <strong>{{ $entry->created_at->format('h:i:s A (e)') }}</strong>
            <hr>
            <span class="guestbook-message">{{ $entry->message }}</span>
            <hr>
            @if($entry->agent === "Agent Unavailable")
                <address>Agent unavailable</address>
            @else
                @if ($user_agent->ua->toString() == "Other" || $user_agent->os->toString() == "Other")
                    <address>Posted using <strong>{{ $entry->agent }}</strong></address>
                @else
                    <address>Posted using <strong>{{ $user_agent->ua->toString() }}</strong>
                        on <strong>{{ $user_agent->os->toString() }}</strong></address>
                @endif
            @endif
            @if(Auth::check())
                <hr>
                @if($entry->flagged)
                <form action="{{ route('guestbook.flag',$entry->id) }}" method="GET">
                    @csrf
                    <button type="submit">Remove Flag</button>
                </form>
                &nbsp;
                @endif
                <form action="{{ route('guestbook.destroy',$entry->id) }}" method="POST">
                    @csrf
                    @method('DELETE')
                    <button type="submit">Delete</button>
                </form>
            @endif
        </div>
        <br>
    @endforeach
    @endif
</x-layout>
