import { abs } from 'mathjs';
import { mapState } from 'vuex';
import imgInfo from './imgInfo';
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;sale,desc;sort,desc',
                最HOT收藏品: 'soldOut;likes,desc;sort,desc;createdAt,desc',
                更多藏品: 'soldOut;source,asc;sale,desc;likes,desc'
            },
            startTimetimer: null,
            saleTimetimer: null,
            saleTime: ''
        };
    },
    mixins: [imgInfo],
    computed: {
        ...mapState(['usedBuy']),
        bannerList() {
            return [...this.changeImgs(this.banners)].map(item => {
                return this.getImg(item, '', 800);
            });
        },
        isAppointment() {
            if (this.info.startTime) {
                return this.dayjs().isBefore(this.dayjs(this.info.startTime));
            } else {
                return false;
            }
        },
        isSale() {
            if (this.info.saleTime) {
                return this.dayjs().isBefore(this.dayjs(this.info.saleTime));
            } else {
                return false;
            }
        },
        isSold() {
            return this.info && this.info.soldOut && this.info.salable;
        },
        isSolded() {
            return this.info && !this.info.noSoldOut && this.info.salable && this.info.source === 'OFFICIAL';
        },
        assignment() {
            return this.info.assignment || 0;
        }
    },
    methods: {
        changeImgs(list = []) {
            // console.log(list);
            return list.map(item => {
                if (item.type === 'video/mp4') {
                    return item.thumb;
                } else {
                    return item.url;
                }
            });
        },
        isVideo(info = {}) {
            return info.type === 'video/mp4';
        },
        pad(n, width, z) {
            z = z || '0';
            n = n + '';
            return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
        },
        getTime(startTime, key = 'startTime') {
            if (!startTime) {
                return;
            }
            if (key == 'startTime' && !this.isAppointment) {
                return;
            }

            if (key == 'saleTime' && !this.isSale) {
                return;
            }

            let subTime = parseInt(this.$store.state.netTime / 1000);

            let seconds = this.dayjs(startTime, 'YYYY-MM-DD HH:mm:ss').diff(this.dayjs(), 'second');
            seconds = seconds - subTime;
            let str = '';
            let d = Math.floor(seconds / 24 / 3600);
            if (abs(d) > 0) {
                str += d + '天 ';
            }

            this[key] =
                str +
                this.pad(parseInt(seconds / 3600) % 24, 2) +
                ':' +
                this.pad(parseInt(seconds / 60) % 60, 2) +
                ':' +
                this.pad(seconds % 60, 2);
            if (this[key + 'timer']) {
                clearTimeout(this[key + 'timer']);
                this[key + 'timer'] = null;
            }
            if (!this.dayjs().isBefore(this.dayjs(startTime, 'YYYY-MM-DD HH:mm:ss'))) {
                if (this.getProduct) {
                    this.getProduct();
                }
                this[key] = '';
            } else {
                this[key + 'timer'] = setTimeout(() => {
                    this.getTime(startTime, key);
                }, 1000);
            }
        },
        getCutNum(num = 0) {
            num = Math.floor(num);
            if (num < 10) {
                return '0' + num;
            } else {
                return num;
            }
        },
        getShort(str = '') {
            if (str) {
                str = '...' + str.substr(-8, 8);
            }
            return str;
        },
        clearTime() {
            if (this.timer) {
                clearTimeout(this.timer);
                this.timer = null;
            }
        },
        updateTime() {
            if (this.startTime) {
                this.getTime(this.info.startTime);
            }
        }
    },
    deactivated() {
        this.clearTime();
    },
    activated() {
        this.updateTime();
    },
    beforeUnmount() {
        if (this.timer) {
            clearTimeout(this.timer);
            this.timer = null;
        }
    }
};
