import { request } from 'umi';
import type { UserListParams, UserListItem, UserListParamsV2, UserInfo } from './data.d';
import service from '@/services/http/service';

//查询用户列表
export async function queryUserList(params: UserListParamsV2) {

  // params.current = Number(params.current)
  // params.current = params.current ? params.current - 1 : params.current
  console.log(params)
  let result = await service.post('/api/v1/platform/list/user', {
    "user_name": params.user_name,
    "page_no": params.current,
    "page_size": params.pageSize
  });

  return Promise.resolve({
    data: result.data.users,
    success: true,
  })
}

//查询用户的关糸
// export async function queryAllRelations(params: UserListParams) {
//   let result = await service.post('/api/v1/platform/list/role', {
//     "role_name": "",
//     "page_no": params.current,
//     "page_size": params.pageSize
//   });

//   return Promise.resolve({
//     data: result.data.roles,
//     success: true,
//   })
// }


//删除用户
export async function removeUser(params: { user_name: string[] }) {
  let result = await service.post('/api/v1/platform/delete/user', {
    user_name: params.user_name[0],
  });

  return Promise.resolve({
    data: result.data,
    success: true,
  })
}

//添加用户
export async function addUser(params: UserInfo) {
  let result = await service.post('/api/v1/platform/create/user', {
    user_name: params.user_name,
    user_alias: params.user_alias,
    phone_number: params.phone_number,
    password: params.password,
    remark: params.remark,
    role_name: params.role_name,
  });

  return Promise.resolve({
    data: result.data,
    success: true,
  })
}

//更新用户
export async function updateUser(params: UserInfo) {
  let result = await service.post('/api/v1/platform/edit/user', {
    user_name: params.user_name,
    user_alias: params.user_alias,
    phone_number: params.phone_number,
    password: params.password,
    remark: params.remark,
    role_name: params.role_name,
  });

  return Promise.resolve({
    data: result.data,
    success: true,
  })
}