import config from "@/config";
import http from "@/utils/request";

export default {
    /** 分类列表 GET adminapi/file/category?file_type= */
    categoryList: {
        url: `${config.API_URL}/file/category`,
        name: "素材分类列表",
        get: async function (fileType, params = {}) {
            return await http.get(this.url, { file_type: fileType, ...params });
        },
    },
    /** 上级分类 GET adminapi/file/category/cate_list?file_type= */
    categoryParentList: {
        url: `${config.API_URL}/file/category/cate_list`,
        name: "素材上级分类",
        get: async function (fileType) {
            return await http.get(this.url, { file_type: fileType });
        },
    },
    /** 分类保存 POST adminapi/file/category/save/:id */
    categorySave: {
        url: `${config.API_URL}/file/category/save`,
        name: "素材分类保存",
        post: async function (id, data = {}) {
            const cid = id == null || id === "" ? 0 : id;
            return await http.post(`${this.url}/${cid}`, data);
        },
    },
    /** 分类删除 DELETE adminapi/file/category/delete/:id */
    categoryDelete: {
        url: `${config.API_URL}/file/category/delete`,
        name: "素材分类删除",
        delete: async function (id) {
            return await http.delete(`${this.url}/${id}`);
        },
    },
    /** 素材上传 POST adminapi/upload/data/1 图片，/2 视频 */
    upload: {
        url: `${config.API_URL}/upload/data`,
        name: "素材上传",
        post: async function (fileType, data, reqConfig = {}) {
            const type = String(fileType) === "2" ? "2" : "1";
            return await http.post(`${this.url}/${type}`, data, reqConfig);
        },
    },
    /** 文件列表 GET adminapi/file */
    fileList: {
        url: `${config.API_URL}/file`,
        name: "素材文件列表",
        get: async function (params = {}) {
            return await http.get(this.url, params);
        },
    },
    /** 文件删除 DELETE adminapi/file/delete body: ids 为逗号拼接字符串，如 "1,2,3" */
    fileDelete: {
        url: `${config.API_URL}/file/delete`,
        name: "素材文件删除",
        delete: async function (ids = []) {
            const list = Array.isArray(ids) ? ids : [ids];
            const data = new FormData();
            data.append("ids", list.map((id) => String(id)).join(","));
            return await http.delete(this.url, data);
        },
    },
};
