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) this.initTemplate();
    },
    /* 初始化模板 */
    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 => {
            console.log("新增初始化模板", 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
        })
    },
    selectUser() {
        let result = this.data.user.userid ? [this.data.user.userid] : [];
        wx.navigateTo({
            url: `/packageA/group/select?data=${JSON.stringify({
                    ownertable:this.data.ownertable,
                    ownerid:this.data.ownerid
                })}&radio=true&obj=true&result=${result}`,
        })
    },
    handelSubmit(arr) {
        wx.navigateBack();
        this.setData({
            user: arr[0]
        })
    },
    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 => {
            console.log("新增或编辑", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            });
            wx.showToast({
                title: '保存成功',
                icon: "none"
            });
            setTimeout(() => {
                getCurrentPages().forEach(v => {
                    //详情界面更新数据
                    if (['packageA/setclient/modules/trace/detail/index'].includes(v.__route__)) v.getDetail();
                    //列表页更新数据
                    if (["packageA/setclient/modules/contacts/detail/index", "packageA/setclient/detail"].includes(v.__route__)) {
                        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)
        })
    },
})