<template>
    <div>
        <!--主要监测点列表 -->
        <TableHeader :showReset="false" showExport  showCustomColumn :timeKeyList="prop.timeKey" @selectChange="selectChange"
            v-if="fullscreen" ref="TableHeaderRef">
            <template v-slot:select>
                <el-form-item label="关键字筛选">
                    <el-input maxlength="32" show-word-limit 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, watch, computed } 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'

const prop = defineProps({
    w: { type: [String, Number] },
    h: { type: [String, Number] },
    width: { type: [String, Number] },
    height: { type: [String, Number] },
    timeKey: { type: Array as () => string[] },
    timeValue: { type: Object },
    interval: { type: Number },
    flag: { type: Boolean }
})
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: '/cs-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: 150,


        },
        { title: '设备名称', field: 'devName', minWidth: 150, align: 'center' },
        { title: '项目名称', field: 'projectName', minWidth: 150, align: 'center' },
        { title: '工程名称', field: 'engineeringName', minWidth: 150, align: 'center' },
        {
            title: '监测对象类型',
            field: 'objType',
            minWidth: '100',
            formatter: (row: any) => {
                return row.cellValue || '/'
            }
        },
        {
            title: '是否治理',
            field: 'govern',
            minWidth: '80',
            formatter: (row: any) => {
                return row.cellValue || '/'
            }
        },

        {
            title: '主要存在的电能质量问题', field: 'problems', minWidth: '250', render: 'customTemplate', customTemplate: (row: any) => {
                return `<span style='cursor: pointer;text-decoration: underline;'>${row.problems}</span>`
            }
        }
    ],
    beforeSearchFun: () => {
        setTime()
    },
    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 == 'problems') {

        let time = getTimeOfTheMonth('3');
        OverLimitDetailsRef.value.open(
            row,
            time[0],
            time[1],
            tableStore.table.data
        )
    }
}

const setTime = () => {
    // const time = getTime(
    //     (TableHeaderRef.value?.datePickerRef.interval || prop.interval) ?? 0,
    //     prop.timeKey,
    //     fullscreen.value
    //         ? [tableStore.table.params.searchBeginTime, tableStore.table.params.searchEndTime]
    //         : prop.timeValue
    // )

    // if (Array.isArray(time)) {
    //     tableStore.table.params.searchBeginTime = time[0]
    //     tableStore.table.params.searchEndTime = time[1]
    //     // TableHeaderRef.value?.setInterval(time[2] - 0)
    //     // TableHeaderRef.value?.setTimeInterval([time[0], time[1]])
    // } else {
    //     console.warn('获取时间失败，time 不是一个有效数组')
    // }
}

// 在组件挂载时设置缓存值到 DatePicker
onMounted(() => {
    tableStore.index()
})

watch(
    () => prop.timeValue,
    (newVal, oldVal) => {
        tableStore.index()
    },
    {
        deep: true
    }
)
watch(
    () => prop.flag,
    val => {
        tableStore.showPage = fullscreen.value ? true : false
    }
)

const addMenu = () => { }
</script>
<style lang="scss" scoped></style>
