<template>
  <div class="split-wrapper">
    <div ref="scalable" class="scalable" :style="{ width: state.localWidth || props.leftWidth, maxWidth: props.maxLeftWidth, minWidth: props.minLeftWidth }">
      <div class="left-content">
        <slot name="left-content">  </slot>
      </div>
      <div ref="separator" class="separator" @mousedown="startDrag"><i></i><i></i></div>
    </div>
    <div class="right-content">
      <slot name="right-content">  </slot>
    </div>
  </div>
</template>

<script setup lang="ts">
import {ref, PropType, reactive, watch} from 'vue';
import { throttle } from 'lodash-es';
const emits=defineEmits(['dragEnd','splitLeftWidthChange','update:leftWidth'])

const props = defineProps({
  leftWidth: {
    type: String as PropType<string>
  },
  maxLeftWidth: {
    type: String as PropType<string>,
    default: '100%'
  },
  minLeftWidth: {
    type: String as PropType<string>,
    default: '0px'
  },
  ifGetWidth:{
    type: Boolean as PropType<boolean>,
    default: false
  }
})

const scalable = ref<HTMLDivElement>();
const state = reactive({
  splitLeftWidth:0,
  timer:null,
  localWidth: null// 新增本地存储的宽度
})

let startX: number;
let startWidth: number;

// 拖拽中
// @throttle(20)
const onDrag = throttle(function (e: MouseEvent) {
  if (scalable.value) {
    const newWidth = startWidth + e.clientX - startX;
    scalable.value.style.width = `${newWidth}px`;
    state.splitLeftWidth = newWidth;
    state.localWidth = `${newWidth}px`; // 更新本地存储的宽度
    emits('update:leftWidth', `${newWidth}px`); // 同步到父组件
  }
}, 20);

// 拖拽结束
const dragEnd = () => {
  document.documentElement.style.userSelect = 'unset';
  document.documentElement.removeEventListener('mousemove', onDrag);
  document.documentElement.removeEventListener('mouseup', dragEnd);
  emits('dragEnd')
};

// 鼠标按下
const startDrag = (e: MouseEvent) => {
  startX = e.clientX;
  scalable.value && (startWidth = parseInt(window.getComputedStyle(scalable.value).width, 10));
  state.splitLeftWidth=startX
  document.documentElement.style.userSelect = 'none';
  document.documentElement.addEventListener('mousemove', onDrag);
  document.documentElement.addEventListener('mouseup', dragEnd);
};

watch(()=>state.splitLeftWidth,(nVal:number)=>{
  if(props.ifGetWidth){
    if(state.timer){
      clearInterval(state.timer)
    }
    state.timer=setTimeout(()=>{
      emits('splitLeftWidthChange',nVal)
    },1000)
  }
})
// 新增：监听props变化但只在初始时生效
watch(() => props.leftWidth, (newVal) => {
  // if (!state.localWidth) { // 只有本地宽度未设置时才更新
  state.localWidth = newVal;
  // }
},{immediate:true});
</script>

<style lang="less">
@import 'theme.less';

@classNames: split-wrapper, separator;
.themeBgColor(@classNames);

.split-wrapper {
  display: flex;
  width: 100%;
  height: 100%;

  .scalable {
    position: relative;
    width: 240px;
    min-width: 100px;
    max-width: 50vw;
    overflow: auto;

    .left-content {
      height: 100%;
      padding-right: 8px; //12px 20px 12px 12px;
      padding-top:0;
    }

    .separator {
      display: flex;
      position: absolute;
      top: 0;
      right: 0;
      align-items: center;
      justify-content: center;
      width: 10px;
      height: 100%;
      box-shadow: -4px -2px 4px -5px rgb(0 0 0 / 35%), 4px 3px 4px -5px rgb(0 0 0 / 35%);
      cursor: col-resize;
      border:1px solid #d1dbe5;

      i {
        width: 1px;
        height: 14px;
        margin: 0 1px;
        background-color: #e9e9e9;
      }
    }
  }

  .right-content {
    flex: 1;
  }

  .left-content,
  .right-content {
    overflow: auto;
  }
}
</style>
