import { Get, Post } from "../../lib/request/method";

export function getNotDealLists() {
  return Post(
    "/surplus/getPatientPendingWorks",
    {},
    {
      transform({ data }: AnyObject) {
        return Array.isArray(data)
          ? data.map((item) => ({
              ...item,
            }))
          : [];
      },
    }
  );
}
// 获取调养计划
export function getCareList() {
  return Post(
    "/patientCrManage/getCurrentPcrs",
    {},
    {
      transform({ data }: AnyObject) {
        return data;
      },
    }
  );
}

export function getPatients(id?: string) {
  id ??= wx.getStorageSync("patientId");
  const transform = ({ data }: AnyObject) => {
    const patient =
      void 0 ??
      data.find((item: AnyObject) => item.patientId == id) ??
      data.find((item: AnyObject) => item.isDefault?.toUpperCase() === "Y") ??
      data[0];

    return { patient, patients: data };
  };
  return Post("/mobileAccountManage/getPatsByAid", {}, { transform }).then(
    (res) => {
      const patientId = res.patient?.patientId;
      if (patientId) wx.setStorageSync("patientId", patientId);
      else wx.removeStorageSync("patientId");
      return res;
    }
  );
}

export async function getPatientDescription(patient: {
  sex: "0" | "1";
  patientId: string;
}) {
  if (patient.sex === "0") return "";
  try {
    const transform = ({ data }: any) => data;
    const dict = await Get("/dict/getDicts", { transform });
    const options = dict.find(
      (item: any) => item.dictType === "women_special_period"
    ).items;
    const { womenSpecialPeriod } = await Post(
      "/patientInfoManage/getPatientInfoDetail",
      {},
      { transform }
    );
    if (womenSpecialPeriod)
      return womenSpecialPeriod
        .split(",")
        .map((k: string) => {
          const [key, value] = k.split(":");
          const label = options.find(
            (option: any) => option.dictValue === key
          ).dictLabel;
          return value ?? label !== "无" ? label : "";
        })
        .filter(Boolean)
        .join(",");
  } catch (error) {
    return "";
  }
}

export function healthReportMethod() {
  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 Post(`/analysisManage/getLastHealRepDetail`, {}, { transform });
}

export function healthIndexMethod() {
  const transform = ({ data }: AnyObject) => {
    // console.log(data, "healthIndexMethod",Array.isArray(data), data.map((item: any) => ({
    //   ...item,
    //   ...item.patientQuotaRecordDTOS?.slice(-1)[0],
    // })));
    return Array.isArray(data)
      ? data.map((item) => ({
          ...item,
          ...item.patientQuotaRecordDTOS?.slice(-1)[0],
        }))
      : [];
  };
  return Post(`/patientQuota/getCurQuoval`, {}, { transform });
}

export function getSolarTerms() {
  return Get(`/solarTerm/getCurSolarTerm`, {
    transform({ data }) {
      return { description: data };
    },
  });
}

export function getShortScienceList(page = 1, size = 3) {
  return Post(
    `/psarticle/pagePsarticle?pageNum=${page}&pageSize=${size}`,
    void 0,
    {
      transform({ data }: AnyObject) {
        return {
          total: data.total,
          page: data?.current ?? page,
          size: data?.size ?? size,
          data: data.data.map(
            (item: AnyObject) => (
              (item.id = item.popularScienceArticleId), item
            )
          ),
        };
      },
    }
  );
}

// 跳转聊天页面
export function toChats(component: "guide" | "questionnaire") {
  const navigate = wx.navigateTo({
    url: `/module/chats/pages/index/index?component=${component}`,
  });
  return navigate;
}
export function getPatientPhone() {
  return Post(
    `/patientInfoManage/getPatientInfoDetail`,
    {},
    {
      transform({ data }: AnyObject) {
        return data;
      },
    }
  );
}
// 获取订单列表
export function getOrderList(
  patientId: string | number,
  progress: string | number
) {
  return Post(
    `/patientCrManage/pagePcrOrders`,
    { patientId, status: 0, progress },
    {
      transform({ data }: AnyObject) {
        return data;
      },
    }
  );
}
// 获取各状态调理记录数量
export function getRecordCountMethod() {
  return Post(`/patientCrManage/getDiffProgressPcrCount`, {
    transform({ data }: AnyObject) {
      return data;
    },
  });
}
// 获取患者线上调理方案打卡记录
export function getPatientOnlineRecord(
  id: string | number,
  startDate: string,
  endDate: string
) {
  return Get(`/patientCrManage/ponlineCp/${id}`, {
    params: { startDate, endDate },
    transform({ data }: AnyObject) {
      return data;
    },
  });
}
// 线上调理方案补卡
export function addPatientOnlineRecord(id: string | number) {
  return Post(
    `/patientCrManage/clockIn/repair/${id}`,
    {},
    {
      transform({ data }: AnyObject) {
        return data;
      },
    }
  );
}
// 线上调理方案打卡
export function addPatientOnlineRecordClockIn(id: string | number) {
  return Post(
    `/patientCrManage/clockIn/${id}`,
    {},
    {
      transform({ data }: AnyObject) {
        return data;
      },
    }
  );
}
