{{ define "main" }}

{{ partial "page-title.html" . }}

<!-- checking blog -->
{{ if or (eq .Section "post") (eq .Section "posts") (eq .Section "blog") (eq .Section "blogs") (eq .Section "news") (eq .Section "categories") (eq .Section "tags") }}

<div class="page-wrapper">
  <div class="container">
    <div class="row">
      <div class="col-md-8">
        {{ $paginator := .Paginate .Data.Pages -}}
        {{ range $paginator.Pages -}}
        <div class="post">
          <div class="post-media post-thumb">
            {{ if isset .Params "image" -}}
            <a href="{{ .RelPermalink }}">
              <img src="{{ .Params.image | relURL }}" alt="{{ .Title }}">
            </a>
            {{- end }}
          </div>
          <h3 class="post-title"><a href="{{ .RelPermalink }}">{{ .Title }}</a></h3>
          <div class="post-meta">
            <ul>
              <li><i class="fas fa-calendar-week"></i> {{ time.Format ":date_long" .PublishDate }}</li>
              <li><i class="fas fa-user"></i>
                {{ i18n "posted_by" }}
                {{ $authors := slice }}{{ if reflect.IsSlice .Params.author }}{{ $authors = .Params.author }}{{ else }}{{ $authors = (slice .Params.author) }}{{ end -}}
                {{ range $index, $elements := $authors }}{{ if ne $index 0 }}, {{ end }}<a class="text-primary" href="{{ `author/` | relLangURL }}{{ . | urlize }}">{{ . }}</a>{{ end }}
              </li>
              <li><i class="fas fa-tags"></i>
                {{ range $index, $elements:= .Params.tags }}{{ if ne $index 0 }}, {{ end }}<a href="{{ `tags/` | relLangURL }}{{ . | urlize }}">{{ . }}</a>{{ end }}
              </li>
            </ul>
          </div>
          <div class="post-content">
            <p>{{ .Summary }}</p>
            <a href="{{ .RelPermalink }}" class="btn btn-main">{{ i18n "read_more" }}</a>
          </div>
        </div>
        {{- end }}

        <!-- pagination -->
        {{ $paginator := .Paginator -}}
        {{ $adjacent_links := 2 -}}
        {{ $max_links := (add (mul $adjacent_links 2) 1) -}}
        {{ $lower_limit := (add $adjacent_links 1) -}}
        {{ $upper_limit := (sub $paginator.TotalPages $adjacent_links) -}}
        {{ if gt $paginator.TotalPages 1 -}}
        <nav class="text-center">
          <ul class="pagination post-pagination">
            <!-- Previous page. -->
            {{ if $paginator.HasPrev -}}
            <li>
              <a href="{{ $paginator.Prev.URL }}" class="page-link">{{ i18n "page_prev" }}</a>
            </li>
            {{- end }}
            <!-- Page numbers. -->
            {{ range $paginator.Pagers -}}
            {{ $page_number_flag := false -}}
            <!-- Advanced page numbers. -->
            {{ if gt $paginator.TotalPages $max_links -}}
            <!-- Lower limit pages. -->
            <!-- If the user is on a page which is in the lower limit.  -->
            {{ if le $paginator.PageNumber $lower_limit -}}
            <!-- If the current loop page is less than max_links. -->
            {{ if le .PageNumber $max_links -}}
            {{ $page_number_flag = true -}}
            {{ end -}}
            <!-- Upper limit pages. -->
            <!-- If the user is on a page which is in the upper limit. -->
            {{ else if ge $paginator.PageNumber $upper_limit -}}
            <!-- If the current loop page is greater than total pages minus $max_links -->
            {{ if gt .PageNumber (sub $paginator.TotalPages $max_links) -}}
            {{ $page_number_flag = true -}}
            {{ end -}}
            <!-- Middle pages. -->
            {{ else -}}
            {{ if and (ge .PageNumber (sub $paginator.PageNumber $adjacent_links)) (le .PageNumber (add $paginator.PageNumber $adjacent_links)) -}}
            {{ $page_number_flag = true -}}
            {{ end -}}
            {{ end -}}
            <!-- Simple page numbers. -->
            {{ else -}}
            {{ $page_number_flag = true -}}
            {{ end -}}
            <!-- Output page numbers. -->
            {{ if eq $page_number_flag true -}}
            <li class="{{ if eq . $paginator }} active {{ end }}">
              <a href="{{ .URL }}">
                {{ .PageNumber }}
              </a>
            </li>
            {{- end }}
            {{- end }}
            <!-- Next page. -->
            {{ if $paginator.HasNext -}}
            <li>
              <a href="{{ $paginator.Next.URL }}">{{ i18n "page_next" }}</a>
            </li>
            {{- end }}
          </ul>
        </nav>
        {{- end }}
      </div>
      <div class="col-md-4">
        {{ partial "blog-sidebar.html" . }}
      </div>
    </div>
  </div>
</div>

<!-- regular page -->
{{ else -}}
{{ .Render "default" }}
{{- end }}
<!-- /regular page -->

{{ end }}
