<!-- 发布器-预览卡片 -->
<template>
    <div class="wrapper">
        <div class="card-container">
            <!-- 安装之后的卡片样式 -->
            <div v-show="installStatus" class="left" :style="{'width': reviewCanvasParams.width+ 'px'}">
                <div class="head" :style="{'zoom': reviewCanvasParams.zoom}">
                    <img :src="userInfo.avatarUrl"
                        class="avatar"/>
                    <div class="article-wrapper">
                        <div class="nickname">
                            {{userInfo.name}}
                        </div>
                        <div class="name">
                            @{{userInfo.nickName}}
                        </div>
                    </div>
                </div>
                <div class="after-cover-wrapper-parent" :style="{'zoom': reviewCanvasParams.zoom}">
                    <div class="after-cover-wrapper">
                        <custom-card-cover :data="{
                            totalCount: baseFormData.totalCount,
                            amountValue: baseFormData.amountValue,
                            tokenSymbol: currentCurrencyInfo.tokenSymbol,
                            currencyIconUrl: currentCurrencyInfo.iconPath,
                            userInfo: {
                                nickName: userInfo.name,
                                avatarUrl: userInfo.avatarUrl
                            }
                        }"></custom-card-cover>
                    </div>
                </div>
            </div> 

            <!-- 安装之前的卡片样式 -->
            <div class="content-before"
                v-show="!installStatus" 
                :style="{'width': reviewCanvasParams.width+ 'px'}">
                <div class="head" 
                    :style="{'zoom': reviewCanvasParams.zoom}">
                    <img :src="userInfo.avatarUrl"
                        class="avatar"/>
                    <div class="article-wrapper">
                        <div class="nickname">
                            {{userInfo.name}}
                        </div>
                        <div class="name">
                            @{{userInfo.nickName}}
                        </div>
                    </div>
                </div>
                <div class="card-wrapper" 
                    :style="{'zoom': reviewCanvasParams.zoom}">
                    <img :src="require('@/assets/subject/img-card-cover-blue.png')"
                        class="card-cover"/>
                    <div class="bottom-bar">
                        <div class="title">
                            DeNet.me
                        </div>
                        <div class="desc">
                            🎁 <template v-if="currentCurrencyInfo.tokenSymbol=='USD'">$</template>{{baseFormData.amountValue}} GIVEAWAY
                        </div>
                    </div>
                    <div class="user-info">
                        <img :src="userInfo.avatarUrl" 
                        class="avatar"/> {{userInfo.name}}
                    </div>
                    <div class="content-text">
                        <div class="title">
                            {{currentCurrencyInfo.tokenSymbol}} GIVEAWAY
                        </div>
                        <div class="center"
                            :style="{
                                fontSize: amountFontSize + 'px'
                            }">
                            <img :src="currentCurrencyInfo.iconPath" class="icon">
                            <span id="preview-before-amount">
                                {{baseFormData.amountValue}}
                            </span>
                        </div>
                        <div class="desc">
                            {{baseFormData.totalCount}} WINNERS TO SHARE
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, defineProps, onMounted, nextTick, watch, reactive, inject, onUnmounted } from "vue";

import customCardCover from '@/view/components/custom-card-cover.vue'

import {getChromeStorage} from "@/uilts/chromeExtension"
import {getUser} from "@/http/publishApi"

let userInfo = ref({});
let reviewCanvasParams = reactive({
    width: 396,
    zoom: 1
});
let timer = ref(null);
let installStatus = inject('installStatus');

defineProps({
    postData: {
        type: Object,
        default: () => {
            return {

            }
        }
    },
    baseFormData: {
        type: Object,
        default: () => {
            return {

            }
        }
    },
    currentCurrencyInfo: {
        type: Object,
        default: () => {
            return {

            }
        }
    },
    amountFontSize: {
        type: Number,
        default: '56'
    }
})

const getUserInfo = (cb) => {
    getChromeStorage('userInfo', (res) => {
        if(res) {
            userInfo.value = res;
        }
        cb && cb(res);
    })
}

const getUserName = (screenName) => {
    getUser({
        params:{
            screenName
        }
    }).then(res => {
        console.log(res);
        if(res.code == 0) {
            userInfo.value.name = res.data.name || ''
        }
    });
}

const calcPreviewCanvasParams = () => {
    nextTick(() => {
        let containerDom = document.querySelector('.card-container');
        let domHeight = containerDom && containerDom.offsetHeight || 500;
        const canvasHeight = 820, canvasWidth = 600;
        if(domHeight < canvasHeight) {
            //比例: 高 / 宽
            let hWRatio = canvasHeight / canvasWidth; 
            //缩小宽度 = 高度 / 比例  
            let width = domHeight / hWRatio;  
            if(width > canvasWidth) {
                width = canvasWidth;
            }
            //缩小比例 
            let zoom = width / canvasWidth;
            if(zoom > 1) {
                zoom = 1;
            }
            reviewCanvasParams.width = width;
            reviewCanvasParams.zoom = zoom;
        } else {
            reviewCanvasParams.width = canvasWidth;
            reviewCanvasParams.zoom = 1;
        }
    });
}

