<script setup lang="ts">
import { ref, onMounted, reactive, nextTick } from 'vue'
import Page from '@/components/Pagination/page.vue'
import { ElMessageBox, ElMessage, ElTable, FormRules, ElTree } from 'element-plus'
import { getDeviceByType } from '@/api/device'
import Eldialog from '@/components/seccmsdialog/eldialog.vue'
import { useUserStore } from '@/store/modules/user';
import * as echarts from 'echarts'
import { getAlgorithmClassTree, getAlgorithmDeviceParams, getAlgorithmDeviceCurve, getAlgorithmParamsNameList, callAlgorithmAnalyse } from '@/api/question';
import { fa } from 'element-plus/es/locale'
const userStore = useUserStore();
//树形控件类型定义
interface Tree {
    [x: string]: any;
    label: string;
    children?: Tree[];
}
const vMove = {
    mounted(el: any) {
        el.onmousedown = function (e: any) {
            var init = e.clientX;
            var parent: any = document.getElementById("silderLeft");
            const initWidth: any = parent.offsetWidth;
            document.onmousemove = function (e) {
                var end = e.clientX;
                var newWidth = end - init + initWidth;
                parent.style.width = newWidth + "px";
            };
            document.onmouseup = function () {
                document.onmousemove = document.onmouseup = null;
            };
        };
    }
}
const defaultProps = { label: "name" };
const treeRef = ref<InstanceType<typeof ElTree>>()
const treeData: any = ref([])
const treeloading = ref(false)
onMounted(() => {
    init()
})
const algorithmClassName = ref('')
const algorithmId = ref('')
const componentId = ref('')
function init() {
    const params = {
        algorithmClassName: algorithmClassName.value
    }
    treeloading.value = true
    getAlgorithmClassTree(params).then((res: any) => {
        if (res.data.length !== 0 && res.data !== null) {
            treeData.value = res.data
            treeloading.value = false
            if (res.data.length !== 0 && res.data !== null) {
                componentId.value = res.data[0].children[0].id
                algorithmId.value = res.data[0].children[0].parentId

                nextTick(() => {
                    treeRef.value?.setCurrentKey(componentId.value);
                });
                gatdata()
                getecharts()
                getfenList()
            }
        }
    })
}
function handleNodeClick(data: any, node: any) {
    if (data.children.length != 0) {
        node.isCurrent = false
        nextTick(() => {
            treeRef.value?.setCurrentKey(componentId.value);
        })
    } else {
        componentId.value = data.id
        algorithmId.value = data.parentId
        gatdata()
        getecharts()
        getfenList()
        canshuList.value = ''
    }
}
const parameter: any = ref([])
function gatdata() {
    fixloading.value = true
    getAlgorithmDeviceParams({ algorithmId: algorithmId.value, componentId: componentId.value }).then((res: any) => {
        parameter.value = res.data
        fixloading.value = false
    })
}
//echarts
const eacherslist: any = ref([1, 2, 3, 4])
function getecharts() {
    checloading.value = true
    getAlgorithmDeviceCurve({ algorithmId: algorithmId.value, componentId: componentId.value }).then((res: any) => {
        checloading.value = false
        eacherslist.value = res.data
        setTimeout(() => {
            eacherslist.value.forEach((item: any, index: any) => {
                drawEchart(item, index)
            })
        }, 100);

    })
}
function getRandomColor(): string {
  const hue = Math.floor(Math.random() * 360); // 色相 0~359
  const saturation = 70 + Math.floor(Math.random() * 20); // 饱和度 70~90%
  const lightness = 50 + Math.floor(Math.random() * 10); // 亮度 50~60%

  return `hsl(${hue}, ${saturation}%, ${lightness}%)`;
}
function drawEchart(item: any, index: any) {
  // 生成随机颜色
  const randomColor = getRandomColor();

  let myChart = echarts.init(document.getElementById("deviceEachers" + index) as HTMLDivElement);
  myChart.setOption({
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: [item.series[0].name],
      textStyle: { color: randomColor, fontSize: 14 }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      top: '18%',
      containLabel: true,
      borderColor: randomColor
    },
    xAxis:item.xAxis ,
    yAxis:item.yAxis ,
    series: item.series.map((series: any) => ({
      ...series,
      itemStyle: { color: randomColor }
    }))
  });
}
//fenxi
const checloading = ref(false)
const fixloading = ref(false)
const fenloading = ref(false)
const fenList: any = ref([])
function getfenList() {
    fenloading.value = true
    getAlgorithmParamsNameList({ algorithmId: algorithmId.value, componentId: componentId.value }).then((res: any) => {
        fenList.value = res.data
        fenloading.value = false
    })

}
const canshuList: any = ref()
const mapList: any = ref()
function analysis() {
    fenloading.value = true
    callAlgorithmAnalyse({ id: algorithmId.value, componentId: componentId.value }).then((res: any) => {
        fenloading.value = false
        if (res.data.code == -2) {
            ElMessage.warning(res.data.msg)
        } else {
            ElMessage.success('分析成功')
            mapList.value = res.data.mapList
            canshuList.value = res.data.result.description
            getovercanshu()
        }
        

    })
}
function getovercanshu() {
    mapList.value.forEach((item: any) => {
        fenList.value.forEach((items: any) => {
            if (item.paramId == items.id) {
                items.paramValue = item.paramValue
            }
        })
    })
}

