const _Http = getApp().globalData.http;
Component({
    options: {
        addGlobalClass: true
    },
    properties: {
        ownertable: {
            type: String
        },
        ownerid: {
            type: String
        },
    },
    data: {
        contentText: "",
        loading: false,
        content: {
            nocache: true,
            pageNumber: 1,
            pageSize: 20,
            pageTotal: 1,
            total: null
        },
        list: [],
        replyid: 0,
    },
    lifetimes: {
        attached: function () {
            getApp().globalData.Language.getLanguagePackage(this)
            this.setData({
                userid: wx.getStorageSync('userMsg').userid,
            })
        }
    },
    methods: {
        getList(id, init = false) {
            this.setData({
                userid: wx.getStorageSync('userMsg').userid
            })
            console.log("userid", this.data.userid)
            let content = {
                ...this.data.content,
                "ownertable": this.data.ownertable,
                "ownerid": this.data.ownerid,
            };
            if (init) {
                content.pageNumber = 1
                content.pageTotal = 1
            }
            _Http.basic({
                "id": 20240429161501,
                content
            }).then(res => {
                console.log("评论", res)
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                });
                this.setData({
                    "content.pageNumber": res.pageNumber + 1,
                    "content.pageTotal": res.pageTotal,
                    "content.total": res.total,
                    list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data)
                })
            })
        },
        textareaInput(e) {
            this.setData({
                contentText: e.detail.value
            })
        },
        onSend() {
            this.setData({
                loading: true
            })
            _Http.basic({
                "id": 20240429161601,
                "content": {
                    "ownertable": this.data.ownertable, //数据表
                    "ownerid": this.data.ownerid,
                    "content": this.data.contentText,
                    "replyid": "0"
                }
            }).then(res => {
                console.log("发送评论", res)
                this.setData({
                    loading: false
                })
                wx.showToast({
                    title: res.code == '1' ? getApp().globalData.Language.getMapText('评论成功') : res.msg,
                    icon: "none"
                })
                if (res.code == '1') {
                    this.getList(0, true)
                    this.setData({
                        contentText: ""
                    })
                }
            })
        },
        deleteItem(e) {
            const {
                item
            } = e.currentTarget.dataset,
                that = this;
            wx.showModal({
                title: getApp().globalData.Language.getMapText('提示'),
                content: getApp().globalData.Language.getMapText('是否确定删除该评论') + '?',
                cancelText: getApp().globalData.Language.getMapText('取消'),
                confirmText: getApp().globalData.Language.getMapText('确定'),
                complete: ({
                    confirm
                }) => {
                    if (confirm) _Http.basic({
                        "id": 20240429161701,
                        "content": {
                            "sys_datacommentid": item.sys_datacommentid
                        }
                    }).then(res => {
                        console.log("删除评论", res)
                        wx.showToast({
                            title: res.code == '1' ? getApp().globalData.Language.getMapText('删除成功') : res.msg,
                            icon: "none"
                        });
                        if (res.code == '1') that.setData({
                            list: that.data.list.filter(v => v.sys_datacommentid != item.sys_datacommentid)
                        })
                    })
                }
            })
        },
        revert(e) {
            const {
                id
            } = e.currentTarget.dataset;
            this.setData({
                replyid: this.data.replyid == id ? 0 : id
            })
        },
        itemTextareaInput(e) {
            const {
                item
            } = e.currentTarget.dataset;
            let index = this.data.list.findIndex(v => v.sys_datacommentid == item.sys_datacommentid)
            this.setData({
                [`list[${index}].contentText`]: e.detail.value
            })
        },
        onSendItem() {
            let index = this.data.list.findIndex(v => v.sys_datacommentid == this.data.replyid),
                item = this.data.list[index];

            this.setData({
                [`list[${index}].loading`]: true
            })
            _Http.basic({
                "id": 20240429161601,
                "content": {
                    "ownertable": this.data.ownertable, //数据表
                    "ownerid": this.data.ownerid,
                    "content": item.contentText,
                    "replyid": this.data.replyid
                }
            }).then(res => {
                console.log("回复评论", res)
                this.setData({
                    [`list[${index}].loading`]: false
                })
                wx.showToast({
                    title: res.code == '1' ? getApp().globalData.Language.getMapText('回复成功') : res.msg,
                    icon: "none"
                })
                if (res.code == '1') {
                    this.getList(0, true);
                    this.setData({
                        replyid: 0
                    })
                }
            })
        }
    }
})