const _Http = getApp().globalData.http;

Component({
    /**
     * 组件的属性列表
     */
    properties: {
        //富文本内容
        content: {
            type: String,
            value: ""
        },
        //是否开启编辑
        editable: {
            type: Boolean,
            value: true
        },
        show: {
            type: Boolean,
            value: false
        },
        callback: {
            type: Function
        }
    },

    lifetimes: {
        attached: function () {
            this.ctx = this.selectComponent('#article');
            this.ctx.getSrc = (type, value) => {
                return new Promise((resolve, reject) => {
                    if (type === 'img' || type === 'video') {
                        // 本地选取
                        if (type === 'img') {
                            resolve(this.data.fileMsg.url)
                        } else {
                            resolve(this.data.fileMsg.url)
                        }
                    } else {
                        this.callback = {
                            resolve,
                            reject
                        }
                        let title
                        if (type === 'audio') {
                            title = '音频链接'
                        } else if (type === 'link') {
                            title = '链接地址'
                        }
                        this.setData({
                            modal: {
                                title,
                                value
                            }
                        })
                    }
                })
            }
        },
        detached: function () {
            // 在组件实例被从页面节点树移除时执行
        },
    },

    /**
     * 组件的初始数据
     */
    data: {},

    /**
     * 组件的方法列表
     */
    methods: {
        insertImgEdit({
            detail
        }) {
            this.binding('insertImg', detail)
        },
        insertVideoEdit({
            detail
        }) {
            this.binding('insertVideo', detail)
        },
        binding(type, id) {
            const that = this;
            let pages = getCurrentPages();
            let prevPage = pages[pages.length - 1];
            _Http.basic({
                "classname": "system.attachment.Attachment",
                "method": "createFileLink",
                "content": {
                    "ownertable": "SAT_SHAREMATERIAL",
                    "ownerid": prevPage.data.detailsData.sat_sharematerialid,
                    "usetype": "richtext",
                    "attachmentids": id
                }
            }).then(res => {
                if (res.msg != '成功') return wx.showToast({
                    title: res.msg,
                    icon: "none"
                });
                this.setData({
                    fileMsg: res.data[0]
                })
                prevPage.setData({
                    richTextFile: prevPage.data.richTextFile.concat(res.data[0])
                })
                that.ctx[type]();
            })
        },
        // 删除图片/视频/音频标签事件
        remove({
            detail
        }) {
            let pages = getCurrentPages();
            let prevPage = pages[pages.length - 1];
            let richTextFile = prevPage.data.richTextFile;
            let index = richTextFile.findIndex((value) => value.url == detail.src);
            _Http.basic({
                "classname": "system.attachment.Attachment",
                "method": "deleteFileLink",
                "content": {
                    "linksids": [richTextFile[index].linksid]
                }
            }).then(res => {
                richTextFile.splice(index, 1);
                prevPage.setData({
                    richTextFile
                })
            })
        },
        // 处理模态框
        modalInput(e) {
            this.value = e.detail.value
        },
        modalConfirm() {
            this.callback.resolve(this.value || this.data.modal.value || '')
            this.setData({
                modal: null
            })
        },
        modalCancel() {
            this.callback.reject()
            this.setData({
                modal: null
            })
        },
        // 调用编辑器接口
        edit(e) {
            this.ctx[e.currentTarget.dataset.method]()
        },
        // 清空编辑器内容
        clear() {
            wx.showModal({
                title: '确认',
                content: '确定清空内容吗？',
                success: res => {
                    if (res.confirm) this.ctx.clear()
                }
            })
        },
        // 保存编辑器内容
        save() {
            // 避免无法获取到正在编辑的文本内容
            setTimeout(() => {
                let content = this.ctx.getContent(),
                    that = this;
                wx.showModal({
                    title: '保存',
                    content: "是否确认保存",
                    confirmText: '完成',
                    success: res => {
                        if (res.confirm) {
                            that.triggerEvent("callback", content);
                            that.closeShow();
                        }
                    }
                })
            }, 50)
        },
        closeShow() {
            let pages = getCurrentPages();
            let prevPage = pages[pages.length - 1];
            prevPage.setData({
                editRichText: false
            })
        }
    }
})