import { http } from '@/utils/http/axios';
import type { PaginationResponse } from '@/types/common/type';

export interface UserType {
  userId?: number;
  password?: string;
  deptId?: number | null;
  username?: string;
  staffNo?: string;
  mobile?: string;
  isDisabled?: boolean | number;
  realname?: string;
  roleIdList?: number[];
  passwordRe?: string;
  /** 租户id */
  tenantId?: number;
}

interface AdminUserType {
  /** 昵称 */
  nickname: string;
  /** 登录用户名 */
  username: string;
  /** 部门id */
  deptId?: number;
  /** 员工编号 */
  staffNo?: string;
  /** 密码 */
  password: string;
  /** 所属租户id */
  tenantId: number;
}

export interface JobListItem {
  createdAt: string;
  id: number;
  idtJobCode: string;
  idtJobId: string;
  idtJobName: string;
  isDeleted: number;
  updatedAt: string;
}
export interface DeptListItem {
  children: [];
  createdAt: string;
  createdBy: string;
  deptName: string;
  id: number;
  isDeleted: number;
  isDisabled: number;
  orderNum: number;
  parentId: string;
  tenantId: number;
  updatedAt: string;
  updatedBy: string;
}

export interface RoleListItem {
  createdAt: string;
  createdBy: number;
  id: number;
  isDeleted: number;
  roleName: string;
  tenantId: number;
  updatedAt: string;
  updatedBy: null;
}
export interface UserLisItem {
  avatar: string;
  createdAt: string;
  createdBy: null;
  deptId: number;
  deptList: DeptListItem[];
  email: null;
  gender: null;
  id: number;
  isDeleted: number;
  isDisabled: number;
  mobile: null;
  password: string;
  realname: string;
  remark: null;
  roleList: RoleListItem[];
  roleType: number;
  staffNo: string;
  tenantId: number;
  updatedAt: string;
  updatedBy: null;
  username: string;
  certify: string;
  jobList: JobListItem[];
}

/** root用户给某个租户添加管理员 */
export function addAdminUser(params: AdminUserType) {
  return http.request({
    url: '/user/adds',
    method: 'POST',
    params,
  });
}

// 添加普通用户-交付管理员
export function addSingleUser(params: UserType) {
  return http.request(
    {
      url: '/admin/user/saveUser',
      method: 'POST',
      params,
    },
    {
      isTransformResponse: false,
    },
  );
}

//添加批量用户
export function addMultipleUser(params: File) {
  return http.request({
    url: '/user/import',
    method: 'POST',
    params,
  });
}

export interface UserTypeDel {
  deleteStatus?: string;
  deptId?: number;
  email?: string;
  isEnable?: boolean;
  mobile?: string;
  nickname?: string;
  opertor?: string;
  password?: string;
  postId?: string;
  remark?: string;
  roleIds?: string[];
  sex?: string;
  staffNo?: string;
  userId?: number;
  username?: string;
  updateTime?: string;
  createTime?: string;
  certify?: string;
}

export interface UserProps {
  userId: number;
}
//删除用户
export function delUser(params: UserProps) {
  return http.request({
    url: '/admin/user/deleteUser',
    method: 'POST',
    params,
  });
}

//修改用户
export function updateUser(params: UserType) {
  return http.request(
    {
      url: '/admin/user/updateUser',
      method: 'POST',
      params,
    },
    {
      isTransformResponse: false,
    },
  );
}

//获取单个用户信息
// export function getUser(params: string): Promise<UserType> {
//   return http.request({
//     url: '/user/info',
//     method: 'get',
//     params,
//   });
// }

export interface UserList {
  pageNumber: number;
  totalPage: number;
  pageSize: number;
  page: number;
  records: UserTypeDel[];
  totalCount: number;
  pageNum: number;
}

export interface QueryUser {
  staffNo?: string; //工号
  realname?: string; //姓名
  mobile?: string; //手机号
  roleName?: string; //角色
  deptName?: string; //组织
  pageNumber?: number; //当前页数
  pageSize?: number; //每页个数
  deptId?: string;
  roleId?: string;
  userName: string; //登录用户
}

export interface UserPramas {
  realname?: string; // 用户姓名
  mobile?: string; // 手机号
  staffNo?: string; // 工号
  deptId?: number; // 部门
  roleId?: number; // 角色id
}

export interface QueryUserPramas {
  pageNumber: number; //当前页数
  pageSize: number; //每页个数
  queryParam: UserPramas;
}

//获取单个用户信息
export function getUserList(params: QueryUserPramas | null): Promise<PaginationResponse<UserLisItem>> {
  return http.request({
    url: '/admin/user/queryUserPage',
    method: 'post',
    params,
  });
}

/* V4: 修改密码参数 */
interface ResetCodeType {
  userId: number;
  newPwd: string;
}

// V4: 修改密码
export function resetCode(data: ResetCodeType) {
  return http.request({
    url: '/admin/user/changePassword',
    method: 'POST',
    data,
  });
}

export interface AddTenantAdminProps {
  username: string;
  realname: string;
  roleIdList: number[];
  isDisabled: boolean | number;
  password: string;
  userId?: number;
  deptId?: number;
  passwordRe?: string;
}

// V4: 添加租户管理员
export function addTenantAdmin(data: AddTenantAdminProps) {
  return http.request({
    url: 'admin/user/saveTenantAdmin',
    method: 'POST',
    data,
  });
}

// V4: 删除用户
export function deleteUser(params: UserProps) {
  return http.request(
    {
      url: `admin/user/deleteUser?userId=${params.userId}`,
      method: 'DELETE',
    },
    {
      isTransformResponse: false,
    },
  );
}
