<template>
    <div>
        <!-- 电镀数据数据 -->
        <div>
            <TableHeader ref="tableHeaderRef" :showSearch="false" @selectChange="selectChange">
                <template v-slot:select>
                    <el-form-item>
                        <DatePicker ref="datePickerRef"></DatePicker>
                    </el-form-item>
                    <el-form-item label="电度指标" label-width="80px">
                        <el-select style="width: 200px" multiple :multiple-limit="3" collapse-tags filterable
                            collapse-tags-tooltip v-model="searchForm.index" placeholder="请选择统计指标"
                            @change="onIndexChange($event)">
                            <el-option v-for="item in indexOptions" :key="item.id" :label="item.name"
                                :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-radio-group v-model="searchForm.dataLevel" :disabled="props?.TrendList?.lineType != 1"
                            @change="init()">
                            <el-radio-button label="一次值" value="Primary" />
                            <el-radio-button label="二次值" value="Secondary" />
                        </el-radio-group>
                    </el-form-item>
                    <!-- <el-form-item label="统计类型">
                        <el-select style="min-width: 120px !important" placeholder="请选择" v-model="searchForm.valueType">
                            <el-option value="max" label="最大值"></el-option>
                            <el-option value="min" label="最小值"></el-option>
                            <el-option value="avg" label="平均值"></el-option>
                            <el-option value="cp95" label="cp95"></el-option>
                        </el-select>
                    </el-form-item> -->
                </template>
                <template #operation>
                    <el-button type="primary" icon="el-icon-Search" @click="init()">查询</el-button>
                    <el-button :type="timeControl ? 'primary' : ''" icon="el-icon-Sort" @click="setTimeControl">
                        缺失数据
                    </el-button>
                </template>
            </TableHeader>
        </div>
        <div class="history_chart" :style="pageHeight" v-loading="loading">
            <MyEchart ref="historyChart" :options="echartsData" v-if="showEchart" />
            <el-empty :style="pageHeight" v-else description="暂无数据" />
        </div>
    </div>
