const _Http = getApp().globalData.http;
let sys_workreportmodelid = null;
import {
    formatTime
} from "../../utils/getTime"

Page({
    data: {
        date: formatTime(new Date(), "-").split(" ")[0],
        loading: true,
        loading1: false
    },
    onLoad(options) {
        let model = JSON.parse(options.model)
        sys_workreportmodelid = model.sys_workreportmodelid;
        wx.setNavigationBarTitle({
            title: model.reportname,
        })
        _Http.basic({
            "id": "20230523131702",
            "content": {
                sys_workreportmodelid
            }
        }).then(res => {
            console.log("查询模板数据", res)
            if (res.msg != '成功') {
                wx.showToast({
                    title: res.msg,
                    icon: "none",
                    mask: true
                })
                setTimeout(() => {
                    wx.navigateBack()
                }, 500)
            } else {
                this.setData({
                    reportname: model.reportname,
                    list: res.data.map(v => {
                        v.content = v.content.map(s => {
                            return v.editable == 1 && s.content != "" ? `${s.rowindex}、${s.content}` : s.content
                        }).join('\n\n');
                        return v
                    }),
                    loading: false
                })
            }
        })
    },
    /* 绑定媒体 */
    insertImgEdit({
        detail
    }) {
        _Http.basic({
            "classname": "system.attachment.Attachment",
            "method": "createFileLink",
            "content": {
                "ownertable": "sys_workreport",
                "ownerid": 0,
                "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)
        })
    },
    onInput(e) {
        let {
            item,
            index
        } = e.currentTarget.dataset;
        item.content = e.detail.value;
        this.setData({
            [`list[${index}]`]: item
        })
    },
    onVoiceInput(e) {
        let {
            item,
            index
        } = e.currentTarget.dataset;
        item.content = e.detail;
        this.setData({
            [`list[${index}]`]: item
        })
    },
    /* 取消汇报 */
    onCancel() {
        wx.showModal({
            title: '提示',
            content: '是否确定取消汇报，确认后将丢失填写/修改进度',
            complete: (res) => {
                if (res.confirm) wx.navigateBack()
            }
        })
    },
    /* 提交汇报 */
    submit() {
        console.log()
        const that = this;

        wx.showModal({
            title: '提示',
            content: '是否确认提交汇报?',
            complete: ({
                confirm
            }) => {
                if (confirm) {
                    this.setData({
                        loading1: true
                    })
                    _Http.basic({
                        "id": "20230523094702",
                        "content": {
                            sys_workreportmodelid,
                            "items": that.data.list.map(v => {
                                v.content = [{
                                    rouindex: 1,
                                    content: v.content
                                }]
                                return v
                            })
                        },
                    }).then(res => {
                        console.log("提交汇报", res)
                        that.setData({
                            loading1: false
                        })
                        wx.showToast({
                            title: res.msg == '成功' ? '提交成功' : res.msg,
                            icon: "none",
                            mask: true
                        });
                        let {
                            attachmentids
                        } = this.selectComponent("#Yl_Files").getFiles();

                        if (attachmentids.length == 0) return setTimeout(() => {
                            wx.redirectTo({
                                url: '/packageA/report/detail?id=' + res.data.sys_workreportid,
                            })
                        }, 500)

                        _Http.basic({
                            "classname": "system.attachment.Attachment",
                            "method": "createFileLink",
                            "content": {
                                "ownertable": "sys_workreport",
                                "ownerid": res.data.sys_workreportid,
                                "usetype": "default",
                                attachmentids
                            }
                        }).then(s => {
                            console.log('附件改绑', s)
                            if (s.msg != '成功') wx.showToast({
                                title: s.msg,
                                icon: "none",
                                mask: true
                            });
                            setTimeout(() => {
                                wx.redirectTo({
                                    url: '/packageA/report/detail?id=' + res.data.sys_workreportid,
                                })
                            }, 300)
                        })
                    })
                }
            }
        })
    }
})