---
import DefaultLayout from '../../layouts/DefaultLayout.astro'
import { Card, Pagination } from 'accessible-astro-components'
import blogPosts from '../../../public/blog/blog-posts.json'

export async function getStaticPaths({ paginate }) {
  //const response = await fetch('https://jsonplaceholder.typicode.com/posts')
  //const data = await response.json()
  const data = blogPosts;

  return paginate(data, { pageSize: 6 })
}

const { page } = Astro.props
---

<DefaultLayout
  title="Blog"
  description="Just my personal blog, where I write about cool projects and endeavours."
>
  <section class="my-12">
    <div class="space-content container">
      <h1>Blog</h1>
      <p class="text-2xl">
        Just my personal blog, where I write about cool projects and endeavours.
      </p>
    </div>
  </section>
  <section class="my-12">
    <div class="container">
      <p class="text-sm"><em>Post {page.start + 1} through {page.end + 1} of {page.total} total posts</em></p>
      <ul class="my-3">
        {
          page.data.map((post) => (
            <li>
              <Card
                url={'/blog/' + post.postId}
                title={post.title}
                footer={'Published: ' + post.date}
                img={post.img}
              >
                {post.body}
              </Card>
            </li>
          ))
        }
      </ul>
      <div class="mt-12 grid place-content-center">
        <Pagination
          firstPage={page.url.prev ? '/blog' : null}
          previousPage={page.url.prev ? page.url.prev : null}
          nextPage={page.url.next ? page.url.next : null}
          lastPage={page.url.next ? `/blog/${Math.round(page.total / page.size)}` : null}
          currentPage={page.currentPage}
          totalPages={Math.round(page.total / page.size)}
        />
      </div>
    </div>
  </section>
</DefaultLayout>

<style lang="scss">
  ul {
    display: grid;
    grid-template-columns: 1fr;
    grid-gap: 4rem;

    @media (min-width: 550px) {
      grid-template-columns: repeat(2, 1fr);
      grid-gap: 2rem;
    }

    @media (min-width: 950px) {
      grid-template-columns: repeat(3, 1fr);
    }
  }
</style>
