<script setup lang="ts">
import { onMounted, ref, watch } from 'vue';
import './BasicMessageBox.less'
import moment from 'moment';
import { handleGetServiceInfo } from '../../service';

// 当前时间
let nowTime = ref<string>('')
// 更新时间的计时器
let nowTimeInterval = ref<any>(null)
// 天气内容
let weatherInfo = ref<any>()
// 周几
let weekDay = ref<string>('')
// 周几的枚举
let daysEnum: any = {
    "Sunday": "星期天",
    "Monday": "星期一",
    "Tuesday": "星期二",
    "Wednesday": "星期三",
    "Thursday": "星期四",
    "Friday": "星期五",
    "Saturday": "星期六"
}

onMounted(async () => {
    // 开始准备时间的一些内容
    handleGetTodayMessage()
    // 拿到天气
    handleGetNowWeather()
})

// 拿到传入的数据
const props = defineProps<{
    currentService?: any;
}>();

// 监听传入的选中服务区
watch(
    () => props.currentService,
    (newVal, oldVal) => {
        // 开始准备时间的一些内容
        handleGetTodayMessage()
        // 拿到天气
        handleGetNowWeather()
    },
    { deep: true }
);

// 获取今日的一些时间信息
const handleGetTodayMessage = () => {
    nowTime.value = moment().format('YYYY-MM-DD HH:mm:ss')
    handleUpdateTime()
    weekDay.value = moment().format('dddd')
}

// 开始计时 每秒加1
const handleUpdateTime = () => {
    nowTimeInterval.value = setInterval(() => {
        nowTime.value = moment().format('YYYY-MM-DD HH:mm:ss')
    }, 1000)
}


// 拿到当前的天气
const handleGetNowWeather = async () => {
    const req: any = {
        ServerpartId: props.currentService?.SERVERPART_ID || "1230",
    }
    const data = await handleGetServiceInfo(req)
    let weatherObj = data.weatherModel
    weatherInfo.value = weatherObj
}

</script>

<template>
    <div class="BasicMessageBox">
        <div class="rightBox">
            <span class="nowTime">{{ nowTime || "" }}</span>
            <div class="weatherBox">
                <img class="weatherIcon" :src="weatherInfo?.CUR_WEATHER_PIC" />
                <span class="weatherText">{{ weatherInfo?.CUR_WEATHER || "-" }}</span>
            </div>
            <span class="nowDay">{{ weekDay ? daysEnum[weekDay] : "" }}</span>

        </div>
    </div>
</template>