<template>
    <TableHeader area datePicker ref="TableHeaderRef">
        <template #select>
            <el-form-item label="统计类型:">
                <el-select
                    v-model="tableStore.table.params.statisticalType"
                    value-key="id"
                    placeholder="请选择统计类型"
                >
                    <el-option
                        v-for="item in classificationData"
                        :key="item.id"
                        :label="item.name"
                        :value="item"
                    ></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="电压等级:">
                <el-select
                    v-model="tableStore.table.params.scale"
                    multiple
                    collapse-tags
                    clearable
                    value-key="id"
                    placeholder="请选择电压等级"
                >
                    <el-option
                        v-for="item in voltageleveloption"
                        :key="item.id"
                        :label="item.name"
                        :value="item"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="终端厂家:">
                <el-select
                    v-model="tableStore.table.params.manufacturer"
                    multiple
                    collapse-tags
                    clearable
                    value-key="id"
                    placeholder="请选择终端厂家"
                >
                    <el-option
                        v-for="item in terminaloption"
                        :key="item.id"
                        :label="item.name"
                        :value="item"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="干扰源类型:">
                <el-select
                    v-model="tableStore.table.params.loadType"
                    multiple
                    collapse-tags
                    clearable
                    value-key="id"
                    placeholder="请选择干扰源类型"
                >
                    <el-option
                        v-for="item in interfereoption"
                        :key="item.id"
                        :label="item.name"
                        :value="item"
                    ></el-option>
                </el-select>
            </el-form-item>
        </template>
    </TableHeader>
    <Table ref="tableRef" :tree-config="{ transform: true, parentField: 'pid' }" :scroll-y="{ enabled: true }" />
</template>
<script setup lang="ts">
import { ref, onMounted, provide, nextTick } 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'
const dictData = useDictData()
const tableRef = ref()

const classificationData = dictData.getBasicData('Statistical_Type', ['Report_Type'])
const voltageleveloption = dictData.getBasicData('Dev_Voltage_Stand')
const terminaloption = dictData.getBasicData('Dev_Manufacturers')
const interfereoption = dictData.getBasicData('Interference_Source')

const TableHeaderRef = ref()
const tableStore = new TableStore({
    url: '/device-boot/terminalOnlineRateData/getOnlineRateData',
    publicHeight: 65,
    showPage: false,
    method: 'POST',
    column: [
        { field: 'name', title: '电网拓扑', minWidth: '180px', align: 'left', treeNode: true },
        {
            field: 'ip',
            title: '网络参数',
            formatter: ({ row }: any) => {
                return row.ip || '/'
            }
        },
        {
            field: 'dataName',
            title: '终端名称',
            formatter: ({ row }: any) => {
                return row.dataName || '/'
            }
        },
        {
            field: 'manufacturer',
            title: '厂家',
            formatter: ({ row }: any) => {
                return row.manufacturer || '/'
            }
        },
        {
            field: 'comFlag',
            title: '通讯状态',
            render: 'tag',
            custom: {
                0: 'danger',
                1: 'success',
                3: 'info'
            },
            replaceValue: {
                0: '中断',
                1: '正常',
                3: '/'
            }
            // formatter: ({ row }: any) => {
            //     return row.comFlag || '/'
            // }
        },
        {
            field: 'updateTime',
            title: '最新数据时间',
            formatter: ({ row }: any) => {
                return row.updateTime || '/'
            }
        },
        {
            field: 'onlineRate',
            title: '在线率(%)',
            formatter: ({ row }: any) => {
                return row.onlineRate == 3.14159 ? '暂无数据' : row.onlineRate.toFixed(2)
            }
        },
        {
            field: 'assess',
            title: '评估',
            render: 'tag',
            custom: {
                0: 'info',
                1: 'danger',
                2: 'warning',
                3: 'success'
            },
            replaceValue: {
                0: '暂无数据',
                1: '不合格',
                2: '合格',
                3: '优秀'
            }
        }
    ],
    beforeSearchFun: () => {
        tableStore.options.column[0].title = tableStore.table.params.statisticalType.name
    },
    loadCallback: () => {
        let treeData = []
        tableStore.table.data.forEach((item: any) => {
            if (item.children.length > 0) {
                item.children.forEach((k: any) => {
                    k.pid = item.id
                })
            }
        })
        treeData = tree2List(tableStore.table.data)
        tableStore.table.data = JSON.parse(JSON.stringify(treeData))

        setTimeout(() => {
            tableRef.value.getRef().setAllTreeExpand(true)
        }, 0)
    }
})

tableStore.table.params.statisticalType = classificationData.filter(item => item.name == '电网拓扑')[0]
tableStore.table.params.monitorFlag = 2
tableStore.table.params.powerFlag = 2
tableStore.table.params.serverName = 'harmonicBoot'

provide('tableStore', tableStore)
const tree2List = (list: any) => {
    //存储结果的数组
    let arr: any = []
    // 遍历 tree 数组
    list.forEach((item: any) => {
        item.comFlag = item.comFlag == null ? 3 : item.comFlag
        item.assess = item.onlineRate == 3.14159 ? 0 : item.onlineRate < 60 ? 1 : item.onlineRate < 90 ? 2 : 3
        // 判断item是否存在children
        if (!item.children) return arr.push(item)
        // 函数递归，对children数组进行tree2List的转换
        const children = tree2List(item.children)
        // 删除item的children属性
        delete item.children
        // 把item和children数组添加至结果数组
        //..children: 意思是把children数组展开
        arr.push(item, ...children)
    })
    // 返回结果数组
    return arr
}
onMounted(() => {
    tableStore.index()
})
</script>

<style scoped lang="scss"></style>
