<template>
    <div class="view">
        <TableHeader datePicker ref="TableHeaderRef" :showReset="false" showExport>
            <template #operation>
                <el-button type="primary" :icon="Setting" @click="recall1">事件补召</el-button>
                <el-button type="primary" :icon="Setting" @click="recall2">波形补召</el-button>
            </template>
        </TableHeader>
        <Table ref="tableRef" />
    </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, provide, nextTick, defineEmits, watch } from 'vue'
import TableStore from '@/utils/tableStore'
import Table from '@/components/table/index.vue'
import TableHeader from '@/components/table/header/index.vue'
import { Setting } from '@element-plus/icons-vue'
import { eventRecall, fileRecall, } from '@/api/cs-device-boot/recall'
import { ElMessage } from 'element-plus'

const props = defineProps({
    checkedNodes: {
        type: Array,
        default: () => []
    }
})

const tableStore: any = new TableStore({
    url: '/cs-device-boot/csTerminalReply/bzLogs',
    publicHeight: 80,
    method: 'POST',
    exportName: '暂态补召',
    column: [
        {
            title: '序号',
            width: 80,
            formatter: (row: any) => {
                return (tableStore.table.params.pageNum - 1) * tableStore.table.params.pageSize + row.rowIndex + 1
            }
        },
        {
            field: 'logTime',
            title: '补召时间',
            width: 160,
            formatter: row => {
                return row.cellValue ? row.cellValue : '/'
            }
        },
        {
            field: 'log',
            title: '类型',
            width: 120,
            formatter: row => {
                return row.cellValue ? row.cellValue : '/'
            }
        },
        // {
        //     field: 'status',
        //     title: '状态',
        //     width: 120,
        //     formatter: row => {
        //         return row.cellValue ? row.cellValue : '/'
        //     }
        // },
        {
            field: 'result',
            title: '结果',
            minWidth: 300,
            formatter: row => {
                return row.cellValue ? row.cellValue : '/'
            }
        },
        // {
        //     field: 'engineeringName',
        //     title: '项目名称',
        //     width: 150,
        //     formatter: row => {
        //         return row.cellValue ? row.cellValue : '/'
        //     }
        // },
        // {
        //     field: 'projectName',
        //     title: '工程名称',
        //     width: 150,
        //     formatter: row => {
        //         return row.cellValue ? row.cellValue : '/'
        //     }
        // },
        // {
        //     field: 'deviceName',
        //     title: '设备名称',
        //     width: 150,
        //     formatter: row => {
        //         return row.cellValue ? row.cellValue : '/'
        //     }
        // },
        // {
        //     field: 'lineName',
        //     title: '监测点名称',
        //     width: 150,
        //     formatter: row => {
        //         return row.cellValue ? row.cellValue : '/'
        //     }
        // },

    ],
    beforeSearchFun: () => {
        if (!nodeClick.value || nodeClick.value.level !== 3) {
            ElMessage.warning('请先选中监测点')
            return // 阻止查询
        }
        if (nodeClick.value) {
            tableStore.table.params.searchValue = nodeClick.value.id
        }
    },
    loadCallback: () => { }
})
tableStore.table.params.sortBy = 'event'
provide('tableStore', tableStore)

const nodeClick = ref(null)
// 处理父组件传递的树节点点击事件
const handleTreeNodeClick = (node: any) => {
    nodeClick.value = node
    if (tableStore && tableStore.index) {
        // 判断当前节点是否为监测点层级
        if (node) {
            if (node.level !== 3) {
                tableStore.table.loading = false
                ElMessage.warning('请先选中监测点')
                return
            }
        } else {
            tableStore.table.loading = false
            ElMessage.warning('请先选中监测点')
            return
        }
        tableStore.index()
    }
}

const recall1 = async () => {
    if (!props.checkedNodes || props.checkedNodes.length === 0) {
        ElMessage.warning('请先勾选监测点')
        return
    }

    let list = props.checkedNodes.filter((node: any) => node.devConType == 'MQTT')
    if (list.length > 0) {
        ElMessage.warning('通讯协议为MQTT的装置暂不支持补召,[' + list.map((node: any) => node.name).join(',') + ']')
        return
    }

    await eventRecall({
        startTime: tableStore.table.params.startTime,
        endTime: tableStore.table.params.endTime,
        lineList: props.checkedNodes.map((node: any) => node.id),

    }).then((res: any) => {
        ElMessage.success(res.message)
        tableStore.index()
    })
}

const recall2 = async () => {
    if (!props.checkedNodes || props.checkedNodes.length === 0) {
        ElMessage.warning('请先勾选监测点')
        return
    }

    let list = props.checkedNodes.filter((node: any) => node.devConType == 'MQTT')
    if (list.length > 0) {
        ElMessage.warning('通讯协议为MQTT的装置暂不支持补召,[' + list.map((node: any) => node.name).join(',') + ']')
        return
    }

    await fileRecall({
        startTime: tableStore.table.params.startTime,
        endTime: tableStore.table.params.endTime,
        lineList: props.checkedNodes.map((node: any) => node.id)
    }).then((res: any) => {
        ElMessage.success(res.message)
    })
}

// 暴露方法给父组件调用
defineExpose({
    handleTreeNodeClick
})
onMounted(() => {
    setTimeout(() => {
        tableStore.table.loading = false
    }, 3000)
})
</script>

<style lang="scss" scoped>
.header_btn {
    width: 100%;
    height: 30px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
}

.view {
    display: flex;
    flex-direction: column;
    height: 100%;
}

.view :deep(.el-table) {
    height: calc(100% - 56px);
}
</style>
