<template>
<!-- paypal 支付按钮 -->
    <div class="pay-wrapper">
        <slot name="balance"></slot>
        <div class="pay-btn">
            <div class="iframe-pay"
                v-show="currentCurrencyInfo.currencyCode == 'USD'"
                @click="paypalPay">
                <iframe
                    class="iframe-pay"
                    ref="iframe"
                    :src="`${payConfig.paypalHtml}?paypalClientId=${payConfig.paypalClientId}&amount=${props.finalAmountData.finalAmountValue}`"></iframe>
            </div>
            <div class="token-pay" 
                :class="{ disabled: Number(currentCurrencyInfo.balance) < Number(payConfig.amount) }"
                v-if="currentCurrencyInfo.currencyCode != 'USD'"
                @click="tokenPay">
                Pay {{payConfig.amount || 0}} {{currentCurrencyInfo.tokenSymbol}}
            </div>
        </div>
    </div>
</template>

<script setup>
import { onMounted, ref, defineProps, defineEmits, watch } from "vue";

import {payTaskLuckdropWithBalance} from "@/http/publishApi"
import Report from "@/log-center/log"

const props = defineProps({
    finalAmountData: {
        type: Object,
        default: () => {
            return {
                currencyCode: "",
                feeAmountValue: 0,
                finalAmountValue: 0,
                requestAmountValue: 0
            }
        },
    },
    payConfig: {
        type: Object,
        default: () => {
            return {
                paypalClientId: '',
                paypalHtml: 'https://d1mcov78iir8kk.cloudfront.net/website/paypal_1.html'
            }
        }
    },
    currentCurrencyInfo: {
        type: Object, 
        default: () => {

        }
    },
});

let iframe = ref(null);
let payIng = false;

watch(
    () => props.finalAmountData.finalAmountValue,
    (newVal) => {
        iframe.value.contentWindow.postMessage({ 
            actionType: "setAmount", amount: newVal
        },
            "*"
        );
    },
    {
        deep: true
    }
);

const emits = defineEmits(["payPalFinsh"]);

const paypalPay = () => {
    Report.reportLog({
        pageSource: Report.pageSource.previewPage,
        businessType: Report.businessType.buttonClick,
        objectType: Report.objectType.confirmButton
    }, {
        type: 'paypal'
    });  
};

const tokenPay = () => {  
    Report.reportLog({
        pageSource: Report.pageSource.previewPage,
        businessType: Report.businessType.buttonClick,
        objectType: Report.objectType.confirmButton
    }, {
        type: 'confirm'
    });  
    if(payIng) {
        return;
    }
    payIng = true;
    payTaskLuckdropWithBalance({
        params: {
            currencyCode: props.currentCurrencyInfo.currencyCode,
            postId: props.payConfig.postId
        }
    }).then(res => {
        if(res.code == 0) {
            emits("payPalFinsh", {...res.data, payNetwork: 'bsc'});
        }
        payIng = false;
    }).catch(() => {
        payIng = false;
    })
}

onMounted(() => {
    window.addEventListener("message", function (event) {
        if (event.data && event.data.actionType) {
            switch (event.data.actionType) {
                case "payCallBack":
                    console.log(
                        "payCallBack",
                        event.data.orderData,
                        event.data.transaction
                    );
                    emits("payPalFinsh", {...event.data, payNetwork: 'paypal'});
                    break;
            }
        }
    });
});

</script>

<style lang="scss" scoped>
.pay-wrapper {
    width: 100%;
    height: 80px;
    background-color: #fff;
    position: absolute;
    left: 0;
    bottom: 0;
    box-shadow: 0px -1px 0px #ECECEC;
    border-bottom-right-radius: 16px;
    padding: 12px 30px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    border-bottom-left-radius: 16px;
    z-index: 999;

    .pay-msg {
        text-align: right;
        margin-right: 25px;

        .row {
            font-weight: 600;
            font-size: 16px;
            color: #1D9BF0;
            display: flex;
            align-items: center;
            justify-content: end;
            margin-bottom: 6px;
            span {
                display: inline-block;
                color: #000000;
                margin-left: 6px;
            }

            .icon {
                width: 14px;
                margin-left:6px;
            }
            
            .desc {
                margin-right:6px
            }
        }
        .msg {
            font-size: 13px;
            color: #898989;
        }
    }

    .pay-btn {
        height: 48px;

        .token-pay {
            height: 100%;
            background: #1D9BF0;
            border-radius: 10000px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 600;
            font-size: 18px;
            color: #fff;
            cursor: pointer;
            padding: 0 30px;
            &.disabled {
                background: #DEDEDE;
            }
        }

        .iframe-pay {
            width: 100%;
            height: 100%;
        }

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