<template>
    <div>
        <!--指标拟合图 -->
        <TableHeader :showReset="false" datePicker @selectChange="selectChange" v-if="fullscreen">
            <template v-slot:select>
                <el-form-item label="监测点">
                    <el-select
                        v-model="tableStore.table.params.lineId"
                        placeholder="请选择监测点"
                        clearable
                        style="width: 130px"
                    >
                        <el-option
                            v-for="item in lineList"
                            :key="item.lineId"
                            :label="item.name"
                            :value="item.lineId"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="用户功率">
                    <el-select
                        v-model="tableStore.table.params.power"
                        placeholder="请选择用户功率"
                        clearable
                        style="width: 130px"
                    >
                        <el-option
                            v-for="item in powerList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="电能质量指标">
                    <el-select
                        v-model="tableStore.table.params.indicator"
                        placeholder="请选择电能质量指标"
                        clearable
                        style="width: 130px"
                    >
                        <el-option
                            v-for="item in indicatorList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="越限情况">
                    <el-select
                        v-model="tableStore.table.params.exceedingTheLimit"
                        placeholder="请选择越限情况"
                        clearable
                        style="width: 90px"
                    >
                        <el-option
                            v-for="item in exceedingTheLimitList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
            </template>
        </TableHeader>
        <my-echart
            class="tall"
            :options="echartList"
            :style="{
                width: prop.width,
                height: `calc(${prop.height} - ${headerHeight}px + ${fullscreen ? 0 : 56}px )`
            }"
        />
    </div>
</template>
<script setup lang="ts">
import { ref, onMounted, provide, reactive, watch, h, computed } 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 { ElMessage, ElMessageBox } from 'element-plus'
import { getTimeOfTheMonth } from '@/utils/formatTime'
import MyEchart from '@/components/echarts/MyEchart.vue'
import { useConfig } from '@/stores/config'
import { useRoute } from 'vue-router'
import { useTimeCacheStore } from '@/stores/timeCache'
import { cslineList } from '@/api/harmonic-boot/cockpit/cockpit'

const prop = defineProps({
    w: { type:  [String, Number]},
    h: { type:  [String, Number]},
    width: { type:  [String, Number]},
    height: { type:  [String, Number]},
    timeKey: { type:  [String, Number]},
    timeValue: { type: Object }
})

const route = useRoute()
const timeCacheStore = useTimeCacheStore()
const config = useConfig()

const lineList: any = ref()

const powerList: any = ref([
    {
        label: '三相总有功功率',
        value: '1'
    },
    {
        label: '三相总无功功率',
        value: '2'
    }
])
// 计算是否全屏展示
const fullscreen = computed(() => {
    const w = Number(prop.w)
    const h = Number(prop.h)
    if (!isNaN(w) && !isNaN(h) && w === 12 && h === 6) {
        initLineList()
        // 执行相应逻辑
        return true
    } else {
        return false
    }
})
const exceedingTheLimitList: any = ref([
    {
        label: '越限',
        value: '1'
    },
    {
        label: '不越限',
        value: '0'
    }
])
const indicatorList: any = ref([
    {
        label: '谐波电压总畸变率',
        value: '1'
    },
    {
        label: '各次谐波电压',
        value: '2'
    },
    {
        label: '各次谐波电压',
        value: '3'
    },
    {
        label: '三相电压不平衡',
        value: '4'
    }
])

const initLineList = async () => {
    cslineList({}).then(res => {
        lineList.value = res.data
        if (lineList.value && lineList.value.length > 0) {
            tableStore.table.params.lineId = lineList.value[0].lineId
        }
    })
}

