<template>
    <div>
        <transition name="fade">
            <div class="alert" @touchstart.stop.prevent="" v-show="visible">

            </div>
        </transition>
        <transition name="fade">
            <div class="alert-wrapper" v-show="visible">
                <div class="alert-body">
                    <div class="title" v-if="title">{{title}}</div>
                    <div class="content">{{content}}</div>
                    <div class="button-wrapper">
                        <div class="btn-cancel" v-if="showCancel" @click="cancel">取消</div>
                        <div class="btn-confirm" @click="confirm">{{confirmTxt}}</div>
                    </div>
                </div>
            </div>
        </transition>
    </div>
</template>
<script>
export default {
    mounted() {
        this.visible = true
    },
    data() {
        return {
            title: '提示',
            content: '提示内容提示内容提示内容提示内容',
            showCancel: false,
            visible: false,
            cancelFn: undefined,
            confirmFn: undefined,
            confirmTxt:'确认'
        }
    },
    methods: {
        cancel() {
            if (this.cancelFn) {
                this.cancelFn()
            }
            this.close()
        },
        confirm() {
            if (this.confirmFn) {
                this.confirmFn()
            }
            this.close()
        },
        close() {
            this.visible = false
            this.$el.addEventListener('transitionend', this.destroyView)
        },
        destroyView() {
            this.$el.removeEventListener('transitionend', this.destroyView)
            this.$destroy(true)
            this.$el.parentNode.removeChild(this.$el)
        }
    }
}
</script>
<style lang="less" scoped>
.alert {
    background: rgba(0, 0, 0, 0.5);
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: 500;
}
.alert-wrapper {
    width: 100%;
    height: 100%;
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 550;
}
.alert-body {
    width: 5.4rem;
    background: #fff;
    border-radius: 0.2rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    .title {
        color: #000;
        font-size: 0.32rem;
        font-weight: bold;
        color: rgba(3, 3, 3, 1);
        line-height: 0.44rem;
        margin-top: 0.3rem;
    }
    .content {
        margin: 0.4rem 0.7rem;
        text-align: center;
        font-size: 0.32rem;
        font-weight: bold;
        color: rgba(0, 0, 0, 1);
        line-height: 0.4rem;
    }
    .button-wrapper {
        height: 0.88rem;
        display: flex;
        width: 100%;
        border-top: 1px solid #ebeef0;
        .btn-cancel {
            color: #666;
            font-size: 0.32rem;
            font-weight: normal;
            flex-grow: 1;
            flex-basis: 0;
            height: 100%;
            border-right: 1px solid #ebeef0;
            display: flex;
            align-items: center;
            justify-content: center;
            border-bottom-left-radius: 0.2rem;
            &:active {
                background: shade(#fff, 20%);
            }
        }
        .btn-confirm {
            color: #15a8aa;
            font-size: 0.32rem;
            font-weight: bold;
            flex-grow: 1;
            flex-basis: 0;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            border-bottom-right-radius: 0.2rem;
            &:active {
                background: shade(#fff, 20%);
            }
        }
    }
}
.fade-enter,
.fade-leave-active {
    opacity: 0;
}

.fade-enter-active,
.fade-leave-active {
    transition: all 0.5s;
}
</style>
