<template>
  <div>
    <list-page-top
      :breads="breads"
      :show-create="true"
      :show-refresh="true"
      :show-delete="showDelete"
      @create="create"
      @delete="deleteBypasses"
      @refresh="getBypasses"
    />
    <v-data-table
      v-model="selected"
      v-model:sort-by="sortBy"
      :headers="headers"
      :items="bypasses"
      :items-per-page-options="[5, 10, 15, 20, 50, 100]"
      :items-per-page="15"
      item-value="name"
      density="compact"
      show-select
    >
      <template #item.name="{ item }">
        <router-link
          style="color: inherit"
          :to="{ name: 'bypassEdit', params: { id: item.id } }"
        >
          {{ item.name }}
        </router-link>
      </template>
      <template #item.updated_at="{ item }">
        <date-time-display :timestamp="item.updated_at" />
      </template>
      <template #item.actions="{ item }">
        <v-menu>
          <template #activator="{ props: activatorProps }">
            <v-btn variant="text" icon size="x-small" v-bind="activatorProps">
              <v-icon>fa-ellipsis-v</v-icon>
            </v-btn>
          </template>
          <v-list class="ml-2 mr-2">
            <v-list-item key="view" link>
              <router-link
                class="text-decoration-none"
                style="color: inherit"
                :to="{ name: 'bypassEdit', params: { id: item.id } }"
              >
                <v-list-item-title>
                  <v-icon>fa-binoculars</v-icon>
                  View
                </v-list-item-title>
              </router-link>
            </v-list-item>
            <v-list-item
              key="copy"
              :to="{ name: 'bypassNew', query: { copy: true, id: item.id } }"
              link
            >
              <v-list-item-title>
                <v-icon>fa-clone</v-icon>
                Copy
              </v-list-item-title>
            </v-list-item>
            <v-divider class="pb-4" />
            <v-list-item key="delete" link @click="deleteBypass(item)">
              <v-list-item-title>
                <v-icon>fa-trash-alt</v-icon>
                Delete
              </v-list-item-title>
            </v-list-item>
          </v-list>
        </v-menu>
      </template>
    </v-data-table>
  </div>
</template>

<script>
import ListPageTop from "@/components/ListPageTop.vue";
import DateTimeDisplay from "@/components/DateTimeDisplay.vue";
import { useBypassStore } from "@/stores/bypass-module";
import { mapState } from "pinia";

export default {
  name: "Bypasses",
  components: {
    DateTimeDisplay,
    ListPageTop,
  },
  inject: ["confirm"],
  data() {
    return {
      breads: [
        {
          title: "Bypasses",
          disabled: true,
          href: "/bypasses",
        },
      ],
      headers: [
        { title: "Name", key: "name" },
        { title: "Updated At", key: "updated_at" },
        { title: "Actions", key: "actions", sortable: false },
      ],
      sortBy: [{ key: "name", order: "asc" }],
      selected: [],
    };
  },
  computed: {
    bypassStore() {
      return useBypassStore();
    },
    ...mapState(useBypassStore, ["bypasses"]),
    showDelete() {
      return this.selected.length > 0;
    },
  },
  mounted() {
    this.getBypasses();
  },
  methods: {
    getBypasses() {
      this.bypassStore.getBypasses();
    },
    create() {
      this.$router.push({ name: "bypassNew" });
    },
    view(item) {
      this.$router.push({ name: "bypassEdit", params: { id: item.id } });
    },
    async deleteBypass(item) {
      if (
        await this.confirm(
          "Delete",
          `Are you sure you want to delete bypass ${item.name}?`,
          { color: "red" },
        )
      ) {
        await this.bypassStore.deleteBypass(item.id);
      }
    },
    async deleteBypasses() {
      if (
        await this.confirm(
          "Delete",
          `Are you sure you want to delete ${this.selected.length} bypasses?`,
          { color: "red" },
        )
      ) {
        this.selected.forEach((bypass) => {
          this.bypassStore.deleteBypass(bypass.id);
        });
      }
    },
  },
};
</script>

<style></style>