</script>
<template>
    <div class="faulttemplate-box">
        <aside id="silderLeft">
            <div class="tree_title">
                <img src="@/assets/navigation/ty_bq.png" alt="">
                <div>算法分类</div>
            </div>
            <el-input v-model="algorithmClassName" clearable placeholder="请输入名称" @change="init"
                style="width:100%;margin-top: 10px;" class="videomonitor-input">
                <template #suffix>
                    <img src="@/assets/videoimg/u2937.png" style="cursor:pointer ;" @click="init">
                </template>
            </el-input>
            <el-tree v-loading="treeloading" ref="treeRef" node-key="id" :data="treeData" :highlight-current="true"
                :props="defaultProps" :expand-on-click-node="false" @node-click="handleNodeClick"
                style="height: calc(80vh); overflow: auto;margin-top: 10px;">
            </el-tree>
            <div class="moveBtn" v-move>
                <div class="moveBtn-line"></div>
            </div>
        </aside>
        <section class="silderRight1">
            <div class="right_center" v-loading="checloading">
                <div class="tree_title">
                    <img src="@/assets/navigation/ty_bq.png" alt="">
                    <div>信号/点位折线图</div>
                </div>
                <el-scrollbar height="440px">
                    <div class="echarts_box">
                        <div v-for="(item, index) in eacherslist" :id="'deviceEachers' + index"
                            :style="{ width: '100%', height: '160px' }"></div>
                    </div>
                </el-scrollbar>
            </div>
            <div class="right_yop" v-loading="fixloading">
                <div class="tree_title">
                    <img src="@/assets/navigation/ty_bq.png" alt="">
                    <div>固定参数</div>
                </div>
                <el-scrollbar height="60px">
                    <div class="fixed_params">
                        <div v-for="item in parameter" class="fixed_params_item">
                            <div>{{ item.paramName }}：<span>{{ item.paramValue }}</span></div>
                        </div>
                    </div>
                </el-scrollbar>
            </div>
            <div class="right_bottom" v-loading="fenloading">
                <div class="tree_title">
                    <img src="@/assets/navigation/ty_bq.png" alt="">
                    <div>分析结果</div>
                </div>
                <el-button class="searchButton" type="primary" style="margin: 10px 0px;"
                    @click="analysis()">开始分析</el-button>
                <el-scrollbar height="80px">
                    <div class="fixed_params">
                        <div v-for="item in fenList" class="fixed_params_item">
                            <div>{{ item.paramName }}：<span>{{ item.paramValue ? item.paramValue : '--' }}</span></div>
                        </div>
                    </div>
                </el-scrollbar>
                <div class="right_botom">
                    分析结果：<span v-if="!canshuList">--</span><span v-else>{{ canshuList }}</span>
                </div>
            </div>
        </section>

    </div>
