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

import { healthAnalysisModel, healthReportModel } from "./model/health.model";

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, scene, ...query }: Record<'id' | 'scene', string | void>): Promise<ReturnType<typeof healthReportModel>> {
  const transform = ({ data }: AnyObject) => healthReportModel(data);

  let params = { ...query } as any;
  if (id) { params.healthAnalysisReportId = id; }

  if (scene) {
    params.scene = decodeURIComponent(scene);
    return Get(`/analysisManage/getHealRepDetailByScene`, { params, transform });
  }

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

export function healthAnalysisMethod({ id, scene, ...query }: Record<'id' | 'scene', string | void>): Promise<ReturnType<typeof healthAnalysisModel>> {
  const transform = ({ data }: AnyObject) => healthAnalysisModel(data);
  let params = { ...query } as any;
  if (id) { params.tonguefaceAnalysisReportId = id; }

  if (scene) {
    params.scene = decodeURIComponent(scene);
    return Get(`/analysisManage/getTofRepDetailByScene`, { params, transform });
  }
  return id
    ? Get(`/analysisManage/getTofRepDetailById`, { params, transform })
    : Post(`/analysisManage/getLastTofRepDetail`, {}, { params, transform })
}

export function healthAnalysisListMethod() {
  const transform = ({ data }: AnyObject) => {
    return Array.isArray(data) ? data.map(item => {
      return {
        id: item.id,
        reportTime: item.time3,
        description: item.content || '',
      }
    }) : []
  }
  return Get(`/analysisManage/getTofsTid`, { 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 {
              buyUrl: item?.buyUrl || '',
              buyType: item?.buyType || '',
              shortImageUrl: item?.miniprogram || '',
              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
          }
        })
      };
    }
  })
}
// 获取随访评估列表
export function getFollowEvaluationListMethod() {
  return Post(`/followupTaskManage/pageFollowupTaskGroup`, {}, {
    transform({ data }: AnyObject) {
      return data;
    }
  })
}
// 获取随访记录列表
export function getFollowRecordListMethod() {
  return Post(`/followupTaskManage/pageFollowupTask`, {}, {
    transform({ data }: AnyObject) {
      return data;
    }
  })
}

// 获取调养记录列表 
export function getCareRecordListMethod(status: string) {
  return Post(`/patientCrManage/pagePcrs`, {status}, {
    transform({ data }: AnyObject) {
      return data;
    }
  })
}

// 线下非药物治疗
export function getOfflineTreatmentListMethod(progress: string) {
  return Post(`/patientCrManage/pagePofflineCp`, {progress}, {
    transform({ data }: AnyObject) {
      return data;
    }
  })
}

// 核销记录
// export function getVerifyRecordMethod(id: number) {
//   console.log(id, "核销记录");
//   // return Post(`/patientCrManage/pofflineCp`, {id}, {
//   //   transform({ data }: AnyObject) {
//   //     return data;
//   //   }
//   // })
// }

// 获取调理中的调理记录
export function getCareRecordMethod() {
  return Post(`/patientCrManage/getPcing`, {}, {
    transform({ data }: AnyObject) {
      return data;
    }
  })
}



