<template>
  <div class="card">
    <header class="card-header has-background-grey-lighter is-unselectable">
      <p class="card-header-title" @click="toggle">{{ header }}</p>
      <p v-if="refreshable" class="card-header-icon px-0">
        <button class="tag button icon is-info" @click="refresh">
          <font-awesome-icon icon="fa-solid fa-arrows-rotate" />
        </button>
      </p>
      <button class="card-header-icon" @click="toggle">
        <span class="icon">
          <font-awesome-icon
            :icon="'fa-solid fa-angle-' + (is_open ? 'down' : 'right')"
          />
        </span>
      </button>
    </header>

    <template v-if="is_open">
      <slot v-if="ready" name="default" />
      <div v-else class="card-content">
        <progress class="progress is-info" max="100" />
      </div>
    </template>
  </div>
</template>

<script lang="ts">
import { Options, Vue } from "vue-class-component";

@Options({
  props: {
    header: {
      type: String,
      required: false,
      default: "",
    },
    ready: Boolean,
    refreshable: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  emits: ["open"],
})
export default class extends Vue {
  public header!: string;
  public ready!: boolean;
  public refreshable!: boolean;

  public is_open = false;

  public toggle() {
    this.is_open = !this.is_open;
    this.refresh();
  }

  public refresh() {
    if (!this.is_open) return;
    this.$emit("open");
  }
}
</script>

<style lang="scss" scoped>
div.card {
  &:not(:last-child) {
    margin-bottom: 1.5rem;
  }
  > .card-header {
    cursor: pointer;
  }
}
</style>
