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

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

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

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

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

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

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

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

/**
 * 重置门店账号密码
 */
export async function resetStorePassword(data: {
  id?: number;
  userId?: number;
  password: string;
}) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-store/reset-password',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 门店账号密码登录
 */
export async function storeLogin(data: {
  phone: string;
  password: string;
}) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-store/store-login',
    data
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 检查门店名称是否存在
 */
export async function checkStoreNameExist(name: string, excludeId?: number) {
  const res = await request.get<ApiResult<boolean>>(
    '/shop/shop-store/check-name',
    {
      params: { name, excludeId }
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
