<template>
    <div class="default-main">
        <el-tabs v-model="activeName" type="border-card">
            <el-tab-pane label="干扰源用户台账" name="1" v-if="tabList.includes('1')">
                <interferenceUserTable :id="id" v-if="activeName == '1'" />
            </el-tab-pane>
            <el-tab-pane label="敏感及重要用户台账" name="2" v-if="tabList.includes('2')">
                <sensitiveUserTable v-if="activeName == '2'" />
            </el-tab-pane>
            <el-tab-pane label="变电站台账" name="3" v-if="tabList.includes('3')">
                <substationLedger v-if="activeName == '3'" />
            </el-tab-pane>
            <el-tab-pane label="终端台账" name="4" v-if="tabList.includes('4')">
                <deviceLedgerTable v-if="activeName == '4'" />
            </el-tab-pane>
            <el-tab-pane label="监测点台账" name="5" v-if="tabList.includes('5')">
                <monitorLedgerTable ref="monitorLedgerRef" v-if="activeName == '5'" />
            </el-tab-pane>
            <el-tab-pane label="分布式光伏台区台账" name="6" v-if="tabList.includes('6') && VITE_FLAG">
                <transformerStationLedger ref="monitorLedgerRef" v-if="activeName == '6'" />
            </el-tab-pane>
            <el-tab-pane label="分布式光伏用户台账" name="7" v-if="tabList.includes('7') && VITE_FLAG">
                <userLedger ref="monitorLedgerRef" v-if="activeName == '7'" />
            </el-tab-pane>
        </el-tabs>
    </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref, provide, nextTick } from 'vue'
import interferenceUserTable from './components/interferenceUserTable.vue'
import sensitiveUserTable from './components/sensitiveUserTable.vue'
import substationLedger from './components/substationLedger.vue'
import deviceLedgerTable from './components/deviceLedgerTable.vue'
import monitorLedgerTable from './components/monitorLedgerTable.vue'
import transformerStationLedger from './components/transformerStationLedger.vue'
import userLedger from './components/userLedger.vue'
import { mainHeight } from '@/utils/layout'
import { useRoute } from 'vue-router'
const route = useRoute()
const id = ref('')
const VITE_FLAG = import.meta.env.VITE_NAME == 'jibei'
defineOptions({
    name: 'Supervision/Terminaldetection'
})

const activeName = ref('1')
const tabList = ref(['1', '2', '3', '4', '5', '6', '7'])
const monitorLedgerRef = ref()
onMounted(() => {
    let key = window.location.href.split('?')[0].slice(-1)
    const isNumber = /^\d$/.test(key)
    if (isNumber) {
        tabList.value = [key]
        activeName.value = key
    }
})
watch(
    () => route.query.t,
    async (newValue, oldValue) => {
        if (route.fullPath.includes('Supervision/Terminaldetection')) {
            let type = (route.query.type as string) || 'null'
            if (type == 'null') {
            } else if (type == '1') {
                activeName.value = '1'
            } else if (type == '5') {
                activeName.value = '5'
                setTimeout(() => {
                    monitorLedgerRef.value.setSearchValue((route.query.name as string) || '')
                    console.log('🚀 ~ route.query.:', route.query)
                }, 500)
            } else if (type == 'lodType') {
                activeName.value = '5'
                setTimeout(() => {
                    monitorLedgerRef.value.setSearchType(
                        (route.query.name as string) || '',
                        route.query.statisticalType
                    )
                }, 500)
            }
            id.value = (route.query.name as string) || ''
            id.value = id.value + '@' + route.query.t
        }
    },
    { deep: true, immediate: true }
)

const layout = mainHeight(63) as any
</script>

<style lang="scss" scoped>
.bars_w {
    width: 100%;
    height: 500px;
}

:deep(.el-tabs__content) {
    height: v-bind('layout.height');
    overflow-y: auto;
}
</style>
