import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { ShopStoreUser, ShopStoreUserParam } from './model';

/**
 * 分页查询店员
 */
export async function pageShopStoreUser(params: ShopStoreUserParam) {
  const res = await request.get<ApiResult<PageResult<ShopStoreUser>>>(
    '/shop/shop-store-user/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询店员列表
 */
export async function listShopStoreUser(params?: ShopStoreUserParam) {
  const res = await request.get<ApiResult<ShopStoreUser[]>>(
    '/shop/shop-store-user',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加店员
 */
export async function addShopStoreUser(data: ShopStoreUser) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-store-user',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改店员
 */
export async function updateShopStoreUser(data: ShopStoreUser) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-store-user',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除店员
 */
export async function removeShopStoreUser(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/shop/shop-store-user/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除店员
 */
export async function removeBatchShopStoreUser(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/shop/shop-store-user/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询店员
 */
export async function getShopStoreUser(id: number) {
  const res = await request.get<ApiResult<ShopStoreUser>>(
    '/shop/shop-store-user/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据手机号查询关联用户
 */
export async function getUserByPhone(phone: string) {
  const res = await request.get<ApiResult<any>>(
    '/shop/shop-store-user/getUserByPhone',
    {
      params: { phone }
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 店员角色类型
 */
export const ROLE_TYPE_OPTIONS = [
  { label: '门店经理', value: 1 },
  { label: '店员', value: 2 }
];

/**
 * 店员状态
 */
export const STATUS_OPTIONS = [
  { label: '正常', value: 0, color: 'green' },
  { label: '禁用', value: 1, color: 'red' }
];
