import { request } from '@/sheep/request';
import { isEmpty } from 'lodash';
import { CRUD } from '@/sheep/request/crud';

export default {
  // 系统初始化
  init: () =>
    request({
      url: 'admin/index/init',
    }),

  // 获取验证码
  captcha: () =>
    request({
      url: 'admin/index/captcha',
      method: 'GET',
    }),
  // 登录配置
  loginConfig: () =>
    request({
      url: 'admin/index/loginConfig',
    }),

  // 清空后端缓存
  clearCache: () =>
    request({
      url: 'admin/index/clearCache',
      method: 'POST',
    }),
  // 账号信息
  account: {
    // 登录
    login: (data) =>
      request({
        url: 'admin/index/login',
        method: 'POST',
        data,
        options: {
          showSuccessMessage: true,
        },
      }),
    // 登出
    logout: () =>
      request({
        url: 'admin/index/logout',
      }),
    // 个人信息
    info: () =>
      request({
        url: 'admin/index/profile',
      }),
    // 菜单权限
    rules: () =>
      request({
        url: 'admin/index/rules',
      }),
    // 更新个人信息
    update: (data) =>
      request({
        url: 'admin/index/update',
        method: 'PUT',
        data,
        options: {
          showSuccessMessage: !isEmpty(data),
        },
      }),
    // 当前管理员操作日志
    adminLog: (params) =>
      request({
        url: 'admin/index/adminLog',
        method: 'GET',
        params,
      }),
  },

  // 权限设置
  auth: {
    // 管理员
    admin: {
      ...CRUD('admin/auth/admin'),
    },
    // 角色管理
    role: {
      // 角色组
      ...CRUD('admin/auth/role'),
      select: () =>
        request({
          url: 'admin/auth/role/select',
          method: 'GET',
        }),
    },
    // 菜单权限
    access: {
      ...CRUD('admin/auth/access'),
      list: (params) =>
        request({
          url: 'admin/auth/access',
          method: 'GET',
          params,
        }),
      select: (params) =>
        request({
          url: 'admin/auth/access/select',
          method: 'GET',
          params,
        }),
    },
    adminLog: {
      ...CRUD('admin/auth/adminLog'),
    },
  },
  // 系统设置
  config: {
    // 基础配置
    basic: {
      // 站点配置
      site: (data) =>
        request({
          url: 'admin/config/basic/site',
          method: isEmpty(data) ? 'GET' : 'PUT',
          data,
          options: {
            showSuccessMessage: !isEmpty(data),
          },
        }),
      // 登录配置
      login: (data) =>
        request({
          url: 'admin/config/basic/login',
          method: isEmpty(data) ? 'GET' : 'PUT',
          data,
          options: {
            showSuccessMessage: !isEmpty(data),
          },
        }),
      // 用户配置
      user: (data) =>
        request({
          url: 'admin/config/basic/user',
          method: isEmpty(data) ? 'GET' : 'PUT',
          data,
          options: {
            showSuccessMessage: !isEmpty(data),
          },
        }),
      // 邮件配置
      mail: (data) =>
        request({
          url: 'admin/config/basic/mail',
          method: isEmpty(data) ? 'GET' : 'PUT',
          data,
          options: {
            showSuccessMessage: !isEmpty(data),
          },
        }),
    },
    //  短信配置
    easysms: (data) =>
      request({
        url: 'admin/config/easysms',
        method: isEmpty(data) ? 'GET' : 'PUT',
        data,
        options: {
          showSuccessMessage: !isEmpty(data),
        },
      }),
  },
  notification: {
    config: (id, type, data) =>
      request({
        url: `admin/notification/config/${id}${type ? '/' + type : ''}`,
        method: isEmpty(data) ? 'GET' : 'PUT',
        data,
        options: {
          showSuccessMessage: !isEmpty(data),
        },
      }),
    getTemplateId: (id, type, params) =>
      request({
        url: 'admin/notification/config/getTemplateId/' + id + '/' + type,
        method: 'GET',
        params,
      }),
    setStatus: (id, type, data) =>
      request({
        url: 'admin/notification/config/setStatus/' + id + '/' + type,
        method: 'PUT',
        data,
      }),
  },

  // 文件管理
  file: {
    // 文件列表
    list: (params) =>
      request({
        url: 'admin/file',
        method: 'GET',
        params,
      }),
    // 文件详情
    detail: (id) =>
      request({
        url: `admin/file/${id}`,
        method: 'GET',
      }),
    // 文件上传
    upload: (params, data) =>
      request({
        url: 'admin/file/upload',
        method: 'POST',
        params,
        data,
        headers: {
          'Content-Type': 'multipart/form-data',
        },
      }),
    // 删除文件
    delete: (id) =>
      request({
        url: `admin/file/${id}`,
        method: 'DELETE',
        options: {
          showSuccessMessage: true,
        },
      }),
    // 批量删除文件
    batchDelete: (ids) =>
      request({
        url: 'admin/file/batchDelete',
        method: 'POST',
        data: { ids },
        options: {
          showSuccessMessage: true,
        },
      }),
    // 移动文件到分组
    move: (data) =>
      request({
        url: 'admin/file/move',
        method: 'POST',
        data,
        options: {
          showSuccessMessage: true,
        },
      }),
    // 重命名文件
    rename: (id, data) =>
      request({
        url: `admin/file/rename/${id}`,
        method: 'PUT',
        data,
        options: {
          showSuccessMessage: true,
        },
      }),
    // 文件分组管理
    group: {
      // 分组列表
      list: () =>
        request({
          url: 'admin/file/group',
          method: 'GET',
        }),
      // 添加分组
      add: (data) =>
        request({
          url: 'admin/file/group',
          method: 'POST',
          data,
          options: {
            showSuccessMessage: true,
          },
        }),
      // 编辑分组
      edit: (id, data) =>
        request({
          url: `admin/file/group/${id}`,
          method: 'PUT',
          data,
          options: {
            showSuccessMessage: true,
          },
        }),
      // 删除分组
      delete: (id) =>
        request({
          url: `admin/file/group/${id}`,
          method: 'DELETE',
          options: {
            showSuccessMessage: true,
          },
        }),
    },
  },
};
