<template>
    <div class="flows_body_right_tanks">
        <div class="tank_single">
            <div class="flow_single_title"> {{ title }} </div>
            <div class="flow_single_body">
                <div class="body_show">
                    当前：
                    <div class="body_show_value">
                        {{ sensorArr[0] }}
                    </div>
                    <div class="body_show_unit"> {{ unit || '' }}</div>
                </div>
                <div class="body_show">
                    设置：
                    <input class="body_show_input" v-model="setValue" @blur="handleSet" @keyup.enter="handleSet" />
                    <div class="body_show_unit"> {{ unit || '' }}</div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getMainSetParam } from '@/api/hnyz/param'
import { setValues } from '@/api/dcs/flowControl';

const props = defineProps({
    title: String,
    sensorArr: Array,
    code: String,
})

const unit = ref('')
const setValue = ref('')
const registerData = ref({})
// 获取初始设置值和单位
onMounted(() => {
    if (props.code) {
        getMainSetParam(props.code)
            .then(res => {
                unit.value = res.data.unitType || ''
                registerData.value = res.data
            })
            .catch(err => {
                console.error('获取设置值失败:', err)
            })
    }
})

// 提交设置值
const handleSet = () => {
    if (!props.code || setValue.value === '') return

    //   updateMainSetParam(props.code, setValue.value)
    //     .then(() => {
    //       console.log('设置成功')
    //     })
    //     .catch(err => {
    //       console.error('设置失败:', err)
    //     })
    const data = {
        protocol: registerData.value.protocolId,                    // MODBUS_TCP 协议
        unitId: registerData.value.unitId,                      // 从站 ID
        dataType: registerData.value.dataType,                 // 1 表示布尔型
        // resource: '05',                  // 功能码，05 是写单个线圈
        offsetOrIndex: registerData.value.address,   // 起始地址
        lengthOrQos: 1,                // 数量
        data: [setValue.value],                 // 写入的值，例如 [true]
        ipAddress: registerData.value.ipAddress,    // 你的 Modbus TCP 服务地址
        port: registerData.value.port                      // 默认端口
    }
    setValues(data)
}
</script>

<style lang="scss" scoped>
.flows_body_right_tanks {
    display: flex;
    flex-direction: column;
    align-items: center;

    .tank_single {
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 16px;
        border-radius: 12px;
        border: 1px solid #58c3f0;
        background: linear-gradient(to bottom, #0e1a2d, #11243e);
        box-shadow: 0 4px 12px rgba(0, 123, 255, 0.1);
        margin: 10px;
        min-width: 220px;

        .flow_single_title {
            font-size: 20px;
            font-weight: 600;
            color: #76e1ff;
            padding: 6px 12px;
            border-radius: 6px;
            background: rgba(118, 225, 255, 0.1);
            margin-bottom: 12px;
        }

        .flow_single_body {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;

            .body_show {
                display: flex;
                align-items: center;
                gap: 8px;
                font-size: 16px;
                color: #b8cee5;

                .body_show_value {
                    font-size: 26px;
                    font-weight: bold;
                    color: #3ecfff;
                    padding: 6px 14px;
                    border-radius: 6px;
                    border: 1px solid #3ecfff;
                    background: #0b1c2f;
                    min-width: 100px;
                    min-height: 45px;
                    text-align: center;
                }

                .body_show_unit {
                    font-size: 16px;
                    color: #b8cee5;
                }

                .body_show_input {
                    font-size: 26px;
                    font-weight: bold;
                    color: #3ecfff;
                    padding: 6px 14px;
                    border-radius: 6px;
                    border: 1px solid #3ecfff;
                    background: #0b1c2f;
                    min-width: 100px;
                    min-height: 45px;
                    text-align: center;
                    width: 100px;
                }

                .body_show_input:focus {
                    outline: none;
                    border-color: #76e1ff;
                }
            }
        }
    }
}
</style>
