import {
    getHeight
} from "../../utils/getHeight";
Component({
    properties: {
        height: {
            type: Number
        }, //组件高度
        getlist: {
            type: Function
        },
        pullDown: { //是否开启下拉
            type: Boolean,
            value: true
        },
        safety: { //适配苹果底部安全距离
            type: Boolean,
            value: true
        },
        automatic: { //自动设置高度
            type: Boolean,
            value: true
        },
    },
    lifetimes: {
        attached: function () {
            if (this.data.automatic) this.automaticSetHei()
        },
    },
    data: {
        inRefresh: false, //下拉开启自定义项
    },
    methods: {
        /* 下拉刷新 */
        pullToRefresh() {
            this.setData({
                inRefresh: true
            })
            this.triggerEvent("getlist", true)
        },
        /* 刷新完成 */
        RefreshToComplete() {
            setTimeout(() => {
                this.setData({
                    inRefresh: false
                })
            }, 500)
        },
        /* 加载分页 */
        loadThePage() {
            this.triggerEvent("getlist", false)
        },
        automaticSetHei(mode, num) {
            this.setHeight("#mylisttop", this, mode, num)
        },
        /* 设置组件高度 */
        setHeight(element, that, mode, num) {
            return new Promise((resolve) => {
                getHeight(element, that).then(res => {
                    let height = res;
                    switch (mode) {
                        case 'add':
                            height = (res - 0) + (num - 0);
                            break;
                        case 'minus':
                            height = res - num;
                            break;
                    }
                    this.setData({
                        height
                    })
                    resolve(height)
                })
            });
        }
    }
})