<template>
    <div class="alarm-wrap">
        <div class="alarm-t">
            <div class="alarm-tag">
                <img src="@/assets/images/warningHome/alarm-bg.png" />
            </div>
            <div class="alarm-item-wrap">
                <div class="item-wrap">
                    <div
                        class="alarm-item"
                        v-for="(item, index) in alarmList"
                        :key="index"
                        @click="toggleAlarm(item, 1)"
                        :class="{ active: activeAlarm === item.id }"
                    >
                        {{ item.name }}
                        <div class="active-arrow" v-if="activeAlarm === item.id">
                            <img src="@/assets/images/warningHome/alarm-active-arrow.png" />
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="alarm-t alarm-b">
            <div class="alarm-tag">
                <img src="@/assets/images/warningHome/alarm-bg2.png" />
            </div>
            <div class="alarm-item-wrap">
                <div class="item-wrap">
                    <div
                        class="alarm-item"
                        v-for="(item, index) in alarmFactorList"
                        :key="index"
                        @click="toggleAlarm(item, 2)"
                        :class="{ active: activeAlarm === item.id }"
                    >
                        {{ item.name }}
                        <div class="active-arrow" v-if="activeAlarm === item.id">
                            <img src="@/assets/images/warningHome/alarm-active-arrow.png" />
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import eventBus from "@/api/eventBus";
import { nextTick, onMounted, ref } from "vue";

const alarmList = ref([
    // { name: "低温冻害", id: 1 },
    // { name: "干旱缺水", id: 2 },
    // { name: "冲梢风险", id: 3 },
    // { name: "病虫风险", id: 4 },
    // { name: "干热风险", id: 1 },
    { name: "阴雨沤花", id: 2 },
    { name: "阴天寡照", id: 3 },
]);
const alarmFactorList = ref([
    { name: "日间温度", id: 21 },
    { name: "夜间温度", id: 22 },
    { name: "土壤水分", id: 23 },
]);

const mapLayerList = ref({});
onMounted(() => {
    VE_API.warning.fetchWarningLayer({
        k: "risk_map",
        resultType: "json",
    }).then(({ data }) => {
        mapLayerList.value = data;
        eventBus.emit("alarmList:warningLayers", data);
        // 页面刚进入时不主动触发任何预警图层，等待用户点击
    });
    eventBus.on("warningHome:toggleArea", (id) => {
        activeAlarm.value = null;
    });
    // 用户从“预警分布”tab 切走时，清空当前选中项
    eventBus.on("warningHome:clearAlarm", () => {
        activeAlarm.value = null;
    });
    // 切换到“预警分布”tab 时，默认选中预警列表的第一个（阴雨沤花），并联动地图
    eventBus.on("warningHome:activeFirstAlarmFactor", () => {
        if (alarmList.value && alarmList.value.length) {
            toggleAlarm(alarmList.value[0], 1);
        }
    });
});

// 初始不选中任何预警项，避免一进页面就显示预警地图
const activeAlarm = ref(null);
const toggleAlarm = (item, type) => {
    activeAlarm.value = item.id;
    // eventBus.emit("alarmList:changeMapLayer", {name: item.name, url: mapLayerList.value[item.name]})
    if (type === 1) {
        // 种植风险
        eventBus.emit("chat:showMapLayer", {name: item.name, mapName: item.name, isHome: true})
    } else {
        eventBus.emit("alarmList:changeMapLayer", {url: mapLayerList.value[item.name], type, name: item.name})
    }
};
</script>

<style lang="scss" scoped>
.alarm-wrap {
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid #9f9f9f;
    border-radius: 8px;
    width: 87px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .alarm-t {
        width: 100%;
        padding-top: 18px;
        .alarm-tag {
            position: relative;
            left: -8px;
            img{
                width: 94px;
                height: 32px;
            }
        }
        .alarm-item-wrap {
            padding: 0 12px;
            .item-wrap {
                padding: 12px 0 24px 0;
                border-bottom: 1px solid rgba(255, 255, 255, 0.3);
                .alarm-item {
                    cursor: pointer;
                    background: #232323;
                    width: 62px;
                    padding: 7px 10px;
                    box-sizing: border-box;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    font-size: 20px;
                    font-family: "PangMenZhengDao";
                    border-radius: 4px;
                    &.active {
                        margin-bottom: 34px;
                        position: relative;
                        color: #ffd489;
                        background: url("@/assets/images/warningHome/alarm-active.png") no-repeat center center / cover;
                    }
                    .active-arrow {
                        position: absolute;
                        bottom: -30px;
                    }
                    &:last-child {
                        &.active {
                            margin-bottom: 14px;
                        }
                    }
                }
                .alarm-item + .alarm-item {
                    margin-top: 8px;
                }
            }
        }
    }
}
</style>
