<template>
    <div class="activity">
        <div class="detail" v-html="info.detail"></div>
        <div class="action-btn" v-if="info.onShelf">
            <van-notice-bar
                color="#FF4F50"
                background="#271515"
                text="商品为定制化产品，定制周期较长，且不允许退货"
                z-index="99"
            />

            <div class="info">
                <div class="text1">限量</div>
                <div>{{ info.total }}份</div>
            </div>
            <div class="info">
                <div>剩余 {{ info.stock }}</div>
            </div>

            <div class="flex1"></div>
            <van-button :disabled="startTime" type="primary" round v-if="startTime">{{ startTime }}</van-button>
            <van-button v-else :disabled="isSolded" type="primary" round @click="goBuild">{{
                isSolded ? '已售罄' : '立即铸造'
            }}</van-button>
        </div>
    </div>
</template>

<script>
import { abs } from 'mathjs';
export default {
    data() {
        return {
            info: {},
            timer: null,
            startTime: ''
        };
    },
    computed: {
        isSolded() {
            return !this.info.stock;
        }
    },
    mounted() {
        this.getProduct(true);
    },
    methods: {
        goBuild() {
            this.$router.push('/chooseProduct?name=' + this.info.collectionName + '&activityId=' + this.info.id);
        },
        getProduct(init = false) {
            this.$http.get('/mintActivity/get/' + this.$route.query.id).then(res => {
                this.info = res;
                if (res.scheduleSale && init && res.startTime) {
                    this.getTime();
                }
            });
        },
        pad(n, width, z) {
            z = z || '0';
            n = n + '';
            return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
        },
        getTime() {
            let startTime = this.dayjs(this.info.startTime || '2022-03-01 00:00:00');
            let seconds = this.dayjs(startTime, 'YYYY-MM-DD HH:mm:ss').diff(this.dayjs(), 'second');
            let str = '';
            let d = Math.floor(seconds / 24 / 3600);
            if (abs(d) > 0) {
                str += d + '天 ';
            }

            this.startTime =
                str +
                this.pad(parseInt(seconds / 3600) % 24, 2) +
                ':' +
                this.pad(parseInt(seconds / 60) % 60, 2) +
                ':' +
                this.pad(seconds % 60, 2);
            if (this.timer) {
                clearTimeout(this.timer);
                this.timer = null;
            }
            if (!this.dayjs().isBefore(this.dayjs(startTime, 'YYYY-MM-DD HH:mm:ss'))) {
                if (this.getProduct) {
                    this.getProduct(false);
                }
                this.startTime = '';
            } else {
                this.$toast.clear();
                this.timer = setTimeout(() => {
                    this.getTime();
                }, 1000);
            }
        }
    }
};
</script>

<style lang="less" scoped>
.activity {
    background-color: #181818;
    padding-bottom: 150px;
    .van-image {
        img {
            display: block;
            width: 100%;
            height: auto;
        }
        display: block;
    }
}

.name {
    font-size: 14px;
    color: @text0;
    line-height: 24px;
    margin-bottom: 4px;
}

.info {
    .flex();
    border-radius: 4px;
    overflow: hidden;
    background-color: #27272b;
    margin-right: 10px;

    div {
        font-size: 12px;
        color: #939599;
        line-height: 22px;
        padding: 0 8px 0 6px;

        &.text1 {
            padding: 0 6px 0 8px;
            font-size: 12px;
            color: #000000;
            line-height: 22px;
            background: #939599;
        }
    }
}

.action-btn {
    position: fixed;
    z-index: 20;
    background: #181818;
    padding: 9px 16px;
    .flex();
    left: 0;
    right: 0;
    bottom: 0;
    .bottom(9px);

    /deep/.van-button {
        width: 132px;
        &.van-button--disabled {
            background: #303133;
            color: #939599;
            opacity: 1;
        }
    }
}

/deep/.van-notice-bar {
    position: absolute;
    bottom: 56px;
    left: 0;
    right: 0;
    .van-notice-bar__wrap {
        justify-content: center;
    }
}
.detail {
    // background-color: #181818;
    overflow: hidden;
    img {
        width: 100%;
        display: block;
    }
}
</style>
