<template>
    <div class="dataAnalysisAngleAna" :class="!theme ? 'themeDark' : 'themeLight'">
        <div class="dataAnalysisAngleAnaMain">
            <div class="main_top">
                <p class="topPsty">浆距角分析</p>
            </div>
            <div class="main">
                <div class="treeDataMain">
                    <tree-cop :data="treeData" :height="treeHeight" @currentChange="funCurrentChange"
                        @refresh="funGetTree">
                    </tree-cop>
                </div>
                <div class="excelDataMain">
                    <excel-cop :data="excelList" :height="excelHeight" :theme="theme" @excelChange="funExcelChange">
                    </excel-cop>
                </div>
                <div class="tableDataMain">
                    <div class="px-[10px] shadow rounded-[6px] shadow-blue-500 ">
                        <CurrentScatterChart ref="chartRef" width="100%" :height="`calc( ${tableHeight})`"
                            :chartTitle="''" :xAxisData="xAxisData" :yAxisData="{ splitLine: { show: false } }"
                            :seriesData="seriesData" :seriesAllData="seriesAllData" :showLegend="true"
                            :brushSelected="false" :dataSet="dataSet" :theme="theme" :echartsTheme="echartsTheme"
                            @getSelected="funChartSelect" />
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script setup name="prepare">
    import excelCop from '@/components/generatingCapacityComponent/excel.vue'
    import treeCop from '@/components/generatingCapacityComponent/tree.vue'
    import {
        ElMessage
    } from 'element-plus';
    import {
        onMounted,
        ref,
        onActivated,
        watch
    } from 'vue'
    import {
        useStore
    } from 'vuex';
    import CurrentScatterChart from './components/current-scatter-chart.vue'
    import httpRequest from '@/utils/request.js'
    /**配置参数 */
    const tableHeight = ref(window.innerHeight - 120 + 'px')
    const treeHeight = ref(window.innerHeight - 120 + 'px') //tree高度
    const excelHeight = ref(window.innerHeight - 120 + 'px') //excel高度
    /**excel 开始 */
    const excelList = ref([])
    const funExcelChange = async (obj) => { //点击excel项时
        funSubmit({
            ids: obj.id
        })
    }
    /**tree 开始 */
    const treeData = ref([])
    const actTreeNode = ref(null) //当前激活的treeNode
    const funRepeatMap = (arr, type) => {
        return arr.map(o => {
            if (o.children) {
                const findIndex = o.children.findIndex(p => !!p.type)
                if (findIndex !== -1) {
                    o.childs = o.children
                    o.children = []
                    if (!actTreeNode.value && type === 'prepare') { //判断当且仅有process获取tree时 赋值
                        actTreeNode.value = o
                    }
                }
            }
            return {
                ...o,
                children: o.children.length ? funRepeatMap(o.children, type) : []
            }
        })
    }
    const funGetTree = async () => {
        const res = await httpRequest.get("/power/prepare/tree")
        treeData.value = funRepeatMap(res.data, 'prepare')
        excelList.value = []
        if (actTreeNode.value) {
            funCurrentChange({
                current: actTreeNode.value,
                currentNode: null
            })
            const child = actTreeNode.value.childs[0]
            const obj = {
                id: child.id,
                interval: child.interval,
                path: child.path,
                prepareid: child.prepareid,
                station: child.station,
                time: child.time,
                type: child.type,
                windturbine: child.windturbine,
                name: child.path.substring(child.path.indexOf(child.station + '_') + (child.station + '_')
                    .length)
            }
            funExcelChange(obj)
        }
    }
    const funCurrentChange = ({
        current,
        currentNode
    }) => {
        if (current.childs) {
            excelList.value = current.childs.map(o => {
                return {
                    id: o.id,
                    interval: o.interval,
                    path: o.path,
                    prepareid: o.prepareid,
                    station: o.station,
                    time: o.time,
                    type: o.type,
                    windturbine: o.windturbine,
                    name: o.path.substring(o.path.indexOf(o.station + '_') + (o.station + '_').length)
                }
            })
            if (excelList.value.length > 0) {
                funExcelChange(excelList.value[0])
            }
        } else {
            excelList.value = []
        }
    }
    /**table 开始 */
    const tableShowId = ref('')
    const tableName = ref('')
    const tableColumn = ref([])
    const tableLoading = ref(false)
    const tableData = ref([])
    /**tab  */
    const activeTab = ref('1')
    /**chart Data */
    const xAxisData = ref([])
    const chartRef = ref() //chart 的ref
    const seriesData = ref([])
    const seriesAllData = ref([])
    const dataSet = ref('')
    const funChartSelect = async (batch) => {
        return false
    }
    /**submit */
    const funSubmit = async (params) => {
        const res = await httpRequest.get('/blade/angle', {
            params: params
        })
        if (res.code !== 200) {
            return false
        }

        let exTime = []
        let yp1 = [],
            yp2 = [],
            yp3 = []
        res.data.bw.forEach(it => {
            exTime.push(it.time)
            yp1.push(it.yp1)
            yp2.push(it.yp2)
            yp3.push(it.yp3)
        })
        seriesAllData.value = res.data.bw
        // res.data.bw.forEach(it => {
        //     yp2.push(it.yp2)
        // })
        xAxisData.value = exTime

        seriesData.value = [
            // {
            //     name: "并网(绿)/停机(红)",
            //     type: "line",
            //     symbol: "line", //设定为实心点
            //     symbolSize: 0, //设定实心点的大小
            //     smooth: false, //这个是把线变成曲线
            //     data: yp1,
            //     xAxisIndex: 0,
            // },
            {
                name: "叶片1",
                type: "line",
                symbol: "line", //设定为实心点
                symbolSize: 0, //设定实心点的大小
                smooth: false, //这个是把线变成曲线
                data: yp1,
                xAxisIndex: 0,
            },
            {
                name: "叶片2",
                type: "line",
                symbol: "line", //设定为实心点
                symbolSize: 0, //设定实心点的大小
                smooth: false, //这个是把线变成曲线
                data: yp2,
                xAxisIndex: 0,
            },
            {
                name: "叶片3",
                type: "line",
                symbol: "line", //设定为实心点
                symbolSize: 0, //设定实心点的大小
                smooth: false, //这个是把线变成曲线
                data: yp3,
                xAxisIndex: 0,
            }
        ]
    }
    /**created */
    // funGetTree()
    const theme = ref(null)
    const echartsTheme = ref('')
    const store = useStore()
    watch(() => store.state.theme, (newVal, oldVal) => {
        theme.value = newVal
        echartsTheme.value = !newVal ? 'dark' : ''
        funGetTree()
    }, {
        deep: true
    })
    /**mounted */
    onMounted(() => {
        funGetTree()
        theme.value = store.state.theme
        echartsTheme.value = !theme.value ? 'dark' : ''
        tableHeight.value = window.innerHeight - 120 + 'px'
        excelHeight.value = window.innerHeight - 120 + 'px'
        treeHeight.value = window.innerHeight - 120 + 'px'
        window.addEventListener('resize', () => {
            tableHeight.value = window.innerHeight - 120 + 'px'
            excelHeight.value = window.innerHeight - 120 + 'px'
            treeHeight.value = window.innerHeight - 120 + 'px'
        })
    })
    /**activated */
    onActivated(() => {
        // funGetTree()
        // funSubmit()
    })
