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

export default {
    /** 小程序用户列表 adminapi/users/list */
    list: {
        url: `${config.API_URL}/users/list`,
        name: "用户列表",
        get: async function (params = {}) {
            return await http.get(this.url, params);
        },
    },
    setStatus: {
        url: `${config.API_URL}/users/set_status`,
        name: "用户状态",
        get: async function (id, status) {
            return await http.get(`${this.url}/${id}/${status}`);
        },
    },
    pwd: {
        url: `${config.API_URL}/user/passwd`,
        name: "-",
        post: async function (params) {
            return await http.post(this.url, params);
        },
    },
    batch: {
        url: `${config.API_URL}/user/batch`,
        name: "-",
        post: async function (params) {
            return await http.post(this.url, params);
        },
    },
    save: {
        url: `${config.API_URL}/user/save`,
        name: "-",
        post: async function (params) {
            return await http.post(this.url, params);
        },
    },
    state: {
        url: `${config.API_URL}/user/state`,
        name: "-",
        post: async function (params) {
            return await http.post(this.url, params);
        },
    },
    del: {
        url: `${config.API_URL}/user/del`,
        name: "-",
        post: async function (params) {
            return await http.post(this.url, params);
        },
    },
    /** 用户等级列表 adminapi/user/level/list */
    levelList: {
        url: `${config.API_URL}/user/level/list`,
        name: "用户等级列表",
        get: async function (params = {}) {
            return await http.get(this.url, params);
        },
    },
    /** 用户等级详情 adminapi/user/level/info/:id */
    levelInfo: {
        url: `${config.API_URL}/user/level/info`,
        name: "用户等级详情",
        get: async function (id) {
            return await http.get(`${this.url}/${id}`);
        },
    },
    /** 用户等级新增/编辑：新增 POST .../save；编辑 POST .../save/:id */
    levelSave: {
        url: `${config.API_URL}/user/level/save`,
        name: "用户等级保存",
        post: async function (data = {}) {
            const id = data.id;
            const colorRaw = data.color;
            const color = Array.isArray(colorRaw)
                ? colorRaw.map((x) => String(x ?? "").trim()).filter(Boolean)
                : [];
            const payload = {
                name: String(data.name ?? ""),
                grade: String(data.grade ?? ""),
                icon: String(data.icon ?? ""),
                explain: String(data.explain ?? ""),
                exp_num: String(data.exp_num ?? ""),
                background: String(data.background ?? ""),
                color,
            };
            const url = id != null && String(id) !== "" ? `${this.url}/${id}` : this.url;
            return await http.post(url, payload);
        },
    },
    /** 用户等级删除 adminapi/user/level/del/:id */
    levelDel: {
        url: `${config.API_URL}/user/level/del`,
        name: "用户等级删除",
        delete: async function (id) {
            return await http.delete(`${this.url}/${id}`);
        },
    },
    /** 显示开关 adminapi/user/level/set_show/:id/:show */
    levelSetShow: {
        url: `${config.API_URL}/user/level/set_show`,
        name: "用户等级显示设置",
        get: async function (id, show) {
            return await http.get(`${this.url}/${id}/${show}`);
        },
    },
}