<template>
  <div :class="[
    'dynamic-form-group', 
    {
      'collapsed': collapsed,
      'collapsible': collapsible,
      'plain': plain,
    }
  ]">
    <h5 v-if="title" class="title" @click="collapsible ? collapsed = !collapsed : null">
      <span class="title-text">{{ title }}</span>
      <view class="title-right">
        <span class="title-right-text" v-show="collapsed">点击展开更多</span>
        <Icon :size="22" icon="arrow-down" v-if="collapsible" innerClass="collapsible-icon" />
      </view>
    </h5>
    <Row v-if="!collapsed" :justify="(justify as any)" :gutter="gutter">
      <slot />
    </Row>
  </div>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import Row from "@/components/layout/grid/Row.vue";
import Icon from "@/components/basic/Icon.vue";
const props = defineProps({
  /**
   * 标题
   */
  title: {
    type: String,
    default: "",
  },
  /**
   * 栅格间隔 px
   */
  gutter: {
    type: Number,
    default: null,
  },
  /**
   * flex 布局下的水平排列方式：start end center space-around space-between
   */
  justify: {
    type: String,
    default: "start",
  },
  /**
   * 是否可折叠
   */
  collapsible: {
    type: Boolean,
    default: false,
  },
  /**
   * 是否为朴素样式
   */
  plain: {
    type: Boolean,
    default: false,
  },
  /**
   * 是否默认折叠
   */
  collapsed: {
    type: Boolean,
    default: false,
  },
});

const collapsed = ref(props.collapsed);

defineOptions({
  options: {
    virtualHost: true,
    inheritAttrs: false,
    styleIsolation: 'shared',
  }
})
</script>

<style lang="scss">
.dynamic-form-group {
  padding: 10px 0;
  background-color: var(--dynamic-form-background-color);
  border-radius: var(--dynamic-form-border-radius);

  &.collapsed {
    .collapsible-icon {
      transform: rotate(0deg);
    }
  }
  &.collapsible {
    .title {
      cursor: pointer;
    }
  }

  .collapsible-icon {
    transform: rotate(180deg);
    transition: transform 0.3s ease-in-out;
    width: 16px;
    height: 16px;
  }

  .title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: var(--dynamic-form-text-color);
    margin: 0;
    margin-bottom: 12px;

    .title-right {
      display: flex;
      flex-direction: row;
      align-items: center;

      .title-right-text {
        font-size: 11px;
        margin-right: 10rpx;
        color: var(--dynamic-form-secondary-color);
      }
    }
  }

  &.plain {
    padding: 0;
    background-color: transparent;
  }
}
</style>