</template>
<script lang="ts" setup>
import { mainHeight } from '@/utils/layout'
import { queryByCode, queryCsDictTree } from '@/api/system-boot/dictTree'
import { ref, onMounted } from 'vue'
import MyEchart from '@/components/echarts/MyEchart.vue'
import { useDictData } from '@/stores/dictData'
import { yMethod, exportCSV, completeTimeSeries } from '@/utils/echartMethod'
import TableHeader from '@/components/table/header/index.vue'
import { getTabsDataByType } from '@/api/cs-device-boot/EquipmentDelivery'
import DatePicker from '@/components/form/datePicker/index.vue'
import { color } from '@/components/echarts/color'
import { ElMessage } from 'element-plus'
const dictData = useDictData()
defineOptions({
    // name: 'govern/device/control'
})
const props = defineProps({
    TrendList: {
        type: Array
    }
})
// console.log("🚀 ~ props:", props.TrendList)
const showEchart = ref(true)
//电压等级
const voltageLevelList = dictData.getBasicData('Dev_Voltage_Stand')
//接线方式
const volConTypeList = dictData.getBasicData('Dev_Connect')
const timeControl = ref(false)
//值类型
const pageHeight = ref(mainHeight(290))
const loading = ref(true)
const searchForm: any = ref({})
const tableHeaderRef = ref()
const typeOptions = [
    {
        name: '平均值',
        id: 'avg'
    },
    {
        name: '最大值',
        id: 'max'
    },
    {
        name: '最小值',
        id: 'min'
    },
    {
        name: 'CP95值',
        id: 'cp95'
    }
]
searchForm.value = {
    index: [],
    type: typeOptions[0].id,
    searchBeginTime: '',
    searchEndTime: '',
    dataLevel: 'Primary',
    valueType: 'avg'
}
//统计指标
const indexOptions: any = ref([])
queryByCode('Kilowatt_Hour').then(res => {
    queryCsDictTree(res.data.id).then(item => {
        indexOptions.value = item.data.sort((a: any, b: any) => {
            return a.sort - b.sort
        })
        searchForm.value.index[0] = indexOptions.value[0].id
        init()
    })
})
const chartsList = ref<any>([])
const activeName: any = ref()
const deviceData: any = ref([])
//历史趋势devId
const historyDevId: any = ref('')
const chartTitle: any = ref('')
const echartsData = ref<any>(null)
//加载echarts图表
//历史趋势数据
const historyDataList: any = ref([])
//获取请求趋势数据参数
const trendRequestData = ref()
const getTrendRequest = (val: any) => {
    trendRequestData.value = val
    // init()
}
//初始化趋势图
const headerRef = ref()
const datePickerRef = ref()
const lineStyle = [{ type: 'solid' }, { type: 'dashed' }, { type: 'dotted' }]
const init = async () => {
    loading.value = true
    echartsData.value = {}
    historyDataList.value = []
    chartTitle.value = ''

    searchForm.value.index.map((item: any, indexs: any) => {
        indexOptions.value.map((vv: any) => {
            if (vv.id == item) {
                chartTitle.value += indexs == searchForm.value.index.length - 1 ? vv.name : vv.name + '/'
            }
        })
    })
    const lists = searchForm.value.index.map((id: any) => ({
        statisticalId: id,
        frequencys: []
    }))
    let obj = {
        ...trendRequestData.value,
        list: lists,
        // valueType: searchForm.value.type,
        dataLevel: searchForm.value.dataLevel,
        // valueType: searchForm.value.valueType,
        startTime: datePickerRef.value && datePickerRef.value.timeValue[0],
        endTime: datePickerRef.value && datePickerRef.value.timeValue[1]
    }
    if (searchForm.value.index.length == 0) {
        ElMessage.warning('请选择统计指标')
        loading.value = false
        return
    }
    if (obj.devId && obj.list.length != 0) {
        try {
            showEchart.value = true
            await getTabsDataByType(obj)
                .then((res: any) => {
                    if (res.code == 'A0000') {
                        if (res.data.length == 0) {
                            loading.value = false
                            showEchart.value = false
                            return
                        }
                        historyDataList.value = res.data
                        chartsList.value = formatChartData(JSON.parse(JSON.stringify(res.data)))
                        loading.value = false
                        setEchart()
                    }
                })
                .catch(error => {
                    loading.value = false
                })
        } catch (error) {
            loading.value = false
        }
    }
}
const setEchart = () => {
    loading.value = true
    echartsData.value = {}
    //icon图标替换legend图例

    // y轴单位数组
    let unitList: any = []

    let groupedData = chartsList.value.reduce((acc: any, item: any) => {
        const key = item.statisticalName

        if (!acc[key]) {
            acc[key] = []
        }
        acc[key].push(item)
        return acc
    }, {})
    let result = Object.values(groupedData)
    // console.log("🚀 ~ .then ~ result:", result)
    // console.log("🚀 ~ .then ~ result:", result)
    if (chartsList.value.length > 0) {
        unitList = result.map((item: any) => {
            return item[0].unit
        })
    }

    echartsData.value = {
        legend: {
            itemWidth: 20,
            itemHeight: 20,
            itemStyle: { opacity: 0 }, //去圆点
            type: 'scroll', // 开启滚动分页
            // orient: 'vertical', // 垂直排列
            top: 5,
            right: 70
            // width: 550,
            // height: 50
        },
        grid: {
            top: '80px'
        },
        tooltip: {
            axisPointer: {
                type: 'cross',
                label: {
                    color: '#fff',
                    fontSize: 16
                }
            },
            textStyle: {
                color: '#fff',
                fontStyle: 'normal',
                opacity: 0.35,
                fontSize: 14
            },
            backgroundColor: 'rgba(0,0,0,0.55)',
            borderWidth: 0,
            formatter(params: any) {
                const xname = params[0].value[0]
                let str = `${xname}<br>`
                params.forEach((el: any, index: any) => {
                    let marker = ''

                    if (el.value[3] == 'dashed') {
                        for (let i = 0; i < 3; i++) {
                            marker += `<span style="display:inline-block;border: 2px ${el.color} solid;margin-right:5px;width:10px;height:0px;background-color:#ffffff00;"></span>`
                        }
                    } else {
                        marker = `<span style="display:inline-block;border: 2px ${el.color} ${el.value[3]};margin-right:5px;width:40px;height:0px;background-color:#ffffff00;"></span>`
                    }
                    let unit = el.value[2] ? el.value[2] : ''
                    str += `${marker}${el.seriesName.split('(')[0]}：${el.value[1]}${unit}
                                            <br>`
                })
                return str
            }
        },
        color: ['#DAA520', '#2E8B57', '#A52a2a', ...color],
        xAxis: {
            type: 'time',
            axisLabel: {
                formatter: {
                    day: '{MM}-{dd}',
                    month: '{MM}',
                    year: '{yyyy}'
                }
            }
        },
        yAxis: [{}],
        toolbox: {
            featureProps: {
                myTool1: {
                    show: true,
                    title: '下载csv',
                    icon: 'path://M588.8 551.253333V512H352v39.253333h236.373333z m0 78.933334v-39.253334H352v39.253334h236.373333z m136.533333 78.933333V334.933333l-157.866666-157.866666H273.066667A59.306667 59.306667 0 0 0 213.333333 236.373333v551.253334a59.306667 59.306667 0 0 0 59.306667 59.306666h274.773333v42.666667H853.333333v-180.48zM568.746667 234.666667l100.266666 100.693333h-81.066666a20.053333 20.053333 0 0 1-19.626667-20.053333z m-20.48 573.013333H273.066667a19.2 19.2 0 0 1-17.493334-19.626667V236.373333a19.2 19.2 0 0 1 19.626667-19.626666h256v98.133333a58.88 58.88 0 0 0 58.88 59.306667h96.426667v334.933333h-98.133334v-39.68H352v39.68h196.266667z m100.266666 23.04a37.973333 37.973333 0 0 1-32 15.786667 38.826667 38.826667 0 0 1-32.426666-15.786667 53.76 53.76 0 0 1-10.24-32.853333 42.666667 42.666667 0 0 1 42.666666-47.786667 35.84 35.84 0 0 1 37.546667 29.866667h-12.8a23.893333 23.893333 0 0 0-24.746667-19.2c-17.066667 0-29.013333 14.08-29.013333 35.84s11.52 37.546667 28.586667 37.546666a26.453333 26.453333 0 0 0 26.453333-25.6h12.8a39.253333 39.253333 0 0 1-7.253333 22.186667z m59.733334 15.786667a35.84 35.84 0 0 1-40.106667-34.56H682.666667a23.893333 23.893333 0 0 0 26.88 23.04c12.8 0 22.613333-6.4 22.613333-15.786667s-4.266667-11.52-14.506667-13.653333l-21.333333-5.12c-17.066667-4.266667-24.32-11.52-24.32-23.893334s12.8-26.453333 34.133333-26.453333a31.573333 31.573333 0 0 1 35.413334 30.293333h-13.653334a19.626667 19.626667 0 0 0-22.613333-18.773333c-12.8 0-20.48 5.12-20.48 12.8s5.12 11.093333 17.066667 13.653333l14.933333 2.986667a42.666667 42.666667 0 0 1 20.906667 8.96 23.893333 23.893333 0 0 1 7.68 17.92c-0.426667 17.066667-14.506667 28.16-37.12 28.16z m88.746666 0h-14.506666l-32.426667-92.16h14.08l19.626667 59.733333 6.4 20.053333c0-9.386667 3.413333-12.8 5.546666-20.053333l19.2-59.733333h14.08z',
                    onclick: e => {
                        // console.log("🚀 ~ init ~ echartsData.value:", echartsData.value.options.series.map(item => item.data))

                        let list = echartsData.value.options.series?.map((item: any) => item.data)
                        let dataList = list[0]?.map((item: any, index: any) => {
                            let value = [item[0], item[1]]
                            list.forEach((item1: any, index1: any) => {
                                if (index1 > 0) {
                                    value.push(item1 && item1[index] ? item1[index][1] : null)
                                }
                            })

                            return value
                        })
                        exportCSV(
                            echartsData.value.options.series.map((item: any) => item.name),
                            dataList,
                            '电镀数据.csv'
                        )
                    }
                }
            }
        },
        options: {
            series: []
        }
    }
    // console.log("🚀 ~ unitList.forEach ~ unitList:", unitList)

    if (chartsList.value.length > 0) {
        let yData: any = []
        echartsData.value.yAxis = []
        let setList = [...new Set(unitList)]

        setList.forEach((item: any, index: any) => {
            if (index > 2) {
                echartsData.value.grid.right = (index - 1) * 80
            }
            yData.push([])
            let right = {
                position: 'right',
                offset: (index - 1) * 80
            }
            // console.log("🚀 ~ unitList.forEach ~ right.index:", index)
            echartsData.value.yAxis.push({
                name: item,
                yAxisIndex: index,
                splitNumber: 5,
                minInterval: 1,
                splitLine: {
                    show: false
                },
                ...(index > 0 ? right : null)
            })
        })
        // console.log("🚀 ~ result.forEach ~ result:", result)
        // '电压负序分量', '电压正序分量', '电压零序分量'
        let ABCName = [...new Set(chartsList.value.map((item: any) => item.statisticalName))]
        // console.log("🚀 ~ .then ~ ABCName:", ABCName)
        result.forEach((item: any, index: any) => {
            let yMethodList: any = []

            let ABCList = Object.values(
                item.reduce((acc: any, cur: any) => {
                    const key = cur.phase == null ? cur.statisticalIndex : cur.phase

                    if (!acc[key]) {
                        acc[key] = []
                    }
                    acc[key].push(cur)
                    return acc
                }, {})
            )
            // console.log("🚀 ~ ABCList.forEach ~ ABCList:", ABCList)

            ABCList.forEach((kk: any) => {
                let colorName = kk[0].phase?.charAt(0).toUpperCase()
                let lineS = ABCName.findIndex(name => name === kk[0].statisticalName)
                const seriesLineStyle = lineStyle[lineS] || lineStyle[0]
                const seriesColor = kk[0].phase
                    ? colorName == 'A'
                        ? '#DAA520'
                        : colorName == 'B'
                            ? '#2E8B57'
                            : colorName == 'C'
                                ? '#A52a2a'
                                : ''
                    : '#082e6c'

                let seriesList: any = []
                const sortedData = [...kk].sort(
                    (a: any, b: any) => new Date(a.time).getTime() - new Date(b.time).getTime()
                )
                sortedData.forEach((cc: any) => {
                    if (cc.statisticalData !== null) {
                        yData[setList.indexOf(kk[0].unit)].push(cc.statisticalData?.toFixed(2))
                    }

                    seriesList.push([cc.time, cc.statisticalData?.toFixed(2), cc.unit, seriesLineStyle.type])
                })

                echartsData.value.options.series.push({
                    name: kk[0].phase ? kk[0].phase + '相' + kk[0].anotherName : kk[0].anotherName,
                    type: 'line',
                    smooth: true,
                    color: seriesColor,
                    symbol: 'none',
                    data: timeControl.value ? completeTimeSeries(seriesList) : seriesList,
                    lineStyle: seriesLineStyle,
                    yAxisIndex: setList.indexOf(kk[0].unit)
                })
            })
            // let [min, max] = yMethod(yMethodList)
            // echartsData.value.yAxis[index].min = min
            // echartsData.value.yAxis[index].max = max
        })
        yData.forEach((item: any, index: any) => {
            let [min, max] = yMethod(item)
            echartsData.value.yAxis[index].min = min
            echartsData.value.yAxis[index].max = max
        })
        // console.log("🚀 ~ result.forEach ~ echartsData.value:", echartsData.value)
    }
    loading.value = false
}
const setTimeControl = () => {
    timeControl.value = !timeControl.value
    setEchart()
}

