<template>
    <div class="page">
        <HeaderComponent title="M2出料"></HeaderComponent>
        <div class="content_page">
            <PageDrawer type="SHZT" class="page_drawer" />
            <!-- 阀门 -->
            <div class="valves">
                <component v-for="item in deviceConfigGroup.VALVES" :key="item.id" :is="getComponentName(item.modelId)"
                    :title="getEquipmentTitle(item.equipmentName)" :valveStatusArr="getDataArrByCode(item.code)"
                    :rotateAngle="item.rotate || 0" :iconSize="item.size" :style="getComponentStyle(item)" />
            </div>

            <!-- 泵 -->
            <div class="pumps">
                <component v-for="item in deviceConfigGroup.PUMPS" :key="item.id" :is="getComponentName(item.modelId)"
                    :title="getEquipmentTitle(item.title)" :pumpDataArr="pumpHelper.getDataArrByCode(item.code)"
                    :isReverse="item.isReverse" :iconSize="item.size" :style="getComponentStyle(item)" />
            </div>

            <!-- 罐体 -->
            <div class="tanks">
                <component v-for="item in deviceConfigGroup.TANKS" :key="item.id" :is="getComponentName(item.modelId)"
                    :title="getEquipmentTitle(item.equipmentName)" :iconSize="item.size"
                    :style="getComponentStyle(item)" />
            </div>

            <!-- 传感器 -->
            <div class="sensors">
                <component v-for="item in deviceConfigGroup.SENSORS" :key="item.id" :is="getComponentName(item.modelId)"
                    :sensorValue="sensorHelper.getDataArrByCode(item.code)" :sensorWidth="item.width"
                    :sensorHeight="item.height" :sensorSize="item.size" :style="getComponentStyle(item)" />
            </div>

            <!-- 其他设备 -->
            <div class="icon_others">
                <component v-for="item in deviceConfigGroup.ICONS" :key="item.id" :is="getComponentName(item.modelId)"
                    :iconSize="item.size" :iconClass="getIconClass(item.modelId)" :style="getComponentStyle(item)"
                    :title="getEquipmentTitle(item.equipmentName)" />
            </div>

            <!-- 管道  -->
            <div class="pipelines">
                <component v-for="item in pipeConfig" :key="item.id" :is="getComponentName(item.modelId)"
                    :pipeStatus="evaluateCondition(item.flowCondition)" :strokeWidth="item.pipeWidth"
                    :style="getPipelineStyle(item, evaluateCondition(item.reverseCondition))"
                    :class="getPipelineClass(item.pipeClass, getPipeType(item.dynamicPipelineCondition))" />
            </div>

            <!-- 大箭头指向 -->
            <div class="arrows">
                <component v-for="item in deviceConfigGroup.ARROWS" :key="item.id" :is="getComponentName(item.modelId)"
                    :arrowText="getEquipmentTitle(item.equipmentName)" :iconSize="item.size"
                    :specialCondition="JSON.parse(item.specialCondition)" :style="getComponentStyle(item)" />
            </div>
        </div>
    </div>
</template>

<script setup>
import { useValveHelper } from '@/hooks/useValveHelper'
import { computed, onMounted, onBeforeUnmount, reactive } from 'vue';
import { stompClient } from '@/utils/ws/stompClient';
import { updateZTPageConfig } from '@/api/dcs/configurePage';

import { useEquipmentLayout } from '@/hooks/useEquipmentLayout'
import { useComponentHelper } from '@/hooks/useComponentHelper'
import { useRoute } from 'vue-router'
const route = useRoute()
const pageCode = route.name

// 获取设备布局 & 数据订阅
const {
    pipeConfig,//管道配置信息
    deviceConfigGroup,//设备配置分组
    modelMap,//设备模型映射
    pageParams,//页面参数
    fetchPageConfig,//获取页面配置
} = useEquipmentLayout(pageCode)

// 组件渲染辅助方法
const {
    getComponentName,
    getComponentStyle,
    getIconClass,
    getEquipmentTitle,
    getPipelineClass,
    getPipelineStyle,
} = useComponentHelper(modelMap)


onMounted(async () => {
    await fetchPageConfig(pageCode).then(res => {
        updatePageConfig()
    })
})

onBeforeUnmount(() => {
    // 页面销毁时取消订阅
    stompClient.unsubscribeFromPage(pageCode);
});

