const _Http = getApp().globalData.http;
Page({
    data: {
        loading: false,
        list: [{
            label: "跟进内容",
            key: "content",
            value: "",
        }],
        content: {

        }
    },
    onLoad(options) {
        getApp().globalData.Language.getLanguagePackage(this, '跟进');
        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.code != '1') {
                    wx.showToast({
                        title: res.msg,
                        icon: "none"
                    })
                    setTimeout(() => {
                        wx.navigateBack()
                    }, 300)
                };
                this.setData({
                    [`list[0].value`]: res.data.content,
                    resource: res.data.resource
                })
                if (res.data.attinfos.length) this.selectComponent("#Yl_files").handleFiles(res.data.attinfos);
                this.data.content = Object.assign(res.data, options);

            })
        } else {
            options.sys_datafollowupid = 0;
            this.data.content = options;
        }
    },
    changeState({
        detail
    }) {
        this.setData({
            loading: detail
        })
    },
    onInput(e) {
        const {
            index
        } = e.target.dataset;
        this.data.list[index].value = e.detail.value;
        this.setData({
            list: this.data.list
        })
    },
    onVoiceInput(e) {
        let {
            index
        } = e.target.dataset;
        this.setData({
            [`list[${index}].value`]: e.detail
        })
    },
    /* 绑定媒体 */
    insertImgEdit({
        detail
    }) {
        this.handleFileLink(detail)
    },
    handleFileLink(attachmentids, ownertable = "temporary", ownerid = 1, data) {
        _Http.basic({
            "classname": "system.attachment.Attachment",
            "method": "createFileLink",
            "content": {
                ownertable,
                ownerid,
                usetype: 'default',
                attachmentids
            }
        }).then(res => {
            console.log('跟进记录绑定附件', res)
            if (res.code != '1') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            if (ownertable == 'temporary') {
                this.selectComponent("#Yl_files").handleFiles(res.data)
            } else {
                if (res.data.length) data.attinfos = res.data;
                _Http.basic({
                    "classname": "system.attachment.Attachment",
                    "method": "createFileLink",
                    "content": {
                      ownertable: this.data.ownertable,
                      ownerid: this.data.ownerid,
                      usetype: 'default',
                      attachmentids
                    }
                  })
                this.changeItem(data)
                setTimeout(() => {
                    wx.navigateBack()
                }, 500)
            }
        })
    },
    changeItem(data) {
        this.setData({
            loading: false
        })
        let page = getCurrentPages()[getCurrentPages().length - 2],
            isDetail = page.__route__ == 'prsx/trace/detail/index';
        if (isDetail) {
            page.getDetail();
            page = getCurrentPages()[getCurrentPages().length - 3];
        }
        let model = page.selectComponent("#Trace");
        if (this.data.content.sys_datafollowupid == 0) {
            if (page) {
                if (model) model.getList(0, true)
            }
        } else {
            let list = model.data.list,
                index = list.findIndex(v => v.sys_datafollowupid == data.sys_datafollowupid);
            if (index != -1) list[index] = data;
            model.setData({
                list
            })

        }
    },
    submit() {
        this.setData({
            loading: true
        })
        let content = Object.assign(this.data.content, this.selectComponent("#Form").submit())
        content.content = this.data.list[0].value;
        content.resource = this.data.resource || ''
        _Http.basic({
            "id": 20220930121601,
            content
        }).then(res => {
            console.log("保存跟进内容", res)
            this.setData({
                loading: false
            })
            wx.showToast({
                title: getApp().globalData.Language.getMapText(res.code != 1 ? res.msg : content.sys_datafollowupid == 0 ? '保存成功' : '修改成功'),
                icon: "none",
                mask: res.code != 1
            });
            if (res.msg != '成功') return;
            let attachmentids = this.selectComponent("#Yl_files").getFiles().attachmentids;
            if (attachmentids.length) return this.handleFileLink(attachmentids, 'sys_datafollowup', res.data.sys_datafollowupid, res.data);
            this.changeItem(res.data)
            setTimeout(() => {
                wx.navigateBack()
            }, 500)
        })
    },
    onUnload() {
        if (this.data.content.sys_datafollowupid == 0) this.selectComponent("#Yl_files").deleteAll()
    }
})