import { Get, Post } from "../../lib/request/method";
import { createHealthIndex, transformHealthIndex2Chart } from "./tools/health-index";
import dayjs from "dayjs";

export function healthPatientMethod() {
  return Post(`/patientInfoManage/getPatientInfoDetail`, {}, { transform({ data }) { return data } });
}

export function healthReportListMethod() {
  const transform = ({ data }: AnyObject) => {
    return Array.isArray(data) ? data.map(item => {
      return {
        id: item.healthAnalysisReportId,
        reportTime: item.time3,
        description: [item.willillStateName, item.willillDegreeName, item.willillSocialName, item.willillFunctionName].filter(Boolean).join('，')
      }
    }) : []
  }
  return Get(`/analysisManage/getHarsTid`, { transform })
}

export function healthReportMethod(id?: string) {
  const transform = ({ data }: AnyObject) => {
    if (Array.isArray(data?.conditProgram?.types)) {
      data.conditProgram.types = data.conditProgram.types.map((item: AnyObject) => (item.summary = item.summary?.replace(/null/g, '') || '', item))
    }
    return data;
  };

  return id
    ? Get(`/analysisManage/getHealRepDetailById`, { params: { healthAnalysisReportId: id }, transform })
    : Post(`/analysisManage/getLastHealRepDetail`, {}, { transform })
}

/**
 * 获取指标信息
 * @param id 健康分析报告
 */
export function healthIndexMethod(id?: string) {
  const transform = ({ data }: AnyObject) => Array.isArray(data) ? data.map(item => createHealthIndex(item)) : [];

  return id
    ? Get(`/analysisManage/getLast7Day`, { params: { healthAnalysisReportId: id }, transform })
    : Post(`/patientQuota/getCurQuoval`, {}, { transform })
}

export function healthSchemeMethod(id: string) {
  const transform = ({ data }: AnyObject) => {
    return {
      reportTime: dayjs(data?.time).format('YYYY年MM月DD日'),
      children: data?.types?.map((item: AnyObject, i: number) => {
        return {
          title: item?.type || '',
          children: item?.groups?.map((item: AnyObject, j: number) => {
            return {
              title: item?.name || '',
              descriptions: item?.attrs?.map((attr: AnyArray, k: number) => {
                return {...attr, id: `description-${i}-${j}-${k}`,}
              }) ?? [], 
              children: item?.items?.map((item: AnyObject, k: number) => {
                switch (item?.type) {
                  case 'img':
                    return {
                      id: `${item?.type}-${i}-${j}-${k}`,
                      type: item.imgUrl ? 'image' : null,
                      poster: item.imgUrl,
                      url: item.mediaUrl ?? item.imgUrl,
                      title: item.name,
                      description: item.description,
                    }
                  case 'video':
                    return {
                      id: `${item?.type}-${i}-${j}-${k}`,
                      type: item.mediaUrl ? 'video' : null,
                      poster: item.imgUrl,
                      url: item.mediaUrl,
                      title: item.name,
                      description: item.description,
                    }
                  case 'acupoint':
                    return {
                      id: `${item?.type}-${i}-${j}-${k}`,
                      type: item.imgUrl ? 'image' : item.name ? 'text' : null,
                      poster: item.imgUrl,
                      url: item.mediaUrl ?? item.imgUrl,
                      title: [item.name, `${item.doase || ''}${item.unit || ''}`].filter(Boolean).join(' '),
                      description: item.description,
                    }
                  case 'medicine':
                    return {
                      id: `${item?.type}-${i}-${j}-${k}`,
                      type: item.imgUrl ? 'image' : item.name ? 'text' : null,
                      poster: item.imgUrl,
                      url: item.mediaUrl ?? item.imgUrl,
                      title: [item.name, `${item.doase || ''}${item.unit || ''}`].filter(Boolean).join(' '),
                      description: item.description,
                    }
                  default:
                    return {
                      id: `${item?.type}-${i}-${j}-${k}`,
                      type: null
                    }
                }
              }).filter((item: AnyObject) => item.type) ?? [],
              description: item?.description,
            }
          }) ?? [],
        }
      }) ?? []
    }
  };
  return Get(`/analysisManage/getCondProgDetailById`, { params: { healthAnalysisReportId: id }, transform })
}


export function getStatusRecordMethod() {
  return Post(`/analysisManage/pageHarStatu`, {}, {
    transform({ data }: AnyObject) {
      return {
        total: data.total,
        data: data.data?.map((item: AnyObject) => {
          return {
            reportTime: dayjs(item.analysisEndTime).format('YY/MM/DD'),
            ...item
          }
        })
      };
    }
  })
}