import {
  GetNoticeListParams,
  BaseListResult,
  NoticeListData,
  SemiManufacturesAddParams,
  BaseResult,
  SemiManufacturesOutParams,
  GetWarehousingFlowingListParams,
  SemiInStreamListData,
  GetSemiManufacturesDrawList,
  SemiDrawListData,
  SemiOutStreamListData,
  GetSemiInStreamParams,
  GetSemiOutStreamListParams,
} from '@models';
import {request} from './request';
import {ENABLED_GS} from '@utils';
import {userStore} from '@stores';

const BASE_URL = '/warehousing',
  BASE_URL2 = '/askGoods';

/** 报工单列表 */
export function getNoticeList(
  data: GetNoticeListParams,
  signal?: AbortSignal,
): BaseListResult<NoticeListData> {
  return request({
    method: 'GET',
    url: `${BASE_URL}/getNotice`,
    data,
    signal,
  });
}

/** 获取报工单详情 */
export function getNoticeInfo(id: string): BaseListResult<NoticeListData> {
  return request({
    method: 'GET',
    url: `${BASE_URL}/getNotice`,
    data: {page: '1', limit: '1', id},
  });
}

/** 删除报工单 */
export function deleNotice(id: string): BaseResult {
  return request({
    method: 'DELETE',
    url: `${BASE_URL}/delInventoryNotice`,
    data: {id},
  });
}

/** 半成品入库 */
export async function semiManufacturesAdd(
  data: SemiManufacturesAddParams,
): BaseResult<string> {
  const result = await request({
    method: 'POST',
    url: `${BASE_URL}/addInventoryNotice`,
    data,
  });

  // 成功后触发成功回传
  // 触发向gs回调接口
  if (result.msg === '200' && ENABLED_GS) {
    const {realName} = userStore.getState();

    request({
      method: 'GET',
      url: '/gsPlugOut/CreateGRReqInStock',
      data: {removalCode: result.data, realName: realName ?? '李兆峰'},
      skipError: true,
    });
  }

  return result;
}

/** 半成品出库 */
export async function semiManufacturesOut(
  data: SemiManufacturesOutParams,
): BaseResult<string> {
  const result = await request({
    method: 'POST',
    url: `${BASE_URL}/updateInventoryNotice`,
    data,
  });

  // 成功后触发成功回传
  // 触发向gs回调接口
  if (result.msg === '200' && ENABLED_GS) {
    const {realName} = userStore.getState();

    request({
      method: 'GET',
      url: '/gsPlugOut/plugOutRemoval',
      data: {removalCode: result.data, realName: realName ?? '李兆峰'},
      skipError: true,
    });
  }

  return result;
}

/** 半成品入库流水单 */
export function getSemiManufacturesInStream(
  data: GetSemiInStreamParams,
  signal?: AbortSignal,
): BaseListResult<SemiInStreamListData> {
  return request({
    method: 'GET',
    url: `${BASE_URL}/getProduct`,
    data,
    signal,
  });
}

/** 半成品入库流水导出 */
export function exportSemiManufacturesInStream(
  data: GetWarehousingFlowingListParams,
): any {
  return request({
    method: 'GET',
    url: `${BASE_URL}/productExcel`,
    data,
    skipError: true,
  });
}

/** 半成品出库物料列表 */
export function getSemiManufacturesDrawList(
  data: GetSemiManufacturesDrawList,
  signal?: AbortSignal,
): BaseListResult<SemiDrawListData> {
  return request({
    method: 'GET',
    url: `${BASE_URL2}/getGoodsHalf`,
    data,
    signal,
  });
}

/** 半成品出库物料详情 */
export function getSemiManufacturesDrawInfo(
  id: string,
): BaseListResult<SemiDrawListData> {
  return request({
    method: 'GET',
    url: `${BASE_URL2}/getGoodsHalf`,
    data: {page: '1', limit: '1', id, partType: '半成品'},
  });
}
/** 半成品出库流水列表 */
export function getSemiManufacturesDrawStream(
  data: GetSemiOutStreamListParams,
  signal?: AbortSignal,
): BaseListResult<SemiOutStreamListData> {
  return request({
    method: 'GET',
    url: `${BASE_URL2}/getRemovalHalfProduct`,
    data,
    signal,
  });
}

/** 半成品出库流水导出 */
export function exportSemiManufacturesDrawStream(
  data: GetSemiOutStreamListParams,
): any {
  return request({
    method: 'GET',
    url: `${BASE_URL2}/getRemovalHalfProductExcel`,
    data,
    skipError: true,
  });
}
