<template>
    <view v-if="isOpen && !isInApp">
        <view class="vt-layer">
            <view
                class="vt-modal"
                :style="{
                    'background-image': 'url(' + data.bg + ')',
                    'background-color': data.bgColor,
                } | imgCompress('background-image')"
            >
                <view class="vt-modal-ctn">
                    <view class="vt-modal-ctn-title c-tac">
                        <view
                            v-if="data.title"
                            class="c-font-medium"
                        >{{ data.title }}</view>
                        <view
                            v-if="data.text && data.text.length"
                            class="vt-modal-ctn-text"
                        >
                            <view
                                v-for="(t, i) in data.text"
                                :key="i"
                                class="vt-font-18 vt-bold"
                            >{{ t }}</view>
                        </view>
                    </view>
                    <view
                        class="vt-gif c-gap-top-large"
                        :class="isStart ? 'start' : ''"
                    >
                        <view
                            class="vt-bg"
                            :style="{
                                'background-image': data.vtBg && 'url(' + data.vtBg + ')',
                            } | imgCompress('background-image')"
                        />
                        <view
                            class="vt-bar"
                            :style="{
                                'background-image': data.vtBar && 'url(' + data.vtBar + ')',
                            } | imgCompress('background-image')"
                        />
                        <view class="vt-icon"/>
                        <view class="vt-mask"/>
                    </view>
                    <view
                        v-if="data.remind"
                        class="c-tac c-gap-top-large"
                        @tap="onTap"
                    >
                        <view
                            v-if="isChk"
                            class="vt-rm-chked c-inline-block"
                        >
                            <c-icon
                                name="check-circle-solid"
                                font-size="18px"
                                class="vt-rm-chked-icon"
                            />
                        </view>
                        <view
                            v-else
                            class="vt-rm c-inline-block"
                        />
                        <text class="c-inline-block c-gap-left-small c-font-medium">{{ data.remind }}</text>
                    </view>
                </view>
                <view
                    v-if="data.btns"
                    class="vt-modal-foot c-tac"
                    @tap="onClose"
                >
                    <view>{{ data.btns.text }}</view>
                </view>
            </view>
        </view>
    </view>
</template>
<script>
/* global getApp */
/**
* @file 复访入口
* @author haoyan
*/
import Icon from '@baidu/med-ui/components/Icon/Icon';
export default {
    config: {
        component: true
    },
    data() {
        return {
            isStart: false,
            isChk: false,
            isOpen: false,
            // 是否在APP内
            isInApp: false
        };
    },
    created() {
        // APP头部兼容
        this.isInApp = getApp().globalData.isInApp;
    },
    props: {
        data: {
            type: Object,
            default: () => {
                return {};
            }
        },
        storeKey: {
            type: String,
            default: 'wz_chat_visit'
        }
    },
    components: {
        'c-icon': Icon
    },
    watch: {
        data(v) {
            if (v && v.title) {
                this.$api.getStorage({
                    key: this.storeKey
                }).then(res => {
                    if (res.data) {
                        // 获取store value, 不再提醒
                        this.isOpen = false;
                    }
                    else {
                        this.isOpen = true;
                        // 开始执行动画

                        setTimeout(() => {
                            this.isStart = true;
                        }, 1000);
                    }
                });
            }
        }
    },
    methods: {
        onTap() {
            this.isChk = !this.isChk;
        },
        onClose() {
            // 设置storage
            if (this.isChk) {
                this.$api.setStorage({
                    key: this.storeKey,
                    data: {
                        t: Date.now()
                    }
                });
            }
            this.isOpen = false;
        }
    }
};
</script>
<style lang="less" scoped>
.vt-layer {
    background-color: rgba(0, 0, 0, .4);
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 10;
    display: flex;
    align-items: center;
}

.vt-modal {
    border-radius: 8px;
    width: 300px;
    background-color: #24c1c1;
    background-size: cover;
    background-repeat: no-repeat;
    position: absolute;
    z-index: 10;
    left: 0;
    right: 0;
    margin: auto;
    color: #fff;

    &-ctn {
        padding: 13px 0 16px 0;

        &-text {
            margin-top: 2px;
        }
    }
}

.vt-gif {
    position: relative;
    width: 131px;
    height: 206px;
    margin-left: auto;
    margin-right: auto;

    &.start {
        .vt-icon {
            transform: scale(1.6, 1.6) translate(0, -2px);
        }

        .vt-mask {
            opacity: .2;
        }

        .vt-bar {
            opacity: 1;
            transform: scale(1.8, 1.8) translate(0, -6px);
            box-shadow: 0px 5px 5px rgba(0, 0, 0, .05);
        }
    }
}

.vt-bg {
    background-image: url('https://med-fe.cdn.bcebos.com/wz/visitNewBg.png');
    background-size: cover;
    background-repeat: no-repeat;
    width: 131px;
    height: 206px;
    border-radius: 12px;
    overflow: hidden;
    margin: 7px auto 0 auto;
    position: relative;
}

.vt-bar {
    background-image: url('./img/bar.png');
    background-size: cover;
    background-repeat: no-repeat;
    position: absolute;
    width: 117px;
    height: 34px;
    top: 76px;
    left: 7px;
    z-index: 5;
    opacity: 1;
    border-radius: 4px;
    transition: all .5s ease .93s;
}

.vt-icon {
    background-image: url('./img/icon.png');
    background-size: cover;
    background-repeat: no-repeat;
    position: absolute;
    width: 17px;
    height: 17px;
    bottom: 3px;
    right: 9px;
    z-index: 5;
    transition: transform .33s;
}

.vt-mask {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 3;
    background-color: rgb(0, 0, 0);
    opacity: 0;
    border-radius: 12px;
    transition: opacity .33s ease .93s;
}

.vt-rm {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    border: 1px solid #fff;
}

.vt-rm-chked {
    width: 14px;
    height: 14px;
    background-color: #fff;
    border-radius: 50%;
    color: #2fa3ae;
    position: relative;

    &-icon {
        position: absolute;
        top: -4px;
        left: -2px;
    }
}

.vt-modal-foot {
    background-color: #fff;
    font-size: 18px;
    height: 49px;
    line-height: 49px;
    color: #00c8c8;
    border-bottom-left-radius: 8px;
    border-bottom-right-radius: 8px;
}

.vt-font-18 {
    font-size: 18px;
    line-height: 28px;
}

.vt-bold {
    font-weight: bold;
}
</style>
