<template>
    <div class="invite-friends">
        <div class="txt">To open the treasure chest you need to share the URL to your friends. Make sure they finish
            the
            tasks.</div>
        <div class="area-url">
            <div class="url">{{ state.detail.inviteUrl }}</div>
            <div class="btn copy-btn" @click="clickCopy"
             v-click-log="state.log_invite_copy_btn_click"
            :data-clipboard-text="state.detail.inviteCopyUrl">
                Copy
            </div>
        </div>
        <div class="share-list">
            <img :src="item.iconPath" alt="" v-for="item in state.share_list" :data-clipboard-text="item.inviteContent"
                @click="clickShare(item)" class="share-item" />
        </div>
        <v-btn :txt="state.open_btn.txt" :font-size="'17px'" class="btn" :icon="false"
            :disabled="state.open_btn.disabled"
            v-show-log="state.log_invite_btn_show"
            v-click-log="state.log_invite_btn_click"
             @onClick="clickBtn" font-weight="600"></v-btn>

    </div>
</template>
<script setup>
import VBtn from '@/view/iframe/treasure-hunt/components/btn.vue'
import { inviteChannel } from '@/http/treasure'
import { inject, onMounted } from 'vue'
import Report from "@/log-center/log"

let ClipboardJS = require('clipboard');

let state = inject('state')

state.log_invite_btn_show = {
    businessType: Report.businessType.buttonView,
    pageSource: Report.pageSource.inviteFriendsPage,
    objectType: Report.objectType.openChestButton,
    redPacketType: Report.redPacketType.treasure,
    shareLinkId:  state.invite_code,
    myShareLinkId: state.detail.inviteCopyUrl,
    currentInvitedNum: state.detail.inviteCount,
    postId: state.postId
}

state.log_invite_btn_click = {
    businessType: Report.businessType.buttonClick,
    pageSource: Report.pageSource.inviteFriendsPage,
    objectType: Report.objectType.openChestButton,
    redPacketType: Report.redPacketType.treasure,
    shareLinkId:  state.invite_code,
    myShareLinkId: state.detail.inviteCopyUrl,
    currentInvitedNum: state.detail.inviteCount,
    postId: state.postId
}
state.log_invite_copy_btn_click = {
    businessType: Report.businessType.buttonClick,
    pageSource: Report.pageSource.inviteFriendsPage,
    objectType: Report.objectType.copyButton,
    redPacketType: Report.redPacketType.treasure,
    shareLinkId:  state.invite_code,
    myShareLinkId: state.detail.inviteCopyUrl,
    currentInvitedNum: state.detail.inviteCount,
    postId: state.postId
}
onMounted(() => {
    try {
        chrome.management.get('ophjlpahpchlmihnnnihgmmeilfjmjjc', (res) => {
            let linePluginInstalled = 0
            if (res) {
                linePluginInstalled = 1
            }
            inviteChannel({
                params: {
                    linePluginInstalled,
                    postId: state.postId
                }
            }).then((res) => {
                if (res.code == 0) {
                    state.share_list = res.data
                }
            })
        })
    } catch (error) {
        console.error(error)
    }

})

const clickBtn = () => {
    state.treasureOpen()
}

const clickShare = (item) => {
    var clipboard = new ClipboardJS('.share-item');
    clipboard.on('success', function (e) {
        state.toast.txt = 'Copy Successfully'
        state.toast.has_icon = true
        state.toast.show = true
        setTimeout(() => {
            state.toast.show = false
        }, 2000)
        e.clearSelection();
    })
    chrome.tabs.create({
        url: item.redirectPath
    })
}


const clickCopy = () => {
    var clipboard = new ClipboardJS('.copy-btn');
    clipboard.on('success', function (e) {
        state.toast.txt = 'Copy Successfully'
        state.toast.has_icon = true
        state.toast.show = true
        setTimeout(() => {
            state.toast.show = false
        }, 2000)
        e.clearSelection();
    })

    clipboard.on('error', function (e) {
        state.toast.txt = 'Copy Error'
        state.toast.has_icon = false
        state.toast.show = true
        setTimeout(() => {
            state.toast.show = false
        }, 2000)
    })
}
</script>
<style lang="scss" scoped>
.invite-friends {
    padding: 18px 16px 25px 16px;
    background: #fff;

    .txt {
        font-style: normal;
        font-weight: 500;
        font-size: 13px;
        line-height: 18px;
        /* or 129% */
        margin-bottom: 18.5px;

        letter-spacing: 0.3px;

        color: #000000;
    }

    .area-url {
        height: 70px;
        background: rgba(29, 155, 240, 0.01);
        border: 1px solid #1D9BF0;
        border-radius: 5px;
        display: flex;
        align-items: center;
        padding-left: 15px;
        padding-right: 11px;
        justify-content: space-between;

        .url {
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 3;
            overflow: hidden;
            width: 194px;

            color: #737373;
            font-weight: 400;
            font-size: 13px;
            white-space: normal;

            word-wrap: break-word;

            word-break: break-all;
        }

        .btn {
            user-select: none;
            background: #1D9BF0;
            border-radius: 35px;
            width: 100px;
            text-align: center;
            line-height: 37px;
            height: 37px;
            font-weight: 700;
            font-size: 15px;
            color: #fff;
            cursor: pointer;
        }
    }

    .share-list {
        margin-top: 20px;
        text-align: center;
        margin-bottom: 14px;

        img {
            user-select: none;
            cursor: pointer;
            width: 33px;
            height: 33px;
            margin-right: 14px;
            border-radius: 100px;
        }
    }


}
</style>