</script>
<style lang="less" scoped>
    .dataAnalysisAngleAna {
        height: 100%;

        .dataAnalysisAngleAnaMain {
            height: 100%;

            .main_top {
                height: 40px;
                display: flex;
                align-items: center;

                .topPsty {
                    position: relative;
                    top: 5px;
                    padding: 7px 20px;
                    font-size: 12px;
                    font-weight: 600;
                    margin-left: 10px;
                    border-radius: 3px;
                }
            }

            .main {
                display: flex;
                width: 100%;

                .treeDataMain,
                .excelDataMain,
                .tableDataMain {
                    border-radius: 10px;
                }

                .treeDataMain {
                    margin-right: 10px;
                    padding: 10px 0 10px 10px;
                    width: calc(19% - 20px);

                }

                .excelDataMain {
                    margin-right: 10px;
                    padding: 10px 0 10px 10px;
                    width: calc(15% - 20px);
                }

                .tableDataMain {
                    padding: 10px;
                    width: calc(66% - 20px);
                    position: relative;

                    .butten_com {
                        position: absolute;
                        right: 20px;
                        z-index: 111111;
                    }
                }

            }
        }


    }

    .themeDark {

        .dataAnalysisAngleAnaMain {
            .main_top {

                .topPsty {
                    color: #1C99FF;
                    background: #1E2126;
                }
            }

            .main {
                background: #13171e;

                .treeDataMain {
                    background: transparent;
                }

                .excelDataMain {
                    background: #313233;
                }

                .tableDataMain {
                    margin-top: 5px;
                    background: #212223;
                }
            }

        }

    }

    .themeLight {
        padding: 0;

        .dataAnalysisAngleAnaMain {
            .main_top {

                .topPsty {
                    color: #2778FF;
                    background: #FFFFFF;
                }
            }

            .main {
                background: #E6E8F2;

                .treeDataMain {
                    background: transparent;
                }

                .excelDataMain {
                    background: #F4F6FB;
                }

                .tableDataMain {
                    background: #fff;
                    margin-top: 5px;
                }
            }
        }

    }
</style>