<template>
    <div>
        <!--主要监测点列表 -->
        <TableHeader :showReset="false" @selectChange="selectChange" v-if="fullscreen" datePicker ref="tableHeaderRef">
            <template v-slot:select>
                <el-form-item label="关键词">
                    <el-input v-model="tableStore.table.params.keywords" clearable placeholder="请输关键字" />
                </el-form-item>
            </template>
        </TableHeader>
        <Table
            ref="tableRef"
            @cell-click="cellClickEvent"
            :height="`calc(${prop.height} - ${headerHeight}px + ${fullscreen ? -58 : 56}px )`"
        ></Table>
        <!-- 指标越限详情 -->
        <OverLimitDetails ref="OverLimitDetailsRef" />
    </div>
</template>
<script setup lang="ts">
import { ref, onMounted, provide, reactive, watch, nextTick } from 'vue'
import TableStore from '@/utils/tableStore'
import Table from '@/components/table/index.vue'
import TableHeader from '@/components/table/header/index.vue'
import { getTimeOfTheMonth } from '@/utils/formatTime'
import OverLimitDetails from '@/components/cockpit/indicatorFittingChart/components/overLimitDetails.vue'
import { useRoute } from 'vue-router'
import { useTimeCacheStore } from '@/stores/timeCache'
import { log } from 'console'

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 OverLimitDetailsRef = ref()
const headerHeight = ref(57)

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

const tableHeaderRef = ref()

// 计算是否全屏展示
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 selectChange = (showSelect: any, height: any, datePickerValue?: any) => {
    headerHeight.value = height

    if (datePickerValue && datePickerValue.timeValue) {
        // 更新时间参数
        tableStore.table.params.searchBeginTime = datePickerValue.timeValue[0]
        tableStore.table.params.searchEndTime = datePickerValue.timeValue[1]
    }
}

const tableStore: any = new TableStore({
    url: '/harmonic-boot/mainLine/list',
    method: 'POST',
    showPage: fullscreen.value ? true : false,
    exportName: '主要监测点列表',
    column: [
        {
            field: 'index',
            title: '序号',
            width: '80',
            formatter: (row: any) => {
                return (tableStore.table.params.pageNum - 1) * tableStore.table.params.pageSize + row.rowIndex + 1
            }
        },
        {
            title: '监测点名称',
            field: 'lineName',
            minWidth: '90',
            render: 'customTemplate',
            customTemplate: (row: any) => {
                return `<span style='cursor: pointer;text-decoration: underline;'>${row.lineName}</span>`
            }
        },

        {
            title: '监测对象类型',
            field: 'objType',
            minWidth: '90'
        },
        {
            title: '是否治理',
            field: 'govern',
            minWidth: '70'
        },

        { title: '主要存在的电能质量问题', field: 'problems', minWidth: '150', showOverflow: true }
    ],
    beforeSearchFun: () => {
        tableStore.table.params.searchBeginTime = tableStore.table.params.searchBeginTime ?? prop.timeValue?.[0]
        tableStore.table.params.searchEndTime = tableStore.table.params.searchEndTime ?? prop.timeValue?.[1]
    },
    loadCallback: () => {
        tableStore.table.height = `calc(${prop.height} - 80px)`
    }
})

const tableRef = ref()
provide('tableRef', tableRef)
tableStore.table.params.keywords = ''
provide('tableStore', tableStore)

// 点击行
const cellClickEvent = ({ row, column }: any) => {
    if (column.field == 'lineName') {
        OverLimitDetailsRef.value.open(
            row,
            tableStore.table.params.searchBeginTime || prop.timeValue?.[0],
            tableStore.table.params.searchEndTime || prop.timeValue?.[1]
        )
    }
}

// 获取缓存的初始值
const getInitialInterval = (): 3 => {
    if (fullscreen.value) {
        const cached = timeCacheStore.getCache(route.path)
        if (cached && cached.interval !== undefined) {
            return cached.interval as 3 // 强制断言为 3 或根据实际类型调整
        }
    }
    return 3 // 明确返回字面量类型 3
}
// 外部总的时间值
const getInitialTimeValue = () => {
    if (fullscreen.value) {
        const cached = timeCacheStore.getCache(route.path)
        if (cached && cached.timeValue) {
            return cached.timeValue
        }
    }
    return prop.timeValue // 使用传入的默认值
}

// 在组件挂载时设置缓存值到 DatePicker
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></style>
