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

        <div class="content">
            <div class="name van-ellipsis">
                {{ info.name }}
            </div>
            <div class="text van-ellipsis">
                {{ info.name }}
            </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: 255px;
    // margin: 8px;
    background-color: @bg2;
    display: inline-block;
    border-radius: 30px;
    overflow: hidden;

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

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

        .text {
            font-size: @font1;
            color: @text3;
            line-height: 20px;
        }
    }
}
</style>
