
<template>
    <van-pull-refresh
        v-model="refreshing"
        pulling-text="↓ 下拉即可刷新"
        loosing-text="↑ 释放即可刷新"
        loading-text="加载中..."
        :success-text="refreshErrorText || '刷新成功'"
        @refresh="handleRefresh">
        <slot></slot>
    </van-pull-refresh>
</template>

<script>
    import { PullRefresh as VanPullRefresh } from 'vant'
    export default {
        name: 'AsScrollRefresh',
        data () {
            return {
                refreshing: false,
                refreshErrorText: '',
                loading: false,
                loadError: false,
                loadErrorText: '',
            }
        },
        props: {
            finished: { type: Boolean, default: false },
        },
        methods: {
            handleRefresh () {
                this.$emit('refresh', err => {
                    this.refreshErrorText = err || ''
                    this.refreshing = false
                    const defaultSlot = this.$slots.default[0]
                    if (defaultSlot) {
                        const refEl = defaultSlot.context.$refs.refScrollLoading
                        if (refEl) {
                            refEl.resetError()
                            refEl.check()
                        }
                    }
                });
            },
        },
        components: {
            VanPullRefresh,
        }
    }
</script>
