<template>
  <div class="split-wrapper">
    <div ref="scalable" class="scalable" :style="{ width: width + 'px' }">
      <div class="left-content">
        <slot name="left-content"> {{ $cyt("左边内容区") }} </slot>
      </div>
      <div
        v-if="enableCollapse"
        ref="separator"
        class="separator"
        @mousedown="startDrag"
      >
        <i></i><i></i>
      </div>
    </div>
    <div class="right-content">
      <slot name="right-content"> {{ $cyt("右边内容区") }} </slot>
    </div>
  </div>
</template>

<script lang="ts">
export default {
  name: "c-split-panel",
  inheritAttrs: false,
};
</script>

<script setup lang="ts">
import { computed, ref } from "vue";

import { throttle } from "lodash";

const props = defineProps({
  width: { type: String, default: undefined },
  enableCollapse: {
    type: Boolean,
    default: false,
  },
});

const scalable = ref<HTMLDivElement>();

const leftRightGap = computed(() => `${props.enableCollapse ? 0 : 6}px`);

let startX: number;
let startWidth: number;

// 拖拽中
// @throttle(20)
const onDrag = throttle(function (e: MouseEvent) {
  scalable.value &&
    (scalable.value.style.width = startWidth + e.clientX - startX + "px");
}, 20);

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

// 鼠标按下
const startDrag = (e: MouseEvent) => {
  startX = e.clientX;
  scalable.value &&
    (startWidth = parseInt(window.getComputedStyle(scalable.value).width, 10));

  document.documentElement.style.userSelect = "none";
  document.documentElement.addEventListener("mousemove", onDrag);
  document.documentElement.addEventListener("mouseup", dragEnd);
};
</script>

<style lang="less" scoped>
.split-wrapper {
  display: flex;
  width: 100%;
  height: 100%;
  // background-color: white;

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

    .left-content {
      height: 100%;
      // background: red;
      // padding: 12px 20px 12px 12px;
    }

    .separator {
      position: absolute;
      top: 0;
      right: 0;
      display: flex;
      width: 14px;
      height: 100%;
      cursor: col-resize;
      background-color: white;
      box-shadow: -4px -2px 4px -5px rgba(0, 0, 0, 0.35),
        4px 3px 4px -5px rgba(0, 0, 0, 0.35);
      align-items: center;
      justify-content: center;

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

  .right-content {
    margin-left: v-bind(leftRightGap);
    flex: 1;
  }

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