<template>
    <view>
        <van-overlay v-if="overlay" :show="show" :z-index="zIndex" :custom-style="overlayStyle" :duration="duration" @click="onClickOverlay" />
        <view
            v-if="inited"
            :class="'custom-class ' + classes + ' ' + utils.bem('popup', [position, { round, safe: safeAreaInsetBottom, safeTop: safeAreaInsetTop }])"
            :style="
                'z-index: ' +
                zIndex +
                '; -webkit-transition-duration:' +
                currentDuration +
                'ms; transition-duration:' +
                currentDuration +
                'ms; ' +
                (display ? '' : 'display: none;') +
                ';' +
                customStyle
            "
            @transitionend="onTransitionEnd"
        >
            <slot />
            <van-icon
                v-if="closeable"
                :name="closeIcon"
                :class="'close-icon-class van-popup__close-icon van-popup__close-icon--' + closeIconPosition"
                @tap.native="onClickCloseIcon"
            />
        </view>
    </view>
</template>
<script module="utils" lang="wxs" src="@/node_modules/@vant/weapp/dist/wxs/utils.wxs"></script>
<script>
import { VantComponent } from '../common/component';
import { transition } from '../mixins/transition';
export default {
    data() {
        return {
            show: '',
            duration: '',
            inited: '',
            classes: '',
            display: false,
            currentDuration: ''
        };
    },
    classes: ['enter-class', 'enter-active-class', 'enter-to-class', 'leave-class', 'leave-active-class', 'leave-to-class', 'close-icon-class'],
    mixins: [transition(false)],
    props: {
        round: Boolean,
        closeable: Boolean,
        customStyle: String,
        overlayStyle: String,
        transition: {
            type: String
        },
        zIndex: {
            type: Number,
            default: 100
        },
        overlay: {
            type: Boolean,
            default: true
        },
        closeIcon: {
            type: String,
            default: 'cross'
        },
        closeIconPosition: {
            type: String,
            default: 'top-right'
        },
        closeOnClickOverlay: {
            type: Boolean,
            default: true
        },
        position: {
            type: String,
            default: 'center'
        },
        safeAreaInsetBottom: {
            type: Boolean,
            default: true
        },
        safeAreaInsetTop: {
            type: Boolean,
            default: false
        }
    },
    created() {
        this.observeClass();
    },
    methods: {
        onClickCloseIcon() {
            this.$emit('close');
        },

        onClickOverlay() {
            this.$emit('click-overlay');
            if (this.closeOnClickOverlay) {
                this.$emit('close');
            }
        },

        observeClass() {
            const { transition, position } = this;
            const updateData = {
                name: transition || position
            };
            if (transition === 'none') {
                updateData.duration = 0;
            }
            this.setData(updateData);
        },

        onTransitionEnd() {
            console.log('占位：函数 onTransitionEnd 未声明');
        }
    },
    watch: {
        transition: {
            handler: function () {
                const { transition, position } = this;
                const updateData = {
                    name: transition || position
                };
                if (transition === 'none') {
                    updateData.duration = 0;
                }
                this.setData(updateData);
            },

            immediate: true
        },

        position: {
            handler: function () {
                const { transition, position } = this;
                const updateData = {
                    name: transition || position
                };
                if (transition === 'none') {
                    updateData.duration = 0;
                }
                this.setData(updateData);
            },

            immediate: true
        }
    }
};
</script>
<style>
@import './index.css';
</style>
