<template>
    <router-link
        :to="{
            path: '/productDetail',
            query: {
                id: info.id
            }
        }"
        class="product"
    >
        <van-image width="100%" height="calc(50vw - 24px)" :src="getImg(changeImgs(info.pic, 600))" fit="cover" />

        <div class="content">
            <div class="name van-ellipsis">
                {{ info.name }}
            </div>
            <div class="price-content" v-if="info.salable">
                <div class="price">
                    <img src="../../assets/jiage_huang.png" alt="" />
                    <span>{{ info.price }}</span>
                </div>
                <div class="flex1"></div>
                <div class="text1">
                    <span>{{ info.sale }}/</span>
                    <span>{{ info.total }}</span>
                </div>
            </div>
            <div class="status" v-else>仅展示</div>
            <div class="text">
                <div class="minter">
                    <van-image width="18" height="18" radius="18" :src="info.minterAvatar" fit="cover" />
                    <span>铸造者</span>
                </div>
                <div class="flex1"></div>
                <like-button :isLike="info.liked" @click="likeProduct">
                    {{ info.likes }}
                </like-button>
            </div>
        </div>
    </router-link>
</template>

<script>
import product from '../../mixins/product';
export default {
    mixins: [product],
    props: {
        info: {
            type: Object,
            default: () => {
                return {};
            }
        }
    },
    methods: {
        likeProduct() {
            if (!this.info.liked) {
                this.$http.get(`/collection/${this.info.id}/like`).then(() => {
                    this.$emit('update:info', {
                        ...this.info,
                        liked: true,
                        likes: this.info.likes + 1
                    });
                    this.$toast.success('收藏成功');
                });
            } else {
                this.$http.get(`/collection/${this.info.id}/unlike`).then(() => {
                    this.$emit('update:info', {
                        ...this.info,
                        liked: false,
                        likes: this.info.likes - 1
                    });
                    this.$toast.success('取消收藏');
                });
            }
        }
    }
};
</script>

<style lang="less" scoped>
.product {
    width: calc(50vw - 24px);
    margin: 8px;
    background-color: @bg2;
    display: inline-block;
    border-radius: 8px;
    overflow: hidden;

    .van-image {
        overflow: hidden;
        display: block;
    }
    .content {
        padding: 6px 10px 8px;

        .name {
            font-size: @font2;
            color: #ffffff;
            line-height: 24px;
        }

        .price {
            font-size: @font4;
            font-family: OSP;
            color: @prim;
            line-height: 18px;
            padding: 12px 0;

            img {
                display: inline-block;
                width: 8px;
            }
        }
        .status {
            line-height: 24px;
            padding: 10px 0 8px;
            color: @text3;
            font-size: @font2;
        }
    }

    .text {
        display: flex;
        justify-content: space-between;
        align-items: center;
        .text1 {
            font-weight: 400;
            color: @text3;
            line-height: 24px;
            span {
                &:last-child {
                    color: #fff;
                    font-size: @font1;
                }
            }
        }
    }
}
.price-content {
    display: flex;
    align-items: center;
    .text1 {
        font-size: @font1;
        color: @text3;
        line-height: 12px;
    }
}

.minter {
    display: flex;
    font-size: @font1;
    color: @text3;
    line-height: 22px;
    align-items: center;
    span {
        margin-left: 4px;
    }
}
</style>
