<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" :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" @click="clickShare(item)" />
        </div>
        <v-btn :txt="state.open_btn.txt" :font-size="'17px'" class="btn" :icon="false"
            :disabled="state.open_btn.disabled" @onClick="clickBtn"></v-btn>
        <v-toast :show="state.toast.show" :txt="state.toast.txt"></v-toast>
        <open-box v-if="state.open_box.show"></open-box>
    </div>
</template>
<script setup>
import VBtn from '@/view/iframe/treasure-hunt/components/btn.vue'
import VToast from '@/view/iframe/treasure-hunt/components/toast.vue'
import OpenBox from '@/view/iframe/treasure-hunt/components/dialog.vue'
import { inviteChannel } from '@/http/treasure'
import { treasureOpen } from '@/http/treasure'
import { inject, onMounted } from 'vue'
let ClipboardJS = require('clipboard');
let state = inject('state')
state.toast = {}

onMounted(() => {
    inviteChannel({
        params: {
            postId: state.postId
        }
    }).then((res) => {
        if (res.code == 0) {
            state.share_list = res.data
        }
    })
})

const clickBtn = () => {
    treasureOpen({
        params: {
            postId: state.postId,
            treasureId: state.treasureId,
        }
    }).then((res) => {
        if (res.code == 0) {
            state.open_box.show = true
            state.open_box = res.data
        }
    })
}

const clickShare = (item) => {
    window.open(item.redirectPath)
}
var clipboard = new ClipboardJS('.copy-btn');

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

    clipboard.on('error', function (e) {
        state.toast.txt = 'Copy Error'
        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: 14px;
        line-height: 18px;
        /* or 129% */

        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>