<template>
    <view class="vacc-coupon-modal">
        <coupon-modal
            v-if="isShow"
            :is-show="isShow"
            :title="title"
            :login="{
                isLogin,
                successToH5,
            }"
            :sub-title="subTitle"
            :modal-storage="modalStorage"
            :expire-date="expireDate"
            :coupons="coupons"
            :button="button"
            @updateFirstLoginStatus="updateFirstLoginStatus"
        />
    </view>
</template>

<script>
import CouponModal from '@components/act/coupon-modal';
import * as Conf from '@utils/conf';
import {request} from '@utils/request';

/**
 * @file 优惠券弹窗
 * @author wangying51
 * @categories 业务组件
 * @tags [component]
 */
export default {
    config: {
        component: true
    },
    components: {
        CouponModal
    },
    options: {
        addGlobalClass: true
    },
    data() {
        return {
            isShow: false,
            isLogin: false,
            successToH5: null,
            // 首次登录标记
            firstLogin: null,
            title: '',
            subTitle: '',
            expireDate: 0,
            coupons: [],
            button: null,
            modalStorage: null,
            query: {}
        };
    },
    props: {
        topic: {
            type: String,
            default: ''
        }
    },
    computed: {
    },
    created() {
        this.getCouponData();
    },
    methods: {
        getCouponData() {
            const h5IsFirstLogin = process.env.MARS_ENV === 'h5' && document.URL.indexOf('&firstLogin=1') > 0;
            const firstLogin = this.firstLogin || h5IsFirstLogin ? 1 : 0;

            // 重置弹窗展示状态
            this.isShow = false;
            request({
                url: Conf.apiHost + '/med/api/physical/coupon_popup?vn=med',
                data: {
                    firstLogin
                },
                isFirstScreen: false,
                isNeedLogin: false
            }).then(res => {
                let data = res && res.data || {};
                let modal = data.modal || null;
                let isLogin = data.isLogin === 1;
                const storageTime = data.storageTime || 24 * 60 * 60 * 1000;

                if (modal) {
                    let {title, subTitle, coupons, expireDate, button} = modal;

                    Object.assign(this, {
                        isLogin,
                        successToH5: process.env.MARS_ENV === 'h5' ? window.location.href + '&firstLogin=1' : '',
                        title,
                        subTitle,
                        expireDate,
                        coupons,
                        button
                    });
                }
                // 设置弹窗缓存
                const modalStorage = this.$api.getStorageSync('vacc-coupon-modal') || {};
                if (isLogin) {
                    modal && (setTimeout(() => {
                        if (this.topic != 'jhb'){
                            this.isShow = true;
                        }
                    }, 100));
                    // 首次登录，老用户弹Toast提示已领取
                    if (data.toast) {
                        this.$api.showToast({title: data.toast, icon: 'none'});
                    }
                    modalStorage.st && modalStorage.st > 0 && (this.modalStorage = {
                        name: 'vacc-coupon-modal',
                        st: null
                    });
                }
                else {
                    // 未登录，storageTime 内不展示弹窗
                    const now = Date.now();

                    if ((!modalStorage.st || (now - modalStorage.st > storageTime))
                        && modal) {
                        if (this.topic != 'jhb'){
                            this.isShow = true;
                        }
                        // 动态更新 modal 缓存过期时间
                        this.modalStorage = {
                            name: 'vacc-coupon-modal',
                            st: now
                        };
                    }
                }


                // H5若首次登录，去掉H5上的firstLogin参数，避免刷新重复弹toast
                h5IsFirstLogin && window.history.replaceState({}, 0, document.URL.replace('&firstLogin=1', ''));
            })
                .catch(() => {});
        },
        updateFirstLoginStatus(e) {
            this.firstLogin = e;
            e === 1 && this.getCouponData();
            /**
             * 登录结束标志，控制页面刷新
             */
            this.$emit('updateFirstLoginStatus', e);
        }
    }
};
</script>
<style lang="less" scoped>
</style>