</template>

<style scoped lang="scss">
.silderLeft-default {
    :deep(.el-tree-node__label) {
        font-size: 16px !important;
    }
}

.faulttemplate-box {
    height: 100%;
    display: flex;
    display: -webkit-flex;
    background-color: #f2f4f900;
    padding: 15px 15px 0px 15px;

}

.tree_title {
    width: 100%;
    display: flex;
    align-items: center;
    font-size: 16px;
    color: #fff;
    font-weight: bold;

    img {
        margin-right: 10px;
    }
}

#silderLeft {
    width: 300px;
    padding: 10px;
    box-sizing: border-box;
    border-radius: 3px;
    position: relative;
    background: url(@/assets/navigation/ty_260x988.png);
    background-size: 100% 100%;

    &:hover {
        .moveBtn {
            opacity: 1;
        }
    }


}

/* 拖动条 */
.moveBtn {
    height: 100%;
    width: 15px;
    padding: 0 6px;
    opacity: 0;
    position: absolute;
    right: -15px;
    top: 0;
}

.moveBtn-line {
    width: 100%;
    height: 100%;
    cursor: col-resize;
    user-select: none;
    background-color: #60bfff;
}

.silderRight1 {
    flex: 1;
    width: 100%;
    height: calc(90vh);
    overflow: auto;
    border-radius: 3px;
    box-sizing: border-box;
    margin-left: 15px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;

    .right_yop {
        width: 100%;
        height: 110px;
        box-sizing: border-box;
        padding: 15px;
        background: url(@/assets/navigation/110tu.png);
        background-size: 100% 100%;

        .fixed_params {
            width: 100%;
            display: flex;
            align-items: center;
            font-family: 'Arial Normal', 'Arial';
            font-weight: 400;
            color: #FFFFFF;
            font-size: 14px;

            .fixed_params_item {
                margin-right: 20px;

                span {
                    font-family: 'Arial Negreta', 'Arial Normal', 'Arial';
                    font-weight: 700;
                    font-size: 18px;
                    color: #00FFFF
                }
            }
        }
    }

    .right_center {
        width: 100%;
        height: 500px;
        box-sizing: border-box;
        padding: 15px;
        background: url(@/assets/navigation/sw1.png);
        background-size: 100% 100%;

        .echarts_box {
            width: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-top: 10px;
        }
    }

    .right_bottom {
        width: 100%;
        height: 220px;
        box-sizing: border-box;
        padding: 15px;
        background: url(@/assets/navigation/sw.png);
        background-size: 100% 100%;

        .fixed_params {
            width: 100%;
            display: flex;
            align-items: center;
            font-family: 'Arial Normal', 'Arial';
            font-weight: 400;
            color: #FFFFFF;
            font-size: 14px;

            .fixed_params_item {
                margin-right: 10px;

                span {
                    font-family: 'Arial Negreta', 'Arial Normal', 'Arial';
                    font-weight: 700;
                    font-size: 18px;
                    color: #00FFFF
                }
            }
        }

        .right_botom {
            width: 100%;
            font-family: 'Arial Normal', 'Arial';
            font-weight: 400;
            font-style: normal;
            font-size: 16px;
            color: #FFFFFF;

            span {
                font-family: 'Arial Negreta', 'Arial Normal', 'Arial';
                font-weight: 700;
                font-style: normal;
                font-size: 24px;
                letter-spacing: normal;
                color: #009900;
            }
        }

        :deep(.el-scrollbar) {
            height: 80px !important;
        }
    }

}
</style>