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: {
    ...CRUD('user'),
    // 登录
    login: (data) =>
      request({
        url: 'login/login',
        method: 'POST',
        data,
        options: {
          showSuccessMessage: false,
        },
      }),
    // 登出
    logout: () =>
      request({
        url: 'admin/index/logout',
      }),
    // 个人信息
    info: () =>
      request({
        url: 'user/detail',
        method: 'POST',
        data,
        options: {
          showSuccessMessage: true,
        },
      }),
    queryMenu: () =>
      request({
        url: 'login/queryMenu',
        method: 'GET',
      }),
    // 菜单权限
    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'),
    },
    // 用户管理
    user: {
      ...CRUD('user'),
      // 用户列表
      userList: (data) =>
        request({
          url: 'user/list',
          method: 'POST',
          data,
        }),
      // 用户状态切换
      status: (id) =>
        request({
          url: '/user/status',
          method: 'POST',
          params: { id },
        }),
      // 修改密码
      restpwd: (data) =>
        request({
          url: '/user/restpwd',
          method: 'POST',
          data,
        }),
    },
    // 角色管理
    role: {
      // 角色组
      ...CRUD('role'),
      // 更新个人信息
      roleList: (data) =>
        request({
          url: 'role/list',
          method: 'POST',
          data,
        }),
      select: () =>
        request({
          url: 'admin/auth/role/select',
          method: 'GET',
        }),
    },
    // 菜单权限
    access: {
      ...CRUD('permission'),
      list: (params) =>
        request({
          url: 'admin/auth/access',
          method: 'GET',
          params,
        }),
      select: (params) =>
        request({
          url: 'admin/auth/access/select',
          method: 'GET',
          params,
        }),
      // 获取权限树
      getTree: () =>
        request({
          url: 'permission/getTree',
          method: 'GET',
        }),
    },
    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: '/operating/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,
          },
        }),
    },
  },

  // 轮播图管理
  banner: {
    ...CRUD('/mall/banner'),
  },

  // 支付配置管理
  payment: {
    // 支付方式管理
    method: {
      // 获取支付方式列表
      list: (params) =>
        request({
          url: '/cif/payment/method/list',
          method: 'POST',
          data: params,
        }),
      midUpdate: (data) =>
        request({
          url: '/cif/mid/channel/method/update',
          method: 'POST',
          data,
        }),
      // 更新支付方式
      update: (data) =>
        request({
          url: '/cif/payment/method/update',
          method: 'POST',
          data,
        }),
    },

    // 支付通道管理
    channel: {
      // 获取支付通道列表
      list: (params) =>
        request({
          url: '/cif/payment/channel/list',
          method: 'POST',
          data: params,
        }),

      // 新增支付通道
      add: (data) =>
        request({
          url: '/cif/payment/channel/add',
          method: 'POST',
          data,
        }),

      // 更新支付通道
      update: (data) =>
        request({
          url: '/cif/payment/channel/update',
          method: 'POST',
          data,
        }),

      // 删除支付通道（软删除）
      delete: (id) =>
        request({
          url: '/cif/payment/channel/update',
          method: 'POST',
          data: { id, isValid: 0 },
        }),
    },

    // 支付限额管理
    editPaymentLimit: (paymentId, data) =>
      request({
        url: `/cif/goods/editInterval/${paymentId}`,
        method: 'PATCH',
        data,
      }),

    // 金额商品管理
    goods: {
      // 获取商品列表
      list: (data) =>
        request({
          url: '/cif/goods/list',
          method: 'POST',
          data,
        }),

      // 新增商品
      add: (data) =>
        request({
          url: '/cif/goods/add',
          method: 'POST',
          data,
        }),

      // 更新商品
      update: (data) =>
        request({
          url: '/cif/goods/update',
          method: 'POST',
          data,
        }),

      // 删除商品
      delete: (id) =>
        request({
          url: '/cif/goods/delete',
          method: 'GET',
          params: { id },
        }),

      // 获取商品详情
      detail: (id) =>
        request({
          url: '/cif/goods/detail',
          method: 'GET',
          params: { id },
        }),
    },
  },
};
