import qs from 'query-string';
import request from '../utils/request';
import { MenuFields } from './MenuController';
import { Result, ResultList, _Pager_Params, _TimeRange_Params } from './types';

export interface RoleFields {
  roleId: string;
  roleName: string;
  roleKey: string;
  status: string;
  roleSort: string;
  remark: string;
  createTime: string;
  roleType: string; // 1 内部 2 外部
  defaultPage: string;
}
// 角色列表
interface GetList_Params extends _Pager_Params, _TimeRange_Params {
  roleName: string;
  roleKey: string;
  status: string;
}
type UserListResult = ResultList<RoleFields[]>;

export async function getListApi(params: Partial<GetList_Params>) {
  return request<Partial<UserListResult>>({
    url: '/role/list?' + qs.stringify(params),
    method: 'get',
  });
}
// 切换状态
interface _ChangeStatus_Params {
  roleId: RoleFields['roleId'];
  status: string;
}
export async function changeStatusApi(data: _ChangeStatus_Params) {
  return request({
    url: '/role/changeStatus',
    method: 'post',
    data,
  });
}
// 新增
type _Add_Params = Partial<Omit<RoleFields, 'roleId'>> & {
  menuIds: Array<string>;
  buttonIds: Array<string>;
};
export async function addApi(data: _Add_Params) {
  return request({
    url: '/role',
    method: 'post',
    data,
  });
}
// 编辑
type _Update_Params = Partial<RoleFields> & {
  menuIds: Array<string>;
  buttonIds: Array<string>;
};
export async function updateApi(data: _Update_Params) {
  return request({
    url: '/role/edit',
    method: 'post',
    data,
  });
}
// 删除
export async function delApi(roleId: RoleFields['roleId']) {
  return request({
    url: '/role/delete/' + roleId,
    method: 'get',
  });
}
// 详情
type DetailsResult = Result<RoleFields>;
export async function getDetailsApi(roleId?: RoleFields['roleId']) {
  return request<Partial<DetailsResult>>({
    url: `/role/${roleId || ''}`,
    method: 'get',
  });
}

// 授权菜单信息
type MenuDetailsResult = Result<MenuFields[]>;
export async function getMenuDetailsApi(roleId?: RoleFields['roleId']) {
  const { data } = await request<Partial<MenuDetailsResult>>({
    url: `/menu/roleMenuTreeselect/${roleId || ''}`,
    method: 'get',
  });
  return (data || []).map((item) => item.menuId || '');
}
// 授权按钮信息
type ButtonDetailsResult = Result<MenuFields[]>;
export async function getButtonDetailsApi(roleId?: RoleFields['roleId']) {
  const { data } = await request<Partial<ButtonDetailsResult>>({
    url: `/menu/roleButtonTreeselect/${roleId || ''}`,
    method: 'get',
  });
  return (data || []).map((item) => item.menuId || '');
}

// 菜单列表

interface TreeSelectFields {
  id: string;
  label: string;
  children: TreeSelectFields[];
}

export interface MenuTreeFields {
  value: string;
  label: string;
  children: MenuTreeFields[];
}

type MenuTreeListResult = Result<TreeSelectFields[]>;

export async function getMenuTreeListApi(): Promise<MenuTreeFields[]> {
  const { data } = await request<Partial<MenuTreeListResult>>({
    url: `/menu/treeselect`,
    method: 'get',
  });
  const formatTreeList = (treeList: TreeSelectFields[]): MenuTreeFields[] => {
    return treeList.reduce((ret: MenuTreeFields[], cur) => {
      const { label, id: value, children: preChildren } = cur || {};
      const children = formatTreeList(preChildren || []);
      return [...ret, { label, value, children }];
    }, []);
  };
  return formatTreeList(data || []);
}
// 按钮列表
export interface ButtonTreeFields {
  value: string;
  label: string;
  children: ButtonTreeFields[];
}

type ButtonTreeListResult = Result<TreeSelectFields[]>;

export async function getButtonTreeListApi(): Promise<ButtonTreeFields[]> {
  const { data } = await request<Partial<ButtonTreeListResult>>({
    url: `/menu/treeselectButton`,
    method: 'get',
  });

  const formatTreeList = (treeList: TreeSelectFields[]): ButtonTreeFields[] => {
    return treeList.reduce((ret: ButtonTreeFields[], cur) => {
      const { label, id: value, children: preChildren } = cur || {};
      const children = formatTreeList(preChildren || []);
      return [...ret, { label, value, children }];
    }, []);
  };
  return formatTreeList(data || []);
}
