<!DOCTYPE html>
<html lang="en">
<head>
    <title>GoCMS | Posts</title>
    {{.Head}}

    <style>
        .btn-outline-danger:hover > * {
            fill: #fff !important;
            transition: fill ease-in-out 50ms;
        }
    </style>
</head>
<body class="text-dark">
{{.Navbar}}
<div class="container mt-3 text-black-50">
    <h1>Posts</h1>
    <p>List of your posts</p>
    <table class="table table-hover">
        <thead>
        <tr>
            <th scope="col">Name</th>
            <th scope="col">Creation Date</th>
            <th scope="col">Edition Date</th>
            <th scope="col">Actions</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td colspan="4" class="text-center">
                <form action="post/create" method="get">
                    <button class="btn btn-sm btn-link w-100 h-100" type="submit">Create a new post...</button>
                </form>
            </td>
        </tr>
        {{ range $post := .Posts }}
        <tr>
            <td>{{ $post.Title }}</td>
            <td class="date">{{ $post.CreatedAt }}</td>
            <td class="date">{{ $post.UpdatedAt }}</td>
            <td>
                <a href="/post/{{ $post.ID }}/edit" class="btn btn-secondary btn-sm">
                    <svg width="12px" height="12px" fill="currentColor">
                        <use xlink:href="/static/bootstrap-icons.svg#pen"/>
                    </svg>
                </a>
                <a href="/post/{{ $post.Title }}/delete" class="btn btn-outline-danger btn-sm" data-bs-toggle="modal"
                   data-bs-target="#{{ $post.ID }}">
                    <svg width=" 12px" height="12px" fill="currentColor">
                        <use xlink:href="/static/bootstrap-icons.svg#trash"/>
                    </svg>
                </a>
                <div class="modal fade" id="{{ $post.ID }}" tabindex="-1" aria-labelledby="{{ $post.ID }}Label"
                     aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h1 class="modal-title fs-5" id="{{ $post.ID }}Label">Delete post</h1>
                                <button type="button" class="btn-close" data-bs-dismiss="modal"
                                        aria-label="Close"></button>
                            </div>
                            <div class="modal-body">
                                <p>You are about to delete the post <b>« {{ $post.Title }} »</b>, this is definitive, are you sure?</p>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel
                                </button>
                                <a href="/post/{{$post.ID}}/delete" class="btn btn-danger">Delete</a>
                            </div>
                        </div>
                    </div>
                </div>
            </td>
        </tr>
        {{ end }}
        </tbody>
    </table>

</div>

<script>
    const datesElements = document.querySelectorAll("td.date")
    datesElements.forEach(date => {
        date.innerText = new Date(date.innerText).toLocaleDateString(undefined, {
            year: "2-digit",
            month: "2-digit",
            day: "2-digit",
            hour: "2-digit",
            minute: "2-digit"
        })
    })
</script>

</body>
</html>
