<template>
    <view class="container">
        <view id="mylisttop" />
        <scroll-view class="scroll-view" scroll-y :refresher-enabled='pullDown' :refresher-triggered='inRefresh'
            :style="{ height: height + 'px' }" :triggered='true' @refresherrefresh='pullToRefresh'
            :scroll-into-view="scrollIntoView" :lower-threshold='300' :scroll-with-animation="true"
            @scrolltolower='loadThePage'>
            <view id="header">
                <slot />
            </view>
            <view v-if="empty">
                <view :style="{ height: tovw(occupyHeight) }" />
                <u-empty :mode="mode" />
            </view>
            <view id="bottom" />
        </scroll-view>
    </view>
</template>

<script>
export default {
    name: 'My_listbox',
    props: {
        getlist: Function,
        empty: Boolean,
        mode: {
            type: String,
            default: "data"
        },
        pullDown: {
            type: Boolean,
            default: true
        },
        occupyHeight: {
            type: Number,
            default: 50
        }
    },
    data() {
        return {
            inRefresh: false, //下拉开启自定义项
            height: 0,
            scrollIntoView: ""
        };
    },
    methods: {
        /* 下拉刷新 */
        pullToRefresh() {
            this.inRefresh = true
            this.$emit("getlist", true)
        },
        /* 刷新完成 */
        RefreshToComplete() {
            setTimeout(() => {
                this.inRefresh = false
            }, 500)
        },
        /* 加载分页 */
        loadThePage() {
            this.$emit("getlist", false)
        },
        /* 设置组件高度 */
        setHeight(mode, num) {
            return new Promise((resolve) => {
                this.getHeight("#mylisttop", this).then(res => {
                    let height = res;
                    switch (mode) {
                        case 'add':
                            height = (res - 0) + (num - 0);
                            break;
                        case 'minus':
                            height = res - num;
                            break;
                    }
                    if (this.height != height) this.height = height;
                    resolve(height)
                });
            })
        }
    },
}
</script>

<style lang="scss" scoped>
.scroll-view {
    position: relative;
}
</style>
