import type { List } from '@/model';
import type { SupplierQuery, SupplierModel, SystemItemModel, SystemIteQuery, SystemCwModel, OpenConditioningSchemeModel, ConditioningSchemeModel } from '@/model/care.model';
import request from '@/request/alova';

// 供应商搜索列表
export function supplierMethod(page: number, size: number, query?: SupplierQuery) {
  return request.Post<List<SupplierModel>>('/fdhb-pc/conditioningManage/supplier/pageCpSupplier', query ?? {}, {
    hitSource: /plan$/, // 匹配失效源
    params: { pageNum: page, pageSize: size },
  });
}

// 新增和编辑供应商
export function supplierEditMethod(data: Partial<SupplierModel>) {
  return data?.id
    ? request.Post(`/fdhb-pc/conditioningManage/supplier/updateCpSupplier`, { ...data, id: data.id }, { name: 'edit-supplier' })
    : request.Post(`/fdhb-pc/conditioningManage/supplier/addCpSupplier`, { ...data }, { name: 'edit-supplier' });
}

// 删除供应商
export function supplierDeleteMethod(data: Partial<SupplierModel>) {
  return request.Post(`/fdhb-pc/conditioningManage/supplier/deleteCpSupplier/${data.id}`, {
    name: 'update-supplier',
    cacheFor: null,
  });
}

//系统项目列表
export function pageSystemCpMethod(page: number, size: number, query?: SystemIteQuery) {
  return request.Post<List<SystemItemModel>>('/fdhb-pc/conditioningManage/program/pageSystemCp', query ?? {}, {
    hitSource: /system-cp$/, // 系统项目专用缓存标识
    params: { pageNum: page, pageSize: size },
  });
}
// 获取所有的系统项目
export function getAllSystemCpMethod() {
  return request.Post(
    '/fdhb-pc/conditioningManage/program/getAllSystemCp',
    {},
    {
      name: 'get-all-system-cp',
      cacheFor: null,
    }
  );
}
// 新增和编辑系统项目和新增编辑项目列表。  项目列表就是机构项目
export function systemCpEditMethod(data: Partial<SystemItemModel>) {
  if (data.isType === 'itemsList') {
    delete data.id;
  }
  if (data.addType === 'system') {
    return data?.id
      ? request.Post(`/fdhb-pc/conditioningManage/program/updateSystemCp`, { ...data, id: data.id }, { name: 'edit-system-cp' })
      : request.Post(`/fdhb-pc/conditioningManage/program/addSystemCp`, { ...data }, { name: 'edit-system-cp' });
  } else if (data.addType === 'itemsList') {
    return data?.id
      ? request.Post(`/fdhb-pc/conditioningManage/program/updateInstitutionCp`, { ...data, id: data.id }, { name: 'edit-system-cp' })
      : request.Post(`/fdhb-pc/conditioningManage/program/addInstitutionCp`, { ...data }, { name: 'edit-system-cp' });
  }
}

// 删除系统项目
export function deleteSystemCpMethod(data: Partial<SystemItemModel>) {
  return request.Post(`/fdhb-pc/conditioningManage/program/deleteSystemCp/${data.id}`, {
    name: 'delete-system-cp',
    cacheFor: null,
  });
}

// 选择引入调理方案
export function selectSystemCpMethod(data: Array<string>) {
  return request.Post(`/fdhb-pc/conditioningManage/program/copyCp`, [...data], {
    name: 'select-system-cp',
    cacheFor: null,
  });
}

// 机构待确认列表
export function pageOrgConfirmMethod(page: number, size: number, query?: SystemIteQuery) {
  return request.Post<List<SystemItemModel>>('/fdhb-pc/conditioningManage/program/pageUnconfirmedCp', query ?? {}, {
    hitSource: /org-confirm$/, // 待确认项目专用缓存标识
    params: { pageNum: page, pageSize: size },
  });
}
// 待确认列表删除
export function deleteOrgConfirmMethod(data: Partial<SystemItemModel>) {
  return request.Post(`/fdhb-pc/conditioningManage/program/deleteUnconfirmedCp/${data.id}`, {
    name: 'delete-org-confirm',
    cacheFor: null,
  });
}
// 机构待确认列表确认
export function confirmOrgConfirmMethod(data: Partial<SystemItemModel>) {
  return request.Post(
    `/fdhb-pc/conditioningManage/program/confirmedCp`,
    { ...data },
    {
      name: 'confirm-org-confirm',
      cacheFor: null,
    }
  );
}

