const _Http = getApp().globalData.http;
Page({
    data: {
        isNew: false, //是否为新增
        ownerid: null,
        ownertable: null,
        sys_datafollowupid: 0, //数据ID
        type: "",
        content: "",
        user: {}, //联系人
        actions: [{
            name: "上门拜访"
        }, {
            name: "电话拜访"
        }, {
            name: "微信联系"
        }, {
            name: "QQ联系"
        }],
        show: false
    },
    onLoad(options) {
        if (options.ownertable) {
            this.setData({
                ...options
            })
        };
        //编辑获取原信息,新建初始化模板
        if (options.sys_datafollowupid) {
            _Http.basic({
                "id": 20221026085601,
                "content": {
                    "sys_datafollowupid": options.sys_datafollowupid
                }
            }).then(res => {
                console.log("跟进详情", res)
                if (res.msg != '成功') {
                    wx.showToast({
                        title: res.data,
                        icon: "none"
                    })
                    setTimeout(() => {
                        wx.navigateBack()
                    }, 300)
                };
                this.selectComponent("#Yl_files").handleFiles(res.data.attinfos)
                this.setData({
                    type: res.data.type,
                    content: res.data.content
                })
            })
        } else {
            this.initTemplate()
        }
    },
    /* 绑定媒体 */
    insertImgEdit({
        detail
    }) {
        _Http.basic({
            "classname": "system.attachment.Attachment",
            "method": "createFileLink",
            "content": {
                "ownertable": "sys_datafollowup",
                "ownerid": this.data.sys_datafollowupid,
                "usetype": "default",
                "attachmentids": detail
            }
        }).then(res => {
            console.log('跟进记录绑定附件', res)
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            this.selectComponent("#Yl_files").handleFiles(res.data)
        })
    },
    /* 初始化模板 */
    initTemplate() {
        _Http.basic({
            "id": 20220930121601,
            content: {
                type: this.data.type,
                content: this.data.content,
                ownerid: this.data.ownerid,
                ownertable: this.data.ownertable,
                sys_datafollowupid: this.data.sys_datafollowupid
            }
        }).then(res => {
            this.setData({
                sys_datafollowupid: res.data.sys_datafollowupid,
                isNew: true
            })
        })
    },
    //开始选择跟进方式
    openSelect() {
        this.setData({
            show: true
        })
    },
    //取消选择
    onCancel() {
        this.setData({
            show: false
        })
    },
    //确定选择
    onSelect({
        detail
    }) {
        this.setData({
            type: detail.name
        })
        this.onCancel();
    },
    //文本域输入
    onInput(e) {
        this.setData({
            content: e.detail.value
        })
    },
    submit() {
        const content = {
            type: this.data.type,
            content: this.data.content,
            ownerid: this.data.ownerid,
            ownertable: this.data.ownertable,
            sys_datafollowupid: this.data.sys_datafollowupid
        };
        if (!content.content) return;
        _Http.basic({
            "id": 20220930121601,
            content
        }).then(res => {
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            wx.showToast({
                title: '保存成功',
                icon: "none"
            });
            this.setData({
                isNew: false
            })
            setTimeout(() => {
                getCurrentPages().forEach(v => {
                    //详情界面更新数据
                    if (['packageA/setclient/modules/trace/detail/index'].includes(v.__route__)) v.getDetail();
                    //列表页更新数据
                    if (v.selectComponent("#Trace")) {
                        let page = v.selectComponent("#Trace"),
                            list = page.data.list,
                            index = list.findIndex(value => value.sys_datafollowupid == res.data.sys_datafollowupid);
                        if (index != -1) {
                            //列表中存在说明是编辑，返回上一级页面并更新数据
                            list[index] = res.data;
                            page.setData({
                                list
                            });
                            wx.navigateBack();
                        } else {
                            //列表中不存在说明是新增，返回上一级页面更新数据 并进入详情
                            list.push(res.data);
                            page.setData({
                                list,
                                "content.total": page.data.content.total + 1
                            });
                            wx.navigateBack();
                            wx.navigateTo({
                                url: `/packageA/setclient/modules/trace/detail/index?data=` + JSON.stringify({
                                    "sys_datafollowupid": res.data.sys_datafollowupid,
                                    "ownertable": this.data.ownertable,
                                    "ownerid": this.data.ownerid
                                }),
                            })
                        }
                    };
                });
            }, 300)
        })
    },
    onUnload() {
        if (this.data.isNew) _Http.basic({
            "id": 20220930121701,
            "content": {
                "sys_datafollowupid": this.data.sys_datafollowupid,
                "deletereason": "系统删除"
            }
        }).then(res => {
            console.log("初始化模板后未保存删除", res)
        })
    }
})