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

        <div class="content">
            <div class="name van-ellipsis">
                {{ info.name }}
            </div>
            <div class="price" v-if="info.salable"><i class="font_family icon-icon_jiage"></i>{{ info.price }}</div>
            <div class="status" v-else>仅展示</div>
            <div class="text">
                <div class="text1" v-if="info.salable">
                    <span>{{ info.sale }}/</span>
                    <span>{{ info.total }}</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: 10px;

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

        .price {
            font-size: 24px;
            font-family: OSP;
            color: @prim;
            line-height: 22px;
            padding: 7px 0;

            i {
                vertical-align: text-bottom;
                font-size: 10px;
            }
        }
        .status {
            line-height: 22px;
            padding: 7px 0;
            color: @text3;
            font-size: @font1;
        }
    }

    .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;
                }
            }
        }
    }
}
</style>