// 已确认项目列表
export function pageConfirmedCpMethod(page: number, size: number, query?: SystemIteQuery) {
  return request.Post('/fdhb-pc/conditioningManage/program/pageInstitutionCp', query ?? {}, {
    hitSource: /plan$/, // 匹配失效源
    params: { pageNum: page, pageSize: size },
  });
}
// 获取服务包内容里的列表
export function getCpContentListMethod() {
  return request.Post(`/fdhb-pc/conditioningManage/program/getAllInstitutionCp`, {});
}
// 已确认项目列表删除
export function deleteConfirmedCpMethod(data: Partial<SystemItemModel>) {
  return request.Post(`/fdhb-pc/conditioningManage/program/deleteInstitutionCp/${data.id}`, {
    name: 'delete-confirmed-cp',
    cacheFor: null,
  });
}
//  获取所有的供应商
export function getAllSupplierMethod(data: Record<string, any>) {
  return request.Post(
    '/fdhb-pc/conditioningManage/supplier/getAllCpSupplier',
    { ...data },
    {
      name: 'get-all-supplier',
      cacheFor: null,
    }
  );
}

// 获取系统调理包列表
export function getSystemCpListMethod(page: number, size: number, query?: SystemIteQuery) {
  return request.Post<List<SystemCwModel>>('/fdhb-pc/conditioningManage/wrap/pageSystemCw', query ?? {}, {
    hitSource: /plan$/, // 匹配失效源
    params: { pageNum: page, pageSize: size },
    name: 'get-system-cp-list',
    cacheFor: null,
  });
}
// 删除系统调理包
export function deleteSystemCwMethod(data: Partial<SystemCwModel>) {
  return request.Post(`/fdhb-pc/conditioningManage/wrap/deleteSystemCw/${data.id}`, {
    name: 'delete-system-cw',
    cacheFor: null,
  });
}
// 新增和编辑系统调理包
export function addSystemCwMethod(data: Partial<SystemCwModel>) {
  return data?.id
    ? request.Post(`/fdhb-pc/conditioningManage/wrap/updateSystemCw`, { ...data, id: data.id }, { name: 'edit-system-cw' })
    : request.Post(`/fdhb-pc/conditioningManage/wrap/addSystemCw`, { ...data }, { name: 'add-system-cw' });
}
// 获取系统调理包详情
export function getSystemCwDetailMethod(data: Partial<SystemCwModel>) {
  return request.Post(`/fdhb-pc/conditioningManage/wrap/getCwDetailById/${data.id}`, {
    name: 'get-system-cw-detail',
    cacheFor: null,
  });
}
// 根据调理方案id获取调理方案详情
export function getConditioningSchemeDetailMethod(data: Partial<SystemCwModel>) {
  return request.Post(`/fdhb-pc/conditioningManage/program/getCpDetailById/${data.id || data.sourceId}`, {
    name: 'get-system-cw-detail',
    cacheFor: null,
  });
}
// 疾病分页查查询
export function pageMedicineMethod(page: number, size: number, query?: Record<string, any>) {
  return request.Post(
    `/fdhb-pc/common/pageDiagnoseDisease`,
    { page, limit: size, keyWord: query?.keyword, ...query },
    {
      transform(data: any, headers) {
        return {
          data: data.records,
          total: data.total,
        };
      },
    }
  );
}
// 证型分页查询
export function pageDiagnoseTypeMethod(page: number, size: number, query?: Record<string, any>) {
  return request.Post(
    `/fdhb-pc/common/pageDiagnoseSyndrome`,
    { page, limit: size, keyWord: query?.keyword, ...query },
    {
      transform(data: any, headers) {
        return {
          data: data.records,
          total: data.total,
        };
      },
    }
  );
}
// 症状分页查询
export function pageDiagnoseSymptomMethod(page: number, size: number, query?: Record<string, any>) {
  return request.Post(
    `/fdhb-pc/common/pageDiagnoseSymptom`,
    { page, limit: size, keyWord: query?.keyword, ...query },
    {
      transform(data: any, headers) {
        return {
          data: data.records,
          total: data.total,
        };
      },
    }
  );
}
// 穴位分页查询
export function pageAcupointMethod(page: number, size: number, query?: Record<string, any>) {
  return request.Post(
    `/fdhb-pc/common/pageAcuPoint`,
    { page, limit: size, keyWord: query?.keyword, ...query },
    {
      transform(data: any, headers) {
        return {
          data: data.records,
          total: data.total,
        };
      },
    }
  );
}
// 经络分页查询
export function pageMeridianMethod(page: number, size: number, query?: Record<string, any>) {
  return request.Post(
    `/fdhb-pc/common/pageAcuMeridian`,
    { page, limit: size, keyWord: query?.keyword, ...query },
    {
      transform(data: any, headers) {
        return {
          data: data.records,
          total: data.total,
        };
      },
    }
  );
}
// 体质分页查询
export function pagePhysicalTypeMethod(page: number, size: number, query?: Record<string, any>) {
  return request.Post(
    `/fdhb-pc/common/pagePhysicalType`,
    { page, limit: size, keyWord: query?.keyword, ...query },
    {
      transform(data: any, headers) {
        return {
          data: data.records,
          total: data.total,
        };
      },
    }
  );
}
// 机构调理包分页列表
export function pageOrgCwMethod(page: number, size: number, query?: Record<string, any>) {
  return request.Post<List<SupplierModel>>('/fdhb-pc/conditioningManage/wrap/pageInstitutionCw', query ?? {}, {
    hitSource: /plan$/, // 匹配失效源
    params: { pageNum: page, pageSize: size },
  });
}
// export function pageOrgCwMethod(page: number, size: number, query?: Record<string, any>) {
//   return request.Post<List<SupplierModel>>('/fdhb-pc/conditioningManage/wrap/pageInstitutionCw', query ?? {}, {
//     hitSource: /plan$/, // 匹配失效源
//     params: { pageNum: page, pageSize: size },
//   });
// }
// 新增机构调理包
export function addOrgCwMethod(data: Partial<SystemCwModel>) {
  return data?.id
    ? request.Post(`/fdhb-pc/conditioningManage/wrap/updateInstitutionCw`, { ...data, id: data.id }, { name: 'edit-org-cw' })
    : request.Post(`/fdhb-pc/conditioningManage/wrap/addInstitutionCw`, { ...data }, { name: 'add-org-cw' });
}
// 删除机构调理包
export function deleteOrgCwMethod(data: Partial<SystemCwModel>) {
  return request.Post(`/fdhb-pc/conditioningManage/wrap/deleteInstitutionCw/${data.id}`, {
    name: 'delete-org-cw',
    cacheFor: null,
  });
}

