<template>
    <router-link
        :to="{
            path: '/' + $route.params.companyId + '/productDetail/' + info.id,
            query: {
                id: info.id
            }
        }"
        class="product"
    >
        <van-image width="120" height="120" :radius="2" :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" v-if="info.category">
                    <span>{{ info.category }}</span>
                </div>
                <div class="sales">
                    <span class="sales-fir">限量</span>
                    <span>{{ info.total }}份</span>
                </div>
            </div>
            <div class="flex1"></div>

            <div class="bottom">
                <div class="miner">
                    <van-image width="18" height="18" radius="18" :src="getImg(info.minterAvatar)" fit="cover" />
                    <span class="van-ellipsis">{{ info.minter }}</span>
                </div>
                <div class="price" v-if="info.salable">
                    <van-icon name="icon-icon_jiage" class-prefix="font_family" />
                    <span> {{ info.price }}</span>
                </div>
                <div v-else class="status">仅展示</div>
            </div>
        </div>

        <div class="sold xianliang" v-if="time && info.scheduleSale">
            <van-icon name="icon-clock" class-prefix="font_family" />
            <span>开售：{{ startTime || time }}</span>
        </div>
        <div class="sold" v-else-if="isSolded">
            <van-icon name="icon-jinzhi" class-prefix="font_family" />
            <span>已售罄</span>
        </div>
        <div class="sold" v-else-if="isSold">
            <van-icon name="icon-info_icon_qianggouzhong" class-prefix="font_family" />
            <span>即将售罄</span>
        </div>
        <div class="sold hot" v-else>
            <van-icon name="icon-info_icon_qianggouzhong" class-prefix="font_family" />
            <span>火爆抢购中…</span>
        </div>
    </router-link>
</template>

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

            return '';
        }
    },
    mounted() {
        if (this.info.startTime) {
            var x = this.dayjs(this.info.startTime, 'YYYY-MM-DD HH:mm:ss');
            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('取消收藏');
                });
            }
        }
    }
};
</script>

<style lang="less" scoped>
.product {
    // width: 255px;
    // margin: 8px;
    .flex();
    align-items: stretch;

    background-color: var(--bglight);
    border-radius: 2px;
    overflow: hidden;
    margin: 8px 16px;
    position: relative;

    .van-image {
        overflow: hidden;
        display: block;
        flex-shrink: 0;
    }
    .content {
        padding: 12px 12px 3px;
        flex-grow: 1;
        .flex-col();
        overflow: hidden;

        .name {
            font-size: @font2;
            font-weight: bold;
            color: var(--text1);
            line-height: 24px;
        }

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

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

            span {
                padding: 0 10px;
                line-height: 20px;
                height: 20px;
                display: inline-block;
                &.sales-fir {
                    background: var(--prim);
                    color: var(--text2);
                }
                background-color: var(--fadePrim);
                color: var(--prim);
            }
        }
        .sales + .sales {
            margin-left: 14px;
        }

        .bottom {
            display: flex;
            margin-top: 14px;
            align-items: center;
            overflow: hidden;
            width: 100%;

            .miner {
                display: flex;
                align-items: center;
                margin-right: 6px;
                overflow: hidden;
                flex-grow: 1;

                span {
                    color: var(--info);
                    font-size: @font1;
                    margin-left: 6px;
                }
            }

            .price {
                flex-shrink: 0;
                font-size: 28px;
                color: var(--text1);
                line-height: 20px;
                font-family: OSP;
                .flex();
                .font_family {
                    font-size: 8px;
                    line-height: 16px;
                    margin-top: 8px;
                }
            }
        }
    }
}

.status {
    font-size: @font2;
    color: var(--text1);
}
.sold {
    background-color: var(--bglight);
    font-size: @font1;
    color: var(--info);
    padding: 0 6px;
    line-height: 24px;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 3;
    .flex();
    .font_family {
        margin-right: 2px;
    }

    &.preSold {
        color: var(--danger);
    }

    &.xianliang {
        color: var(--primlight);
    }

    &.hot {
        color: var(--danger);
    }
}
</style>
