<template>
    <div class="product" @click="goDetail">
        <van-image width="100%" height="calc(50vw - 24px)" :src="getImg(changeImgs(info.pic, 600))" fit="cover" />

        <div class="number" v-if="info.number">编号：{{ info.number }}</div>
        <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" v-if="info.total !== 1">
                    <span>{{ Math.min(info.sale, info.total) }}/</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 class="van-ellipsis">{{ info.minter }}</span>
                </div>
                <div class="flex1"></div>
                <like-button :isLike="info.liked" @click="likeProduct">
                    {{ info.likes }}
                </like-button>
            </div>
            <div class="sold xianliang" v-if="time && info.salable">
                <img src="@assets/shizhong.png" alt="" />
                <span>{{ startTime || time }}</span>
            </div>
            <div class="sold" v-if="isSolded">已售罄</div>
            <div class="sold" v-else-if="isSold" style="color: #ff4f50">即将售罄</div>
        </div>
    </div>
</template>

<script>
import product from '../../mixins/product';
export default {
    mixins: [product],
    props: {
        info: {
            type: Object,
            default: () => {
                return {};
            }
        }
    },
    computed: {
        time() {
            if (this.info.startTime) {
                if (this.dayjs().isSameOrBefore(this.info.startTime, 'YYYY-MM-DD HH:mm:ss')) {
                    return this.dayjs(this.info.startTime, 'YYYY-MM-DD HH:mm:ss').format('MM月DD日');
                }
            }

            return '';
        }
    },
    mounted() {
        if (this.info.startTime) {
            var x = this.dayjs(this.info.startTime);
            var y = this.dayjs();
            let d = this.dayjs.duration(x.diff(y));
            let day = parseInt(d.asDays());
            if (day <= 0) {
                this.getTime(this.info.startTime);
            }
        }
    },
    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('取消收藏');
                });
            }
        },
        goDetail() {
            if (!this.usedBuy && this.info.source === 'TRANSFER') {
                this.$toast('该通道暂且关闭');
            } else {
                this.$router.push({ path: '/productDetail', query: { collectionId: this.info.id } });
            }
        }
    }
};
</script>

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

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

        .name {
            font-size: @font2;
            color: @text0;
            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;
    margin-right: 10px;
    overflow: hidden;
    .van-image {
        flex-shrink: 0;
    }
    span {
        margin-left: 4px;
    }
}
.sold {
    background-color: @bg2;
    font-size: @font1;
    color: #939599;
    padding: 0 17px;
    border-radius: 13px;
    line-height: 24px;
    position: absolute;
    top: 6px;
    left: 6px;
    z-index: 5;
}

.xianliang {
    position: absolute;
    top: 16px;
    left: 16px;
    font-size: @font1;
    color: @prim;
    display: flex;
    align-items: center;
    // border-radius: 13px !important;
    z-index: 4;
    padding: 0 10px !important;

    img {
        width: 18px;
        height: 18px;
        margin-right: 3px;
    }
}

.number {
    position: absolute;
    top: calc(50vw - 46px);
    left: 0;
    right: 0;
    font-size: 12px;
    color: #939599;
    line-height: 22px;
    background-color: fade(#000, 80%);
    padding: 0 10px;
}
</style>
