<template>
  <div class="app-accordion">
    <h3
      class="
        app-accordion-header
        mb-0
        d-flex
        justify-content-between
        align-items-center
      "
      @click="handlerClick"
    >
      <span class="app-accordion-button">{{ title }}</span>
      <div class="app-accordion-icon d-flex align-items-center">
        <MngIcon
          :name="isShow ? 'chevron-up' : 'chevron-down'"
        />
      </div>
    </h3>

    <div :class="['app-accordion-collapse', { show: show }]">
      <transition name="fade">
        <div class="app-accordion-body" v-if="isShow">
          <slot></slot>
        </div>
      </transition>
    </div>
  </div>
</template>

<script>
export default {
  name: "app-accordion",
  props: {
    title: {
      type: String,
      default: "",
    },
    show: {
      type: Boolean,
      default: false,
    },
    collapse: {
      type: Boolean,
      default: true,
    },
  },
  emits: ["click"],
  data() {
    return {
      visible: this.show,
    };
  },
  methods: {
    handlerClick() {
      if (this.collapse) {
        this.visible = !this.visible;
      } else {
        this.visible = true;
        this.$emit("click");
      }
    },
  },
  computed: {
    isShow() {
      return this.visible && this.collapse;
    },
  },
};
</script>

<style lang="sass" scoped>
@import "../assets/_vars"
@import "../assets/_transition"

.app
  &-accordion
    border-bottom: 1px solid $gray6
    &-header
      padding: 1rem 0
      cursor: pointer
    &-collapse
      // height: 0
      transition: height 0.3s ease
      .show
        // height: auto
        transition: height 0.3s ease
    &-body
      padding: 1rem
</style>