{{ define "title" }}pulls &middot; {{ .RepoInfo.FullName }}{{ 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" (string .RepoInfo.Stats.PullCount.Open)) }}
  {{ $merged :=
     (dict
       "Key" "merged"
       "Value" "merged"
       "Icon" "git-merge"
       "Meta" (string .RepoInfo.Stats.PullCount.Merged)) }}
  {{ $closed :=
     (dict
       "Key" "closed"
       "Value" "closed"
       "Icon" "ban"
       "Meta" (string .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 .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/{{ .PullId }}" class="dark:text-white">
                        {{ .Title | description }}
                        <span class="text-gray-500 dark:text-gray-400">#{{ .PullId }}</span>
                    </a>
                </div>
                <div class="text-sm text-gray-500 dark:text-gray-400 flex flex-wrap items-center gap-1">
                    {{ $bgColor := "bg-gray-800 dark:bg-gray-700" }}
                    {{ $icon := "ban" }}

                    {{ if .State.IsOpen }}
                        {{ $bgColor = "bg-green-600 dark:bg-green-700" }}
                        {{ $icon = "git-pull-request" }}
                    {{ else if .State.IsMerged }}
                        {{ $bgColor = "bg-purple-600 dark:bg-purple-700" }}
                        {{ $icon = "git-merge" }}
                    {{ end }}


                    <span
                        class="inline-flex items-center rounded px-2 py-[5px] {{ $bgColor }} text-sm"
                    >
                        {{ i $icon "w-3 h-3 mr-1.5 text-white" }}
                        <span class="text-white">{{ .State.String }}</span>
                    </span>

                    <span class="ml-1">
                        {{ template "user/fragments/picHandleLink" .OwnerDid }}
                    </span>

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

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

                    <span class="before:content-['·']">
                      round
                      <span class="font-mono">
                        #{{ .LastRoundNumber }}
                      </span>
                    </span>

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

                    {{ $state := .Labels }}
                    {{ range $k, $d := $.LabelDefs }}
                      {{ range $v, $s := $state.GetValSet $d.AtUri.String }}
                        {{ template "labels/fragments/label" (dict "def" $d "val" $v "withPrefix" true) }}
                      {{ end }}
                    {{ end }}
                </div>
              </div>
              {{ if .StackId }}
                {{ $otherPulls := index $.Stacks .StackId }}
                {{ if gt (len $otherPulls) 0 }}
                  <details class="bg-white dark:bg-gray-800 group">
                    <summary class="pb-4 px-6 text-xs list-none cursor-pointer hover:text-gray-500 hover:dark:text-gray-400">
                      {{ $s := "s" }}
                      {{ if eq (len $otherPulls) 1 }}
                        {{ $s = "" }}
                      {{ end }}
                      <div class="group-open:hidden flex items-center gap-2">
                        {{ i "chevrons-up-down" "w-4 h-4" }} expand {{ len $otherPulls }} pull{{$s}} in this stack
                      </div>
                      <div class="hidden group-open:flex items-center gap-2">
                        {{ i "chevrons-down-up" "w-4 h-4" }} hide {{ len $otherPulls }} pull{{$s}} in this stack
                      </div>
                    </summary>
                    {{ block "stackedPullList" (list $otherPulls $) }} {{ end }}
                  </details>
                {{ end }}
              {{ end }}
            </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 }}

{{ define "stackedPullList" }}
  {{ $list := index . 0 }}
  {{ $root := index . 1 }}
  <div class="grid grid-cols-1 rounded-b border-b border-t border-gray-200 dark:border-gray-900 divide-y divide-gray-200 dark:divide-gray-900">
    {{ range $pull := $list }}
      {{ $pipeline := index $root.Pipelines $pull.LatestSha }}
      <a href="/{{ $root.RepoInfo.FullName }}/pulls/{{ $pull.PullId }}" class="no-underline hover:no-underline hover:bg-gray-100/25 hover:dark:bg-gray-700/25">
        <div class="flex gap-2 items-center px-6">
          <div class="flex-grow min-w-0 w-full py-2">
            {{ template "repo/pulls/fragments/summarizedPullHeader" (list $pull $pipeline) }}
          </div>
        </div>
      </a>
    {{ end }}
  </div>
{{ end }}
