<template>
  <div class="layout-lock-screen" style="cursor: pointer">
    <div
      class="layout-lock-screen-date"
      ref="layoutLockScreenRef"
      @mousedown="handleDown"
      @mousemove="handleMove"
      @mouseup="handleEnd"
      :style="{ cursor: isDraging ? 'point' : 'auto' }"
    >
      <div class="layout-lock-screen-date-box">
        <div class="layout-lock-screen-date-box-time">
          {{ currentTime.hoursAndMinutes }}
          <span class="layout-lock-screen-date-box-minutes">
            {{ currentTime.seconds }}
          </span>
        </div>
        <div class="layout-lock-screen-date-box-info">
          {{ currentTime.dateAndWeek }}
        </div>
      </div>
      <div class="layout-lock-screen-date-top">
        <i class="el-icon-top"></i>
        <div class="layout-lock-screen-date-top-text">上滑解锁</div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import {
  reactive,
  ref,
  defineComponent,
  onMounted,
  onBeforeUnmount,
} from "vue";
import { date2StrUtil } from "@/utils";
let TIMER_ID: NodeJS.Timer | undefined = undefined;

export default defineComponent({
  name: "lock-screen-drag-area",

  emits: ["slide-up"],

  setup(_, { emit }) {
    const layoutLockScreenRef = ref<HTMLElement | null>(null);
    const isDraging = ref(false); // 鼠标是否处于点击状态
    const transparency = ref(1); // 初始拖拽区域透明度
    const isSlideOver = ref(false); // 是否已经向上滑动结束
    const currentTime = reactive({
      hoursAndMinutes: "",
      seconds: "",
      dateAndWeek: "",
    });
    const position = reactive({
      downClientY: 0,
      moveDifference: 0,
    });
    // 鼠标按下
    const handleDown = ({ clientY }: MouseEvent) => {
      isDraging.value = true;
      position.downClientY = clientY;
    };
    // 鼠标移动
    const handleMove = ({ clientY }: MouseEvent) => {
      // 首先先点击鼠标
      if (isDraging.value) {
        const targetEle = layoutLockScreenRef.value;
        const opacitys = (transparency.value -= 1 / 200);

        const moveDifference = (position.moveDifference =
          clientY - position.downClientY);
        if (moveDifference >= 0) return;
        targetEle!.setAttribute(
          "style",
          `top:${moveDifference}px;opacity:${opacitys};`
        );

        if (moveDifference < -400) {
          targetEle!.setAttribute(
            "style",
            `top:${-targetEle!
              .clientHeight}px;cursor:pointer;transition:all 0.3s ease;`
          );
          position.moveDifference = -targetEle!.clientHeight;
          setTimeout(() => {
            targetEle && targetEle.parentNode?.removeChild(targetEle);
          }, 300);
        }
        if (
          position.moveDifference === -targetEle!.clientHeight &&
          !isSlideOver.value
        ) {
          isSlideOver.value = true; // 滑动结束 防止多次重复调用该代码
          emit("slide-up");
        }
      }
    };
    // 鼠标松开
    const handleEnd = () => {
      isDraging.value = false;
      transparency.value = 1;
      if (position.moveDifference >= -400) {
        const targetEle = layoutLockScreenRef.value;
        targetEle!.setAttribute(
          "style",
          `top:0px; opacity:1; transition:all 0.3s ease;`
        );
      }
    };

    const getCurrentTime = () => {
      const now = new Date();
      Object.assign(currentTime, {
        hoursAndMinutes: date2StrUtil(now, "HH:mm"),
        seconds: date2StrUtil(now, "ss"),
        dateAndWeek: date2StrUtil(now, "MM月DD日，星期d"),
      });
    };

    onMounted(() => {
      getCurrentTime();
      TIMER_ID = setInterval(() => {
        getCurrentTime();
      }, 1000);
    });

    onBeforeUnmount(() => {
      if (TIMER_ID) {
        clearInterval(TIMER_ID);
      }
    });

    return {
      isDraging,
      currentTime,
      layoutLockScreenRef,
      handleDown,
      handleMove,
      handleEnd,
    };
  },
});
</script>

<style lang="scss" scoped>
.layout-lock-screen-fixed {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.layout-lock-screen {
  @extend .layout-lock-screen-fixed;
  z-index: 9999992;
  &-date {
    @extend .layout-lock-screen-fixed;
    color: #ffffff;
    z-index: 9999993;
    user-select: none;
    &-box {
      position: absolute;
      left: 30px;
      bottom: 50px;
      &-time {
        font-size: 100px;
      }
      &-info {
        font-size: 40px;
      }
      &-minutes {
        font-size: 16px;
      }
    }
    &-top {
      width: 40px;
      height: 40px;
      line-height: 40px;
      border-radius: 100%;
      border: 1px solid rgba(255, 255, 255, 0.3);
      background: rgba(255, 255, 255, 0.1);
      color: #ffffff;
      opacity: 0.8;
      position: absolute;
      right: 30px;
      bottom: 50px;
      text-align: center;
      overflow: hidden;
      transition: all 0.3s ease;
      i {
        transition: all 0.3s ease;
      }
      &-text {
        opacity: 0;
        position: absolute;
        top: 150%;
        font-size: 12px;
        color: #ffffff;
        left: 50%;
        line-height: 1.2;
        transform: translate(-50%, -50%);
        transition: all 0.3s ease;
        width: 35px;
      }
      &:hover {
        border: 1px solid rgba(255, 255, 255, 0.5);
        background: rgba(255, 255, 255, 0.2);
        box-shadow: 0 0 12px 0 rgba(255, 255, 255, 0.5);
        color: #ffffff;
        opacity: 1;
        transition: all 0.3s ease;
        i {
          transform: translateY(-40px);
          transition: all 0.3s ease;
        }
        .layout-lock-screen-date-top-text {
          opacity: 1;
          top: 50%;
          transition: all 0.3s ease;
        }
      }
    }
  }
  &-login {
    position: relative;
    z-index: 9999994;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    color: #ffffff;
    &-box {
      text-align: center;
      margin: auto;
      &-img {
        width: 180px;
        height: 180px;
        margin: auto;
        img {
          width: 100%;
          height: 100%;
          border-radius: 100%;
        }
      }
      &-name {
        font-size: 26px;
        margin: 15px 0 30px;
      }
    }
    &-icon {
      position: absolute;
      right: 30px;
      bottom: 30px;
      i {
        font-size: 20px;
        margin-left: 15px;
        cursor: pointer;
        opacity: 0.8;
        &:hover {
          opacity: 1;
        }
      }
    }
  }
}
</style>
