<template>
    <div class="dataFilePrepare" :class="!theme ? 'themeDark' : 'themeLight'">
        <div class="dataFilePrepareMain">
            <div class="main_top">
                <p class="topPsty">风电数据准备</p>
                <search-cop @submit="funSubmit">
                </search-cop>
            </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" @excelChange="funExcelChange" :theme="theme">
                    </excel-cop>
                </div>
                <div class="tableDataMain">
                    <table-cop class="" :data="tableData" :column="tableColumn" :loading="tableLoading" :theme="theme"
                        :height="tableHeight" :tableId="tableShowId" :tableName="tableName" @export="funExport">
                    </table-cop>
                </div>
            </div>
        </div>

        <el-progress :percentage="progress" v-if="progress" :indeterminate="false" color="rgb(19,206,102)"
            :stroke-width="4" :show-text="false" />
    </div>
</template>
<script setup name="prepare">
    import searchCop from './search.vue'
    import excelCop from '@/components/generatingCapacityComponent/excel.vue'
    import treeCop from '@/components/generatingCapacityComponent/tree.vue'
    import tableCop from '@/components/generatingCapacityComponent/table.vue'
    import {
        ElMessage
    } from 'element-plus';
    import {
        onMounted,
        ref,
        onActivated,
        watch
    } from 'vue'
    import httpRequest from '@/utils/request.js'
    import {
        useStore
    } from 'vuex';
    import {
        apiGetpowerpreparetree
    } from '@/api/gengra'
    // import {
    //     baseURL,
    //     socketURL
    // } from '@/api/axios.js'

    /**配置参数 */
    const treeHeight = ref(window.innerHeight - 130 + 'px') //tree高度
    const excelHeight = ref(window.innerHeight - 130 + 'px') //excel高度
    const tableHeight = ref(window.innerHeight - 130 + 'px')
    /**excel 开始 */
    const excelList = ref([])
    const funExcelChange = async (obj) => { //点击excel项时
        tableShowId.value = obj.id
        tableName.value = obj.name
        tableLoading.value = true
        const res = await httpRequest.get('/power/prepare/show', {
            params: {
                id: obj.id
            }
        })
        if (res.code === 200) {
            tableColumn.value = res.data.title.map(o => {
                return {
                    prop: o.key,
                    label: o.des,
                    width: o.des === '时间' ? 100 : 80,
                }
            })
            tableData.value = res.data.data
            tableLoading.value = false
        } else {
            tableLoading.value = false
        }
    }
    /**tree 开始 */
    const treeData = ref([])
    const actTreeNode = ref(null) //当前激活的treeNode
    const funRepeatMap = (arr) => {
        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) { //判断当且仅有process获取tree时 赋值
                        actTreeNode.value = o
                    }
                }
            }
            return {
                ...o,
                children: o.children.length ? funRepeatMap(o.children) : []
            }
        })
    }
    const funGetTree = async () => {
        actTreeNode.value = null
        // const res = await httpRequest.get("/power/prepare/tree")
        const res = await apiGetpowerpreparetree()
        treeData.value = funRepeatMap(res.data)
        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,
                    isCheck: false,
                    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([])
    const funExport = async () => {
        const a = document.createElement('a')
        // a.href = baseURL + '/power/prepare/download?id=' + tableShowId.value
        a.href = '/power/prepare/download?id=' + tableShowId.value
        a.download = ''
        a.click()
    }
    /**submit */
    const progress = ref(0)
    const funWebSocket = () => {
        // const webSocket = new WebSocket(`${socketURL}/ws/powerfitting/admin`)
        const webSocket = new WebSocket(`ws://10.81.3.155:9002/ws/powerfitting/admin`)
        webSocket.onerror = () => setTimeout(() => {
            funWebSocket()
        }, 2000)

        webSocket.onmessage = (event) => {
            const message = JSON.parse(event.data)
            if (message.code === 200) {
                progress.value = Number(message.data) * 100
                if (progress.value === 100) {
                    ElMessage.success('数据加载完成')
                    funGetTree()
                    progress.value = 0
                }
            }
        }
    }
    const funSubmit = async (params) => {
        const res = await httpRequest.get('/power/prepare/data', {
            params: params
        })
        if (res.code === 200) {
            ElMessage.success(res.msg)

        }
    }
    /**created */
    // funGetTree()
    const theme = ref(null)
    const store = useStore()
    watch(() => store.state.theme, (newVal, oldVal) => {
        theme.value = newVal
        // funWebSocket()
        funGetTree()
    }, {
        deep: true
    })
    /**mounted */
    onMounted(() => {
        // funWebSocket()
        funGetTree()
        theme.value = store.state.theme
        tableHeight.value = window.innerHeight - 130 + 'px'
        excelHeight.value = window.innerHeight - 130 + 'px'
        treeHeight.value = window.innerHeight - 130 + 'px'
        window.addEventListener('resize', () => {
            tableHeight.value = window.innerHeight - 130 + 'px'
            excelHeight.value = window.innerHeight - 130 + 'px'
            treeHeight.value = window.innerHeight - 130 + 'px'
        })
    })
    /**activated */
    // onActivated(() => {
    // 	funGetTree()
    // })
</script>

<style lang="less">
    .dataFilePrepare {
        height: 100%;

        .dataFilePrepareMain {
            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;
                justify-content: space-between;
                // width: calc(100% - 40px);
                width: 100%;
                height: calc(100% - 100px);

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

                .treeDataMain {
                    width: calc(20% - 20px);
                }

                .excelDataMain {
                    width: calc(13% - 20px);
                }

                .tableDataMain {
                    width: calc(66% - 20px);
                    // width: 66%;
                }

            }
        }

    }

    .themeDark {

        .dataFilePrepareMain {
            .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;

        .dataFilePrepareMain {
            .main_top {

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

            .main {
                background: #E6E8F2;

                .treeDataMain {
                    background: transparent;
                }

                .excelDataMain {
                    background: #F4F6FB;
                }

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

    }
</style>