<template>
    <div class="page">
        <HeaderComponent title="设备状态页"></HeaderComponent>
        <div class="content_page">
            <div class="device-status-container">
                <!-- 传感器状态卡片 -->
                <el-card class="status-card" shadow="hover">
                    <template #header>
                        <div class="card-header">
                            <span class="card-title">传感器状态</span>
                            <el-tag type="info" size="small">{{ deviceConfigGroup.SENSORS?.length || 0 }}个</el-tag>
                        </div>
                    </template>
                    <div class="device-grid">
                        <el-card v-for="item in deviceConfigGroup.SENSORS || []" :key="item.code" class="device-item"
                            shadow="hover" :body-style="{ padding: '12px' }">
                            <div class="device-info">
                                <div class="device-name-code">
                                    <div class="device-name">{{ item.equipmentName || item.code }}</div>
                                    <div class="device-code">{{ item.code }}</div>
                                </div>
                                <div class="device-value">
                                    <!-- 实际值显示 -->
                                    <div class="actual-value">
                                        <span class="value-label">实际值：</span>
                                        <el-tag :type="getValueType(sensorActualValues[item.code])" size="large"
                                            class="actual-tag">
                                            {{ formatValue(sensorActualValues[item.code]) }}
                                        </el-tag>
                                    </div>
                                    <!-- 设置值输入框 -->
                                    <div class="set-value">
                                        <span class="value-label">设置值：</span>
                                        <el-input-number v-model="sensorSetValues[item.code]" :min="0" :max="100"
                                            :step="0.1" :precision="2" size="large" controls-position="right"
                                            @change="(val) => handleSensorValueChange(item.code, val)"
                                            class="sensor-input" />
                                    </div>
                                </div>
                            </div>
                        </el-card>
                    </div>
                </el-card>

                <!-- 阀门状态卡片 -->
                <el-card class="status-card" shadow="hover">
                    <template #header>
                        <div class="card-header">
                            <span class="card-title">阀门状态</span>
                            <el-tag type="info" size="small">{{ deviceConfigGroup.VALVES?.length || 0 }}个</el-tag>
                        </div>
                    </template>
                    <div class="device-grid">
                        <el-card v-for="item in deviceConfigGroup.VALVES || []" :key="item.code" class="device-item"
                            shadow="hover" :body-style="{ padding: '12px' }">
                            <div class="device-info">
                                <div class="device-name-code">
                                    <div class="device-name">{{ item.equipmentName || item.code }}</div>
                                    <div class="device-code">{{ item.code }}</div>
                                </div>
                                <div class="device-value">
                                    <!-- 实际值显示 -->
                                    <div class="actual-value">
                                        <span class="value-label">实际值：</span>
                                        <el-tag :type="valveActualValues[item.code] ? 'success' : 'danger'" size="large"
                                            class="actual-tag">
                                            {{ valveActualValues[item.code] ? '开启' : '关闭' }}
                                        </el-tag>
                                    </div>
                                    <!-- 设置值按钮 -->
                                    <div class="set-value">
                                        <span class="value-label">设置值：</span>
                                        <el-button :type="valveSetValues[item.code] ? 'success' : 'danger'"
                                            :loading="valveLoadingMap[item.code]"
                                            @click="handleValveToggle(item.code, !valveSetValues[item.code])"
                                            class="valve-button">
                                            {{ valveSetValues[item.code] ? '开启' : '关闭' }}
                                        </el-button>
                                    </div>
                                </div>
                            </div>
                        </el-card>
                    </div>
                </el-card>
                
                <el-card class="light-control-card" shadow="hover">
                    <template #header>
                        <div class="card-header">
                            <span class="card-title">灯光控制</span>
                        </div>
                    </template>
                    <div class="light-button-grid">
                        <el-button type="primary" class="light-button" @click="lightControl(0)">
                            初始化
                        </el-button>
                        <el-button type="danger" class="light-button" @click="lightSceneControl(1)">
                            分离模块罐体<br>
                            液体泄漏
                        </el-button>
                        <el-button type="primary" class="light-button" @click="lightSceneControl(2)">
                            料仓加料模块<br>
                            物料短缺
                        </el-button>
                        <el-button type="info" class="light-button" @click="lightSceneControl(3)">
                            分离模块罐体<br>
                            试验测试
                        </el-button>
                        <el-button type="warning" class="light-button" @click="lightSceneControl(4)">
                            韫珠车间<br>
                            开关异常
                        </el-button>
                        <el-button type="primary" class="light-button" @click="lightSceneControl(5)">
                            热交换模块<br>
                            液体开关检修维护
                        </el-button>
                    </div>
                </el-card>
            </div>
        </div>
    </div>
