<!-- 底部 Bar 组件 -->

<script setup lang="ts">
    const props = defineProps({
        // 是否显示
        show: {
            type: Boolean,
            default: true,
        },
        // 是否展开
        expand: {
            type: Boolean,
            default: false,
        },
        // 展开按钮标题
        expandTitle: {
            type: String,
            default: '',
        },
        // 是否有展开按钮
        showExpandButton: {
            type: Boolean,
            default: true,
        },
        // 是否有展开按钮下边框
        showExpandBorder: {
            type: Boolean,
            default: false,
        },
        // 高度
        height: {
            type: Number,
            default: 0,
        },
        // 最高高度
        maxHeight: {
            type: Number,
            default: 0,
        },
    })

    const emits = defineEmits(['register'])

    const iHeight = computed(() => `${data.height}rpx`)
    const iMaxHeight = computed(() => `${data.maxHeight}rpx`)

    const data = reactive({
        show: props.show,
        expand: props.expand,
        expandTitle: props.expandTitle,
        showExpandButton: props.showExpandButton,
        showExpandBorder: props.showExpandBorder,
        height: props.height,
        maxHeight: props.maxHeight,
    })

    function toggleShow(show?: boolean) {
        data.show = show ?? !data.show
    }

    function toggleExpand(expand?: boolean) {
        if (data.showExpandButton) {
            data.expand = expand ?? !data.expand
        }
    }

    function setProps(value: Partial<typeof props>) {
        Object.assign(data, value)
    }

    emits('register', {
        setProps,
        toggleShow,
        toggleExpand,
        // TODO: 此处高度还需考虑底部安全区
        height: computed(() => (data.show ? `${(data.expand ? data.maxHeight : data.height) + 60}rpx` : '1rpx')),
    })
</script>

<template>
    <view class="wrap bottom-bar" :class="{ expand: data.expand }" v-show="data.show">
        <view
            class="action flex-center"
            :class="{ border: data.showExpandBorder }"
            v-show="data.showExpandButton"
            @tap="toggleExpand()"
        >
            <text class="text" v-if="data.expandTitle">{{ data.expandTitle }}</text>
            <Icon icon="solar-double-alt-arrow-up-line-duotone" size="42" color="#999" class="icon" />
        </view>

        <!-- 自定义内容 -->
        <scroll-view scroll-y class="content flex-center">
            <slot></slot>
        </scroll-view>

        <!-- 底部安全区 -->
        <fui-safe-area />
    </view>
</template>

<style lang="scss" scoped>
    @import '../../common.scss';

    .wrap {
        @include animate();

        position: absolute;
        left: 30rpx;
        bottom: 30rpx;
        z-index: 99;
        background-color: white;
    }

    .bottom-bar {
        position: fixed;
        bottom: 0;
        left: 0;
        padding: 30rpx;
        width: calc(100% - 60rpx);
        height: v-bind(iHeight);
        max-height: v-bind(iMaxHeight);
        box-shadow: 0 0 15rpx rgba(0, 0, 0, 0.1);

        &.expand {
            height: v-bind(iMaxHeight);

            .icon {
                transform: rotateX(180deg) !important;
            }
        }

        .action {
            width: 100%;
            position: relative;
            top: -15rpx;
            font-size: 24rpx;
            color: #999;
            padding-bottom: 10rpx;

            &.border {
                border-bottom: 2rpx solid #f6f6f6;
            }

            .text {
                margin-right: 0.5em;
            }

            .icon {
                @include animate();

                transform: rotateX(0);
                transform-style: preserve-3d;
            }
        }

        .content {
            width: 100%;
            height: calc(100% - 60rpx - 20rpx);
            overflow: hidden;
            position: relative;
        }
    }
</style>
