<template>
    <div class="default-main online">
        <div class="online_header">
            <TableHeader date-picker area ref="tableHeaderRef">
                <template #select>
                    <el-form-item label="统计类型">
                        <el-select
                            v-model="tableStore.table.params.statisticalType"
                            placeholder="请选择统计类型"
                            value-key="id"
                            style="width: 100%"
                            filterable
                        >
                            <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.manufacturer"
                            multiple
                            collapse-tags
                            clearable
                            placeholder="请选择终端厂家"
                            style="width: 100%"
                            value-key="id"
                            filterable
                        >
                            <el-option
                                v-for="(item, index) in terminaloption"
                                :key="index"
                                :label="item.name"
                                :value="item"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="筛选数据">
                        <el-input
                            v-model.trim="tableStore.table.params.searchValue"
                            clearable
                            style="width: 200px"
                            placeholder="请输入关键字筛选"
                        />
                    </el-form-item>
                </template>
            </TableHeader>
        </div>
        <div class="online_main">
            <el-tabs v-model="activeName" type="border-card" @tab-click="handleClick">
                <el-tab-pane :name="0" :lazy="true" label="终端状态统计表">
                    <div class="table_legend">
                        <ul>
                            <li>
                                <p style="background: #339966">
                                    <svg
                                        t="1722910570852"
                                        class="icon"
                                        viewBox="0 0 1336 1024"
                                        version="1.1"
                                        xmlns="http://www.w3.org/2000/svg"
                                        p-id="6243"
                                        width="16"
                                        height="16"
                                    >
                                        <path
                                            d="M49.588224 883.816448M1139.531776 883.816448M86.939648 862.977024M538.459136 960.381952c109.876224-153.544704 223.55968-289.842176 308.03968-385.787904 49.87392-56.639488 96.715776-108.155904 141.549568-154.500096 40.576-41.945088 80.805888-80.405504 119.478272-116.59264 65.92-61.693952 142.984192-130.034688 191.446016-159.30368l-61.891584-82.050048c-89.732096 56.139776-176.10752 116.103168-242.799616 162.18112-38.863872 26.855424-74.928128 52.667392-108.915712 77.252608-33.668096 24.356864-68.429824 51.106816-105.081856 79.166464-63.633408 48.712704-145.388544 114.194432-224.555008 181.860352L357.07904 374.989824 123.885568 558.770176 538.459136 960.381952zM1335.92064 862.977024"
                                            fill="#ffffff"
                                            p-id="6244"
                                        ></path>
                                    </svg>
                                </p>
                                <span style="color: #339966">投运状态</span>
                            </li>
                            <li>
                                <p style="background: #ffcc33">
                                    <svg
                                        t="1722910570852"
                                        class="icon"
                                        viewBox="0 0 1336 1024"
                                        version="1.1"
                                        xmlns="http://www.w3.org/2000/svg"
                                        p-id="6243"
                                        width="16"
                                        height="16"
                                    >
                                        <path
                                            d="M49.588224 883.816448M1139.531776 883.816448M86.939648 862.977024M538.459136 960.381952c109.876224-153.544704 223.55968-289.842176 308.03968-385.787904 49.87392-56.639488 96.715776-108.155904 141.549568-154.500096 40.576-41.945088 80.805888-80.405504 119.478272-116.59264 65.92-61.693952 142.984192-130.034688 191.446016-159.30368l-61.891584-82.050048c-89.732096 56.139776-176.10752 116.103168-242.799616 162.18112-38.863872 26.855424-74.928128 52.667392-108.915712 77.252608-33.668096 24.356864-68.429824 51.106816-105.081856 79.166464-63.633408 48.712704-145.388544 114.194432-224.555008 181.860352L357.07904 374.989824 123.885568 558.770176 538.459136 960.381952zM1335.92064 862.977024"
                                            fill="#ffffff"
                                            p-id="6244"
                                        ></path>
                                    </svg>
                                </p>
                                <span style="color: #ffcc33">检修状态</span>
                            </li>
                            <li>
                                <p style="background: #a52a2a">
                                    <svg
                                        t="1722910570852"
                                        class="icon"
                                        viewBox="0 0 1336 1024"
                                        version="1.1"
                                        xmlns="http://www.w3.org/2000/svg"
                                        p-id="6243"
                                        width="16"
                                        height="16"
                                    >
                                        <path
                                            d="M49.588224 883.816448M1139.531776 883.816448M86.939648 862.977024M538.459136 960.381952c109.876224-153.544704 223.55968-289.842176 308.03968-385.787904 49.87392-56.639488 96.715776-108.155904 141.549568-154.500096 40.576-41.945088 80.805888-80.405504 119.478272-116.59264 65.92-61.693952 142.984192-130.034688 191.446016-159.30368l-61.891584-82.050048c-89.732096 56.139776-176.10752 116.103168-242.799616 162.18112-38.863872 26.855424-74.928128 52.667392-108.915712 77.252608-33.668096 24.356864-68.429824 51.106816-105.081856 79.166464-63.633408 48.712704-145.388544 114.194432-224.555008 181.860352L357.07904 374.989824 123.885568 558.770176 538.459136 960.381952zM1335.92064 862.977024"
                                            fill="#ffffff"
                                            p-id="6244"
                                        ></path>
                                    </svg>
                                </p>
                                <span style="color: #a52a2a">停运状态</span>
                            </li>
                        </ul>
                    </div>
                    <Table
                        ref="tableRef"
                        :tree-config="{ transform: true, parentField: 'uPid', rowField: 'uId' }"
                        :scroll-y="{ enabled: true }"
                        v-if="activeName == 0"
                    />
                </el-tab-pane>
                <el-tab-pane :name="1" :lazy="true" label="终端状态统计图">
                    <charts v-if="activeName == 1" ref="chartsRef" />
                </el-tab-pane>
            </el-tabs>
        </div>
    </div>
