<template>
  <AppSection label="headers">
    <ul v-if="headers.length !== 0">
      <li>
        <div class="row-wrapper">
          <label for="headerList">{{ $t("header_list") }}</label>
          <div>
            <button
              v-tippy="{ theme: 'tooltip' }"
              :title="$t('clear')"
              class="icon button"
              @click="clearContent('headers', $event)"
            >
              <i class="material-icons">clear_all</i>
            </button>
          </div>
        </div>
      </li>
    </ul>
    <ul
      v-for="(header, index) in headers"
      :key="`${header.value}_${index}`"
      class="
        border-b border-dashed
        divide-y
        md:divide-x
        border-divider
        divide-dashed divide-divider
        md:divide-y-0
      "
      :class="{ 'border-t': index == 0 }"
    >
      <li>
        <SmartAutoComplete
          :placeholder="$t('header_count', { count: index + 1 })"
          :source="commonHeaders"
          :spellcheck="false"
          :value="header.key"
          autofocus
          @input="
            $store.commit('setKeyHeader', {
              index,
              value: $event,
            })
          "
          @keyup.prevent="setRouteQueryState"
        />
      </li>
      <li>
        <input
          class="input"
          :placeholder="$t('value_count', { count: index + 1 })"
          :name="'value' + index"
          :value="header.value"
          @change="
            $store.commit('setValueHeader', {
              index,
              value: $event.target.value,
            })
          "
          @keyup.prevent="setRouteQueryState"
        />
      </li>
      <div>
        <li>
          <button
            v-tippy="{ theme: 'tooltip' }"
            title="{
              content: header.hasOwnProperty('active')
                ? header.active
                  ? $t('turn_off')
                  : $t('turn_on')
                : $t('turn_off'),
            }"
            class="icon button"
            @click="
              $store.commit('setActiveHeader', {
                index,
                value: header.hasOwnProperty('active') ? !header.active : false,
              })
            "
          >
            <i class="material-icons">
              {{
                header.hasOwnProperty("active")
                  ? header.active
                    ? "check_box"
                    : "check_box_outline_blank"
                  : "check_box"
              }}
            </i>
          </button>
        </li>
      </div>
      <div>
        <li>
          <button
            v-tippy="{ theme: 'tooltip' }"
            :title="$t('delete')"
            class="icon button"
            @click="removeRequestHeader(index)"
          >
            <i class="material-icons">delete</i>
          </button>
        </li>
      </div>
    </ul>
    <ul>
      <li>
        <button class="icon button" @click="addRequestHeader">
          <i class="material-icons">add</i>
          <span>{{ $t("add_new") }}</span>
        </button>
      </li>
    </ul>
  </AppSection>
</template>

<script>
import { commonHeaders } from "~/helpers/headers"

export default {
  props: {
    headers: { type: Array, default: () => [] },
  },
  data() {
    return {
      commonHeaders,
    }
  },
  methods: {
    clearContent(headers, $event) {
      this.$emit("clear-content", headers, $event)
    },
    setRouteQueryState() {
      this.$emit("set-route-query-state")
    },
    removeRequestHeader(index) {
      this.$emit("remove-request-header", index)
    },
    addRequestHeader() {
      this.$emit("add-request-header")
    },
  },
}
</script>