//M1罐阀门状态
const valveArr_M2 = [
    {
        code: 'N12',
        value: [true],
    },
    {
        code: 'N14',
        value: [false],
    },
    {
        code: 'N15',
        value: [false],
    },
    {
        code: 'N16',
        value: [true],
    },
    {
        code: 'N17',
        value: [true],
    },
    {
        code: 'N20',
        value: [true],
    },
    {
        code: 'N13',
        value: [true],
    },
    {
        code: 'N18',
        value: [true],
    },
]
//S1罐阀门状态
const valveArr_S1 = [
    {
        code: 'N10',
        value: [true],
    },
    {
        code: 'D5',
        value: [true, false, false],
    },
    {
        code: 'D11',
        value: [true, false, false],
    },
    {
        code: 'N22',
        value: [true]
    },
    {
        code: 'D16',
        value: [true, false, true],
    },
    {
        code: 'D8',
        value: [true, false, false],
    },

]
//S2罐阀门状态
const valveArr_S2 = [
    {
        code: 'N10',
        value: [true],
    },
    {
        code: 'D6',
        value: [true, false, false],
    },
    {
        code: 'D9',
        value: [true, false, false],
    },
    {
        code: 'D17',
        value: [true, false, false],
    },
    {
        code: 'D12',
        value: [true, false, false],
    },
    {
        code: 'N23',
        value: [true],
    },

]
//S3罐阀门状态
const valveArr_S3 = [
    {
        code: 'N10',
        value: [true],
    },
    {
        code: 'D7',
        value: [false, true, false],
    },
    {
        code: 'D10',
        value: [false, true, false],
    },
    {
        code: 'D13',
        value: [false, true, false],
    },
    {
        code: 'N24',
        value: [false],
    },

]
//传感器状态
const sensorArr = [
    {
        code: 'M2cz',
        value: [100],
    },
    {
        code: 'M2wd',
        value: [30],
    },
    {
        code: 'M2yl',
        value: [0.4],
    },
    {
        code: 'S1yl',
        value: [0.6],
    },
    {
        code: 'S1cz',
        value: [100],
    },
    {
        code: 'S1wd',
        value: [30],
    },
    {
        code: 'S2yl',
        value: [0.3],
    },
    {
        code: 'S2cz',
        value: [100],
    },
    {
        code: 'S2wd',
        value: [30],
    },
    {
        code: 'S3yl',
        value: [0.5],
    },
    {
        code: 'S3cz',
        value: [100],
    },
    {
        code: 'S3wd',
        value: [30],
    },
]
const pumpArr = [
    {
        code: 'LB9',
        value: [0, 3],
    },
    {
        code: 'S1jbj',
        value: [0, 3],
    },
    {
        code: 'S2jbj',
        value: [0, 3],
    },
]

//所有阀门状态
const valveArr = computed(() =>
    [...new Set([
        ...valveArr_M2,
        ...valveArr_S1,
        ...valveArr_S2,
        ...valveArr_S3
    ])]
);

//设备数据分组
const deviceDataGroup = reactive({
    VALVES: valveArr.value,
    PUMPS: pumpArr,
    SENSORS: sensorArr,
})

//更新页面配置
function updatePageConfig() {
    updateZTPageConfig(pageCode, pageParams.value)
        .then(res => {
            stompClient.subscribeToPage(pageCode, (data) => {
                deviceDataGroup.VALVES = Object.values(data.VALVES)
                deviceDataGroup.PUMPS = Object.values(data.PUMPS)
                deviceDataGroup.SENSORS = Object.values(data.SENSORS)
            });
        })
        .catch(err => {
            console.log('页面配置失败：', err);
        });
}
const pumpHelper = useValveHelper(computed(() => deviceDataGroup.PUMPS))
const { getDataArrByCode, evaluateCondition, getPipeType } = useValveHelper(computed(() => deviceDataGroup.VALVES))
const sensorHelper = useValveHelper(computed(() => deviceDataGroup.SENSORS))


</script>

<style lang="scss" scoped>
.page {
    // width: 100%;
    // height: 100%;
    width: 1920px;
    height: 1080px;
    background-color: #0b172c;

    .content_page {
        position: relative;
        width: 100%;
        height: 1000px;

        .page_drawer {
            position: absolute;
            left: 2%;
        }
    }
}
</style>