</template>
<script lang="ts" setup>
import { ref, onMounted, watch } from 'vue'
import { useDictData } from '@/stores/dictData'
import DatePicker from '@/components/form/datePicker/index.vue'
import { getAreaDept } from '@/api/harmonic-boot/area'
import TableHeader from '@/components/table/header/index.vue'
import TableStore from '@/utils/tableStore'
import Table from '@/components/table/index.vue'
import charts from './components/charts.vue'
import { filterTree } from '@/utils/tree'
defineOptions({
    name: 'harmonic-boot/area/terminalonlinerate'
})
const tableRef = ref()
const chartsRef = ref()
const dictData = useDictData()
//字典获取电压等级
const voltageleveloption = dictData.getBasicData('Dev_Voltage_Stand')
//字典获取终端厂家
const terminaloption = dictData.getBasicData('Dev_Manufacturers')
//字典获取干扰源类型
const interfereoption = dictData.getBasicData('Interference_Source')
//字典获取统计类型
const classificationData = dictData.getBasicData('Statistical_Type', ['Report_Type', 'Voltage_Level', 'Load_Type'])
//调用区域接口获取区域
const treeData = ref([])
const idArr = ref([])
const activeName = ref(0)
const getTreeData = async () => {
    await getAreaDept().then(res => {
        var data = res.data
        data.forEach(element => {
            idArr.value.push(element.id)
        })
        treeData.value = JSON.parse(JSON.stringify(res.data))
    })
}
getTreeData()

const formData = ref({
    statisticalType: classificationData[0], //统计类型
    deptIndex: treeData.value[0]?.id, //区域选择
    scale: voltageleveloption, //电压等级
    manufacturer: terminaloption, //终端厂家
    loadType: interfereoption //干扰源类型
    // searchBeginTime: '',
    // searchEndTime: ''
})
formData.value.deptIndex = treeData.value[0]?.id
const defaultProps = ref({
    label: 'name',
    value: 'id',
    checkStrictly: true,
    emitPath: false,
    expandTrigger: 'click' as const
})
const handleClick = (tab: any, e: any) => {
    // if(activeName.value===1){
    tableStore.index()
    // }
}

