import {
  AddMenuParams,
  BaseListResult,
  BaseResult,
  EditMenuParams,
  GetMenuListParams,
  MenuListData,
  TreeMenuListData,
  TreeRoleMenuData,
} from '@models';
import {request} from './request';
import {userStore} from '@stores';

const BASE_URL = '/menu';

/** 新增菜单 */
export function addMenu(params: AddMenuParams): BaseResult {
  return request({
    method: 'POST',
    data: {...params, type: 'PC'},
    url: `${BASE_URL}/addMenu`,
  });
}

/** 获取菜单列表 */
export function getMenuList(params: GetMenuListParams): BaseListResult<MenuListData> {
  return request({
    method: 'GET',
    data: {...params, type: 'PC'},
    url: `${BASE_URL}/getPage`,
  });
}

/** 修改菜单 */
export function editMenu(params: EditMenuParams): BaseResult {
  return request({
    method: 'PUT',
    data: params,
    url: `${BASE_URL}/updateMenu`,
  });
}

/** 删除菜单 */
export function deleteMenu(id: string): BaseResult {
  return request({
    method: 'DELETE',
    data: {id},
    url: `${BASE_URL}/delMenu`,
  });
}

/** 获取树形结构菜单 */
export function getTreeMenu(): BaseResult<TreeMenuListData[]> {
  return request({
    method: 'GET',
    data: {pId: '0'},
    url: `${BASE_URL}/getMenu`,
  });
}

/** 根据角色信息获取菜单列表 */
export function getRoleMenu(): BaseResult<TreeRoleMenuData[]> {
  const uid = userStore.getState().id;

  return request({
    method: 'GET',
    data: {userId: uid},
    url: `${BASE_URL}/getUserMenu`,
  });
}

/** pda菜单查询 */

/** 获取菜单列表 */
export function getPDAMenuList(params: GetMenuListParams): BaseListResult<MenuListData> {
  return request({
    method: 'GET',
    data: {...params, type: 'PDA'},
    url: `${BASE_URL}/getPage`,
  });
}

/** 新增PDA菜单 */
export function addPDAMenu(params: AddMenuParams): BaseResult {
  return request({
    method: 'POST',
    data: {...params, type: 'PDA'},
    url: `${BASE_URL}/addMenu`,
  });
}

/** 获取菜单详情 */
export function getMenuInfo(pId: string, id: string, type: 'PC' | 'PDA'): BaseListResult<MenuListData> {
  return request({
    method: 'GET',
    data: {page: '1', limit: '1', id, pId, type},
    url: `${BASE_URL}/getPage`,
  });
}

/** 获取所有pda菜单 */
export function getAllPDAMenu(): BaseListResult<MenuListData> {
  return request({
    method: 'GET',
    data: {page: '1', limit: '10000', type: 'PDA'},
    url: `${BASE_URL}/getPage`,
  });
}
