import dayjs from 'dayjs';
import { abs } from 'mathjs';
require('dayjs/locale/zh-cn');
dayjs.locale('zh-cn');
var relativeTime = require('dayjs/plugin/relativeTime');
dayjs.extend(relativeTime);
var calendar = require('dayjs/plugin/calendar');
dayjs.extend(calendar);
var duration = require('dayjs/plugin/duration');
dayjs.extend(duration);
export default {
    data() {
        return {
            typeOptions: [
                {
                    label: '精选推荐',
                    value: ''
                },
                {
                    label: '原创系列',
                    value: 'DEFAULT'
                },
                {
                    label: '数字盲盒',
                    value: 'BLIND_BOX'
                },
                {
                    label: '拍卖系列',
                    value: 'AUCTION'
                }
            ],
            sourceOptions: [
                { label: '官方', value: 'OFFICIAL' },
                { label: '用户铸造', value: 'USER' },
                { label: '二手市场', value: 'TRANSFER' }
            ],
            picsTypes: [
                {
                    label: '视频',
                    value: 'video/mp4'
                }
            ],
            startTime: '',
            sortOptions: {
                精选盲盒: 'soldOut;sort,desc',
                本期推荐: 'soldOut;sort,desc',
                最HOT收藏品: 'soldOut;likes,desc;sort,desc;createdAt,desc',
                更多藏品: 'soldOut;source,asc;sale,desc;likes,desc'
            },
            timer: null
        };
    },
    computed: {
        bannerList() {
            return [...this.changeImgs(this.banners)].map(item => {
                return this.getImg(item, '', 800);
            });
        },
        isAppointment() {
            if (this.info.startTime) {
                return dayjs().isBefore(dayjs(this.info.startTime));
            } else {
                return false;
            }
        },
        isSold() {
            return !this.info.stock;
        }
    },
    methods: {
        changeImgs(list = []) {
            // console.log(list);
            return list.map(item => {
                if (item.type === 'video/mp4' || item.type === 'model3d') {
                    return item.thumb;
                } else {
                    return item.url;
                }
            });
        },
        isVideo(info = {}) {
            return info.type === 'video/mp4';
        },
        is3D(info = {}) {
            return info.type === 'model3d';
        },
        getTime(startTime) {
            if (!startTime) {
                return;
            }
            if (!this.isAppointment) {
                return;
            }
            var x = dayjs(startTime);
            var y = dayjs();
            let d = dayjs.duration(x.diff(y));
            let day = parseInt(d.asDays());
            let str = '';
            if (abs(day) > 0) {
                str += day + '天 ';
            }
            this.startTime = str + dayjs.duration(x.diff(y)).format('HH:mm:ss');

            if (this.timer) {
                clearTimeout(this.timer);
                this.timer = null;
            }
            if (!dayjs().isBefore(dayjs(startTime))) {
                if (this.getProduct) {
                    this.getProduct();
                }
                this.startTime = '';
            } else {
                this.timer = setTimeout(() => {
                    this.getTime(startTime);
                }, 1000);
            }
        },
        getShort(str = '') {
            if (str) {
                str = '...' + str.substr(-8, 8);
            }
            return str;
        }
    }
};
