<template>
  <div class="expansion-container" ref="expansionContainerRef">
    <el-row>
      <el-col
        ref="displayColRef"
        :span="props.rowCol.spanLeft"
        class="expansion-content"
        :class="{ 'display': stateDisplayExpansion.isDisplay }"
      >
      <slot name="content"></slot>
    </el-col>
    <el-col :span="props.rowCol.spanRight" class="expansion-warp">
        <div class="expansion-buttons">
            <el-button
                class="expansion-display"
                :icon="displayExpansionIcon"
                type="primary"
                link
                @click="hanleDisplayNone"
            >
                {{ displayExpansionText }}
            </el-button>
            <slot name="button"></slot>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script lang="ts" setup>
import { computed } from 'vue';
import { ArrowUp, ArrowDown } from "@element-plus/icons-vue";
import { useDisplayExpansion } from '@/hooks/useDisplayExpansion';

interface RowCol {
    spanLeft: number//左
    spanRight: number//右
}

interface Props {
    /**
     * 列表的class
     */
    itemDomClass?: string,
    /**
     * 默认展示高度
     */
    height: number,
    /**
     * el-row布局，默认左：18，右：6
     */
    rowCol: RowCol
}
// withDefaults ==> 用于辅助defineProps设置默认值(注意：默认值为引用类型的，需要包装一个函数 return 出去)
const props = withDefaults(defineProps<Props>(), {
    itemDomClass: 'el-form-item',
    height: 50,
    rowCol: () => {
        return {
            spanLeft: 18,
            spanRight: 6
        }
    }
})

// 展开文字
const displayExpansionText = computed(() => {
    return stateDisplayExpansion.isDisplay ? '折叠' : '展开'
})
// 展开文字icon
const displayExpansionIcon = computed(() => {
    return stateDisplayExpansion.isDisplay ? ArrowUp : ArrowDown
})

// 折叠与展开
const { displayColRef, expansionContainerRef, stateDisplayExpansion, hanleDisplayNone } = useDisplayExpansion(props.itemDomClass, props.height)

</script>
<style lang="scss" scoped>
.expansion-container {
    position: relative;
    width: 100%;
    height: 38px;
    z-index: 199;
    .expansion-content {
        position: absolute;
        top: 0;
        left: 0;
        transition: height 200ms;
        background-color: #ffffff;
        &.display {
            // padding: 0 2px;
            background-color: #ffffff;
            border: 1px solid #e4e7ed;
            border-top: none;
            box-shadow: 0px 5px 12px rgba(0, 0, 0, .12);
        }
    }
    .expansion-buttons {
        display: inline-flex;
        width: 100%;
        flex-wrap: wrap;
    }
    .expansion-warp {
        position: absolute;
        top: 0;
        right: 0;
        background-color: #ffffff;
        z-index: 199;
        min-width: max-content !important;
    }
    .expansion-display {
        // display: inline-flex;
        padding-top: 10px;
        margin-bottom: 10px;
    }
}
</style>