const cf = require("../../utils/checkFile")
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        butType: {
            type: String,
            value: "下载"
        },
        list: {
            type: Array,
            value: []
        },
        callBack: Function,
        deleteFile: Function,
        downLoadRecord: Function
    },
    options: {
    },

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

    },

    /**
     * 组件的方法列表
     */
    methods: {
        butClick(e) {
            const {
                item,
                type
            } = e.currentTarget.dataset;
            const that = this;
            if (type == '播放') {
                this.triggerEvent("callBack", item);
            } else if (type == '删除') {
                wx.showModal({
                    title: "通知",
                    content: "是否确认删除该附件?",
                    success: res => {
                        if (res.confirm) that.triggerEvent("deleteFile", item);
                    }
                })
            } else if (type == '下载') {
                wx.downloadFile({
                    url: item.url,
                    success(res) {
                        if (res.statusCode === 200) {
                            wx.shareFileMessage({
                                filePath: res.tempFilePath,
                                fileName: item.document,
                                success(res) {
                                    console.log("转发", res)
                                },
                                fail: console.error,
                            })
                        } else {
                            wx.showToast({
                                title: '微信下载保存失败',
                                icon: "none"
                            })
                        }
                    },
                    fail() {
                        wx.showToast({
                            title: '文件下载失败',
                            icon: "none"
                        })
                    }
                });
                this.triggerEvent("downLoadRecord");
            };
        },
        callback(e) {
            const {
                item
            } = e.currentTarget.dataset;
            this.triggerEvent("callBack", item);
        },
        checkFile(e) {
            const {
                item
            } = e.currentTarget.dataset;
            if (item.fileType == 'video' && this.data.butType == '商学院' || this.data.butType == '播放') return this.triggerEvent("callBack", item);
            cf.checkFile(item);
        }
    }
})