import qs from 'query-string';
import request from '../utils/request';
import { ButtonFields } from './ButtonController';
import { SystemConfigFields } from './ConfigurationController';
import { DeptFields } from './DeptController';
import { MenuFields } from './MenuController';
import { OrgFields } from './OrgController';
import { RoleFields } from './RoleController';
import { UserFields } from './UserController';
import {
  AppFields,
  DoctorFields,
  Result,
  ResultList,
  SelectOptionFields,
  _Pager_Params,
} from './types';

// 获取平台配置信息
export async function getConfigurationApi() {
  const { data } = await request<Result<SystemConfigFields>>({
    url: '/config/system/systemConfig',
    method: 'get',
  });
  return data;
}
interface medicalInfoFields {
  userName: string;
  orgName: string;
  deptName: string;
  orgType: string;
  orgCode: string;
  regionCode: string;
}
// 获取用户信息
export interface UserInfoResult extends Result {
  permissions?: string[];
  roles?: string;
  user?: UserFields;
  medicalInfo?: medicalInfoFields;
}

export async function getUserInfoApi() {
  return await request<Partial<UserInfoResult>>({
    url: '/user/getInfo',
    method: 'get',
    skipErrorHandler: true,
  });
}

// 登出
export async function logoutApi() {
  return request({
    url: '/logout',
    method: 'delete',
  });
}

// 菜单列表
type MenuListResult = Result<MenuFields[]>;
export async function getMenuListApi(appId: AppFields['appId']) {
  const params = {
    appId,
  };
  const { data } = await request<Partial<MenuListResult>>({
    url: '/menu/getRouters?' + qs.stringify(params),
    method: 'get',
  });
  return data || [];
}

// 权限按钮列表
type ButtonListResult = Result<MenuFields[]>;
export async function getButtonListApi(appId: AppFields['appId']) {
  const params = {
    appId,
  };
  const { data } = await request<ButtonListResult>({
    url: '/menu/getButtons?' + qs.stringify(params),
    method: 'get',
  });
  return (data || []).map((item) => item.perms).filter(Boolean);
}

// 获取应用列表
type AppListResult = Result<AppFields[]>;

export async function getAppListApi() {
  const params = {
    pageSize: 9999,
    pageNum: 1,
    status: '1',
  };
  const appListRes = await request<Partial<AppListResult>>({
    url: '/app/list?' + qs.stringify(params),
    method: 'get',
  });
  const { data } = appListRes || {};
  return data || [];
}

// 重置密码
interface _ResetPassword_Params {
  userId: UserFields['userId'];
  password: string;
}
export async function resetPasswordApi(data: _ResetPassword_Params) {
  return request({
    url: '/user/resetPwd',
    method: 'post',
    data,
  });
}

// 医生下拉列表
interface GetDocList_params extends _Pager_Params {
  status: string;
}
export interface DocOptionFields extends SelectOptionFields {
  idno: string;
}

export async function getDocSelectApi(
  params?: Partial<GetDocList_params>,
): Promise<DocOptionFields[]> {
  const newParams = {
    pageSize: 99999,
    pageNum: 1,
    docStatus: '1',
    ...params,
  };
  const { data } = await request<Partial<ResultList<DoctorFields[]>>>({
    url: '/doctor/list',
    method: 'get',
    params: newParams,
  });
  return (data || []).map((item) => {
    const { name: label = '', idno = '', uuid: value = '' } = item || {};
    return {
      value,
      label,
      idno,
    };
  });
}
// 应用下拉列表
interface GetAppList_params extends _Pager_Params {
  status: string;
}
export const getAppSelectApi = async (params?: Partial<GetAppList_params>) => {
  const newParams = {
    pageNum: 1,
    pageSize: 9999,
    status: '1',
    ...params,
  };
  const { data } = await request<ResultList<AppFields[]>>({
    url: '/app/list',
    method: 'get',
    params: newParams,
  });
  const selectAppList = (data || []).map((row) => {
    const { appId: value = '', appName: label = '' } = row;
    return {
      value,
      label,
    };
  });
  return selectAppList;
};

// 角色下拉列表
interface GetRoleList_params extends _Pager_Params {
  status: string;
}
export async function getRoleSelectApi(params: Partial<GetRoleList_params>) {
  const newParams = {
    pageNum: 1,
    pageSize: 9999,
    status: '1',
    ...params,
  };
  const { data } = await request<ResultList<RoleFields[]>>({
    url: '/role/list',
    method: 'get',
    params: newParams,
  });
  return (data || []).map((item) => {
    const { roleId: value = '', roleName: label = '' } = item;
    return { value, label };
  });
}

// 按钮下拉
interface GetRoleList_params extends _Pager_Params, ButtonFields {}
export async function getButtonSelectApi(params: Partial<GetRoleList_params>) {
  const newParams = {
    pageNum: 1,
    pageSize: 9999,
    status: '1',
    ...params,
  };
  const { data } = await request<ResultList<ButtonFields[]>>({
    url: '/button/list',
    method: 'get',
    params: newParams,
  });
  return (data || []).map((item) => {
    const { perms: value = '', buttonName: label = '' } = item;
    return { value, label };
  });
}

// 机构下拉
export async function getOrgOptionsApi(
  params?: string,
): Promise<SelectOptionFields[]> {
  const orgStatus = params ? '' : '1';
  const { data } = await request<Result<OrgFields[]>>({
    url: '/orginfo/list',
    method: 'get',
    params: { pageSize: 99999, pageNum: 1, orgStatus },
  });
  return (data || []).map((item) => {
    const { orgCode: value, orgName: label } = item || {};
    return { value, label };
  });
}

// 科室下拉
interface GetDeptOptions_Params {
  orgCode?: string;
  type?: 'clear';
  all?: string;
}
export async function getDeptOptionsApi(
  params?: Partial<GetDeptOptions_Params>,
): Promise<SelectOptionFields[]> {
  if (params?.type === 'clear') {
    return [];
  }

  // const deptStatus = params && params.all ? '' :
  const { data } = await request<Result<DeptFields[]>>({
    url: '/department/list',
    method: 'get',
    params: {
      pageSize: 99999,
      pageNum: 1,
      deptStatus: '1',
      ...params,
    },
  });
  const deptList = (data || []).map((item) => {
    const { insDeptCode: value, deptName: label } = item || {};
    return { value, label };
  });
  return deptList;
}

// 获取平台设置下的字典数据

interface SelectionProps {
  code: string;
  value: string;
}

export async function getSelectionsApi(
  dictType: string,
  searchAll: boolean = true,
): Promise<SelectOptionFields[]> {
  const { data } = await request<Result<SelectionProps[]>>({
    url: `/dict/data/getSimpleData`,
    method: 'post',
    params: {
      searchAll,
      dictType,
    },
  });
  return (data || []).map((item) => {
    const { value: label, code: value } = item || {};
    return {
      value,
      label,
    };
  });
}

export const updatePass = async (data: FormData) => {
  return await request<Result>({
    url: '/user/changePass',
    method: 'post',
    data,
  });
};
