const _Http = getApp().globalData.http;
import {
    fileList
} from "../../utils/FormatTheAttachment";
Component({
    properties: {
        files: {
            type: Object,
            value: {
                images: [],
                viewImages: [],
                videos: [],
                viewVideos: [],
                files: []
            },
        },
        delete: {
            type: Boolean
        },
        deleteCallBack: {
            type: Function
        }
    },
    methods: {
        /* 预览媒体 */
        viewMedias(e) {
            const {
                index,
                type
            } = e.currentTarget.dataset;
            wx.previewMedia({
                current: index,
                sources: type == 'image' ? this.data.files.viewImages : this.data.files.viewVideos,
            })
        },
        /* 预览文档 */
        viewFlies(e) {
            const {
                item
            } = e.currentTarget.dataset;
            wx.showLoading({
                title: '加载中...',
            })
            wx.downloadFile({
                url: item.url,
                complete({
                    statusCode,
                    tempFilePath
                }) {
                    if (statusCode != 200) return;
                    wx.openDocument({
                        filePath: tempFilePath,
                        fileType: item.postfix,
                        showMenu: true,
                        complete({
                            errMsg
                        }) {
                            wx.hideLoading();
                            if (errMsg != "openDocument:ok") wx.showToast({
                                title: '打开失败',
                                icon: "none"
                            })
                        }
                    })
                }
            })
        },
        /* 删除文件 */
        handleDeleteFile(e) {
            let item = e.currentTarget.dataset.item || 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.msg,
                    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
                });
                this.triggerEvent("deleteCallBack")
            })
        },
        /* 处理附件 */
        handleFiles(arr, init = false) {
            let files = init ? {
                    images: [],
                    viewImages: [],
                    videos: [],
                    viewVideos: [],
                    files: []
                } : this.data.files,
                list = fileList(arr);
            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
            })
        },
        /* 初始化数据 */
        initData() {
            this.setData({
                files: {
                    images: [],
                    viewImages: [],
                    videos: [],
                    viewVideos: [],
                    files: []
                }
            })
        },
    }
})