const selectChange = (flag: boolean) => {
    if (flag) {
        pageHeight.value = mainHeight(332)
    } else {
        pageHeight.value = mainHeight(290)
    }
}

// 电度趋势数据预处理：按时间排序
const formatChartData = (data: any[]) => {
    return [...data].sort((a, b) => new Date(a.time).getTime() - new Date(b.time).getTime())
}

const onIndexChange = (val: any) => {
    let pp: any = []
    indexOptions.value.forEach((item: any) => {
        const filteredResult = val.filter((vv: any) => item.id == vv)
        if (filteredResult.length > 0) {
            pp.push(filteredResult[0])
        }
    })
    searchForm.value.index = pp
}
onMounted(() => {
    datePickerRef.value.setInterval(5)
})
defineExpose({ getTrendRequest })
</script>

<style lang="scss" scoped>
.history_header {
    display: flex;

    // flex-wrap: wrap;
    #history_select {
        width: 100%;
        display: flex;
        // justify-content: flex-start;
        // overflow-x: auto;
        height: auto;
        flex-wrap: wrap;

        .el-form-item {
            flex: none !important;
            // max-width: 380px;
        }

        .el-select {
            margin-right: 10px;
        }
    }

    // #history_select::-webkit-scrollbar {
    //     width: 0 !important;
    //     display: none !important;
    // }

    .history_searchBtn {
        flex: 1;
        display: flex;
        justify-content: flex-end;
        margin-top: 3px;
    }
}

.history_chart {
    width: 100%;

    // flex: 1;
    margin-top: 10px;
}

:deep(.el-select__selected-item) {
    .is-closable {
        width: 100px !important;
    }
}

:deep(.el-form--inline .el-form-item) {
    margin-right: 15px !important;
}
</style>
