<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[deviceData?.value] || statusWord[3] }}
                            </div>
                        </div>
                    </div>
                    <div class="show_detail">
                        <div class="btn_set">
                            <el-button size="small" type="primary" @click="setPump(setStatusWordAddress, true)">
                                启动</el-button>
                        </div>
                        <div class="btn_set">
                            <el-button size="small" type="primary" @click="setPump(setStatusWordAddress, false)">
                                关闭</el-button>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 转速 -->
            <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">
                                {{ (speedData?.value / 50) || 0 }}
                            </div>
                            <div class="body_show_unit">%</div>
                        </div>
                    </div>
                    <div class="show_detail">
                        <div class="btn_set">
                            <el-button size="small" type="primary"
                                @click="emitUpdate(setSpeedAddress, 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">%</div>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>
</template>

<script setup>
import { ref } from "vue";
import { openElWarnMessage } from '@/utils/message';
import { setIntRegister } from '@/api/dcs/flowControl';
// 接收 `props`
const props = defineProps({
    title: String,
    deviceData: Object, // 变频状态数据
    speedData: Object,  // 转速数据
    setStatusWordAddress: Number,//变频启动设置地址
    setSpeedAddress:  Number,//转速设置地址
});


// 响应式数据
// const setValue = ref(null);
const speedValue = ref(0);
//变频状态字
// const statusWord = ref(['', '正转', '反转', '停机', '故障'])
//如果title包含S
const statusWord = props.title.includes('S') ? ['', '反转', '正转', '停机', '故障'] : ['', '正转', '反转', '停机', '故障']
//变频器频率选项
const frequencyOptions = props.title.includes('S') ?
    [
        {
            value: 2,
            label: '正转',
        },
        {
            value: 1,
            label: '反转',
        },
        {
            value: 5,
            label: '停止',
        }
    ]
    :
    [
        {
            value: 1,
            label: '正转',
        },
        {
            value: 2,
            label: '反转',
        },
        {
            value: 5,
            label: '停止',
        }
    ]
// 设置泵
function setPump(address, value) {
    //地址未定义
    if (!address) {
        openElWarnMessage('地址未定义')
        return;
    }
    const open = 0;
    const close = 2;
    const setValue = value ? frequencyOptions[open].value : frequencyOptions[close].value;
    emitUpdate(address, setValue);
}
// 触发更新事件
function emitUpdate(address, value){
    if (speedValue.value < 0 || speedValue.value > 100) {
        openElWarnMessage('设置值范围0-100')
        speedValue.value = 0
        return
    }
    const param = {
        dataArr: [value],
        startAddress: address,
    }
    setIntRegister(param).then(res => {
        console.log(res);
    })
};
</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: 21px;
        font-weight: bold;
        border-radius: 0px 0px 5px 5px;
        background: #192846;
    }

    .flow_single_body {
        display: flex;
        color: aliceblue;
        align-items: center;
        justify-content: center;
        width: -webkit-fill-available;

        .body_name {
            min-width: 90px;
            color: #008BB1;
            font-family: OPPOSans;
            font-size: 17px;
            font-style: normal;
            font-weight: 600;
            line-height: normal;
            margin-left: 10px;
        }

        .body_show {
            display: flex;
            align-items: center;
            gap: 5px;

            .valve {
                color: #C7D2E0;
                font-size: 50px !important;
                position: relative;

                @keyframes rotate {
                    from {
                        transform: rotate(0deg);
                    }

                    to {
                        transform: rotate(360deg);
                    }
                }

                .fan {
                    color: #bdbb3b;
                    position: absolute;
                    left: 14%;
                    top: 31%;
                    font-size: 36px !important;
                }

                .icon_fengshan_run {
                    display: inline-block;
                    /* 让 transform 生效 */
                    animation: rotate 1s linear infinite;
                    color: #2fb31e;
                }
            }

            :deep(.el-switch) {
                .el-switch__label {
                    color: #FFF;
                }

                .el-switch__label.is-active {
                    color: var(--el-color-primary);
                }

                .el-switch__core {
                    height: 25px;
                    min-width: 50px;
                }
            }

            .body_show_value {
                display: flex;
                width: 82px;
                padding: 6px 0px;
                justify-content: center;
                align-items: flex-start;
                gap: 10px;
                border-radius: 3px;
                border: 1px solid #B8CEE5;
                color: #66ccff;
            }

            .body_show_unit {
                margin-left: 10px;
            }
        }
    }
}

.flow_others {
    width: 250px;

    .flow_single_show {
        display: flex;
        flex-direction: column;
        color: #FFF;
        gap: 3px;

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

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

            .show_body_right {
                gap: 3px;
                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;
                                }
                            }
                        }
                    }
                }
            }
        }

    }
}
</style>