<template>
    <div class="rank">
        <div class="top">
            <img src="../../assets/stile.png" class="topImg" alt="" />
            <div class="info">
                <div class="title">
                    <div class="text1">月榜</div>
                    <div class="text2">（{{ dayInfo }}）</div>
                </div>

                <div class="persons">
                    <div class="person-box" v-for="(item, index) in topList" :key="index">
                        <div class="person">
                            <van-image width="100%" height="100%" :src="item.avatar" />
                            <i class="person-font font_family icon-a-Page1"></i>
                        </div>
                        <div class="person-text">
                            <div class="text1">
                                <img :src="require(`../../assets/ranking${index + 1}.png`)" alt="" />
                                <span>{{ item.nickname }}</span>
                            </div>
                            <div class="text2">{{ item.num }}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="table">
            <div class="tr header">
                <div class="th">排名</div>
                <div class="th">用户</div>
                <div class="th">藏品数量</div>
            </div>
            <div class="tr rank-item" v-for="(item, index) in rankList" :key="index">
                <div class="td">
                    <span>{{ item.rank }}</span>
                    <van-image radius="26" width="26px" height="26px" :src="item.avatar" />
                </div>
                <div class="td">{{ item.nickname }}</div>
                <div class="td">{{ item.num }}</div>
            </div>
        </div>
        <div class="empty-text" v-if="rankList.length === 0">暂无数据</div>

        <div class="mine-person" v-if="userRank">
            <div class="td">
                <span>{{ userRank.rank }}</span>
                <van-image radius="26" width="26px" height="26px" :src="userRank.avatar" />
            </div>
            <div class="td">{{ userRank.nickname }}</div>
            <div class="td">{{ userRank.num }}</div>
        </div>
    </div>
</template>

<script>
import { mapState } from 'vuex';
export default {
    data() {
        return {
            list: [],
            dayInfo: ''
        };
    },
    computed: {
        ...mapState(['userInfo']),
        topList() {
            let list = [...this.list].slice(0, 3);
            if (list.length < 3) {
                for (let i = 0; i <= 3 - list.length; i++) {
                    list.push({
                        avatar: require('../../assets/img_default_photo.png'),
                        nickname: '虚位以待',
                        num: '***'
                    });
                }
            }
            return list;
        },
        rankList() {
            return [...this.list].filter((item, index) => {
                return index >= 3;
            });
        },
        userRank() {
            if (this.isLogin) {
                let info = [...this.list].find(item => {
                    return item.userId == this.userInfo.id;
                });
                return {
                    avatar: this.userInfo.avatar,
                    nickname: this.userInfo.nickname,
                    rank: info ? info.rank : '未上榜',
                    num: info ? info.num : 0
                };
            } else {
                return null;
            }
        }
    },
    mounted() {
        this.$http.post('/asset/ranking').then(res => {
            this.list = res;
        });
        this.dayInfo = this.dayjs().add(-1, 'month').format('YYYY.MM.DD') + '~' + this.dayjs().format('YYYY.MM.DD');
    }
};
</script>

<style lang="less" scoped>
.rank {
    background-color: #f6f9ff;
}
.top {
    background: linear-gradient(to bottom, #6c35f0, #be65f9);
    padding-top: 116px;
    position: relative;
}
.topImg {
    width: 100%;
    display: block;
    height: auto;
}
@colorList: #ffce51, #d2d2d2, #ebb476;

.info {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    .title {
        .flex();
        padding: 5px 17px;
        background: rgba(255, 255, 255, 0.1);
        border-radius: 18px;
        position: absolute;
        top: 16px;
        left: 50%;
        transform: translateX(-50%);
        white-space: nowrap;
        .text1 {
            font-size: 15px;
            color: #ffffff;
            opacity: 1;
        }

        .text2 {
            font-size: 12px;
            color: #ffffff;
        }
    }
    .persons {
        .flex();
        padding-top: 84px;
    }
    .person-box {
        width: 30%;
        &:first-child {
            order: 2;
            width: 40%;
            transform: translateY(-20px);
            .person {
                width: 55px;
                height: 55px;
            }
        }

        &:nth-child(2) {
            order: 1;
        }

        &:nth-child(3) {
            order: 3;
        }
        .flex-col();
        align-items: center;
        each(@colorList,{
            &:nth-child(@{index}){
                .person {
                    border: 2px solid @value;
                    .person-font {
                        color: @value;
                    }
                }
        }
    });
    }
    .person {
        width: 45px;
        height: 45px;
        position: relative;
        border-radius: 10px;
        .person-font {
            font-size: 16px;
            position: absolute;
            top: -14px;
            left: 50%;
            transform: translateX(-50%);
            z-index: 1;
        }
        .van-image {
            z-index: 0;
            border-radius: 10px;
            overflow: hidden;
        }
    }

    .person-text {
        .flex-col();
        align-items: center;
        margin-top: 6.5vw;
        .text1 {
            .flex();
            img {
                width: 18px;
                height: 23px;
                display: block;
            }
            span {
                font-size: 14px;
                line-height: 14px;
                color: #ffffff;
                margin-left: 5px;
                max-width: 18vw;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }
        }

        .text2 {
            font-size: 14px;
            line-height: 14px;
            color: #ffce51;
        }
    }
}

.table {
    transform: translateY(-20px);
    background-color: #fff;
    border-radius: 18px 18px 0px 0px;
    overflow: hidden;
    .tr {
        height: 55px;
        .flex();
        box-shadow: 0px 1px 0px rgba(229, 229, 229, 0.5);
        &.header {
            background: #7335f0;
        }

        .th {
            width: 33%;
            font-size: 14px;
            line-height: 24px;
            color: #ffffff;
            text-align: center;
        }

        .td {
            font-size: 14px;
            line-height: 14px;
            color: #26273c;
            width: 33%;
            text-align: center;

            &:first-child {
                .flex();
                justify-content: center;
                span {
                    font-size: 12px;
                    margin-right: 18px;
                }
            }

            &:nth-child(3) {
                color: #f7931a;
            }
        }
    }
}

.empty-text {
    text-align: center;
    color: @text3;
    padding: 10px;
    font-size: 12px;
}

.mine-person {
    .bottom();
    height: 55px;
    .flex();
    background: #fff;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 20;

    .td {
        font-size: 14px;
        line-height: 14px;
        color: #26273c;
        width: 33%;
        text-align: center;

        &:first-child {
            .flex();
            justify-content: center;
            span {
                font-size: 12px;
                margin-right: 18px;
            }
        }

        &:nth-child(3) {
            color: #f7931a;
        }
    }
}
</style>
