export default {
    data() {
        return {
            uploadList: []
        }
    },
    computed: {
    },
    methods: {
        // 处理文件上传
        handleUpload() {
            // #ifdef H5
            this.handleH5Upload();
            // #endif

            // #ifdef APP-PLUS
            this.handleAppUpload();
            // #endif
        },
        // H5 平台文件上传
        handleH5Upload() {
            console.log("H5 平台文件上传")
            uni.chooseFile({
                count: 100, // 最多可以选择100个文件
                type: 'all', // 选择所有类型文件
                success: async (res) => {
                    const { tempFilePaths, tempFiles } = res;

                    if (!tempFiles || tempFiles.length === 0) {
                        return;
                    }

                    // 验证文件大小和类型
                    const validFiles = [];
                    for (let i = 0; i < tempFiles.length; i++) {
                        const filePath = tempFilePaths[i];
                        validFiles.push(filePath);
                    }

                    if (validFiles.length === 0) {
                        return;
                    }

                    try {
                        console.log("上传文件成功:1", validFiles)
                        await Promise.all(validFiles.map(filePath => this.uploadFile(filePath)));
                        this.handleUploadSuccess();
                    } catch (error) {
                        console.error('上传失败:', error);
                        uni.$u.toast('上传失败');
                    }
                },
                fail: (err) => {
                    console.error('选择文件失败:', err);
                    uni.$u.toast('选择文件失败');
                }
            });
        },

        // 上传文件
        async uploadFile(fileUrl) {
            try {
                uni.showLoading({
                    title: '上传中...',
                    mask: true
                });

                // 调用统一的上传接口
                const {
                    data
                } = await uni.$u.api.uploadFile(fileUrl);

                const fileObj = {
                    fileSize: data.fileSize,
                    fileSuffix: data.fileSuffix,
                    fileName: data.name,
                    fileUrl: data.url,
                    orderFileType: this.orderFileType
                };

                this.uploadList.push(fileObj);
                this.$emit('update:fileList', this.uploadList);
                this.$emit('change', this.uploadList);

                uni.hideLoading();

            } catch (error) {
                uni.hideLoading();
                console.error('文件上传失败:', error);
                uni.$u.toast('上传失败，请重试');
            }
        },

        // App 平台文件上传
        handleAppUpload() {
            const lemonjkFileSelect = uni.requireNativePlugin('lemonjk-FileSelect');
            //4.0.0+ 使用示例及高级筛选器配置示例
            lemonjkFileSelect.showNativePicker({
                pathScope: "/DCIM/Camera",
                mimeType: "*/*",
                utisType: "public.data",
                multi: 'yes'
            }, async result => {
                const {
                    code,
                    files
                } = result;
                if (code == 0) {
                    // 选择的是否全部为图片
                    const tempFilePaths = files.map(v => "file://" + v.filePath);
                    Promise.all(tempFilePaths.map(v => this.uploadFile(v))).then(() => {
                        this.handleUploadSuccess();
                    }).catch(() => {
                        uni.$u.toast("上传失败");
                    })
                } else if (result.code == 1001) {
                    uni.showModal({
                        title: "需要文件访问权限",
                        content: "您还未授权本应用读取文件。为保证您可以正常上传文件，请在权限设置页面打开文件访问权限（不同手机厂商表述可能略有差异）请根据自己手机品牌设置",
                        confirmText: "去授权",
                        cancelText: "算了",
                        success(e) {
                            if (e.confirm) {
                                // 跳转到应用设置页
                                lemonjkFileSelect.gotoSetting();
                            }
                        }
                    })
                }
            })
        },
    },
}