<template>
    <div class="circle-btn flexcenter">
        <div @click="circleState = !circleState">
            <slot></slot>
        </div>
        <div class="circle-pop flexacenter" v-if="circleState">
            <img class="close-icon" src="@/assets/img/publicImage/circle-close.png" alt="" @click.stop="circleState = !circleState" />
            <img class="circle-bj-green" src="@/assets/img/publicImage/wechat-pop-green.svg" />
            <div class="circle-title flexacenter">
                欢迎联系 <b>{{ wechat["nickname"] }}</b> 咨询公寓
            </div>
            <div class="circle-QRcode flexcenter">
                <img class="circle-QRcode-img" :src="wechat['personalqrcode']" />
            </div>
            <div class="circle-hint">微信扫码添加好友</div>
        </div>
    </div>
</template>

<script setup>
import { useStore } from "vuex";
const store = useStore();
const { wechat } = toRefs(store.state);
import { reactive, onMounted, ref, toRefs, nextTick, onBeforeUnmount, watch, getCurrentInstance, onUnmounted } from "vue";

let circleState = ref(false);

// 定义点击事件处理函数
const handleDocumentClick = (e) => {
    // 点击关闭按钮关闭
    if (e.target.classList.contains("close-icon")) {
        circleState.value = false;
        return;
    }
    // 点击其他地方且弹窗显示时关闭
    if (circleState.value) {
        circleState.value = false;
    }
};

// 监听 circleState 变化，状态为 true 时开始监听，为 false 时停止监听
watch(circleState, (newVal) => {
    if (newVal) {
        setTimeout(() => {
            document.addEventListener("click", handleDocumentClick);
        }, 1000);
    } else {
        setTimeout(() => {
            document.removeEventListener("click", handleDocumentClick);
        }, 1000);
    }
});

// 组件卸载时移除事件监听，避免内存泄漏
onUnmounted(() => {
    document.removeEventListener("click", handleDocumentClick);
});
</script>

<style lang="less" scoped>
// @media screen and (max-width: 1360px) {
//     .circle-btn {
//         right: 20px !important;
//     }
// }

.circle-btn {
    position: relative;
    // position: fixed;
    // bottom: 158px;
    // right: calc((100vw - 1200px) / 2 - 75px);

    // width: 60px;
    // height: 60px;
    // background-color: #50e3c2;
    // border-radius: 50%;
    // cursor: pointer;
    // z-index: 10000;

    .circle-inside {
        flex-direction: column;
        background-color: #cbf7ed;
        width: 50px;
        height: 50px;
        border-radius: 50%;
        position: relative;
        z-index: 1;
        color: #000;
        font-size: 14px;

        .circle-bj {
            position: absolute;
            top: 0;
            left: 0;
            width: 50px;
            height: 50px;
            z-index: -1;
        }

        .circle-icon {
            width: 20px;
            height: 17px;
            margin-bottom: 2px;
        }
    }
}

.circle-pop {
    position: absolute;
    bottom: 40px;
    // right: 65px;
    width: 300px;
    height: 300px;
    border-radius: 10px;
    flex-direction: column;
    z-index: 1100;
    padding-top: 45px;
    font-family: "PingFangSC-Regular", "PingFang SC", sans-serif;
    // -moz-box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.101960784313725);
    // -webkit-box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.101960784313725);
    // box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.101960784313725);
    background-color: #fff;
    filter: drop-shadow(0px 0px 10px rgba(0, 0, 0, 0.50196078));
    .close-icon {
        position: absolute;
        right: 10px;
        top: 10px;
        width: 16px;
        height: 16px;
        cursor: pointer;
    }

    .circle-bj {
        position: absolute;
        top: -15px;
        left: -15px;
        width: 341px;
        height: 336px;
        z-index: -1;
    }

    .circle-bj-green {
        width: 300px;
        // height: 156px;
        position: absolute;
        bottom: -9.5px;
        left: 0;
        z-index: -1;
    }

    .circle-title {
        font-size: 15px;
        color: #555555;
        margin-bottom: 24px;

        b {
            color: #000;
            font-weight: 650;
            margin: 0 5px;
        }
    }

    .circle-QRcode {
        width: 120px;
        height: 120px;
        border-radius: 20px;
        -moz-box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.101960784313725);
        -webkit-box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.101960784313725);
        box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.101960784313725);
        margin-bottom: 20px;
        background: #fff;

        .circle-QRcode-img {
            width: 100px;
            height: 100px;
        }
    }

    .circle-hint {
        margin-bottom: 6px;
    }

    .circle-hint,
    .circle-remark {
        font-size: 13px;
        color: #555555;
        line-height: 18px;
    }

    .circle-remark {
        b {
            font-weight: 650;
            color: #000000;
        }
    }
}
</style>
