const _Http = getApp().globalData.http;
Component({
    lifetimes: {
        attached: function () {
            _Http[this.data.ownerid] = {
                changeReply: function (replyid = 0) {
                    this.setData({
                        replyid: replyid == this.data.replyid ? 0 : replyid
                    })
                }.bind(this),
                updateCommentList: function () {
                    this.triggerEvent("updateCommentList", {
                        "ownertable": this.data.ownertable, //数据表
                        "ownerid": this.data.ownerid,
                    })
                }.bind(this)
            }
            this.setData({
                userid: wx.getStorageSync('userMsg').userid
            })
            getApp().globalData.Language.getLanguagePackage(this)
        },
        detached: function () {
            delete _Http[this.data.ownerid]
        },
    },
    properties: {
        quantity: {
            type: [String, Number],
            value: 0
        },
        comments: {
            type: Array,
            value: []
        },
        ownertable: {
            type: String,
            value: 'sys_datafollowup'
        },
        ownerid: {
            type: String
        },
        updateCommentList: {
            type: Function
        }
    },
    data: {
        loading: false,
        show: false,
        comment: '',
        replyid: 0,
        autoFocus: true
    },
    methods: {
        changeShow() {
            this.setData({
                show: !this.data.show,
                autoFocus: true
            })
            setTimeout(() => {
                this.setData({
                    autoFocus: true
                })
            });
            if (this.data.quantity && this.data.show) {
                const query = this.createSelectorQuery()
                query.select('#CommentNode' + this.data.ownerid).boundingClientRect()
                query.selectViewport().scrollOffset()
                query.exec(function (res) {
                    if (wx.getWindowInfo().windowHeight - res[0].top < 100) return wx.pageScrollTo({
                        scrollTop: res[1].scrollTop + 200,
                        success() {
                            _Http.scrollTop = scrollTop
                        }
                    })
                })
            }

        },
        onInput(e) {
            this.setData({
                comment: e.detail.value
            })
        },
        onSend() {
            this.setData({
                loading: true
            })
            _Http.basic({
                "id": 20240429161601,
                "content": {
                    "ownertable": this.data.ownertable, //数据表
                    "ownerid": this.data.ownerid,
                    "content": this.data.comment,
                    replyid: 0
                }
            }).then(res => {
                console.log("发送评论", res)
                this.setData({
                    loading: false
                })
                wx.showToast({
                    title: getApp().globalData.Language.getMapText(res.code == '1' ? '评论成功' : res.msg),
                    icon: "none"
                })
                if (res.code == '1') {
                    this.setData({
                        comment: ""
                    })
                    _Http[this.data.ownerid].updateCommentList();
                }
            })
        },
    }
})