<template>
    <div class="equip_box">
        <div class="box_arch_ment">
            <div class="page">
                <div class="header-view">
                    <div class="view_item" style="color:#fff" v-for="item in headerList" :key="item">{{ item }}</div>
                </div>
                <div class="warning-view" v-if="data && data.length > 0">
                    <div class="scroll-view" ref="scrollViewRef" @mouseenter="onMouseenter" @mouseleave="onMouseleave">
                        <div ref="listRef" class="list" v-for="(p, n) in count" :key="n">
                            <div class="item" v-for="(item, index) in data" :key="index">
                                <div class="content view_item">{{ item.name }}</div>
                                <div class="content view_item">{{ item.value }}</div>
                                <div class="time view_item" :class="getColor(item.State)">
                                    <div :class="getColoril(item.State)" class="line_qiu"></div>
                                    {{ getState(item.State) }}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <Empty :bottom="true" v-else></Empty>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, onBeforeUnmount, nextTick, watch } from "vue";
import Empty from '@/components/Empty'
const props = defineProps({
    resultData: {
        type: Object,
        default: {}
    }
})
const headerList = ref(['报警项', '报警值', '状态'])
const data = ref([]); //列表数据
const listRef = ref(); //列表dom
const scrollViewRef = ref(); //滚动区域dom
const count = ref(1); //列表个数

let intervalId = null;
let isAutoScrolling = true; //是否自动滚动标识

function getState(state) {
    var title = ''
    if (state == 0) {
        title = '正常'
    } else if (state == 1) {
        title = '待机'
    } else if (state == 2) {
        title = '异常'
    }
    return title;
}
//获取状态颜色
function getColor(state) {
    var title = ''
    if (state == 0) {
        title = 'green_titleil'
    } else if (state == 1) {
        title = 'yellow_titleil'
    } else if (state == 2) {
        title = 'red_title'
    }
    return title
}
//获取状态颜色
function getColoril(state) {
    var title = ''
    if (state == 0) {
        title = 'green_titleils'
    } else if (state == 1) {
        title = 'yellow_titleils'
    } else if (state == 2) {
        title = 'red_titles'
    }
    return title
}
//获取列表数据
const getData = () => {
    //模拟接口请求列表数据
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            //生成10条数据
            let list = new Array(30).fill().map((item, index) => index);
            resolve(list);
        }, 100);
    });
};

watch(() => props.resultData, (newVal) => {
    if (newVal) {
        // data.value = await getData();
        data.value = newVal;
        // data.value = newVal.AlarmList;
        intervalId && clearInterval(intervalId);
        nextTick(() => {
            //判断列表是否生成滚动条
            count.value = hasScrollBar() ? 2 : 1;
            //有滚动条开始自动滚动
            if (count.value == 2) {
                autoScrolling();
            }
        });
    }
}, { deep: true, immediate: true } // 开启深度监听
)
onMounted(() => {
})
//判断列表是否有滚动条
const hasScrollBar = () => {
    if (scrollViewRef.value) {
        return scrollViewRef.value.scrollHeight > scrollViewRef.value.clientHeight;
    }
};
//设置自动滚动
const autoScrolling = () => {
    intervalId = setInterval(() => {
        if (scrollViewRef.value.scrollTop < listRef.value[0].clientHeight) {
            scrollViewRef.value.scrollTop += isAutoScrolling ? 1 : 0;
        } else {
            scrollViewRef.value.scrollTop = 0;
        }
    }, 20);
};

onBeforeUnmount(() => {
    //离开页面清理定时器
    intervalId && clearInterval(intervalId);
});

//鼠标进入，停止滚动
const onMouseenter = () => {
    isAutoScrolling = false;
};
//鼠标移出，继续滚动
const onMouseleave = () => {
    isAutoScrolling = true;
};
</script>

<style scoped lang="scss">
.equip_box {
    width: 100%;
    height: 100%;
}

.ment_equip {
    margin-top: 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-right: 10px;
    padding-left: 10px;
}

.box_bj_equip {
    width: 40px;
    height: 40px;
    border-radius: 50%;
}

.arch_icon_rke {
    width: 25px;
    height: 25px;
}

.card_ment_equip {
    display: flex;
    align-items: center;
}

.equip_ment {
    display: flex;
    flex-direction: column;
    padding-left: 6px;
}

.equip_ment_title {
    color: #fff;
    font-size: 15px;
    user-select: none;
}

.num_ment {
    margin-top: 2px;
    font-size: 18px;
    color: #fff;
    user-select: none;
}

.box_arch_ment {
    // padding-left: 10px;
    height: 100%;
}

.blue_title {
    background-image: -webkit-linear-gradient(bottom, rgb(47, 119, 173), rgb(229, 232, 236));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.yellow_title {
    background-image: -webkit-linear-gradient(bottom, rgb(187, 132, 83), rgb(229, 232, 236));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.green_title {
    background-image: -webkit-linear-gradient(bottom, rgb(87, 172, 115), rgb(229, 232, 236));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.blue_bj_equip {
    background-image: linear-gradient(25deg, rgb(40, 143, 221) 0%, rgba(0, 0, 0, 0)66%, rgb(40, 143, 221) 100%);
}

.yellow_bj_equip {
    background-image: linear-gradient(25deg, rgb(187, 132, 83) 0%, rgba(0, 0, 0, 0)66%, rgb(187, 132, 83) 100%);
}

.green_bj_equip {
    background-image: linear-gradient(25deg, rgb(87, 172, 115) 0%, rgba(0, 0, 0, 0)66%, rgb(87, 172, 115) 100%);
}

.page {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    overflow: hidden;
}

.header-view {
    margin-top: 10px;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 10px 10px 20px;
    background-color: rgba(16, 40, 92, 1);
}

.view_item {
    flex: 1;
    color: #84eaf8;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    max-width: 33%;
}

.warning-view {
    width: 100%;
    height: calc(100% - 51px);
    display: flex;
    flex-direction: column;
}

.label {
    color: #fff;
    padding: 20px;
    font-size: 22px;
}

.scroll-view {
    flex: 1;
    height: auto;
    width: 100%;
    overflow-y: auto;
}

.list {
    width: 100%;
    box-sizing: border-box;
}

.item {
    padding: 0px 10px 0px 20px;
    width: 100%;
    height: 50px;
    min-height: 50px;
    font-size: 15px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: #eee;
}

.item:nth-child(even) {
    background: rgba(16, 40, 92, 0.4);
}

.item:hover {
    cursor: pointer;
    background-image: linear-gradient(to right, #168cdb, transparent);
}

/*隐藏滚动条
 */
::-webkit-scrollbar {
    display: none;
}

.green_titleil {
    color: #68ba44;
}

.yellow_titleil {
    color: #fcdf3f;
}

.red_title {
    color: #e74a28;
}

.green_titleils {
    background-color: #68ba44;
}

.yellow_titleils {
    background-color: #fcdf3f;
}

.red_titles {
    background-color: #e74a28;
}

.line_qiu {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    display: inline-block;
    margin-right: 6px;
}
</style>