const echartList = ref({
    title: {
        text: '谐波电压总畸变率越限与功率负荷曲线拟合图'
    },

    xAxis: {
        type: 'time',
        axisLabel: {
            formatter: {
                day: '{MM}-{dd}',
                month: '{MM}',
                year: '{yyyy}'
            }
        }
    },

    yAxis: [{}, {}],
    grid: {
        left: '10px',
        right: '20px'
    },
    options: {
        series: [
            {
                //   name: '暂降次数',
                type: 'bar',
                name: '有功功率',
                data: [
                    ['2025-10-16 07:00:00', 10],
                    ['2025-10-16 07:15:00', 10],
                    ['2025-10-16 07:30:00', 10],
                    ['2025-10-16 07:45:00', 10],
                    ['2025-10-16 08:00:00', 30],
                    ['2025-10-16 08:15:00', 50],
                    ['2025-10-16 08:30:00', 60],
                    ['2025-10-16 08:45:00', 70],
                    ['2025-10-16 09:00:00', 100],
                    ['2025-10-16 09:15:00', 120],
                    ['2025-10-16 09:30:00', 130],
                    ['2025-10-16 09:45:00', 140],
                    ['2025-10-16 10:00:00', 160],
                    ['2025-10-16 10:15:00', 160],
                    ['2025-10-16 10:30:00', 130],
                    ['2025-10-16 10:45:00', 120],
                    ['2025-10-16 11:00:00', 140],
                    ['2025-10-16 11:15:00', 80],
                    ['2025-10-16 11:30:00', 70],
                    ['2025-10-16 11:45:00', 90],
                    ['2025-10-16 12:00:00', 60],
                    ['2025-10-16 12:15:00', 60],
                    ['2025-10-16 12:30:00', 60],
                    ['2025-10-16 12:45:00', 60]
                ],
                itemStyle: {
                    normal: {
                        //这里是颜色
                        color: function (params: any) {
                            if (params.value[1] == 0 || params.value[1] == 3.14159) {
                                return '#ccc'
                            } else {
                                return config.layout.elementUiPrimary[0]
                            }
                        }
                    }
                },
                yAxisIndex: 0
            },
            {
                name: '谐波总畸变率',
                type: 'line',
                showSymbol: false,
                smooth: true,
                data: [
                    ['2025-10-16 07:00:00', 0],
                    ['2025-10-16 07:15:00', 0],
                    ['2025-10-16 07:30:00', 0],
                    ['2025-10-16 07:45:00', 0],
                    ['2025-10-16 08:00:00', 0],
                    ['2025-10-16 08:15:00', 0.1],
                    ['2025-10-16 08:30:00', 0.1],
                    ['2025-10-16 08:45:00', 0.1],
                    ['2025-10-16 09:00:00', 1],
                    ['2025-10-16 09:15:00', 1],
                    ['2025-10-16 09:30:00', 1],
                    ['2025-10-16 09:45:00', 1],
                    ['2025-10-16 10:00:00', 0.8],
                    ['2025-10-16 10:15:00', 0.8],
                    ['2025-10-16 10:30:00', 0.8],
                    ['2025-10-16 10:45:00', 0.8],
                    ['2025-10-16 11:00:00', 0.8],
                    ['2025-10-16 11:15:00', 0.1],
                    ['2025-10-16 11:30:00', 0.1],
                    ['2025-10-16 11:45:00', 0.1],
                    ['2025-10-16 12:00:00', 0],
                    ['2025-10-16 12:15:00', 0],
                    ['2025-10-16 12:30:00', 0],
                    ['2025-10-16 12:45:00', 0]
                ],
                yAxisIndex: 1
            }
        ]
    }
})
const headerHeight = ref(57)
const selectChange = (showSelect: any, height: any) => {
    headerHeight.value = height
}
const tableStore: any = new TableStore({
    url: '/user-boot/role/selectRoleDetail?id=0',
    method: 'POST',

    showPage: false,
    exportName: '主要监测点列表',
    column: [],
    beforeSearchFun: () => {
        // 尝试从缓存获取时间值
        let beginTime, endTime

        if (fullscreen.value) {
            const cached = timeCacheStore.getCache(route.path)
            if (cached && cached.timeValue) {
                beginTime = cached.timeValue[0]
                endTime = cached.timeValue[1]
            }
        }

        // 如果缓存中没有则使用默认值
        tableStore.table.params.searchBeginTime = beginTime || prop.timeValue?.[0]
        tableStore.table.params.searchEndTime = endTime || prop.timeValue?.[1]
        if (!tableStore.table.params.lineId && lineList.value && lineList.value.length > 0) {
            tableStore.table.params.lineId = lineList.value[0].lineId
        }
    },
    loadCallback: () => {
        tableStore.table.height = `calc(${prop.height} - 80px)`
    }
})

const tableRef = ref()
provide('tableRef', tableRef)
tableStore.table.params.power = '1'
tableStore.table.params.indicator = '1'
tableStore.table.params.exceedingTheLimit = '1'
tableStore.table.params.searchValue = ''
provide('tableStore', tableStore)

onMounted(() => {
    tableStore.index()
})
watch(
    () => prop.timeKey,
    val => {
        tableStore.index()
    }
)
watch(
    () => prop.timeValue, // 监听的目标（函数形式避免直接传递 props 导致的警告）
    (newVal, oldVal) => {
        tableStore.index()
    },
    {
        deep: true // 若 timeValue 是对象/数组，需开启深度监听
    }
)

const addMenu = () => {}
</script>
<style lang="scss" scoped>
:deep(.el-select) {
    min-width: 80px;
}
</style>
