<template>
    <div>
        <!--暂降方向统计 -->
        <TableHeader :showReset="false" @selectChange="selectChange" datePicker v-if="fullscreen"></TableHeader>
        <my-echart class="tall" :options="echartList" :style="{ width: prop.width, height: `calc(${prop.height} )` }" />
    </div>
</template>
<script setup lang="ts">
import { ref, onMounted, provide, reactive, watch, h } from 'vue'
import TableStore from '@/utils/tableStore'
import Table from '@/components/table/index.vue'
import MyEchart from '@/components/echarts/MyEchart.vue'
import { useDictData } from '@/stores/dictData'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getTimeOfTheMonth } from '@/utils/formatTime'
import TableHeader from '@/components/table/header/index.vue'
import { useRoute } from 'vue-router'
import { useTimeCacheStore } from '@/stores/timeCache'

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 headerHeight = ref(57)

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

const selectChange = (showSelect: any, height: any, datePickerValue?: any) => {
    headerHeight.value = height

    if (datePickerValue && datePickerValue.timeValue) {
        // 更新时间参数
        tableStore.table.params.searchBeginTime = datePickerValue.timeValue[0]
        tableStore.table.params.searchEndTime = datePickerValue.timeValue[1]
    }
}

// 计算是否全屏展示
const fullscreen = computed(() => {
    const w = Number(prop.w)
    const h = Number(prop.h)
    if (!isNaN(w) && !isNaN(h) && w === 12 && h === 6) {
        // 执行相应逻辑
        return true
    } else {
        return false
    }
})

const data = [
    {
        name: '来自电网',
        value: 4
    },
    {
        name: '来自负荷',
        value: 41
    }
]
const echartList = ref({
    title: {},

    tooltip: {
        trigger: 'item'
    },
    legend: {
        orient: 'vertical',
        top: 'center',
        right: '5%',
        formatter: function (e: any) {
            return e + '   ' + data.filter(item => item.name == e)[0].value + '次'
        }
    },
    xAxis: {
        show: false
    },
    yAxis: {
        show: false
    },
    grid: {
        left: '10px',
        right: '20px'
    },

    options: {
        dataZoom: null,
        title: [
            {
                text: '暂降方向统计',
                left: 'center'
            },
            {
                text: data[0].value + data[1].value + '次',
                left: 'center',
                top: 'center'
            }
        ],
        series: [
            {
                type: 'pie',
                center: 'center',
                radius: ['55%', '75%'],
                label: {
                    show: false,
                    position: 'outside',
                    textStyle: {
                        //数值样式
                    }
                },
                name: '事件统计',
                data: data
            }
        ]
    }
})
const OverLimitDetailsRef = ref()
const tableStore: any = new TableStore({
    url: '/user-boot/dept/deptTree',
    method: 'POST',

    showPage: false,

    column: [],
    beforeSearchFun: () => {
        tableStore.table.params.searchBeginTime = tableStore.table.params.searchBeginTime || prop.timeValue?.[0]
        tableStore.table.params.searchEndTime = tableStore.table.params.searchEndTime || prop.timeValue?.[1]
    },
    loadCallback: () => {}
})

const tableRef = ref()
provide('tableRef', tableRef)

provide('tableStore', tableStore)

// 点击行
const cellClickEvent = ({ row, column }: any) => {
    if (column.field != 'name') {
        console.log(row)
        OverLimitDetailsRef.value.open(row)
    }
}

onMounted(() => {
    tableStore.index()
})
watch(
    () => prop.timeKey,
    val => {
        tableStore.index()
    }
)
watch(
    () => prop.timeValue,
    (newVal, oldVal) => {
        // 当外部时间值变化时，更新表格的时间参数
        if (newVal && (!oldVal || newVal[0] !== oldVal[0] || newVal[1] !== oldVal[1])) {
            tableStore.table.params.searchBeginTime = newVal[0]
            tableStore.table.params.searchEndTime = newVal[1]
            tableStore.index()
        }
    },
    {
        deep: true
    }
)

const addMenu = () => {}
</script>
<style lang="scss" scoped></style>