</template>

<script setup>
const pageCode = 'MGM';
const flowId = 9;//当前页面流程Id
import { useEquipmentLayout } from '@/hooks/useEquipmentLayout'
import { useValveHelper } from '@/hooks/useValveHelper'
import { updateZTPageConfig } from '@/api/dcs/configurePage';
import { getMainSetParam } from '@/api/hnyz/param'
import { setValues } from '@/api/dcs/flowControl'
import { stompClient } from '@/utils/ws/stompClient';
import { onMounted, computed, reactive, ref } from 'vue';

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

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

let deviceDataGroup = reactive({
    VALVES: [],
    SENSORS: []
})

// 阀门加载状态映射
const valveLoadingMap = ref({})

// 阀门设置值映射（用于控制）
const valveSetValues = ref({})

// 阀门实际值映射（用于显示）
const valveActualValues = ref({})

// 传感器设置值映射（用于控制）
const sensorSetValues = ref({})

// 传感器实际值映射（用于显示）
const sensorActualValues = ref({})

// 更新实际值函数
function updateActualValues(data) {
    if (data.VALVES) {
        Object.values(data.VALVES).forEach(valve => {
            if (valve.code && valve.value && valve.value.length > 0) {
                valveActualValues.value[valve.code] = valve.value[0]
            }
        })
    }
    if (data.SENSORS) {
        Object.values(data.SENSORS).forEach(sensor => {
            if (sensor.code && sensor.value && sensor.value.length > 0) {
                sensorActualValues.value[sensor.code] = sensor.value[0]
            }
        })
    }
}

//更新页面配置
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)

                // 初始化阀门和传感器设置值和实际值（仅在首次加载时）
                if (data.VALVES && Object.keys(valveSetValues.value).length === 0) {
                    Object.values(data.VALVES).forEach(valve => {
                        if (valve.code && valve.value && valve.value.length > 0) {
                            // 设置值初始化为实际值
                            valveSetValues.value[valve.code] = valve.value[0]
                            // 实际值初始化为实际值
                            valveActualValues.value[valve.code] = valve.value[0]
                        }
                    })
                }
                if (data.SENSORS && Object.keys(sensorSetValues.value).length === 0) {
                    Object.values(data.SENSORS).forEach(sensor => {
                        if (sensor.code && sensor.value && sensor.value.length > 0) {
                            // 设置值初始化为实际值
                            sensorSetValues.value[sensor.code] = sensor.value[0]
                            // 实际值初始化为实际值
                            sensorActualValues.value[sensor.code] = sensor.value[0]
                        }
                    })
                }

                if (Object.keys(valveSetValues.value).length > 0 || Object.keys(sensorSetValues.value).length > 0) {
                    // 后续更新只更新实际值
                    updateActualValues(data)
                }
            });
        })
        .catch(err => {
            console.log('页面配置失败：', err);
        });
}

const { getDataArrByCode, getValveStatusArr } = useValveHelper(computed(() => [
    ...deviceDataGroup.VALVES,
    ...deviceDataGroup.SENSORS
]))

// 格式化传感器数值
const formatValue = (value) => {
    if (typeof value === 'number') {
        return value.toFixed(2)
    }
    return value || '0.00'
}

// 获取传感器数值类型（用于标签颜色）
const getValueType = (value) => {
    if (typeof value === 'number') {
        if (value > 0.8) return 'danger'
        if (value > 0.5) return 'warning'
        return 'success'
    }
    return 'info'
}

// 获取阀门状态类型
const getValveStatusType = (status) => {
    return status ? 'success' : 'danger'
}

// 阀门切换处理函数
const handleValveToggle = async (code, value) => {
    // 检查是否正在操作中
    if (valveLoadingMap.value[code]) {
        console.log(`阀门 ${code} 正在操作中，请稍候...`)
        return
    }

    // 设置加载状态，防止重复点击
    valveLoadingMap.value[code] = true

    try {
        const res = await getMainSetParam(code)
        const registerData = res.data

        const data = {
            protocol: registerData.protocolId,
            unitId: registerData.unitId,
            dataType: registerData.dataType,
            // resource: '05',
            offsetOrIndex: registerData.address,
            lengthOrQos: 1,
            data: [value],
            ipAddress: registerData.ipAddress,
            port: registerData.port
        }

        await setValues(data)

        // 更新设置值
        valveSetValues.value[code] = value

        console.log(`阀门 ${code} 状态切换成功: ${value ? '开启' : '关闭'}`)
    } catch (err) {
        console.error('阀门状态切换失败：', err)
        // 切换失败时恢复原状态
        valveSetValues.value[code] = !value
    } finally {
        // 清除加载状态
        valveLoadingMap.value[code] = false
    }
}