onMounted(() => {
    calcPreviewCanvasParams();
    getUserInfo((res) => {
        if(res) {
            getUserName(res.nickName);
        }
        clearInterval(timer.value);
        timer.value = setInterval(() => {
            installStatus.value = !installStatus.value;
        }, 3000)
    });
    window.addEventListener('resize',function () {
        calcPreviewCanvasParams();
    })
})

onUnmounted(() => {
    clearInterval(timer.value);
})

</script>

<style lang="scss" scoped>
.wrapper {
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    overflow-y: auto;
    display: flex;

    &::-webkit-scrollbar {
        display: none;
    }

    .card-container {
        width: 100%;
        height: 100%;
        background: #ffffff;
        box-sizing: border-box;
        border-radius: 20px;
        display: flex;
        align-items: center;

        .preview-txt {
            width: 238px;
            font-weight: 600;
            font-size: 22px;
            span {
                color: #1D9BF0;
            }
        }

        .head {
            position: absolute;
            z-index: 1100;
            top: 132px;
            left: 17px;
            display: flex;

            .avatar {
                width: 47px;
                height: 47px;
                border-radius: 50%;
                object-fit: cover;
                margin-right: 13px;
            }

            .article-wrapper {
                display: flex;
                .nickname {
                    font-weight: 500;
                }
                .nickname, .name {
                    font-size: 15px;
                }
                .name {
                    color: #566370;
                    margin-left: 7px;
                }

                .desc {
                    margin-top: 8px;
                }
            }
        }
        .left {
            background: url('@/assets/img/img-preview-bg-after.png');
            width: 387px;
            height: 100%;
            background-size: contain;
            background-repeat: no-repeat;
            .tips {
                right: 15px !important;
            }
        }

        .after-cover-wrapper-parent {
            position: absolute;
            z-index: 100;
            top: 223px;
            left: 78px;
        }

        .after-cover-wrapper {
            position:relative;
            width:375px;
            .icon-gif {
                position: absolute;
                left: 50%;
                bottom: 80px;
                transform: translateX(-50%);
                width: 160px;
                height: 190px;
            }

            .content-after {
                width: 350px;
                border-radius: 16px;
                display: block;
                object-fit: contain;
            }
        }


        .content-before {
            background: url('@/assets/img/img-preview-bg-before.png');
            background-size: contain;
            background-repeat: no-repeat;
            height: 100%;
            .card-wrapper {
                width: 491px;
                border: 1px solid #D1D9DD;
                background: #ffffff;
                box-sizing: border-box;
                overflow: hidden;
                position: relative;
                box-sizing: border-box;
                border-radius: 16px;
                left: 73px;
                top: 238px;

                .user-info {
                    position: absolute;
                    left: 8px;
                    top: 8px;
                    z-index: 100;
                    display: flex;
                    align-items: center;
                    font-size: 16px;
                    color: #FFF;
                    width: max-content;

                    img {
                        width: 24px;
                        height: 24px;
                        border: 2px solid #FFF4DB;
                        box-sizing: border-box;
                        border-radius: 50%;
                        margin-right: 10px;
                    }
                }
                .content-text {
                    position: absolute;
                    top: 53px;
                    left: 35px;
                    .title {
                        font-weight: 800;
                        font-size: 16px;
                        color: #ffffff;
                    }
                    .center {
                        padding: 12px 0;
                        box-sizing: border-box;
                        font-weight: 800;
                        font-size: 56px;
                        color: #fff;
                        display: flex;
                        align-items: center;
                        .icon {
                            width: 46px;
                            height: 46px;
                            margin-right: 10px;
                            border: 3px solid #fff;
                            border-radius: 50%;
                        }
                    }
                    .desc {
                        font-weight: 800;
                        font-size: 13px;
                        color: #ffffff;
                    }
                }
                .card-cover {
                    width: 100%;
                    object-fit: contain;
                }

                .bottom-bar {
                    padding: 12px;
                    box-sizing: border-box;
                    .title {
                        color: #566370;
                        font-weight: 400;
                        font-size: 14px;
                        margin-bottom: 6px;
                    }
                    .desc {
                        font-weight: 500;
                        font-size: 15px;
                        color: #101419;
                    }
                }
            }
        }

        .left, .content-before {
            position: relative;
        }
    }

    .select-btn-wrapper {
        padding-right: 16px;
        box-sizing: border-box;

        .title {
            font-size: 14px;
            margin-bottom: 15px;
        }

        .select-btn {
            margin-bottom: 16px;
            box-sizing: border-box;
            cursor: pointer;

            .icon {
                width: 14px;
                height: 14px;
                margin-right: 8px;
            }

            .text {
                font-size: 14px;
                display: flex;
                align-items: center;
            }

            .desc {
                white-space: nowrap;
                font-weight: 500;
                font-size: 13px;
                margin-left: 22px;
            }
        }

        .active {
            color: #000;
        }
    }
}
</style>
