<template>
    <div class="com-wrapper" ref="listWrapper"  @scroll="listScroll">
        <!-- <div class="com-nav-bar">
            <img
                :src="require('@/assets/svg/icon-bar-arrow-left.svg')"
                class="icon"
                @click="back"
            />
            Transactions
        </div> -->

            <template v-if="!dataList.length">
                <img class="icon-empty" :src="require('@/assets/svg/icon-empty-list.svg')" />
            </template>
        <div class="list-wrapper" ref="listContent">
            <div>
                <div class="cell" v-for="(item, index) in dataList" :key="index">
                    <red-dot class="red-dots"
                        v-if="item.unReadMsgCount > 0 && isReadMsg"></red-dot> 
                    <div class="img-wrapper">
                        <!-- 收入- 任务红包领取 -->
                        <template v-if="item.bizType == 1">
                            <img class="icon-avatar" :src="item.bizData.avatarUrl" />
                            <img class="icon-give" :src="
                                require('@/assets/svg/icon-giveaways.svg')
                            " />
                        </template>
                        <!-- 收入- 任务红包结余退款， -->
                        <template v-else-if="item.bizType == 2">
                            <img style="margin-left:-4px" :src="
                                require('@/assets/svg/icon-give-refund-list.svg')
                            " />
                        </template>
                        <!-- 收入 - 提现 -- 失败退款 -->
                        <template v-else-if="item.bizType == 3">
                            <img style="margin-left:-4px" :src="
                                require('@/assets/svg/icon-list-withdraw.svg')
                            " />
                        </template>
                        <!-- 收入-充值 -->
                        <template v-else-if="item.bizType == 4">
                            <img style="margin-left:-4px" :src="
                                require('@/assets/svg/icon-list-top-up.svg')
                            " />
                        </template>
                        <!-- 支出 - 提现 -->
                        <template v-else-if="item.bizType == -1">
                            <img style="margin-left:-4px" :src="
                                require('@/assets/svg/icon-list-withdraw.svg')
                            " />
                        </template>
                        <!-- 支出-任务红包余额支付 -->
                        <template v-else-if="item.bizType == -2">
                            <img style="margin-left:-4px" :src="
                                        require('@/assets/svg/icon-big-give.svg')
                            " />
                        </template>
                    </div>
                    <div class="info-wrapper">
                        <div class="left">
                            <div class="nickname">
                                <template v-if="item.bizType == 1">
                                    Giveaways ({{ item.bizData.nickName }})
                                </template>
                                <template v-else-if="item.bizType == 2">
                                    Giveaways Refund
                                </template>
                                <template v-else-if="item.bizType == 3">
                                    Withdrawal Refund
                                </template>
                                <template v-else-if="item.bizType == 4">
                                    Deposit
                                </template>
                                <template v-else-if="item.bizType == -1">
                                    Withdrawal
                                </template>
                                <template v-else-if="item.bizType == -2">
                                    Giveaways
                                </template>
                            </div>
                            <div class="time">{{ moment(item.createTimestamp).format('MM-DD HH:mm:ss') }}</div>
                        </div>
                        <div class="right">
                            <div class="msg">
                                <!-- 支出--提现 -->
                                <template v-if="item.bizType == -1">
                                    <!-- 提现状态（0：已申请，1：支付中，2：提现成功，3：提现失败） -->
                                    <template v-if="item.bizData.withdrawStatus == 0 || item.bizData.withdrawStatus == 1">
                                        <div>
                                            <div class="balance">
                                                <span class="amount">
                                                    <a-tooltip :title="'-'+item.trxAmountValue">
                                                        -{{ getBit(item.trxAmountValue) || 0 }}
                                                    </a-tooltip>
                                                </span>
                                                <span class="name">{{ item.trxAmountCurrencyInfo.tokenSymbol }}</span>
                                                <img :src="item.trxAmountCurrencyInfo.iconPath" alt="">
                                            </div>
                                            <div class="desc">
                                                in progress
                                            </div>
                                        </div>
                                    </template>
                                    <template v-else-if="item.bizData.withdrawStatus == 2">
                                        <div class="balance">
                                            <span class="amount">
                                                <a-tooltip :title="'-'+item.trxAmountValue">
                                                    -{{ getBit(item.trxAmountValue) || 0 }}
                                                </a-tooltip>
                                            </span>
                                            <span class="name">{{ item.trxAmountCurrencyInfo.tokenSymbol }}</span>
                                            <img :src="item.trxAmountCurrencyInfo.iconPath" alt="">
                                        </div>
                                    </template>
                                    <template v-else-if="item.bizData.withdrawStatus == 3">
                                        <div>
                                            <div class="balance">
                                                <span class="amount"> 
                                                    <a-tooltip :title="'-'+item.trxAmountValue">
                                                        -{{ getBit(item.trxAmountValue) || 0 }}
                                                    </a-tooltip>
                                                </span>
                                                <span class="name">{{ item.trxAmountCurrencyInfo.tokenSymbol }}</span>
                                                <img :src="item.trxAmountCurrencyInfo.iconPath" alt="">
                                            </div>
                                            <div class="desc">
                                                Withdrawal failed
                                            </div>
                                        </div>
                                    </template>
                                    <template v-else>
                                        <span class="amount">
                                            <a-tooltip :title="'-'+item.trxAmountValue">
                                                -{{ getBit(item.trxAmountValue) || 0 }}
                                            </a-tooltip>
                                        </span>
                                        <span class="name">{{ item.trxAmountCurrencyInfo.tokenSymbol }}</span>
                                        <img :src="item.trxAmountCurrencyInfo.iconPath" alt="">
                                    </template>
                                </template>
                                <!-- 收入 -->
                                <template v-else>
                                    <span class="amount">
                                        <template v-if="item.bizType == -2">-</template>
                                        <a-tooltip :title="item.bizType == -2 ? '-' + item.trxAmountValue : item.trxAmountValue">
                                            {{ getBit(item.trxAmountValue) || 0 }}
                                        </a-tooltip>
                                    </span>
                                    <span class="name">{{ item.trxAmountCurrencyInfo.tokenSymbol }}</span>
                                    <img :src="item.trxAmountCurrencyInfo.iconPath" alt="">
                                </template>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
