<template>
    <div class="denet-toolbox">
        <div class="head">
            <span></span>
            <div>
                <!-- 缩放 -->
                <img :src="require('@/assets/img/icon-full.png')" alt class="fixed" @click="clickFull" />
                <!-- 关闭 -->
                <img :src="require('@/assets/img/icon-close.png')" alt class="full" @click="clickClose" />
            </div>
        </div>
        <div class="content">
            <iframe :src="state.iframe_url" frameborder="0"></iframe>
        </div>
    </div>
</template>
<script setup>
import { onMounted, reactive, ref } from "vue";
import { sendEventInfo } from "@/uilts/event";
let state = reactive({
    status: '固定', // 全屏
    dom_fixed: null,
    iframe_url: '',
    tweetId: ''
})

window.addEventListener("onEvent", e => {
    let info = e.detail
    switch (info.event_type) {
        // 固定
        case 'ToolBox_To_Fixed':
            // 替换
            if (state.iframe_url || state.tweetId) {
                sendClose()
            }
            state.iframe_url = info.data.iframe_url
            state.tweetId = info.data.tweetId
            break
    }
});


const clickFull = () => {
    if (state.status == '固定') {
        state.status = '全屏'
        changeFull()
    } else {
        state.status = '固定'
        changeFixed()
    }
}
onMounted(() => {
    state.dom_fixed = document.querySelector('#denet-tool-box-fixed')
})

const changeFull = () => {
    state.dom_fixed.style.cssText = `
    width:100%;
    height: 100%;
    position: fixed;
    right: 0px;
    top: 0px;`
}

const changeFixed = () => {
    state.dom_fixed.style.cssText = `
    width: 505px;
    height: 544px;
    position: fixed;
    right: 10px;
    top: 10px;`
}

const clickClose = () => {
    state.dom_fixed.style.display = 'none'
    sendClose()

}
const sendClose = () => {
    let url = state.iframe_url
    let tweetId = state.tweetId
    sendEventInfo({
        event_type: 'ToolBox_Fixed_Close',
        data: {
            url,
            tweetId,
        }
    })
    state.iframe_url = ''
    state.tweetId = ''
}


</script>

<style lang="scss">
.denet-toolbox {
    width: 100%;
    height: 100%;
    filter: drop-shadow(0px 4px 20px rgba(0, 0, 0, 0.2));
    border-radius: 12px;
    overflow: hidden;

    .head {
        width: 100%;
        height: 40px;
        background: #373737;
        display: flex;
        align-items: center;
        justify-content: space-between;

        span {
            color: #FFFFFF;
            font-style: normal;
            font-weight: 500;
            font-size: 14px;
            margin-left: 16px;
        }

        img {
            width: 20px;
            height: 20px;
            cursor: pointer;

        }

        .full {
            margin-right: 16px;
        }

        .fixed {
            margin-right: 20px;
        }
    }

    .content {
        width: 100%;
        height: calc(100% - 40px);
        background: #686868;
        display: flex;
        align-items: center;
        justify-content: center;

        iframe {
            width: 100%;
            height: 100%;
        }
    }
}
</style>