<template>
    <!-- <div class="border_body"> -->
    <div class="data_item">
        <div class="item_left">
            <div class="title" v-if="title">
                <div class="title_icon"></div>
                <text>{{ title }}</text>
            </div>
            <text class="value">{{ value }}</text>
            <text class="unit">{{ unitArr[type][0] }}</text>
        </div>
        <div class="item_right">
            <div class="item_right_top">
                <text>{{ unitArr[type][1] }}</text>
                <input type="number" v-model="settingValue" class="speedInput">
                <text>{{ unitArr[type][2] }}</text>
            </div>
            <div class="item_right_bottom">
                <el-button type="primary">确定</el-button>
                <el-button type="info">取消</el-button>
            </div>
        </div>
    </div>
    <!-- </div> -->
</template>

<script setup>
import { ref, toRefs, onMounted, onUnmounted } from 'vue';
import { EventBus } from '@/utils/eventBus';

onMounted(() => {
    EventBus.on("clearSettingValue", clearSettingValue);
});

onUnmounted(() => {
    EventBus.off("clearSettingValue", clearSettingValue);// 组件销毁时，清除监听事件
});

const props = defineProps({
    title: {
        type: String,
        required: false
    },
    // 类型，用于区分是哪个设备
    type: {
        type: String,
        required: true
    },
    value: {
        type: Number,
        required: true, // 这里的required: true是为了在父组件中使用时，必须传入value值
    },
    setValue: {
        type: Number,
        required: false
    }
});

const { setValue } = toRefs(props);// 这里的toRefs是为了解构props中的值，方便在模板中使用
const settingValue = ref(setValue.value || 500);// 设置 speedInput 的默认值

//不同类别的设备，设置的单位不同，这里根据type来设置(固定配置)
const unitArr = {
    "speed": ['当前转速(RPM)', '设置转速', 'RPM'],// 速度
    "weight": ['重量(KG)', '设置重量', 'KG'],// 质量
    "time": ['剩余时间(分钟)', '增加时间', '分'],// 时间
    "angle": ['阀门开度(%)', '设置开度', '%'],// 开度
    "temperature": ['当前温度(℃)', '设置温度', '℃'],//温度
}

//清除设置值
function clearSettingValue() {
    settingValue.value = '';
}


</script>

<style scoped lang="scss">
.data_item {
    border-radius: 10px;
    background: #EFF7FF;
    display: flex;
    width: 334px;
    padding: 8px;
    align-items: center;
    align-content: space-between;
    column-gap: 20px;
    flex-wrap: wrap;

    .item_left {
        display: flex;
        flex-direction: column;
        justify-content: center;
        // align-items: center;
        width: 98px;
        gap: 5px;

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

            .title_icon {
                width: 4px;
                height: 10px;
                border-radius: 10px;
                background: #1465BE;
            }

            text {
                color: #1465BE;
                // font-family: OPPOSans;
                font-size: 14px;
                // font-style: normal;
                // font-weight: 400;
                // line-height: normal;
            }
        }

        text {
            font-family: OPPOSans;
            font-style: normal;
            font-weight: 400;
            line-height: normal;
        }

        .value {
            color: #0DA4D1;
            font-size: 36px;
        }

        .unit {
            color: #919EB1;
            font-size: 12px;
        }
    }

    .item_right {
        display: flex;
        padding: 4px;
        flex-direction: column;
        align-items: center;
        gap: 5px;
        border-radius: 5px;
        background: #E5EEF9;
        width: 198px;

        .item_right_top {
            display: flex;
            padding: 4px;
            align-items: center;
            gap: 10px;

            text {
                color: #64748B;
                font-family: OPPOSans;
                font-size: 12px;
                font-style: normal;
                font-weight: 400;
                line-height: normal;
            }

            .speedInput {
                display: flex;
                width: 82px;
                padding: 6px 0px;
                padding-left: 15px;
                justify-content: center;
                align-items: flex-start;
                gap: 10px;
                border-radius: 3px;
                border: 1px solid #B8CEE5;
                background: #EDF7FF;
                text-align: center;
            }
        }

        .item_right_bottom {
            display: flex;
            padding: 4px;
            justify-content: center;
            align-items: center;
            gap: 20px;
            align-self: stretch;
        }
    }
}
</style>
