<template>
    <router-link
        :to="{
            path: '/creatorDetail',
            query: {
                id: info.id
            }
        }"
        class="info"
    >
        <van-image
            :width="size === 'large' ? 88 : 70"
            :height="size === 'large' ? 88 : 70"
            :src="getImg(info.avatar)"
            fit="cover"
            radius="100"
        />

        <img v-if="rank" class="NOImg" :src="NOInfo.img1" alt="" />

        <div class="content">
            <div class="text1 van-ellipsis">{{ info.nickname }}</div>
            <div class="text2 van-ellipsis--l2">
                {{ info.intro }}
            </div>
            <div class="flex1"></div>

            <div class="text3">
                <div class="sale" v-if="rank" :style="{ color: NOInfo.color }">
                    <img class="icon" :src="NOInfo.img2" alt="" />
                    <span>已售{{ info.sales }}</span>
                    <i class="font_family icon-a-icon-dianzan2"></i>
                </div>
                <div class="text4" v-else>
                    <span>已售</span>
                    <span>{{ info.sales }}</span>
                </div>
                <van-button
                    v-if="!isFollow"
                    @click.prevent="follow"
                    :class="{ follow: info.follow }"
                    plain
                    type="primary"
                    size="mini"
                    round
                >
                    {{ info.follow ? '已关注' : '关注' }}
                </van-button>
            </div>
        </div>
    </router-link>
</template>

<script>
export default {
    props: {
        rank: {
            type: Number,
            default: 0
        },
        info: {
            type: Object,
            default: () => {
                return {};
            }
        },
        isFollow: {
            type: Boolean,
            default: false
        },
        size: {
            type: String,
            default: 'normal'
        }
    },
    computed: {
        NOInfo() {
            if (this.rank) {
                const colors = ['#FF8E12', '#C37BFF', '#3ACEFF'];
                return {
                    img1: require(`../../assets/NO${this.rank}.png`),
                    img2: require(`../../assets/svgs/NOicon${this.rank}.svg`),
                    color: colors[this.rank - 1]
                };
            }
            return {};
        }
    },
    methods: {
        follow() {
            this.checkLogin().then(() => {
                if (!this.info.follow) {
                    this.$http.get(`/user/${this.info.id}/follow`).then(res => {
                        this.$emit('update:info', {
                            ...this.info,
                            follow: true,
                            followers: this.info.followers + 1
                        });
                        this.$toast.success('关注成功');
                    });
                } else {
                    this.$http.get(`/user/${this.info.id}/unfollow`).then(() => {
                        this.$emit('update:info', {
                            ...this.info,
                            follow: false,
                            followers: this.info.followers - 1
                        });
                        this.$toast.success('取消关注');
                    });
                }
            });
        }
    }
};
</script>

<style lang="less" scoped>
.info {
    display: flex;
    padding: 24px 16px;
    position: relative;

    .content {
        flex-grow: 1;
        padding-left: 12px;
        display: flex;
        flex-direction: column;

        .text1 {
            font-size: 16px;
            font-weight: bold;
            color: #ffffff;
            line-height: 24px;
        }

        .text2 {
            font-size: 14px;
            color: #939599;
            line-height: 20px;
            margin: 6px 0;
        }

        .text3 {
            display: flex;
            justify-content: space-between;
            align-items: center;

            .sale {
                display: flex;
                align-items: center;
                font-size: 14px;
                line-height: 24px;

                .font_family {
                    font-size: 18px;
                    margin-left: 2px;
                }

                .icon {
                    display: block;
                    width: 18px;
                }

                span {
                    transform: translateX(3px);
                }
            }

            .van-button--mini {
                min-width: 70px;
                border: solid 0px transparent;
                padding: 1px;
                background-image: linear-gradient(@bg, @bg), linear-gradient(135deg, #fdfb60, #ff8f3e);
                background-origin: border-box;
                box-sizing: border-box;
                background-clip: content-box, border-box;

                &.follow {
                    background-image: linear-gradient(@bg, @bg), linear-gradient(135deg, #939599, #939599);
                    color: #939599;
                }
            }

            .text4 {
                span {
                    font-size: 14px;
                    color: #fff;
                    &:first-child {
                        color: #939599;
                        margin-right: 2px;
                    }
                }
            }
        }
    }

    .van-image {
        flex-shrink: 0;
    }

    .NOImg {
        position: absolute;
        width: 98px;
        display: block;
        left: 10px;
        top: 14px;
    }
}

.info + .info {
    position: relative;
    &::before {
        content: '';
        position: absolute;
        left: 8px;
        right: 0;
        top: 0;
        height: 1px;
        background-color: #202122;
    }
}
</style>
