{{define "title"}}Message Inbox{{end}}
{{define "content"}}
<div class="container">
    <section class="hero is-info is-bold">
        <div class="hero-body">
            <div class="container">
                <h1 class="title">
                    <i class="fa fa-envelope mr-2"></i>
                    Messages
                </h1>
                <h2 class="subtitle">{{if .IsSentBox}}Sent{{else}}Inbox{{end}}</h2>
            </div>
        </div>
    </section>

    {{$Root := .}}
    {{$UserMap := .UserMap}}
    {{$Request := .Request}}

    <div class="block p-4">
        <div class="columns">
            <div class="column">
                <div class="card">
                    <header class="card-header has-background-link">
                        <p class="card-header-title has-text-light">
                            {{if .ViewThread}}Conversation with {{.ReplyTo}}{{else}}Inbox{{end}}
                        </p>
                    </header>

                    {{if .ViewThread}}
                    <div class="card-content">
                        <div class="block">
                            <form action="/messages/compose" method="POST">
                                {{InputCSRF}}
                                <input type="hidden" name="to" value="{{.ReplyTo}}">
                                <input type="hidden" name="from" value="inbox">
                                <textarea class="textarea" cols="80" rows="4"
                                    name="message"
                                    placeholder="Write a response"></textarea>
                                <p class="help mb-3">
                                    <a href="/markdown" target="_blank">Markdown formatting</a> supported.
                                </p>

                                <div class="notification is-warning is-light p-3 is-size-7">
                                    <i class="fa fa-info-circle mr-1"></i>
                                    <strong>Reminder:</strong> don't share sensitive information over Direct Messages.
                                    Please review the <a href="/privacy#direct-messages" target="_blank">Privacy Policy</a> and
                                    <a href="/tos#direct-messages" target="_blank">Terms of Service</a> as it pertains to DMs on
                                    the main website and the chat room.
                                </div>

                                <div class="columns is-mobile">
                                    <div class="column">
                                        <button type="submit" class="button is-success">Send Reply</button>
                                    </div>
                                    <div class="column is-narrow">
                                        <a href="/contact?intent=report&subject=report.message&id={{.MessageID}}"
                                            class="button has-text-danger ml-4">
                                            <span class="icon"><i class="fa fa-flag"></i></span>
                                            <span>Report</span>
                                        </a>
                                    </div>
                                </div>

                            </form>

                            <hr>
                        </div>
                        {{range .ViewThread}}
                        <div class="media block">
                            {{$SourceUser := $UserMap.Get .SourceUserID}}
                            <div class="media-left">
                                {{template "avatar-64x64" $SourceUser}}
                            </div>
                            <div class="media-content">
                                <p class="title is-4">
                                    {{$SourceUser.NameOrUsername}}

                                    {{if $SourceUser.IsAdmin}}
                                    <span class="tag is-danger is-light ml-2 p-1" style="font-size: x-small">
                                        <i class="fa fa-peace mr-1"></i>
                                        Admin
                                    </span>
                                    {{end}}
                                </p>
                                <p class="subtitle is-6">
                                    <span class="icon"><i class="fa fa-user"></i></span>
                                    <a href="/u/{{$SourceUser.Username}}">{{$SourceUser.Username}}</a>
                                    {{if not $SourceUser.Certified}}
                                    <span class="has-text-danger">
                                        <span class="icon"><i class="fa fa-certificate"></i></span>
                                        <span>Not Certified!</span>
                                    </span>
                                    {{end}}
                                </p>
                            </div>
                        </div>

                        <div class="block content">
                            {{ToMarkdown .Message}}
                        </div>
                        <div class="block">
                            <em>Sent <abbr title="{{.CreatedAt.Format "2006-01-02 15:04:05"}}">
                                {{SincePrettyCoarse .CreatedAt}} ago
                            </em>
                            {{if not .Read}}<span class="tag is-success ml-2">UNREAD</span>{{end}}

                            <!-- Our message? We can delete it. -->
                            {{if eq $Root.CurrentUser.ID $SourceUser.ID}}
                            <form action="/messages/delete" method="POST" class="is-inline" onsubmit="return confirm('Delete this message?')">
                                {{InputCSRF}}
                                <input type="hidden" name="id" value="{{.ID}}">
                                <input type="hidden" name="next" value="{{$Root.Request.URL.Path}}">
                                <button class="button has-text-danger is-outline is-small p-1 ml-4">
                                    <i class="fa fa-trash mr-2"></i>
                                    Delete
                                </button>
                            </form>
                            {{end}}
                        </div>

                        <hr class="block">
                        {{end}}

                        <!-- Pager footer -->
                        <div class="block">
                            <div class="level">
                                <div class="level-left">
                                    <div>
                                        Found <strong>{{.ThreadPager.Total}}</strong> message{{Pluralize64 .ThreadPager.Total}} in this thread
                                        (page {{.ThreadPager.Page}} of {{.ThreadPager.Pages}}).
                                    </div>

                                    <!-- "Delete ALL" Form -->
                                    <form action="/messages/delete" method="POST"
                                        class="is-inline"
                                        onsubmit="return confirm('Are you sure you want to delete this whole entire thread for both of you? It will be like you two had never chatted before at all.')">
                                        {{InputCSRF}}
                                        <input type="hidden" name="intent" value="delete-thread">
                                        <input type="hidden" name="id" value="{{$Root.MessageID}}">
                                        <input type="hidden" name="next" value="/messages">
                                        <button class="button has-text-danger is-outline is-small p-1 ml-4">
                                            <i class="fa fa-trash mr-2"></i>
                                            Delete whole thread
                                        </button>
                                    </form>
                                </div>
                                <div class="level-right">
                                    {{if .ThreadPager.HasPrevious}}
                                    <a href="{{$Request.URL.Path}}?{{QueryPlus "page" .ThreadPager.Previous}}" class="button">Previous</a>
                                    {{end}}
                                    {{if .ThreadPager.HasNext}}
                                    <a href="{{$Request.URL.Path}}?{{QueryPlus "page" .ThreadPager.Next}}" class="button">Next Page</a>
                                    {{end}}
                                </div>
                            </div>
                        </div>
                    </div>
                    {{else}}
                    <div class="card-content content">
                        <p>
                            You have <strong>{{.Unread}}</strong> unread message{{Pluralize64 .Unread}}. Select a message on the
                            other column to read the conversation here.
                        </p>
                    </div>
                    {{end}}
                </div>
            </div>

            <div class="column is-one-third">
                <div class="card block">
                    <header class="card-header has-background-link">
                        <p class="card-header-title has-text-light">Messages</p>
                    </header>

                    <div class="card-content">
                        <div class="tabs is-toggle is-fullwidth">
                            <ul>
                                <li{{if not .IsSentBox}} class="is-active"{{end}}>
                                    <a href="/messages">Inbox</a>
                                </li>
                                <li{{if .IsSentBox}} class="is-active"{{end}}>
                                    <a href="/messages?box=sent">Sent</a>
                                </li>
                            </ul>
                        </div>

                        <ul class="menu-list block">
                            {{$IsSentBox := .IsSentBox}}
                            {{range .Messages}}
                            <li>
                                <a href="/messages/read/{{.ID}}">
                                    <div>
                                        {{if $IsSentBox}}
                                            {{$User := $UserMap.Get .TargetUserID}}
                                            <strong>Sent to {{$User.Username}}</strong>
                                        {{else}}
                                            {{$User := $UserMap.Get .SourceUserID}}
                                            <strong>From {{$User.Username}}</strong>
                                        {{end}}
                                        {{if not .Read}}
                                            <span class="tag is-success">{{if $IsSentBox}}UNREAD{{else}}NEW{{end}}</span>
                                        {{end}}
                                    </div>
                                    <div class="my-1">
                                        <em>
                                            {{Substring .Message 48}}…
                                        </em>
                                    </div>
                                    <div>
                                        Sent {{.CreatedAt.Format "2006-01-02 15:04:05"}}
                                    </div>
                                </a>
                            </li>
                            {{end}}
                        </ul>

                        <!-- Pager footer -->
                        <div class="block">
                            <div>
                                Found <strong>{{.Pager.Total}}</strong> message{{Pluralize64 .Pager.Total}}
                                (page {{.Pager.Page}} of {{.Pager.Pages}}).
                            </div>

                            {{if .Pager.HasPrevious}}
                            <a href="/messages?{{QueryPlus "page" .Pager.Previous}}" class="button">Previous</a>
                            {{end}}
                            {{if .Pager.HasNext}}
                            <a href="/messages?{{QueryPlus "page" .Pager.Next}}" class="button">Next Page</a>
                            {{end}}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{{end}}
