<template>
    <router-link
        :to="{
            path: '/' + $route.params.companyId + '/newsDetail',
            query: {
                id: info.id
            }
        }"
        class="news-small"
        :class="[`news-${theme}`, type === 'light' ? 'isLight' : '']"
    >
        <van-image :src="getImg(info.pic)" :radius="12" width="120" height="120" fit="cover" />

        <div class="content">
            <div class="name van-multi-ellipsis--l2">{{ info.title }}</div>
            <div class="flex1"></div>
            <van-button type="primary" round plain size="mini"> 查看</van-button>
        </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: 'dark'
        }
    },
    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>
.news-small {
    width: calc(100vw - 32px);
    margin: 8px 16px;
    position: relative;
    background-color: var(--bglight);
    display: inline-flex;
    border-radius: 12px;
    overflow: hidden;

    .bg {
        position: absolute;
        width: 92px;
        height: 116px;
        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;
        flex-shrink: 0;
    }
    .content {
        padding: 16px 15px 16px 12px;
        .flex-col();
        flex-grow: 1;
        .name {
            font-size: 16px;
            font-weight: bold;
            color: var(--text1);
            line-height: 22px;
        }

        .van-button {
            width: 68px;
            --van-button-plain-background-color: var(--fadePrim);
            --van-button-primary-background-color: var(--prim);
            --van-button-mini-font-size: 14px;
        }

        .flex1 {
            flex-grow: 1;
        }

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

    &.isLight {
        background-color: @bg;
        .content {
            .name {
                color: #000000;
            }
            .bottom {
                .miner {
                    color: #939599;
                }
            }
        }
    }

    &.news-theme4 {
        .content {
            .van-button {
                width: 68px;
                --van-button-plain-background-color: var(--fadePrimlight);
                --van-button-primary-background-color: var(--primlight);
                --van-button-mini-font-size: 14px;
            }
        }
    }
}

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