// 为患者开具调理方案
export function addConditioningSchemeMethod(data: Partial<OpenConditioningSchemeModel>) {
  return request.Post(`/fdhb-pc/patientCrManage/addPcr`, { ...data }, { name: 'add-conditioning-record' });
}

// 根据调理包id获取调理包详情
export function getConditioningRecordDetailMethod(data: Partial<SystemCwModel>) {
  console.log('data===types', data.types,"data数据",data);
  if (data.types === 'institution' || data.types === 'system') {
    // 机构调理包详情
    return request.Post(`/fdhb-pc/conditioningManage/wrap/getCwDetailById/${data.id}`, {
      name: 'get-conditioning-record-detail',
      cacheFor: null,
    });
  } else if (data.types === 'record') {
    return request.Post(`/fdhb-pc/patientCrManage/getPcrDetailById/${data.id}`, {
      name: 'get-conditioning-record-detail',
      cacheFor: null,
    });
  }
}
// 获取的返回的机构服务包内容
export function getCopyCwMethod(data: Partial<SystemCwModel>) {
  return request.Post(`/fdhb-pc/conditioningManage/wrap/copyCw/${data.id}/${data.institutionId}`, {
    name: 'get-copy-cw',
    cacheFor: null,
  });
}

// 获取患者列表
export function getPatientListMethod(query?: Record<string, any>) {
  return request.Post<List<SystemCwModel>>('/fdhb-pc/patientCrManage/todayPcrs', query ?? {}, {
    hitSource: /plan$/, // 匹配失效源
  });
}