// 传感器数值变化处理函数
const handleSensorValueChange = async (code, value) => {
    try {
        const res = await getMainSetParam(code)
        const registerData = res.data

        const data = {
            protocol: registerData.protocolId,
            unitId: registerData.unitId,
            dataType: registerData.dataType,
            // resource: '06', // 写保持寄存器
            offsetOrIndex: registerData.address,
            lengthOrQos: 1,
            data: [value],
            ipAddress: registerData.ipAddress,
            port: registerData.port
        }

        await setValues(data)

        // 更新设置值
        sensorSetValues.value[code] = value

        console.log(`传感器 ${code} 数值设置成功: ${value}`)
    } catch (err) {
        console.error('传感器数值设置失败：', err)
        // 设置失败时恢复原值
        sensorSetValues.value[code] = value
    }
}


// const lightList = [100, 0]// 灯控配置
const lightList = [
    '00', // 0 白色
    '01', // 韫珠办公楼
    '02', // 雪豹办公室
    '03', // 分离模块
    '04', // 吸附模块
    '05', // 加料模块
    '06', // 公共模块
    '07', 
    '08', 
    '09', 
    '10', 
    '11', 
    '12'
]// 灯控配置
// 0 白色 1 绿色 2 红色 3橙色 4 蓝色 5紫色 6 彩虹色 7 青色 8黄色
const lightColor = [
    'ffffff', // 0 白色
    '00ff00', // 1 绿色 正常运行
    'ff0000', // 2 红色 严重异常/生产线停止
    'ffa500', // 3 橙色 告警/预警
    '0000ff', // 4 蓝色 物料请求/投料
    '800080', // 5 紫色 质检/审核
    'ff69b4', // 6 彩虹色 粉色识别为彩虹色 测试/实验
    '00ffff', // 7 青色 待启动
    'ffff00'  // 8 黄色 保养中
]
const lightState = ref(0)// 当前灯光状态

// 6种情景场景定义，存储在lightScene数组中
const lightScene = [
    [
        {id: 1, color: 6}, {id: 7, color: 6},
        {id: 2, color: 6}, {id: 8, color: 6},
        {id: 3, color: 6}, {id: 9, color: 6},
        {id: 4, color: 6}, {id: 10, color: 6},
        {id: 5, color: 6}, {id: 11, color: 6},
        {id: 6, color: 6}, {id: 12, color: 6}
    ],
    // 情景1：分离模块罐体液体泄露
    [
        {id: 10, color: 2}, {id: 4, color: 2},
        {id: 6, color: 2},
    ],
    // 情景2：料仓加料模块的物料短缺
    [
        {id: 12, color: 4}, {id: 5, color: 4},
    ],
    // 情景3：分离模块罐体1进行试验测试
    [
        {id: 10, color: 6}, {id: 3, color: 6},
    ],
    // 情景4：韫珠车间开关异常
    [
        {id: 8, color: 3}, {id: 1, color: 3},
    ],
    // 情景5：热交换模块液体开关检修维护
    [
        {id: 8, color: 7}, {id: 3, color: 7},
    ],
]
function lightControl(e, colorItem) {
    if (e == 0) {
        setValues({
            "dataType": "string",
            "lengthOrQos": 1,
            "unitId": 0,
            "offsetOrIndex": 0,
            "protocol": 3,
            "data": [
                '00 00 000000', "ygtx", "T!8dFz$kW3#pRgM2"
            ],
            "ipAddress": "222.243.138.146",
            "port": 1883,
            "resource": "yzkj/zkshi/esp32/12led",
        })
        return
    }
    if (e == 99) {
        console.log('99')
        asyncLightControl(
            ...lightScene[0]
        )
        return
    }
    lightChange(e)
}

function lightSceneControl(e) {
    asyncLightControl(
        ...lightScene[e]
    )
}

function asyncLightControl(...e) {
    if (e.length === 0) return;
    lightChange(e[0].id, e[0].color);
    if (e.length > 1) {
        setTimeout(() => {
            asyncLightControl(...e.slice(1));
        }, 100);
    }
}

