<template>
  <div class="chart-box" :class="color">
    <div class="chart-title" v-if="name" ref="titleRef">
      <div class="name">
        <img :src="`/src/assets/images/common/chart-${color || 'icon'}.png`" alt="" />
        <span>{{name}}</span>
        <slot name="title-left"></slot>
      </div>
      <div class="slot">
        <slot name="title-right"></slot>
      </div>
    </div>
    <slot v-else name="title-name"></slot>
    <div class="chart-content" :style="{ height: contentHeight }">
        <div v-show="arrow" :class="['arrow',arrow]" @click="handleShrink">
            <el-icon class="icon" color="#141414"><DArrowLeft /></el-icon>
        </div>
        <slot></slot>
    </div>
  </div>
</template>

<script setup>
import {ref, onMounted, onBeforeUnmount, nextTick, watch} from 'vue'
const props = defineProps({
    name:{
        type:String,
        defalut:''
    },
    arrow:{
        type:String,
        defalut:''
    },
    color:{
        type:String,
        defalut:'icon'
    }
})

const titleRef = ref(null)
const contentHeight = ref('calc(100% - 38px)')
let resizeObserver = null

const updateContentHeight = () => {
    nextTick(() => {
        if (titleRef.value) {
            const titleHeight = titleRef.value.offsetHeight
            contentHeight.value = `calc(100% - ${titleHeight}px)`
        } else {
            // 如果没有标题，内容占满整个高度
            contentHeight.value = '100%'
        }
    })
}

const handleShrink = () =>{

}

onMounted(() => {
    updateContentHeight()
    
    // 使用 ResizeObserver 监听头部高度变化
    if (titleRef.value && window.ResizeObserver) {
        resizeObserver = new ResizeObserver(() => {
            updateContentHeight()
        })
        resizeObserver.observe(titleRef.value)
    }
})

onBeforeUnmount(() => {
    // 清理 ResizeObserver
    if (resizeObserver) {
        resizeObserver.disconnect()
        resizeObserver = null
    }
})

// 监听 name 变化，重新计算高度
watch(() => props.name, () => {
    updateContentHeight()
})
</script>

<style lang="scss" scoped>
.chart-box {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  border-radius: 4px;
  background: #232323;
  border: 0.6px solid rgb(255, 255, 255,0.4);

  .chart-title {
    width: 100%;
    min-height: 38px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: rgba(255, 255, 255, 0.05);
    padding-right: 12px;
    box-sizing: border-box;
    flex: none;
    .name {
      display: flex;
      align-items: center;
      padding: 0 12px;
      span{
        font-size: 18px;
        margin-left: 6px;
        font-family: 'SOURCEHANTIFINE';
        margin-right: 8px;
      }
    }
  }
  .chart-content{
    position: relative;
    width: 100%;
    padding: 4px 8px;
    box-sizing: border-box;
    .arrow{
        position: absolute;
        right: -16px;
        top:calc(50% - 40px);
        background: #fff;
        width: 16px;
        height: 80px;
        line-height: 80px;
        border-radius: 0 5px 5px 0;
        text-align: center;
        cursor: pointer;
    }
    .arrow-left{
        left: -16px;
        border-radius: 5px 0 0 5px;
        .icon{
            transform: rotate(180deg);
        }
    }
  }

  &.yellow{
    border: 0.6px solid #444444;
    border-radius: 8px;
    background: #191919;
    .chart-title{
      min-height: 59px;
      .name{
        color: #FFD489;
        span{
          font-size: 22px;
        }
      }
    }
    .chart-content{
      padding: 0;
    }
  }
}

.shrink-animation {  
  width: 100%; /* 初始宽度，与关键帧中的初始状态一致 */  
  overflow: hidden; /* 防止内容溢出 */  
  animation: shrink-horizontal 2s forwards; /* 应用动画，持续时间为2秒，动画结束后保持最终状态 */  
}

/* 定义动画关键帧 */  
@keyframes shrink-horizontal {  
  0% {  
    width: 100%; /* 初始宽度，可以是具体的像素值或百分比 */  
    transform: scaleX(1); /* 初始缩放比例 */  
  }  
  100% {  
    width: 0%; /* 最终宽度，这里设置为0表示完全收缩 */  
    transform: scaleX(0); /* 最终缩放比例，这里设置为0表示在X轴上完全收缩 */  
  }  
}
</style>