<template>
  <div class="timer">
    <span class="hour">
      {{time}}
    </span>
    <span class="date">
      {{date}}
    </span>
  </div>
</template>

<script>
import {ref, onMounted, onBeforeUnmount} from "vue";
export default {
  name: "MyTimer",
  setup(){
    const date = ref("");
    const time = ref("");
    const getTime  = () =>{
      let year = new Date().getFullYear();
      let month;
      if(new Date().getMonth() + 1 < 10){
        month = "0" + (new Date().getMonth() + 1);
      }else{
        month = new Date().getMonth() + 1
      }
      let day;
      if(new Date().getDate() < 10){
        day = "0" + new Date().getDate()
      }else{
        day = new Date().getDate()
      }
      let hour;
      let minute;
      let second;
      if(new Date().getHours() < 10){
        hour = "0" + new Date().getHours()
      }else{
        hour = new Date().getHours()
      }

      if(new Date().getMinutes() < 10){
        minute = "0" + new Date().getMinutes()
      }else{
        minute = new Date().getMinutes()
      }

      if(new Date().getSeconds() < 10){
        second = "0" + new Date().getSeconds()
      }else{
        second = new Date().getSeconds()
      }

      date.value = year + "-"+ month +"-"+ day;
      time.value = hour+ ":"+ minute +":"+ second;
    }
    let timer = 0;
    onMounted(()=>{
      timer = setInterval(()=>{
        getTime()
      },1000)
    })
    onBeforeUnmount(() =>{
      clearInterval(timer);
      timer = 0;
    })
    return {
      date,
      time
    }
  }
}
</script>

<style scoped lang="scss">
.timer{
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: center;
  .hour{
    font-size: 20px;
    font-family: DIN;
    font-weight: bold;
    color: var(--theme-font-common-color);
  }
  .date{
    font-size: 12px;
    font-family: DIN;
    font-weight: bold;
    color: var(--theme-font-common-color);
    line-height: 18px;
  }

}
</style>