<template>
    <div>
        <!--指标越限明细 -->
        <TableHeader :showReset="false" @selectChange="selectChange" datePicker v-if="fullscreen"></TableHeader>
        <el-calendar v-model="value" :style="{ height: `calc(${prop.height} - ${headerHeight}px + ${fullscreen ? 0 : 56}px )`, overflow: 'auto' }">
            <template #date-cell="{ data }">
                <div style="height: 100%; padding: 8px" :style="{ background: setBackground(data.day) }">
                    <p :class="data.isSelected ? 'is-selected' : ''">
                        {{ data.day.split('-').slice(2).join('-') }}
                    </p>
                    <el-tooltip
                        effect="dark"
                        placement="top"
                        :hide-after="0"
                        v-if="list?.filter(item => item.time == data.day)[0]?.text || false"
                    >
                        <template #content>
                            <span v-html="list?.filter(item => item.time == data.day)[0]?.text || ''"></span>
                        </template>
                        <div
                            :style="{ height: `calc(${prop.height} / 5 - 47px)`, overflow: 'auto' }"
                            v-html="list?.filter(item => item.time == data.day)[0]?.text || ''"
                        ></div>
                    </el-tooltip>
                </div>
            </template>
        </el-calendar>
    </div>
</template>
<script setup lang="ts">
import { ref, onMounted, provide, reactive, watch, h } from 'vue'
import TableStore from '@/utils/tableStore'
import TableHeader from '@/components/table/header/index.vue'
import { getTimeOfTheMonth } from '@/utils/formatTime'
import { dayjs } from 'element-plus'

import { useRoute } from 'vue-router'
import { useTimeCacheStore } from '@/stores/timeCache'

const prop = defineProps({
    w: { type:  [String, Number]},
    h: { type:  [String, Number]},
    width: { type:  [String, Number]},
    height: { type:  [String, Number]},
    timeKey: { type:  [String, Number]},
    timeValue: { type: Object }
})

const headerHeight = ref(57)

const route = useRoute()
const timeCacheStore = useTimeCacheStore()

dayjs.en.weekStart = 1 //设置日历的周起始日为星期一
const value = ref(new Date())

const selectChange = (showSelect: any, height: any, datePickerValue?: any) => {
    headerHeight.value = height

    // 如果有传入 datePicker 的值
    if (datePickerValue) {
        // 更新表格参数
        tableStore.table.params.searchBeginTime = datePickerValue.timeValue?.[0]
        tableStore.table.params.searchEndTime = datePickerValue.timeValue?.[1]
    }
}

// 计算是否全屏展示
const fullscreen = computed(() => {
    const w = Number(prop.w)
    const h = Number(prop.h)
    if (!isNaN(w) && !isNaN(h) && w === 12 && h === 6) {
        // 执行相应逻辑
        return true
    } else {
        return false
    }
})

const list = ref([
    {
        time: '2025-11-01',
        key: 81,
        text: '3次谐波越限<br/>5次谐波越限<br/>三相不平衡越限'
    },
    {
        time: '2025-10-31',
        key: 81,
        text: '3次谐波越限<br/>5次谐波越限<br/>三相不平衡越限'
    },
    {
        time: '2025-11-08',
        key: 20,
        text: '3次谐波越限<br/>5次谐波越限<br/>三相不平衡越限'
    },
    {
        time: '2025-11-16',
        key: 20,
        text: '3次谐波越限<br/>5次谐波越限<br/>三相不平衡越限'
    },
    {
        time: '2025-11-23',
        key: 20,
        text: '3次谐波越限<br/>5次谐波越限<br/>三相不平衡越限'
    },
    {
        time: '2025-11-04',
        key: 0,
        text: ''
    },
    {
        time: '2025-10-05',
        key: 0,
        text: ''
    }
])
const tableStore: any = new TableStore({
    url: '/user-boot/dept/deptTree',
    method: 'POST',

    showPage: false,

    column: [],
      beforeSearchFun: () => {
        // 尝试从缓存获取时间值
        let beginTime, endTime

        if (fullscreen.value) {
            const cached = timeCacheStore.getCache(route.path)
            if (cached && cached.timeValue) {
                beginTime = cached.timeValue[0]
                endTime = cached.timeValue[1]
            }
        }

        // 如果缓存中没有则使用默认值
        tableStore.table.params.searchBeginTime = beginTime || prop.timeValue?.[0] || getTimeOfTheMonth(prop.timeKey)[0]

        tableStore.table.params.searchEndTime = endTime || prop.timeValue?.[1] || getTimeOfTheMonth(prop.timeKey)[1]
    },
    loadCallback: () => {
        tableStore.table.data = []
    }
})

const tableRef = ref([])

const setBackground = (value: string) => {
    let data = []
    data = list.value?.filter(item => item.time == value)

    if (data && data?.length > 0) {
        if (data[0].key < 10) {
            return '#33996690'
        } else if (data[0].key < 80) {
            return '#FFCC3390'
        } else if (data[0].key <= 100) {
            return '#Ff660090'
        }
    }

    return '#fff'
}

provide('tableRef', tableRef)

provide('tableStore', tableStore)

onMounted(() => {
    tableStore.index()
})
watch(
    () => prop.timeKey,
    val => {
        tableStore.index()
    }
)
watch(
    () => prop.timeValue, // 监听的目标（函数形式避免直接传递 props 导致的警告）
    (newVal, oldVal) => {
        tableStore.index()
    },
    {
        deep: true // 若 timeValue 是对象/数组，需开启深度监听
    }
)

const addMenu = () => {}
</script>
<style lang="scss" scoped>
:deep(.el-calendar) {
    .el-calendar__header {
        display: none;
    }
    .el-calendar__body {
        padding: 0px !important;
        height: 100%;
        .el-calendar-table {
            height: 100%;
        }
    }
    .el-calendar-day {
        height: 100%;
        padding: 0px;
        overflow: hidden;
    }
    .el-calendar-table__row {
        .next {
            pointer-events: none;
        }
        .prev {
            pointer-events: none;
        }
    }
    .el-calendar-table .el-calendar-day:hover {
        background-color: #ffffff00;
    }
    .el-calendar-table td.is-selected {
        background-color: #ffffff00;
    }
}

// /*calendar_class 是el-calendar所在父标签的css*/
// .calendar_class >>> .el-calendar-table:not(.is-range) td.next {
//     pointer-events: none;
// }
// .calendar_class >>> .el-calendar-table:not(.is-range) td.prev {
//     pointer-events: none;
// }
</style>
