<template>
    <view class="cm-drawer">
        <view class="mask" v-if="visible" :style="[offsetSize, { zIndex: zIndex - 1 }]" @click="$emit('close')"></view>
        <view class="drawer-content" :class="[position]" :style="[offsetSize, { zIndex: zIndex }]">
            <uni-transition :mode-class="modeClass" :show="visible">
                <view class="content">
                    <!-- 关闭弹框按钮 -->
                    <view class="iconfont icon-iconfontguanbi close" @click="$emit('close')"></view>
                    <view class="title" v-if="title">{{ title }}</view>
                    <!-- 自定义插槽 -->
                    <view><slot></slot></view>
                </view>
            </uni-transition>
        </view>
    </view>
</template>

<script>
export default {
    name: 'cm-drawer',
    props: {
        title: {
            type: String,
            default: ''
        },
        visible: {
            type: Boolean,
            default: false
        },
        position: {
            type: String,
            default: 'center'
        },
        offset: {
            type: Number,
            default: 0
        },
        zIndex: {
            type: Number,
            default: 99999
        }
    },
    computed: {
        offsetSize() {
            if (this.position === 'center' || this.offset === 0) {
                return {}
            }
            return {
                [this.position]: this.offset + 'rpx'
            }
        },
        modeClass() {
            let name = 'fade'
            switch (this.position) {
                case 'center':
                    name = 'fade'
                    break
                case 'top':
                    name = 'slide-top'
                    break
                case 'right':
                    name = 'slide-right'
                    break
                case 'bottom':
                    name = 'slide-bottom'
                    break
                case 'left':
                    name = 'slide-left'
                    break
                default:
                    name = 'fade'
                    break
            }
            return name
        }
    }
}
</script>

<style lang="scss" scoped>
$drawer-size: 100rpx;

.mask {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
}
.cm-drawer {
    .drawer-content {
        position: fixed;
        &.bottom {
            left: 0;
            bottom: 0;
            .content {
                border-radius: 16rpx 16rpx 0 0;
            }
        }
        &.top {
            top: 0;
            left: 0;
            .content {
                border-radius: 0 0 16rpx 16rpx;
            }
        }
        &.right {
            top: 0;
            right: 0;
            .content {
                border-radius: 16rpx 0 16rpx 0;
            }
        }
        &.left {
            top: 0;
            left: 0;
            .content {
                border-radius: 0 16rpx 16rpx 0;
            }
        }
        &.center {
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            .content {
                width: 702rpx;
                min-height: 600rpx;
                max-height: 800rpx;
                overflow-y: scroll;
                border-radius: 16rpx;
            }
        }
        .content {
            position: relative;
            width: 750rpx;
            min-height: 180rpx;
            background: #fff;
            box-sizing: border-box;
            padding: 24rpx;
            .close {
                position: absolute;
                top: 26rpx;
                right: 26rpx;
                font-size: 34rpx;
                color: #b2b2b2;
                width: 60rpx;
                height: 60rpx;
                text-align: center;
                line-height: 60rpx;
            }
            .title {
                width: 100%;
                word-break: break-all;
                overflow: hidden;
                line-height: 50rpx;
                height: 50rpx;
                text-overflow: ellipsis;
                text-align: center;
                font-size: 34rpx;
                color: #333333;
            }
        }
    }
}
</style>
