const _Http = getApp().globalData.http,
    MFT = require("../../utils/matchingFeilType"),
    deleteMark = require("../../utils/deleteMark");
Page({
    data: {
        sat_submiteditmodelid: 0,
        detailsData: {}, //详情数据
        startRecord: false,
        submiteditData: {}, //新建明细
        content: "", //提交内容
        CheckTheRecord: false, //查看记录
        recordL: {}, //记录详情
        done: false, //!是否还可提交明细
        loading: false, //按钮加载状态
    },
    onLoad(options) {
        this.setData({
            sat_submiteditmodelid: options.id,
            ishistory: options.ishistory
        })

        //查询详情
        _Http.basic({
            "classname": "saletool.submitedit.submitedit",
            "method": "select_submitdetailed",
            "content": {
                "sat_submiteditmodelid": options.id
            }
        }).then(res => {
            console.log(res)
            /* 处理附件 */
            let fileList = res.data.attinfos.filter(v => v.usetype != 'cover');
            res.data.attinfos = MFT.fileList(fileList);
            /* 删除提报 */
            let submitedit = res.data.submitedit,
                list = [];
            if (submitedit.length) {
                for (let i = 0; i < submitedit.length; i++) {
                    (submitedit[i].status == '未提报' && submitedit[i].content == '') ? this.delete_submitdetailed(submitedit[i].sat_submiteditid): list.push(submitedit[i]);
                };
                res.data.submitedit = list;
            };
            this.setData({
                detailsData: res.data,
                oneToOne: options.oneToOne
            });
            if (options.ishistory == 1) {
                this.setData({
                    done: true
                })
            } else if (options.oneToOne == 'true' && res.data.submitedit.length == 1) {
                this.setData({
                    done: true
                })
            };
        });
    },
    /* 上传绑定附件 */
    getFile({
        detail
    }) {
        _Http.basic({
            "classname": "system.attachment.Attachment",
            "method": "createFileLink",
            "content": {
                "ownertable": "sat_submitedit",
                "ownerid": this.data.submiteditData.sat_submiteditid,
                "usetype": "default",
                "attachmentids": detail
            }
        }).then(res => {
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            });
            this.setData({
                "submiteditData.attinfos": this.data.submiteditData.attinfos.concat(MFT.fileList(res.data))
            })
        })
    },
    /* 删除附件 */
    deleteFile({
        detail
    }) {
        _Http.basic({
            "classname": "system.attachment.Attachment",
            "method": "deleteFileLink",
            "content": {
                linksids: [detail.linksid]
            }
        }).then(res => {
            console.log(this.data.submiteditData.attinfos)
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            wx.showToast({
                title: "删除成功"
            })
            let list = this.data.submiteditData.attinfos.filter(v => v.linksid != detail.linksid)
            this.setData({
                "submiteditData.attinfos": list
            })
        })
    },
    /* 创建明细 */
    establish() {
        if (this.data.submiteditData.sat_submiteditmodelid) return this.setData({
            startRecord: true
        })
        this.setData({
            content: ""
        })
        _Http.basic({
            "classname": "saletool.submitedit.submitedit",
            "method": "insertorupdate",
            "content": {
                "sat_submiteditmodelid": this.data.sat_submiteditmodelid,
                "content": "",
                "sat_submiteditid": 0
            }
        }).then(res => {
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            })
            this.setData({
                startRecord: true,
                submiteditData: res.data
            })
        })
    },
    /* 隐藏新建 */
    closeRecord() {
        this.setData({
            startRecord: false
        })
        if (this.data.submiteditData.content != '') this.setData({
            submiteditData: {}
        })
    },
    /* 提交明细 */
    submit() {
        if (!this.data.content && this.data.detailsData.istextrequired == 1) return wx.showToast({
            title: '您还未输入提报内容',
            icon: "none"
        });
        if (this.data.submiteditData.attinfos.length == 0 && this.data.detailsData.isattrequired == 1) return wx.showToast({
            title: '您还未上传附件',
            icon: "none"
        });
        if (this.data.loading) return wx.showToast({
            title: '正在提交中，请勿重新提交',
            icon: "none"
        })
        this.setData({
            loading: true
        })
        _Http.basic({
            "classname": "saletool.submitedit.submitedit",
            "method": "insertorupdate",
            "content": {
                "sat_submiteditmodelid": this.data.submiteditData.sat_submiteditmodelid,
                "content": this.data.content.trim(),
                "sat_submiteditid": this.data.submiteditData.sat_submiteditid
            }
        }).then(res => {
            this.setData({
                loading: false
            })
            if (res.msg != '成功') return wx.showToast({
                title: res.msg,
                icon: "none"
            });
            this.setData({
                loading: true
            })
            _Http.basic({
                "classname": "saletool.submitedit.submitedit",
                "method": "sub_submitdetailed",
                "content": {
                    "sat_submiteditmodelid": this.data.submiteditData.sat_submiteditmodelid,
                    "sat_submiteditid": this.data.submiteditData.sat_submiteditid,
                    onceonly: this.data.oneToOne ? 0 : 1
                }
            }).then(s => {
                this.setData({
                    loading: false
                })
                if (s.msg != '成功') return wx.showToast({
                    title: s.data,
                    icon: "none"
                });
                wx.showToast({
                    title: '提交成功',
                });
                res.data.status = '已提报';
                let submitedit = this.data.detailsData.submitedit;
                let i = submitedit.findIndex(v => v.sat_submiteditid == res.data.sat_submiteditid);
                i == -1 ? submitedit.push(res.data) : submitedit[i] = res.data;
                this.setData({
                    "detailsData.submitedit": submitedit,
                    submiteditData: [],
                    startRecord: false,
                    done: this.data.oneToOne == 'true' ? true : '',
                    content: ""
                });
                let pages = getCurrentPages();
                let prevPage = pages[pages.length - 2];
                prevPage.getList(true)
            })
        })
    },
    /* 查看提报记录 */
    seeHistory(e) {
        const {
            item
        } = e.currentTarget.dataset;
        item.attinfos = MFT.fileList(item.attinfos);
        this.setData({
            recordL: item,
            CheckTheRecord: true
        })
    },
    reedit() {
        this.setData({
            CheckTheRecord: false,
            submiteditData: this.data.recordL,
            startRecord: true,
            content: this.data.recordL.content
        })
    },
    recordClose() {
        this.setData({
            recordL: {},
            CheckTheRecord: false
        })
    },
    /* 文本域输入 */
    textareaInput(e) {
        this.setData({
            content: deleteMark.queryStr(e.detail.value)
        })
    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {
        let submiteditData = this.data.submiteditData;
        if (submiteditData != {} && submiteditData.status == '未提报') {
            this.delete_submitdetailed(submiteditData.sat_submiteditid);
            let attinfos = this.data.submiteditData.attinfos;
            if (attinfos.length) {
                let linksids = [];
                for (let i = 0; i < attinfos.length; i++) {
                    linksids.push(attinfos[i].linksid)
                };
                _Http.basic({
                    "classname": "system.attachment.Attachment",
                    "method": "deleteFileLink",
                    "content": {
                        linksids
                    }
                }).then(res => {
                    console.log('删除附件', res)
                })
            }
        }
    },
    /* 删除未提报数据 */
    delete_submitdetailed(id, i = 0) {
        if (i == 5) return;
        _Http.basic({
            "classname": "saletool.submitedit.submitedit",
            "method": "delete_submitdetailed",
            "content": {
                "sat_submiteditid": id
            }
        }).then(res => {
            console.log(res)
            if (res.data != "成功") return this.delete_submitdetailed(id, i + 1);
        })
    },
    onShareAppMessage(res) { }
})