// 调理记录分页列表
export function getConditioningRecordMethod(page: number, size: number, query?: Record<string, any>) {
  return request.Post<List<SystemCwModel>>('/fdhb-pc/patientCrManage/pagePcrs', query ?? {}, {
    hitSource: /plan$/, // 匹配失效源
    params: { pageNum: page, pageSize: size },
  });
}
// 根据调理方案id获取调理记录详情
export function getCpDetailMethod(data: Partial<SystemCwModel>) {
  return request.Post(`/fdhb-pc/patientCrManage/getPcrDetailById/${data.id}`, {
    name: 'get-conditioning-record-detail',
    cacheFor: null,
  });
}
//  获取所有的机构服务包
export function getAllOrgCwMethod(query?: Record<string, any>) {
  return request.Post('/fdhb-pc/conditioningManage/wrap/getAllInstitutionCw', query ?? {}, {
    name: 'get-all-org-cw',
    cacheFor: null,
  });
}
// 患者(已开具调理方案)调理记录列表
export function getPatientConditioningRecordMethod(id: number) {
  return request.Post(`/fdhb-pc/patientCrManage/getPcrs/${id}`, {
    name: 'get-conditioning-record-detail',
    cacheFor: null,
  });
}
// 作废患者调理方案
export function voidConditioningSchemeMethod(id: number) {
  return request.Post(`/fdhb-pc/patientCrManage/repealPcr/${id}`, {
    name: 'void-conditioning-scheme',
    cacheFor: null,
  });
}
// 获取省市区
export function getProvinceMethod() {
  return request.Get('/fdhb-pc/region/regionCascadeProvince', {
    name: 'get-province',
    cacheFor: null,
  });
}
// 获取市
export function getCityMethod(name: string, provincecode: string) {
  return request.Get('/fdhb-pc/region/regionCascadeCity', {
    name: 'get-city',
    cacheFor: null,
    params: { name, provincecode },
  });
}
// 获取区
export function getAreaMethod(name: string, citycode: string) {
  return request.Get('/fdhb-pc/region/regionCascadeArea', {
    name: 'get-area',
    cacheFor: null,
    params: { name, citycode },
  });
}
// 根据调理记录ID获取调理过程
export function getConditioningProcessMethod(id: number) {
  return request.Post(`/fdhb-pc/patientCrManage/getPcrProcessById/${id}`, {
    name: 'get-conditioning-process',
    cacheFor: null,
  });
}
// 获取可用的服务包
export function getAvailableCwMethod(patientId: string, patientConditioningRecordId: string) {
  return request.Post(
    '/fdhb-pc/patientCrManage/getPatUsableCw',
    { patientId, patientConditioningRecordId },
    {
      name: 'get-available-cw',
      cacheFor: null,
    }
  );
}

// 调理方案配置分页列表
export function getConditioningSchemeMethod(page: number, size: number, query?: Record<string, any>) {
  return request.Post<List<ConditioningSchemeModel>>('/fdhb-pc/conditioningManage/config/pageCpConfig', query ?? {}, {
    hitSource: /plan$/, // 匹配失效源
    params: { pageNum: page, pageSize: size },
  });
}
// 更新调理方案配置
export function updateConditioningSchemeMethod(insId: string | number, data: any) {
  return request.Post(`/fdhb-pc/conditioningManage/config/updateCpConfig/${insId}`, data, {
    name: 'update-conditioning-scheme',
    cacheFor: null,
  });
}
// 根据调理方案id获取调理方案配置详情
export function getConditioningDeviceDetailMethod(data: Partial<ConditioningSchemeModel>) {
  // return request.Post(`/fdhb-pc/conditioningManage/config/getCpConfigDetailById/${data.id || 0}/${data.orgId}`, {
  return request.Post(`/fdhb-pc/conditioningManage/config/getCpConfigDetailById/${data.insId}`, {
    name: 'get-conditioning-scheme-detail',
    cacheFor: null,
  });
}
// 根据机构id获取机构调理方案配置中某种类型下可供选择的项目
export function getConditioningSchemeItemsMethod(data: Partial<ConditioningSchemeModel>) {
  return request.Post(
    `/fdhb-pc/conditioningManage/config/getOptionalCps/${data.insId}`,
    {},
    {
      name: 'get-conditioning-scheme-items',
      cacheFor: null,
      params: { conditioningProgramType: data.conditioningProgramType ?? '' },
    }
  );
}