function lightChange(i, colorItem) {
    lightState.value = i
    const light = lightList[i]
    const color = colorItem == undefined ? lightColor[i<=8?i:i-8] : lightColor[colorItem]
    const state = color == 'ff69b4' ? '02' : '01'

    console.log('灯控', light, state, color)

    const data = {
        "dataType": "string",
        "lengthOrQos": 1,
        "unitId": 0,
        "offsetOrIndex": 0,
        "protocol": 3,
        "data": [
            light + " " + state + " " + color, "ygtx", "T!8dFz$kW3#pRgM2"
        ],
        "ipAddress": "222.243.138.146",
        "port": 1883,
        "resource": "yzkj/zkshi/esp32/12led",
    }
    setValues(data)
}

</script>

<style lang="scss" scoped>
.page {
    width: 100%;
    min-height: 100vh;
    background-color: #0b172c;

    .content_page {
        position: relative;
        width: 100%;
        min-height: 100vh;
        padding: 20px;





        .device-status-container {
            display: flex;
            flex-direction: column;
            gap: 20px;
            max-width: 1200px;
            margin: 0 auto;

            .status-card {
                background: rgba(255, 255, 255, 0.95);
                border-radius: 8px;

                .card-header {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;

                    .card-title {
                        font-size: 18px;
                        font-weight: 600;
                        color: #303133;
                    }
                }

                .device-grid {
                    display: grid;
                    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
                    gap: 16px;
                    margin-top: 16px;

                    .device-item {
                        border: 1px solid #e4e7ed;
                        transition: all 0.3s ease;

                        &:hover {
                            transform: translateY(-2px);
                            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
                        }

                        .device-info {
                            display: flex;
                            flex-direction: column;
                            gap: 8px;

                            .device-name-code {
                                display: flex;
                                align-items: center;
                                gap: 12px;

                                .device-name {
                                    font-size: 16px;
                                    font-weight: 600;
                                    color: #303133;
                                    line-height: 1.2;
                                }

                                .device-code {
                                    font-size: 12px;
                                    color: #909399;
                                    background: #f5f7fa;
                                    padding: 4px 8px;
                                    border-radius: 4px;
                                    display: inline-block;
                                    width: fit-content;
                                }
                            }

                            .device-value {
                                display: flex;
                                flex-direction: row;
                                justify-content: space-between;
                                align-items: center;
                                gap: 16px;
                                margin-top: 8px;

                                .sensor-input {
                                    width: 120px;

                                    :deep(.el-input-number__decrease),
                                    :deep(.el-input-number__increase) {
                                        background-color: #f5f7fa;
                                        border-color: #dcdfe6;

                                        &:hover {
                                            background-color: #e4e7ed;
                                        }
                                    }

                                    :deep(.el-input__inner) {
                                        text-align: center;
                                        font-weight: 600;
                                        font-size: 14px;
                                    }
                                }

                                .actual-value,
                                .set-value {
                                    display: flex;
                                    align-items: center;
                                    gap: 8px;

                                    .value-label {
                                        font-size: 12px;
                                        color: #606266;
                                        font-weight: 500;
                                        min-width: 50px;
                                    }

                                    .actual-tag {
                                        min-width: 60px;
                                        text-align: center;
                                        font-weight: 600;
                                        font-size: 16px;
                                    }

                                    .valve-button {
                                        min-width: 80px;
                                        font-weight: 600;
                                        border-radius: 6px;
                                        font-size: 14px;

                                        &:hover {
                                            transform: translateY(-1px);
                                            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
                                        }
                                    }
                                }

                                // 实际值区域整体放大
                                .actual-value {
                                    .value-label {
                                        font-size: 16px;
                                        font-weight: 600;
                                        color: #303133;
                                    }

                                    .actual-tag {
                                        font-size: 18px;
                                        padding: 6px 12px;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            .light-control-card {
                background: rgba(255, 255, 255, 0.95);
                border-radius: 8px;

                .card-header {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;

                    .card-title {
                        font-size: 18px;
                        font-weight: 600;
                        color: #303133;
                    }
                }

                .light-button-grid {
                    display: grid;
                    grid-template-columns: repeat(3, 1fr);
                    gap: 12px;

                    .el-button+.el-button {
                        margin-left: 0;
                    }
                }

                .light-button {
                    width: 100%;
                    font-size: 12px;
                    padding: 8px 4px;
                    line-height: 1.2;
                }
            }
        }
    }
}
</style>