/* eslint-disable */
import { onMounted, ref, defineProps, defineEmits, onBeforeUnmount } from "vue";

import redDot from "@/view/components/red-dot.vue";

import { transactionsList } from "@/http/account";
import { readAllMsgByType, getAllMessageInfo } from "@/http/messageApi"
import { setBadgeInfo, hideBadge } from "@/logic/background/twitter";
import { getBit } from "@/uilts/help";

var moment = require('moment');

let dataList = ref([]);
let listWrapper = ref(null);
let listContent = ref(null);

let listReqParams = {
    params: {
        pageNum: 1,
        pageSize: 10,
    },
    loadMore: false,
};

let isReadMsg = ref(true);

const getTransactionsList = () => {
    transactionsList({
        params: listReqParams.params,
    }).then((res) => {
        console.log(res);
        if (res.code == 0) {
            let resData = res.data;
            if (resData.length) {
                for (let i = 0; i < resData.length; i++) {
                    resData[i]["bizData"] = JSON.parse(resData[i]["bizData"]);
                }

                if (listReqParams.params.pageNum < 2) {
                    dataList.value = resData;
                } else {
                    let data = dataList.value;
                    data = data.concat(resData);
                    dataList.value = data;
                }
                listReqParams.loadMore = false;
            }
        }
    });
};

