    const _Http = getApp().globalData.http,
        MFT = require("../../../../utils/matchingFeilType");
    Page({
        data: {
            detail: null,
            isFollow: false,
            isSubmit: true, //是否可以提交
            files: {
                images: [],
                viewImages: [],
                videos: [],
                viewVideos: [],
                files: []
            },
        },
        onLoad(options) {
            this.setData({
                detail: JSON.parse(options.data),
                isFollow: options.isFollow == 'true'
            });
            if (this.data.detail.attinfos.length != 0) this.handleFiles(MFT.fileList(this.data.detail.attinfos))
            if (this.data.detail.notes != '' && this.data.isFollow) this.setData({
                isSubmit: false
            })
        },
        /* 删除文件 */
        deleteFile({
            detail
        }) {
            let e = detail,
                item = e.detail.attachmentid ? e.detail : e.currentTarget.dataset.item;
            _Http.basic({
                "classname": "system.attachment.Attachment",
                "method": "deleteFileLink",
                "content": {
                    "linksids": [item.linksid]
                }
            }).then(res => {
                if (res.msg != '成功') return wx.showToast({
                    title: res.data,
                    icon: "none"
                });
                let files = this.data.files;
                switch (item.fileType) {
                    case "image":
                        files.images = files.images.filter(v => v.url != item.url);
                        files.viewImages = files.viewImages.filter(v => v.url != item.url);
                        break;
                    case "video":
                        files.videos = files.videos.filter(v => v.url != item.url);
                        files.viewVideos = files.viewVideos.filter(v => v.url != item.url);
                        break;
                    default:
                        files.files = files.files.filter(v => v.attachmentid != item.attachmentid);
                        break;
                };
                this.setData({
                    files,
                    "detail.attinfos": this.data.detail.attinfos.filter(v => v.url != item.url)
                });
                this.changeFile();
            })
        },
        //文本域输入
        onInput(e) {
            this.setData({
                "detail.notes": e.detail.value
            })
        },
        /* 绑定媒体 */
        insertImgEdit({
            detail
        }) {
            _Http.basic({
                "classname": "system.attachment.Attachment",
                "method": "createFileLink",
                "content": {
                    "ownertable": "sa_project_stagework",
                    "ownerid": this.data.detail.sa_project_stageworkid,
                    "usetype": "default",
                    "attachmentids": detail
                }
            }).then(res => {
                console.log('跟进记录绑定附件', res)
                if (res.msg != '成功') return wx.showToast({
                    title: res.data,
                    icon: "none"
                })
                this.handleFiles(MFT.fileList(res.data));
                this.data.detail.attinfos.push(res.data[0])
                this.changeFile();
            })
        },
        /* 处理附件 */
        handleFiles(list) {
            let files = this.data.files;
            list.forEach(v => {
                switch (v.fileType) {
                    case "video":
                        files.videos.push(v)
                        files.viewVideos.push({
                            url: v.url,
                            type: "video",
                            poster: v.subfiles[0].url
                        })
                        break;
                    case "image":
                        files.images.push(v)
                        files.viewImages.push({
                            url: v.url,
                            type: "image"
                        })
                        break;
                    default:
                        files.files.push(v)
                        break;
                }
            });
            this.setData({
                files
            })
        },
        /* 修改源文件附件 */
        changeFile() {
            let pages = getCurrentPages(),
                page = pages[pages.length - 2].selectComponent("#Task"),
                i = page.data.list[page.data.viewIndex].work.findIndex(v => v.sa_project_stageworkid == this.data.detail.sa_project_stageworkid);
            page.setData({
                [`list[${page.data.viewIndex}].work[${i}].attinfos`]: this.data.detail.attinfos
            })
        },
        /* 保存 */
        save() {
            _Http.basic({
                "id": 20221024160202,
                "content": {
                    sa_project_stageworkid: this.data.detail.sa_project_stageworkid,
                    notes: this.data.detail.notes,
                }
            }).then(res => {
                wx.showToast({
                    title: res.msg == '成功' ? '保存成功' : res.data,
                    icon: "none"
                });
                if (res.msg != '成功') return;
                this.setData({
                    isSubmit: false
                })
                let pages = getCurrentPages(),
                    page = pages[pages.length - 2].selectComponent("#Task"),
                    i = page.data.list[page.data.viewIndex].work.findIndex(v => v.sa_project_stageworkid == this.data.detail.sa_project_stageworkid);
                page.setData({
                    [`list[${page.data.viewIndex}].work[${i}].notes`]: this.data.detail.notes
                })
            })

        },
        /* 提交 */
        submit() {
            let that = this;
            wx.showModal({
                title: '提示',
                content: `是否提交该任务，提交后将计算分值(仅可提交一次)`,
                complete: ({
                    confirm
                }) => {
                    if (confirm) _Http.basic({
                        "id": 20221024160302,
                        "content": {
                            "sa_project_stageworkid": that.data.detail.sa_project_stageworkid
                        }
                    }).then(res => {
                        console.log("任务提交", res)
                        if (res.msg != '成功') return wx.showToast({
                            title: res.data,
                            icon: "none"
                        });
                        let pages = getCurrentPages(),
                            page = pages[pages.length - 2].selectComponent("#Task");
                        pages[pages.length - 2].getDetail();
                        page.getList();
                        wx.showToast({
                            title: '提交成功',
                            icon: "none"
                        });
                        that.setData({
                            "detail.finished": 1
                        })
                    })
                }
            })
        }
    })