const _Http = getApp().globalData.http;
Component({
    options: {
        addGlobalClass: true
    },
    properties: {
        ownertable: {
            type: String
        },
        ownerid: {
            type: String
        },
        resource: {
            type: String
        },
        disabled: {
            type: Boolean,
            value: true
        }
    },
    lifetimes: {
        attached: function () {
            _Http.traceHandleDelete = this.handleDelete.bind(this);
            getApp().globalData.Language.getLanguagePackage(this)
        },
        detached: function () {
            delete _Http.traceHandleDelete
        },
    },
    data: {
        content: {
            nocache: true,
            pageNumber: 1,
            pageSize: 10,
            pageTotal: 1,
            total: null,
            where: {
                condition: ""
            }
        },
        list: [],
        showSearch: false,
        focus: false,
        condition: ""
    },
    methods: {
        toSearch() {
            if (this.data.showSearch && this.data.content.where.condition) {
                this.data.content.where.condition = '';
                this.getList("", true);
            } else if (this.data.condition) {
                this.data.content.where.condition = this.data.condition;
                this.setData({
                    condition: this.data.condition
                })
                this.getList("", true);
            }
            this.setData({
                showSearch: !this.data.showSearch
            })
            setTimeout(() => {
                this.setData({
                    focus: this.data.showSearch
                })
            }, 300)
        },
        onChange({
            detail
        }) {
            this.data.condition = detail;
        },
        onSearch({
            detail
        }) {
            this.data.content.where.condition = detail;
            this.getList("", true)
        },
        getList(id, init = false) {
            let content = {
                ...this.data.content,
                "ownertable": this.data.ownertable,
                "ownerid": this.data.ownerid,
            };
            if (init) {
                content.pageNumber = 1
                content.pageTotal = 1
            }
            _Http.basic({
                "id": 20220930121501,
                content
            }).then(res => {
                console.log("跟进动态", res)
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                });
                let deleteList = res.data.filter(v => v.content == "" && v.type == ""),
                    list = res.data.filter(v => v.content != "" || !v.type == "");
                this.handleDeleteList(deleteList);
                this.setData({
                    "content.pageNumber": res.pageNumber + 1,
                    "content.pageTotal": res.pageTotal,
                    "content.total": res.total - deleteList.length,
                    list: res.pageNumber == 1 ? list : this.data.list.concat(list)
                })
            })
        },
        /* 批量删除空模板 */
        handleDeleteList(list) {
            list.forEach(v => {
                _Http.basic({
                    "id": 20220930121701,
                    "content": {
                        "sys_datafollowupid": v.sys_datafollowupid,
                        "deletereason": "系统删除空模板"
                    }
                }, false).then(res => {
                    console.log(res)
                })
            })
        },
        /* 去添加 */
        toAdd() {
            wx.navigateTo({
                url: `/prsx/trace/add/index?ownertable=${this.data.ownertable}&ownerid=${this.data.ownerid}&resource=${this.data.resource}`,
            })
        },
        //跟进组件设置删除
        setDelete(id) {
            this.setData({
                list: this.data.list.filter(v => v.sys_datafollowupid != id)
            });
            wx.showToast({
                title: getApp().globalData.Language.getMapText('作废成功') + '!',
                icon: "none"
            })
            this.changeTotal()
        },
        changeTotal() {
            this.setData({
                "content.total": this.data.content.total - 1
            })
        },
        handleDelete(item) {
            let that = this;
            return new Promise((resolve) => {
                wx.showModal({
                    title: getApp().globalData.Language.getMapText('提示'),
                    content: getApp().globalData.Language.getMapText('是否确定删除该跟进动态'),
                    complete: ({
                        confirm
                    }) => {
                        if (confirm) {
                            _Http.basic({
                                "id": 20220930121701,
                                "content": {
                                    "sys_datafollowupid": item.sys_datafollowupid,
                                    "ownertable": that.data.ownertable,
                                    "ownerid": that.data.ownerid,
                                    deletereason: ""
                                }
                            }).then(res => {
                                console.log("删除", res);
                                getApp().globalData.Language.showToast(res.code != 1 ? res.msg : "删除成功")
                                resolve(res.code)
                                if (res.msg != '成功') return;
                                that.setData({
                                    list: that.data.list.filter(v => v.sys_datafollowupid != item.sys_datafollowupid),
                                    'content.total': that.data.content.total - 1
                                })
                            })
                        } else {
                            resolve(0)
                        }
                    }
                })
            })
        },
    }
})