onMounted(() => {
    chrome.runtime.connect({ name: "popup_transactions" });
    readAllMsgByType({
        params: {
            msgType: 1
        }
    }).then(res => {
        setMessageCount();
    });
    setTimeout(() => {
        isReadMsg.value = false;
        readAllMsgByType({
            params: {
                msgType: 2 // 1:任务红包 2:钱包明细
            }
        }).then(res => {
            setMessageCount();
        })
    }, 2000)
    getTransactionsList();
});

const setMessageCount = () => {
    getAllMessageInfo({params: {
    }}).then(res => {
        if(res.code == 0) {
            let {unReadCountTotal = 0 } = res.data;
            if(unReadCountTotal > 0) {
                let text = unReadCountTotal > 99 ? '99+' : text+'';
                setBadgeInfo({data: {text}});
            } else {
                hideBadge();
            }
        }
    });
}


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

const back = () => {
    emits("back", {});
};

const listScroll = (e) => {
    let wrapperHeight = listWrapper.value.offsetHeight;
    let listContentHeight = listContent.value.offsetHeight;
    let scrollTop = e.target.scrollTop || 0;
    if (
        listReqParams.loadMore === false &&
        wrapperHeight + scrollTop >= listContentHeight - 30
    ) {
        listReqParams.loadMore = true;
        listReqParams.params.pageNum++;
        getTransactionsList();
    }
};
</script>

<style lang="scss" scoped>
.com-wrapper {
    height: 100%;
    position: relative;
    overflow-y: auto;

    .icon-empty {
        position: absolute;
        left: 50%;
        top: 20%;
        transform: translateX(-50%);
    }

    .com-nav-bar {
        padding: 14px;
        box-sizing: border-box;
        display: flex;
        align-items: center;
        font-weight: 500;
        font-size: 13px;

        .icon {
            width: 16px;
            margin-right: 6px;
            cursor: pointer;
        }
    }

    .list-wrapper {
        width: 100%;
        // height: calc(100% - 51px);
        // height: 100%;

        .cell {
            display: flex;
            justify-content: space-between;
            align-items: center;
            min-height: 66px;
            box-sizing: border-box;
            padding-left: 20px;
            position: relative;

            .red-dots {
                position: absolute; 
                right: 4px; 
                top: 4px;
            }

            .img-wrapper {
                position: relative;
                margin-right: 16px;
                box-sizing: border-box;

                .icon-avatar {
                    width: 34px;
                    height: 34px;
                    border-radius: 50%;
                }

                .icon-give {
                    position: absolute;
                    right: -4px;
                    bottom: 2px;
                }
            }

            .info-wrapper {
                flex: 1;
                height: 100%;
                display: flex;
                justify-content: space-between;
                align-items: center;
                border-bottom: 1px solid #d1d1d1;
                box-sizing: border-box;
                padding: 10px 12px 10px 0;

                .left {
                    .nickname {
                        font-weight: 500;
                        font-size: 14px;
                        margin-bottom: 5px;
                        word-break: break-all;
                        max-width: 120px;
                    }

                    .time {
                        font-size: 12px;
                        color: #b6b6b6;
                    }
                }

                .right {
                    display: flex;
                    align-items: center;
                    cursor: pointer;

                    .msg {
                        font-weight: 500;
                        font-size: 14px;
                        display: flex;
                        align-items: center;

                        .amount {
                            max-width: 110px;
                            display: inline-block;
                            word-break: break-all;
                            text-align: right;
                        }

                        .name {
                            margin-left: 3px;
                            color: #E29A2E;
                        }

                        img {
                            width: 14px;
                            height: 14px;
                            margin-left: 4px;
                        }

                        .balance {
                            text-align: right;
                            margin-bottom: 4px;
                            display: flex;
                            align-items: center;
                        }

                        .desc {
                            text-align: right;
                            font-weight: 400;
                            font-size: 12px;
                            color: #797979;
                        }
                    }

                    .icon {
                        width: 18px;
                        height: 24px;
                    }
                }
            }
        }
    }
}
</style>
