{{ define "title" }}Pulls &middot; {{ .RepoInfo.FullName }} &middot; Tangled{{ end }}

{{ define "extrameta" }}
    {{ $title := "Pulls"}}
    {{ $url := printf "https://tangled.org/%s/pulls" .RepoInfo.FullName }}

    {{ template "repo/fragments/og" (dict "RepoInfo" .RepoInfo "Title" $title "Url" $url) }}
{{ end }}

{{ define "repoContent" }}
  {{ $active := .FilterState }}
  {{ $open :=
     (dict
       "Key" "open"
       "Value" "Open"
       "Icon" "git-pull-request"
       "Meta" (scaleFmt .RepoInfo.Stats.PullCount.Open)) }}
  {{ $merged :=
     (dict
       "Key" "merged"
       "Value" "Merged"
       "Icon" "git-merge"
       "Meta" (scaleFmt .RepoInfo.Stats.PullCount.Merged)) }}
  {{ $closed :=
     (dict
       "Key" "closed"
       "Value" "Closed"
       "Icon" "ban"
       "Meta" (scaleFmt .RepoInfo.Stats.PullCount.Closed)) }}
  {{ $values := list $open $merged $closed }}
  <div class="grid gap-2 grid-cols-[auto_1fr_auto] grid-row-2">
    <form id="search-form" class="flex relative col-span-3 sm:col-span-1 sm:col-start-2" method="GET">
      <div class="flex-1 flex relative">
        <input
          id="search-q"
          class="flex-1 py-1 pl-2 pr-10 mr-[-1px] rounded-r-none peer"
          type="text"
          name="q"
          value="{{ .FilterQuery }}"
          placeholder="Search pulls..."
        >
        <a
          {{ if $active }}href="?q=state:{{ $active }}"{{ else }}href="?"{{ end }}
          class="absolute right-3 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 hidden peer-[:not(:placeholder-shown)]:block"
        >
          {{ i "x" "w-4 h-4" }}
        </a>
      </div>
      <button
        type="submit"
        class="p-2 text-gray-400 border rounded-r border-gray-300 dark:border-gray-600"
      >
        {{ i "search" "w-4 h-4" }}
      </button>
    </form>
    <div class="sm:row-start-1">
      {{ template "fragments/tabSelector" (dict "Name" "state" "Values" $values "Active" $active "Include" "#search-q" "Form" "search-form") }}
    </div>
    <a
      href="/{{ .RepoInfo.FullName }}/pulls/new"
      class="col-start-3 btn-create text-sm flex items-center gap-2 no-underline hover:no-underline hover:text-white"
    >
      {{ i "git-pull-request-create" "w-4 h-4" }}
      <span>New</span>
    </a>
  </div>
  <div class="error" id="pulls"></div>
{{ end }}

{{ define "repoAfter" }}
    <div class="flex flex-col gap-2 mt-2">
        {{ range $topPR := .Pulls }}
          <div class="rounded bg-white dark:bg-gray-800">
            <div class="px-6 py-4 z-5">
                <div class="pb-2">
                    <a href="/{{ $.RepoInfo.FullName }}/pulls/{{ $topPR.PullId }}" class="dark:text-white">
                        {{ $topPR.Title | description }}
                        <span class="text-gray-500 dark:text-gray-400">#{{ $topPR.PullId }}</span>
                    </a>
                </div>
                <div class="text-sm text-gray-500 dark:text-gray-400 flex flex-wrap items-center gap-1">
                    {{ template "repo/pulls/fragments/pullState" $topPR.State }}
                    <span class="ml-1 flex items-center gap-1">
                        {{ template "user/fragments/picLink" (list $topPR.OwnerDid.String "size-6" (index $.VouchRelationships $topPR.OwnerDid)) }}
                    <a href="/{{ resolve $topPR.OwnerDid.String }}">{{ resolve $topPR.OwnerDid.String }}</a>
                    </span>

                    <span class="before:content-['·']">
                        {{ template "repo/fragments/time" $topPR.Created }}
                    </span>

                    <span class="before:content-['·']">
                      {{ $commentCount := $topPR.TotalComments }}
                      {{ $commentCount }} comment{{ if ne $commentCount 1 }}s{{ end }}
                    </span>

                    <span class="before:content-['·']">
                      Round
                      <span class="font-mono">
                        #{{ $topPR.LatestVersionNumber }}
                      </span>
                    </span>

                    {{ $pipeline := index $.Pipelines $topPR.LatestVersion.Head }}
                    {{ if and $pipeline $pipeline.Id }}
                      <span class="before:content-['·']"></span>
                      {{ template "repo/pipelines/fragments/pipelineSymbol" (dict "Pipeline" $pipeline "ShortSummary" true) }}
                    {{ end }}

                    {{ $state := $topPR.Labels }}
                    {{ range $k, $d := $.LabelDefs }}
                      {{ range $v, $s := $state.GetValSet $d.AtUri.String }}
                        {{ template "labels/fragments/label" (dict "def" $d "val" $v "withPrefix" true "baseFilterQuery" $.BaseFilterQuery "basePath" (printf "/%s/pulls" $.RepoInfo.FullName)) }}
                      {{ end }}
                    {{ end }}
                </div>
              </div>
            </div>
        {{ end }}
    </div>
    {{if gt .PullCount .Page.Limit }}
      {{ template "fragments/pagination" (dict
          "Page" .Page
          "TotalCount" .PullCount
          "BasePath" (printf "/%s/pulls" .RepoInfo.FullName)
          "QueryParams" (queryParams "q" .FilterQuery)
      ) }}
    {{ end }}
{{ end }}
