<template>
  <div class="count-down" v-if="durationTime > 0">
    <div class="title">会议倒计时</div>
    <div class="count">{{ remainTime }}</div>
  </div>
</template>
<script setup name="CountDown">
import dayjs from "dayjs";
import duration from "dayjs/plugin/duration";
import { onMounted, onUnmounted, ref } from "vue";
dayjs.extend(duration);
const props = defineProps({
  startTime: {
    type: String,
    required: true,
    // default: "2022-10-18 21:22:23",
  },
});
const durationTime = ref();
const remainTime = ref();
let timer;
onMounted(() => {
  timer = setInterval(() => {
    durationTime.value =
      new Date(props.startTime).getTime() - new Date().getTime();
    if (durationTime.value <= 0) {
      clearInterval(timer);
    }
    const duraDayjs = dayjs.duration(durationTime.value);
    remainTime.value = `${parseInt(
      duraDayjs.asHours()
    )}小时${duraDayjs.minutes()}分钟${duraDayjs.seconds()}秒`;
  }, 500);
});
onUnmounted(() => {
  clearInterval(timer);
});
</script>

<style scoped lang="scss">
.count-down {
  position: absolute;
  z-index: -1;
  top: 20%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  .title {
    font-size: 24px;
    font-weight: bold;
  }
  .count {
    font-size: 36px;
    margin-top: 20px;
  }
}
</style>
