<template>
    <div class="content" v-show-log="state.log_invite_list_show">
        <div class="error" v-if="state.invited_list.length == 0">
            Invite people to hunt treasure with you!
        </div>
        <div class="list" v-else @scroll="handleScroll($event)">
            <div class="item" v-for="item in state.invited_list">
                <div class="left">
                    <img :src="item.userInfo.avatarUrl" alt="" @click="clickItem(item)" />
                </div>
                <div class="right">
                    <div>{{ item.userInfo.nickName }}</div>
                    <div>{{ getTime(item.timestamp) }}</div>
                </div>
            </div>
        </div>
        <div class="footer">
            <v-btn :txt="state.open_btn.txt" :font-size="'17px'" class="btn" :icon="false" :loading="state.btn_loading"
                :disabled="state.open_btn.disabled" v-click-log="state.log_invite_btn_click" @onClick="clickBtn"
                font-weight="600"></v-btn>
        </div>
    </div>
</template>
<script setup>
import VBtn from '@/view/iframe/treasure-hunt/components/btn.vue'
import { inviteList, inviteListRefresh } from '@/http/treasure'
import { inject, onMounted } from 'vue'
import Report from "@/log-center/log"

var moment = require('moment')
let state = inject('state')
state.invited_list = []
let page_num = 1
let page_size = 10
let list_end = false

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.inviteCount,
    postId: state.postId
}

state.log_invite_list_show = {
    businessType: Report.businessType.pageView,
    pageSource: Report.pageSource.beenInvitedPage,
    redPacketType: Report.redPacketType.treasure,
    shareLinkId: state.invite_code,
    myShareLinkId: state.detail.inviteCopyUrl,
    currentInvitedNum: state.inviteCount,
    postId: state.postId
}

onMounted(() => {
    state.btn_loading = false
    list()
})

const clickItem = (item) => {
    window.open(`https://twitter.com/${item.userInfo.nickName}`)
}

function handleScroll(e) {
    if (list_end) {
        return
    }
    e = e.target
    if ((e.clientHeight + e.scrollTop) / e.scrollHeight > .8) {
        list_end = true
        inviteListScroll()
    }
}

const list = () => {
    inviteListScroll()
}

// 刷新时调用
state.inviteListRefresh = () => {
    let last_timestamp = 0
    if (state.invited_list.length > 0) {
        last_timestamp = state.invited_list[0].last_timestamp
    }

    inviteListRefresh({
        params: {
            postId: state.postId,
            lastTimestamp: last_timestamp,
        }
    }).then((res) => {
        if (res.code == 0) {
            handleCommon(res.data)
        }
    })
}

const handleCommon = (data) => {
    state.inviteCount = data.inviteCount
    if (state.inviteCount > 0) {
        state.tabs[1].txt = `invited(${state.inviteCount})`
    }
    if (data.inviteUsers.length > 0) {
        data.inviteUsers.forEach(item => {
            if (state.invited_list.filter((item2) => { return item2.userInfo.uid == item.userInfo.uid }).length == 0) {
                state.invited_list.push(item)
            }
        })

        state.invited_list.sort((a, b) => {
            return a.lastTimestamp - b.lastTimestamp
        })

        list_end = false
    } else {
        list_end = false
    }
}

// 滚动和初始化
let inviteListScroll = () => {
    // state.invited_list
    let last_timestamp = 0
    let len = state.invited_list.length
    if (len > 0) {
        last_timestamp = state.invited_list[len - 1].lastTimestamp
    }

    inviteList({
        params: {
            inviteCode: state.invite_code,
            postId: state.postId,
            lastTimestamp: last_timestamp,
            pageSize: page_size
        }
    }).then((res) => {
        if (res.code == 0) {

            handleCommon(res.data)
        }
    })
}

const getTime = (timestamp) => {
    let _d1 = moment(new Date().getTime())
    let _d2 = moment(timestamp)
    const plural = (n, s) => {
        let _str = `${n} ${s} ago`
        if (n > 1) {
            _str = `${n} ${s}s ago`
        }
        return _str
    }
    let _d = moment.duration(_d1.diff(_d2)).days()
    if (_d) {
        return plural(_d, 'day')
    }
    let _h = moment.duration(_d1.diff(_d2)).hours()
    if (_h) {
        return plural(_h, 'hour')
    }
    let _m = moment.duration(_d1.diff(_d2)).minutes()
    if (_m) {
        return plural(_m, 'min')
    }
    let _s = moment.duration(_d1.diff(_d2)).seconds()
    return plural(_s, 'sec')
}

async function clickBtn() {
    let _userInfo = await state.checkIsLogin()
    if (!_userInfo) {
        return
    }
    state.btn_loading = true
    state.treasureOpen()
}

</script>
<style lang="scss" scoped>
.content {
    position: relative;
    height: 292px;

    .footer {
        background: #fff;
        padding: 10px 16px 25px 16px;
    }

    .error {
        height: 204px;
        color: #BABABA;
        background-color: #fff;
        font-weight: 500;
        font-size: 15px;
        line-height: 204px;
        text-align: center;

    }

    .list {
        background: #fff;
        height: 204px;
        overflow-y: auto;

        .item {
            height: 60px;
            display: flex;
            align-items: center;

            .left {
                width: 58px;
                text-align: center;

                img {
                    border-radius: 50px;
                    width: 30px;
                    height: 30px;
                }
            }

            .right {
                flex: 1;
                border-bottom: 1px solid #D9D9D9;
                display: flex;
                align-items: center;
                height: 100%;
                justify-content: space-between;

                div:nth-child(1) {
                    color: #000000;
                    font-weight: 500;
                    font-size: 15px;

                }

                div:nth-child(2) {
                    color: #A6A6A6;
                    font-weight: 400;
                    font-size: 12px;
                    margin-right: 17px;

                }
            }
        }
    }
}
</style>