// const datePickerRef = ref()
const tableHeaderRef = ref()
const tableStore = new TableStore({
    publicHeight: 100,
    showPage: false,
    url: '/harmonic-boot/terminal/getTerminalData',
    method: 'POST',

    column: [
        {
            title: formData.value.statisticalType.name,
            field: 'name',
            align: 'left',
            width: 350,
            treeNode: true
        },
        {
            title: '终端个数(台)',
            field: 'number',
            align: 'center',
            formatter: function (row) {
                return row.cellValue != 0 ? row.cellValue : '/'
            }
        },
        {
            title: '投运(台)',
            field: 'runFlag',
            type: 'html',
            align: 'center',
            formatter: function (row) {
                return row.cellValue == 0
                    ? '/'
                    : row.cellValue !== 0 && row.row.level !== 4
                    ? row.cellValue
                    : row.cellValue !== 0 && row.row.level == 4 && row.row.reaFlag == 0 && row.row.stopFlag == 0
                    ? "<p class='table_icon'  style='width: 18px;height: 18px;margin: 0 auto;display: flex;align-items: center;justify-content: center;border-radius: 50%;background: #339966'><svg t='1722910570852' class='icon' viewBox='0 0 1336 1024' version='1.1' xmlns='http://www.w3.org/2000/svg' p-id='6243' width='16' height='16'><path d='M49.588224 883.816448M1139.531776 883.816448M86.939648 862.977024M538.459136 960.381952c109.876224-153.544704 223.55968-289.842176 308.03968-385.787904 49.87392-56.639488 96.715776-108.155904 141.549568-154.500096 40.576-41.945088 80.805888-80.405504 119.478272-116.59264 65.92-61.693952 142.984192-130.034688 191.446016-159.30368l-61.891584-82.050048c-89.732096 56.139776-176.10752 116.103168-242.799616 162.18112-38.863872 26.855424-74.928128 52.667392-108.915712 77.252608-33.668096 24.356864-68.429824 51.106816-105.081856 79.166464-63.633408 48.712704-145.388544 114.194432-224.555008 181.860352L357.07904 374.989824 123.885568 558.770176 538.459136 960.381952zM1335.92064 862.977024' fill='#ffffff' p-id='6244'></path></svg> </p>"
                    : '/'
            }
        },
        {
            title: '检修(台)',
            field: 'reaFlag',
            type: 'html',
            align: 'center',
            formatter: function (row) {
                return row.cellValue == 0
                    ? '/'
                    : row.cellValue !== 0 && row.row.level !== 4
                    ? row.cellValue
                    : row.cellValue !== 0 && row.row.level == 4 && row.row.runFlag == 0 && row.row.stopFlag == 0
                    ? "<p class='table_icon'  style='width: 18px;height: 18px;margin: 0 auto;display: flex;align-items: center;justify-content: center;border-radius: 50%;background: #ffcc33'><svg t='1722910570852' class='icon' viewBox='0 0 1336 1024' version='1.1' xmlns='http://www.w3.org/2000/svg' p-id='6243' width='16' height='16'><path d='M49.588224 883.816448M1139.531776 883.816448M86.939648 862.977024M538.459136 960.381952c109.876224-153.544704 223.55968-289.842176 308.03968-385.787904 49.87392-56.639488 96.715776-108.155904 141.549568-154.500096 40.576-41.945088 80.805888-80.405504 119.478272-116.59264 65.92-61.693952 142.984192-130.034688 191.446016-159.30368l-61.891584-82.050048c-89.732096 56.139776-176.10752 116.103168-242.799616 162.18112-38.863872 26.855424-74.928128 52.667392-108.915712 77.252608-33.668096 24.356864-68.429824 51.106816-105.081856 79.166464-63.633408 48.712704-145.388544 114.194432-224.555008 181.860352L357.07904 374.989824 123.885568 558.770176 538.459136 960.381952zM1335.92064 862.977024' fill='#ffffff' p-id='6244'></path></svg> </p>"
                    : '/'
            }
        },
        {
            title: '停运(台)',
            field: 'stopFlag',
            type: 'html',
            align: 'center',
            formatter: function (row) {
                return row.cellValue == 0
                    ? '/'
                    : row.cellValue !== 0 && row.row.level !== 4
                    ? row.cellValue
                    : row.cellValue !== 0 && row.row.level == 4 && row.row.runFlag == 0 && row.row.reaFlag == 0
                    ? "<p class='table_icon'  style='width: 18px;height: 18px;margin: 0 auto;display: flex;align-items: center;justify-content: center;border-radius: 50%;background: #A52a2a'><svg t='1722910570852' class='icon' viewBox='0 0 1336 1024' version='1.1' xmlns='http://www.w3.org/2000/svg' p-id='6243' width='16' height='16'><path d='M49.588224 883.816448M1139.531776 883.816448M86.939648 862.977024M538.459136 960.381952c109.876224-153.544704 223.55968-289.842176 308.03968-385.787904 49.87392-56.639488 96.715776-108.155904 141.549568-154.500096 40.576-41.945088 80.805888-80.405504 119.478272-116.59264 65.92-61.693952 142.984192-130.034688 191.446016-159.30368l-61.891584-82.050048c-89.732096 56.139776-176.10752 116.103168-242.799616 162.18112-38.863872 26.855424-74.928128 52.667392-108.915712 77.252608-33.668096 24.356864-68.429824 51.106816-105.081856 79.166464-63.633408 48.712704-145.388544 114.194432-224.555008 181.860352L357.07904 374.989824 123.885568 558.770176 538.459136 960.381952zM1335.92064 862.977024' fill='#ffffff' p-id='6244'></path></svg> </p>"
                    : '/'
            }
        },
        {
            title: '在线率(%)',
            field: 'onlineRateData',
            align: 'center',
            formatter: function (row) {
                return row.cellValue == 3.14159 ? '暂无数据' : row.cellValue.toFixed(2)
            }
        }
    ],
    // beforeSearchFun: () => {
    //     tableStore.table.params.deptIndex = formData.value.deptIndex
    //     tableStore.table.params.statisticalType = formData.value.statisticalType
    //     tableStore.table.params.scale = formData.value.scale
    //     tableStore.table.params.manufacturer = formData.value.manufacturer
    //     tableStore.table.params.loadType = formData.value.loadType
    //     tableStore.table.params.serverName = 'harmonicBoot'
    //     // delete tableStore.table.params.timeFlag
    //     // delete tableStore.table.params.startTime
    //     // delete tableStore.table.params.endTime
    //     // delete tableStore.table.params.pageNum
    //     // delete tableStore.table.params.pageSize
    //     // tableStore.table.params.searchBeginTime =  tableHeaderRef.value.datePickerRef.timeValue[0]
    //     // tableStore.table.params.searchEndTime =  tableHeaderRef.value.datePickerRef.timeValue[1]
    // },
    loadCallback: () => {
        tableStore.table.data = tree2List(
            filterTree(tableStore.table.data, node => {
                return node.name.includes(tableStore.table.params.searchValue)
            }),
            Math.random() * 1000
        )
        tableStore.table.column[0].title = formData.value.statisticalType.name

        chartsRef.value && chartsRef.value.getTableStoreParams(tableStore.table.params)
        setTimeout(() => {
            activeName.value == 0 && tableRef.value && tableRef.value.getRef().setAllTreeExpand(true)
        }, 0)
    }
})
tableStore.table.params.deptIndex = treeData.value[0]?.id
tableStore.table.params.statisticalType = classificationData[0]
tableStore.table.params.scale = voltageleveloption
tableStore.table.params.manufacturer = terminaloption
tableStore.table.params.loadType = interfereoption
tableStore.table.params.serverName = 'harmonicBoot'
tableStore.table.params.searchValue = ''

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

onMounted(() => {})

watch(
    () => treeData.value,
    (val, oldVal) => {
        if (val && val.length != 0) {
            formData.value.deptIndex = val[0].id
            tableStore.index()
        }
    },
    {
        immediate: true,
        deep: true
    }
)
</script>
<style lang="scss" scoped>
.table_legend {
    width: 100%;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    align-items: center;
    ul {
        width: 280px;
        height: 30px;
        display: flex;
        justify-content: space-between;
        li {
            flex: 1;
            list-style-type: none;
            display: flex;
            align-items: center;
            justify-content: space-around;
            p {
                width: 18px;
                height: 18px;
                border-radius: 50%;
            }
        }
    }
}
</style>
