<template>
    <div>
        <!--敏感负荷列表 -->
        <TableHeader ref="TableHeaderRef" :showReset="false" showCustomColumn @selectChange="selectChange" v-if="fullscreen"
            :timeKeyList="prop.timeKey">
            <template #select>
                <el-form-item label="关键字筛选">
                    <el-input maxlength="32" show-word-limit style="width: 240px"
                        v-model.trim="tableStore.table.params.searchValue" clearable placeholder="请输入用户名称" />
                </el-form-item>
            </template>
        </TableHeader>
        <Table ref="tableRef" @cell-click="cellClickEvent"
            :height="`calc(${prop.height} - ${headerHeight}px + ${fullscreen ? -58 : 56}px  )`" isGroup></Table>
    </div>
</template>
<script setup lang="ts">
import { ref, onMounted, provide, reactive, watch, h } from 'vue'
import TableStore from '@/utils/tableStore'
import Table from '@/components/table/index.vue'
import TableHeader from '@/components/table/header/index.vue'
import { useDictData } from '@/stores/dictData'
import { getTime } from '@/utils/formatTime'

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

const TableHeaderRef = ref()

const dictData = useDictData()
const sensitiveUserType = dictData.getBasicData('Interference_Source')

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 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 OverLimitDetailsRef = ref()
const tableStore: any = new TableStore({
    url: '/cs-harmonic-boot/pqSensitiveUser/getListByUser',
    method: 'POST',
    showPage: fullscreen.value ? true : false,
    column: [
        {
            field: 'index',
            title: '序号',
            width: '80',
            formatter: (row: any) => {
                return (tableStore.table.params.pageNum - 1) * tableStore.table.params.pageSize + row.rowIndex + 1
            }
        },
        {
            title: '用户名称',
            field: 'name',
            minWidth: '90'
        },

        {
            title: '用户类型',
            field: 'loadType',
            minWidth: '70',
            formatter: row => {
                return sensitiveUserType.filter(item => item.id == row.cellValue)[0]?.name
            }
        },
        {
            title: '是否监测',
            field: 'isMonitor',
            minWidth: '80',
            formatter: (row: any) => {
                return row.cellValue || '/'
            }
        },
        {
            title: '是否治理',
            field: 'isGovern',
            minWidth: '80',
            formatter: (row: any) => {
                return row.cellValue || '/'
            }
        }
    ],
    beforeSearchFun: () => {
        setTime()
    },

    loadCallback: () => { }
})
tableStore.table.params.searchValue = ''
const tableRef = ref()
provide('tableRef', tableRef)

provide('tableStore', tableStore)

// 点击行
const cellClickEvent = ({ row, column }: any) => {
    if (column.field != 'name') {
        OverLimitDetailsRef.value.open(row)
    }
}

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 不是一个有效数组')
    // }
}

onMounted(() => {
    setTimeout(() => {
        tableStore.index()
    }, 500)
})
watch(
    () => prop.timeKey,
    val => {
        tableStore.index()
    }
)
watch(
    () => prop.timeValue,
    (newVal, oldVal) => {
        tableStore.index()
    },
    {
        deep: true
    }
)
watch(
    () => prop.flag,
    val => {
        tableStore.showPage = fullscreen.value ? true : false
    }
)
</script>
<style lang="scss" scoped></style>
