<template>
  <div class="count-down">
    <!-- <div class="title">会议倒计时</div> -->
    <div v-if="durationTime > 0" class="count">将在{{ remainTime }}后开始</div>
    <div v-else class="count">已开始</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 emit = defineEmits(["timeup"]);
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();
    console.log(durationTime.value);
    if (durationTime.value <= 0 && durationTime.value >= -1200) {
      emit("timeup");
    }
    if (durationTime.value <= 0) {
      clearInterval(timer);
    }
    const duraDayjs = dayjs.duration(durationTime.value);
    remainTime.value = `${parseInt(
      duraDayjs.asDays()
    )}天${duraDayjs.hours()}时${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: 16px;
  //   font-weight: bold;
  // }
  .count {
    font-size: 16px;
    // margin-top: 20px;
  }
}
</style>
