{{ define "title" }}{{ .Tab | capitalize }} &middot; {{.Spindle.Instance}} &middot; Tangled{{ end }}

{{ define "content" }}
  <div class="p-6">
    <p class="text-xl font-bold dark:text-white">Settings</p>
  </div>
  <div class="bg-white dark:bg-gray-800 p-6 rounded relative w-full mx-auto drop-shadow-sm dark:text-white">
    <section class="w-full grid grid-cols-1 md:grid-cols-4 gap-6">
      <div class="col-span-1">
        {{ template "user/settings/fragments/sidebar" . }}
      </div>
      <div class="col-span-1 md:col-span-3 flex flex-col gap-6">
        {{ template "spindleDash" . }}
      </div>
    </section>
  </div>
{{ end }}

{{ define "spindleDash" }}
<div>
  <div class="flex justify-between items-center">
    <h2 class="text-lg pb-2 font-medium">{{ .Tab | capitalize }} &middot; {{ .Spindle.Instance }}</h2>
    <div id="right-side" class="flex gap-2">
      {{ $style := "px-2 py-1 rounded flex items-center flex-shrink-0 gap-2" }}
      {{ $isOwner := and .LoggedInUser (eq .LoggedInUser.Did .Spindle.Owner)  }}
      {{ if .Spindle.Verified }}
        <span class="bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200 {{$style}}">{{ i "shield-check" "w-4 h-4" }} Verified</span>
        {{ if $isOwner }}
          {{ template "spindles/fragments/addMemberModal" .Spindle }}
        {{ end }}
      {{ else }}
        <span class="bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-200 {{$style}}">{{ i "shield-off" "w-4 h-4" }} Unverified</span>
        {{ if $isOwner }}
          {{ block "retryButton" .Spindle }} {{ end }}
        {{ end }}
      {{ end }}

      {{ if $isOwner }}
        {{ block "deleteButton" .Spindle }} {{ end }}
      {{ end }}
    </div>
  </div>
  <div id="operation-error" class="dark:text-red-400"></div>
</div>

{{ if .Members }}
  <section class="bg-white dark:bg-gray-800 p-6 rounded relative w-full mx-auto drop-shadow-sm dark:text-white">
    <div class="flex flex-col gap-2">
      {{ block "member" . }} {{ end }}
    </div>
  </section>
{{ end }}
{{ end }}


{{ define "member" }}
  {{ range .Members }}
    <div>
      <div class="flex justify-between items-center">
        <div class="flex items-center gap-2">
          {{ template "user/fragments/picHandleLink" . }}
        </div>
        {{ if ne $.LoggedInUser.Did . }}
          {{ block "removeMemberButton" (list $ . ) }} {{ end }}
        {{ end }}
      </div>
      <div class="ml-2 pl-2 pt-2 border-l border-gray-200 dark:border-gray-700">
        {{ $repos := index $.Repos . }}
        {{ range $repos }}
          <div class="flex gap-2 items-center">
            {{ i "book-marked" "size-4" }}
            <a href="/{{ .Did }}/{{ .Rkey }}">
              {{ .Name }}
            </a>
          </div>
        {{ else }}
          <div class="text-gray-500 dark:text-gray-400">
            No repositories configured yet.
          </div>
        {{ end }}
      </div>
    </div>
  {{ end }}
{{ end }}

{{ define "deleteButton" }}
  <button
    class="btn text-red-500 hover:text-red-700 dark:text-red-400 dark:hover:text-red-300 gap-2 group"
    title="Delete spindle"
    hx-delete="/settings/spindles/{{ .Instance }}"
    hx-swap="outerHTML"
    hx-confirm="Are you sure you want to delete the spindle '{{ .Instance }}'?"
    hx-headers='{"shouldRedirect": "true"}'
  >
    {{ i "trash-2" "w-5 h-5" }}
    <span class="hidden md:inline">Delete</span> 
    {{ i "loader-circle" "w-4 h-4 animate-spin hidden group-[.htmx-request]:inline" }}
  </button>
{{ end }}


{{ define "retryButton" }}
  <button
    class="btn gap-2 group"
    title="Retry spindle verification"
    hx-post="/settings/spindles/{{ .Instance }}/retry"
    hx-swap="none"
    hx-headers='{"shouldRefresh": "true"}'
  >
    {{ i "rotate-ccw" "w-5 h-5" }}
    <span class="hidden md:inline">Retry</span> 
    {{ i "loader-circle" "w-4 h-4 animate-spin hidden group-[.htmx-request]:inline" }}
  </button>
{{ end }}


{{ define "removeMemberButton" }}
  {{ $root := index . 0 }}
  {{ $member := index . 1 }}
    <button
      class="btn text-red-500 hover:text-red-700 dark:text-red-400 dark:hover:text-red-300 gap-2 group"
      title="Remove member"
      hx-post="/settings/spindles/{{ $root.Spindle.Instance }}/remove"
      hx-swap="none"
      hx-vals='{"member": "{{$member}}" }'
      hx-confirm="Are you sure you want to remove {{ resolve $member }} from this instance?"
    >
      {{ i "user-minus" "w-4 h-4" }}
      Remove
      {{ i "loader-circle" "w-4 h-4 animate-spin hidden group-[.htmx-request]:inline" }}
    </button>
{{ end }}

