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

        <div class="content">
            <div class="name van-ellipsis">{{ info.name }}</div>
            <div class="sales-list">
                <div class="sales">
                    <span class="sales-fir">限量</span>
                    <span>{{ info.total }}份</span>
                </div>
                <div class="sales" v-if="info.category">
                    <span>{{ info.category }}</span>
                </div>
            </div>

            <div class="bottom">
                <div class="miner">
                    <van-image width="18" height="18" radius="18" :src="getImg(info.minterAvatar)" fit="cover" />
                    <span>{{ info.minter }}</span>
                </div>
                <div class="flex1"></div>
                <div class="price" v-if="info.salable">
                    <img src="@assets/icon_jiage.png" alt="" />
                    <span> {{ info.price }}</span>
                </div>
                <div v-else class="status">仅展示</div>
            </div>
        </div>

        <div class="sold xianliang" v-if="time">
            <img src="@assets/shizhong.png" alt="" />
            <span>即将开售:{{ startTime || time }}</span>
        </div>

        <div class="sold" v-if="isSold">已售馨</div>

        <img class="bg" src="@assets/png-diwen.png" alt="" />
        <!-- <img class="top-bg" v-if="time" src="@assets/collecbg.png" alt="" /> -->
    </router-link>
</template>

<script>
import product from '../../mixins/product';
import dayjs from 'dayjs';
import { mapState } from 'vuex';
var isSameOrBefore = require('dayjs/plugin/isSameOrBefore');
dayjs.extend(isSameOrBefore);
var isSameOrAfter = require('dayjs/plugin/isSameOrAfter');
dayjs.extend(isSameOrAfter);
var duration = require('dayjs/plugin/duration');
dayjs.extend(duration);
export default {
    mixins: [product],
    props: {
        info: {
            type: Object,
            default: () => {
                return {};
            }
        }
    },
    computed: {
        ...mapState(['countdown']),
        time() {
            if (this.info.startTime) {
                if (dayjs().isSameOrBefore(this.info.startTime)) {
                    return dayjs(this.info.startTime).format('MM月DD日 HH:mm');
                }
            }

            return '';
        }
    },
    watch: {
        countdown() {
            console.log('3736762');
            this.updateTime();
        }
    },
    mounted() {
        if (this.countdown || this.countdown === 0) {
            this.updateTime();
        }
    },
    methods: {
        updateTime() {
            if (this.info.startTime) {
                var x = dayjs(this.info.startTime);
                var y = dayjs().add(this.countdown, 'hour');
                console.log(x.isBefore(y));
                if (x.isBefore(y)) {
                    this.getTime(this.info.startTime);
                } else {
                    clearTimeout(this.timer);
                    this.timer = null;
                    this.startTime = '';
                }
            }
        },
        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(100vw - 32px);
    margin: 8px 16px;
    position: relative;
    background-color: @bg2;
    display: inline-block;
    border-radius: 30px;
    overflow: hidden;

    .bg {
        position: absolute;
        width: 100px;
        height: 182px;
        right: 0;
        bottom: 0;
        z-index: 0;
    }

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

    .van-image {
        display: block;
        position: relative;
        z-index: 2;
    }
    .content {
        padding: 10px 16px 18px;
        .name {
            font-size: @font2;
            font-weight: bold;
            color: @text0;
            line-height: 24px;
        }

        .sales-list {
            margin-top: 8px;
            .flex();
        }

        .sales {
            overflow: hidden;
            font-size: @font1;
            border-radius: 4px;

            span {
                padding: 0 10px;
                line-height: 20px;
                height: 20px;
                display: inline-block;
                &.sales-fir {
                    background: @prim;
                    color: @bg;
                }
                background-color: @bg3;
                color: @prim;
            }
        }
        .sales + .sales {
            margin-left: 14px;
        }

        .bottom {
            display: flex;
            margin-top: 14px;
            align-items: center;

            .miner {
                display: flex;
                align-items: center;

                span {
                    color: @text3;
                    font-size: @font1;
                    margin-left: 6px;
                }
            }

            .price {
                font-size: @font4;
                color: @text0;
                line-height: 20px;
                font-family: OSP;
                img {
                    width: 8px;
                    display: inline-block;
                }
            }
        }
    }

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

    .top-bg {
        width: 100%;
        display: block;
        position: absolute;
        z-index: 2;
        top: 0;
        left: 0;
    }
}

.status {
    font-size: @font2;
    color: #fff;
}
.sold {
    background-color: @bg2;
    font-size: @font1;
    color: #939599;
    padding: 0 17px;
    border-radius: 13px;
    line-height: 24px;
    position: absolute;
    top: 16px;
    left: 16px;
    z-index: 3;
}
</style>
