<template>
    <div class="flow_single flow_others">
        <div class="flow_single_title">{{ title }}</div>
        <div class="flow_single_show">
            <!-- 变频状态 -->
            <div class="show_body">
                <div class="show_body_left">状态</div>
                <div class="show_body_right">
                    <div class="show_detail">
                        <div class="text">当前</div>
                        <div class="body_show">
                            <div class="body_show_value">
                                {{ statusWord[pumpDataArr[0]] || statusWord[3] }}
                            </div>
                        </div>
                    </div>
                    <div class="show_detail">
                        <div class="btn_set">
                            <el-button size="small" type="primary" :disabled="!pumpSetArr.length"
                                @click="setPump(pumpSetArr[0], true)">启动</el-button>
                        </div>
                        <div class="btn_set">
                            <el-button size="small" type="primary" :disabled="!pumpSetArr.length"
                                @click="setPump(pumpSetArr[0], false)">关闭</el-button>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 转速 -->
            <div class="show_body" v-if="pumpSetArr.length > 1">
                <div class="show_body_left">转速</div>
                <div class="show_body_right">
                    <div class="show_detail">
                        <div class="text">当前</div>
                        <div class="body_show">
                            <div class="body_show_value">
                                {{ (pumpDataArr[1] / 50) || 0 }}
                            </div>
                            <div class="body_show_unit">{{ pumpSetArr[1].unitType || '%' }}</div>
                        </div>
                    </div>
                    <div class="show_detail">
                        <div class="btn_set">
                            <el-button size="small" type="primary" :disabled="!pumpSetArr.length"
                                @click="setPumpVaule(pumpSetArr[1], speedValue * 50)">设置</el-button>
                        </div>
                        <div class="body_set">
                            <div class="body_show_value">
                                <el-input v-model="speedValue" style="width: 75px" size="small" type="number" :min="0"
                                    :max="100" />
                            </div>
                            <div class="body_show_unit">{{ pumpSetArr[1].unitType || '%' }}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, computed, onMounted, watch } from "vue";
import { openElWarnMessage } from '@/utils/message';
import { getAllSetParam } from '@/api/hnyz/param'
import { setValues } from '@/api/dcs/flowControl';

const props = defineProps({
    title: String,
    pumpArr: Array, // 泵数据
    code: String,   // 泵编码
});

// 当前数据显示
const pumpDataArr = computed(() => props.pumpArr || [3, 0]);

// 异步获取设定参数（如地址、协议等）
const pumpSetArr = ref([])

const loadPumpSetParams = async (code) => {
    try {
        const res = await getAllSetParam(code);
        pumpSetArr.value = res.data || [];
    } catch (err) {
        console.log("获取设备参数失败：", err);
        pumpSetArr.value = [];
    }
}

// 页面初始化或 code 变化时获取参数
onMounted(() => {
    if (props.code) {
        loadPumpSetParams(props.code);
    }
});

watch(() => props.code, (newCode) => {
    if (newCode) {
        loadPumpSetParams(newCode);
    }
});

// 响应式转速
const speedValue = ref(0);

// 状态字映射
const statusWord = ref(['', '正转', '反转', '停机', '故障'])

// 控制代码字典
const frequencyOptions = [
    { value: 1, label: '正转' },
    { value: 2, label: '反转' },
    { value: 5, label: '停止' }
]

// 设置泵启停
function setPump(registerData, value) {
    const open = 0;
    const close = 2;
    const setValue = value ? frequencyOptions[open].value : frequencyOptions[close].value;
    setPumpVaule(registerData, setValue);
    //如果是关闭，则同步设置转速为0
    if (!value) {
        setPumpVaule(pumpSetArr.value[1], 0);
    }
}

// 设置转速
function setPumpVaule(registerData, value) {
    if (!registerData) {
        openElWarnMessage('未获取到设定地址');
        return;
    }

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

    setValues(data).then(() => {
        // 可添加提示或日志
    }).catch(err => {
        openElWarnMessage('设置失败：' + err.message || '未知错误');
    });
}
</script>

<style scoped lang="scss">
.flow_single {
    width: 100px;
    display: flex;
    padding: 4px;
    flex-direction: column;
    align-items: center;
    gap: 3px;
    border-radius: 5px;
    border: 1px solid #72E0FF;

    .flow_single_title {
        display: flex;
        padding: 4px 8px;
        justify-content: center;
        align-items: center;
        gap: 10px;
        align-self: stretch;
        color: #76E1FF;
        font-size: 20px;
        font-weight: bold;
        border-radius: 0px 0px 5px 5px;
        background: #192846;
    }

    .flow_single_show {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        color: #FFF;
        height: 100px;
        gap: 3px;

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

            .show_body_left {
                width: 50px;
                text-align: center;
                padding: 7px 0;
                font-size: large;
                background: #0e2b60;
                border-radius: 5px;
                color: #63c8c7;
            }

            .show_body_right {
                gap: 5px;
                width: 170px;
                display: flex;
                flex-direction: column;
                align-items: center;
                justify-content: center;

                .show_detail {
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    gap: 5px;

                    .text,
                    .btn_set {
                        text-align: center;
                        font-size: small;
                        width: 47px;

                        :deep(.el-button--small) {
                            height: 18px !important;
                        }
                    }

                    .body_show,
                    .body_set {
                        width: 115px;
                        display: flex;
                        align-items: center;
                        justify-content: flex-start;
                        gap: 5px;

                        .body_show_value {
                            text-align: center;
                            width: 75px;
                            font-size: small;
                            border: solid 1px #B8CEE5;
                            border-radius: 5px;

                            :deep(.el-select--small .el-select__wrapper) {
                                min-height: 18px;
                                text-align: right;
                            }

                            :deep(.el-input--small) {
                                height: 18px;

                                .el-input__inner {
                                    text-align: center;
                                    margin-left: 10px;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

.flow_others {
    width: 250px;
    margin: 10px;
}
</style>
