<template>
    <div class="dialog">
        <div class="area-title">
            <img :src="require('@/assets/svg/icon-close.svg')" @click="clickClose" />
            <div class="title">Join Group</div>
        </div>
        <div class="area-content" v-show="state.show == 'tip'">
            <!--  -->
            <div class="logo">
                <img :src="state.params.nftGroupIcon" />
            </div>

            <div class="title">
                {{ state.params.nftGroupName }}
            </div>
            <div class="tip">
                Join to Get Posting Permissions and Badges
            </div>
            <div class="tip2">

                <img :src="require('@/assets/svg/icon-line.svg')" />
                <span>Own this Group's NFT to Join</span>
                <img :src="require('@/assets/svg/icon-line.svg')" />
            </div>
            <div class="btn" v-if="state.params.type == 'buy'" @click="clickBuy">
                Buy NFT to Join
            </div>
            <div class="btn" v-if="state.params.type == 'join'" @click="clickJoin">
                Join Now
            </div>
        </div>
        <div class="area-content success" v-show="state.show == 'success'">
            <!--  -->
            <div class="logo">
                <img :src="require('@/assets/svg/icon-group-success.svg')" />
            </div>

            <div class="title">
                Joined Successfully
            </div>

            <div class="btn" @click="clickFinish">
                Finish
            </div>
        </div>
    </div>

</template>

<script setup>
import { reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { setGroupJoin } from '@/http/group.js'
import { ElMessage } from 'element-plus'
import { sendChromeTabMessage } from '@/uilts/chromeExtension.js';
let state = reactive({
    show: 'tip',
    params: {}
})
const router = useRouter()


const clickJoin = () => {
    // 请求接口
    setGroupJoin({
        params: {
            nftGroupId: state.params.nft_group_Id
        }
    }).then((res) => {
        if (res.code == 0) {
            state.show = 'success'
            state.params.type = 'buy'
            sendChromeTabMessage({ actionType: "FINISH_GROUP_BANNNER" }, () => { })
        } else {
            let msg = ''
            switch (String(res.code)) {
                case '5201':
                    msg = 'need to buy NFT to join the discussion group'
                    break
                default:
                    msg = res.msg
            }
            ElMessage({
                message: msg,
                grouping: true,
                type: 'warning',
                appendTo: document.body
            })
        }
    })
}

const clickBuy = () => {
    // 传输参数
    router.push(`/?nft_group_Id=${state.params.nft_group_Id}&nftProjectId=${state.params.buyNftProjectId}`)
}
const clickClose = () => {
    chrome.tabs.getCurrent((tab) => {
        chrome.tabs.sendMessage(tab.id, {
            actionType: "IFRAME_TWITTER_HIDE_BUY_NFT",
        }, (res) => { });
    })
}

window.addEventListener("message", function (event) {
    if (event.data) {
        switch (event.data.actionType) {
            case 'SHOW_JOIN_DATA':
                state.params = event.data.data
        }
    }
});

const clickFinish = () => {
    clickClose()
}

onMounted(() => {
    let params = router.currentRoute.value.query.params || {}
    state.params = JSON.parse(params)
})

</script>
<style lang="scss" scoped>
.dialog {
    background: #fff;
    border-radius: 25px;
    width: 500px;
    height: 420px;
    z-index: 23;
    display: flex;
    flex-direction: column;


    .area-title {
        width: 100%;
        min-height: 48px;
        display: flex;
        align-items: center;
        border-bottom: 1px solid #D9D9D9;
        font-weight: 500;
        font-size: 16px;
        letter-spacing: 0.3px;
        color: #000000;

        img {
            width: 24p;
            height: 24px;
            margin-left: 14px;
            margin-right: 12px;
            cursor: pointer;
        }

    }

    .area-content {
        flex: 1;
        display: flex;
        justify-content: flex-start;
        flex-direction: column;

        align-items: center;

        .logo {
            width: 80px;
            height: 80px;
            border: 1px solid #D0BAA6;
            border-radius: 5px;
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: 33px;
            margin-bottom: 25px;

            img {
                width: 75px;
                height: 75px;

            }
        }

        .title {
            color: #000000;
            font-size: 18px;

            font-weight: 600;
            margin-bottom: 13px;

        }

        .tip {
            color: #9E9E9E;
            font-weight: 400;
            font-size: 16px;
            margin-bottom: 66px;
        }

        .tip2 {
            margin-bottom: 28px;
            display: flex;
            align-items: center;

            span {
                font-weight: 500;
                font-size: 14px;
                margin: 0 10px;
            }

            img {}
        }

        .btn {
            width: 470px;
            height: 46px;
            background: #1D9BF0;
            border-radius: 100px;
            text-align: center;
            line-height: 46px;
            font-weight: 600;
            font-size: 16px;
            color: #fff;
            cursor: pointer;
        }
    }

    .success {
        .logo {
            width: 120px;
            height: 120px;
            margin-bottom: 36px;
            margin-top: 61px;
            border: 0;

            img {
                width: 100%;
                height: 100%;
            }
        }

        .title {
            font-weight: 600;
            font-size: 22px;
            margin-bottom: 60px;
        }
    }
}
</style>