const _Http = getApp().globalData.http;
import {
    fileList
} from "../../utils/matchingFeilType";
Component({
    properties: {
        files: {
            type: Object,
            value: {
                images: [],
                viewImages: [],
                videos: [],
                viewVideos: [],
                files: []
            },
        },
        delete: {
            type: Boolean
        },
        onDeteleFiles: {
            type: Function
        },
        padding: {
            type: String,
            value: '0 30rpx'
        },
        strict: {
            type: Boolean
        },
        attinfos: {
            type: Array
        }
    },
    observers: {
        'attinfos': function (attinfos) {
            this.initData();
            this.handleFiles(attinfos, true)
        }
    },
    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: getApp().globalData.Language.getMapText('加载中...'),
            })
            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") getApp().globalData.Language.showToast('打开失败')
                        }
                    })
                }
            })
        },
        /* 删除文件 */
        handleDeleteFile(e) {
            let that = this;
            if (this.data.strict) {
                wx.showModal({
                    title: getApp().globalData.Language.getMapText('提示'),
                    content: getApp().globalData.Language.getMapText('是否确定删除该附件'),
                    cancelText: getApp().globalData.Language.getMapText('取消'),
                    confirmText: getApp().globalData.Language.getMapText('确定'),
                    complete: (res) => {
                        if (res.confirm) start()
                    }
                })
            } else {
                start()
            }

            function start() {
                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.code != '1') return wx.showToast({
                        title: res.data,
                        icon: "none"
                    });
                    let files = that.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;
                    };
                    that.setData({
                        files
                    });
                    that.triggerEvent("onDeteleFiles", that.getFiles())
                });
            }
        },
        /* 处理附件 */
        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: []
                }
            })
        },
        /* 返回数据ID数组 用来换绑数据 */
        getFiles() {
            let data = {
                    attachmentids: [],
                    list: [],
                },
                files = this.data.files;
            files.files.forEach(v => {
                data.attachmentids.push(v.attachmentid);
                data.list.push(v);
            })
            files.images.forEach(v => {
                data.attachmentids.push(v.attachmentid);
                data.list.push(v);
            })
            files.videos.forEach(v => {
                data.attachmentids.push(v.attachmentid);
                data.list.push(v);
            });
            return data
        },
        deleteAll() {
            let linksids = this.getFiles().list.map(v => v.linksid);
            if (linksids.length) _Http.basic({
                "classname": "system.attachment.Attachment",
                "method": "deleteFileLink",
                "content": {
                    linksids
                }
            }).then(res => {
                console.log("删除所有未保存附件", linksids, res)
                if (res.code != '1') return wx.showToast({
                    title: res.data,
                    icon: "none"
                });
            });
        }
    }
})