import type { List, Tree } from '@/model';
import type { TagModel, TagQuery, PlanQuery, PlanModel } from '@/model/system.model';
import type { TaskQuery, TaskModel, EvaluateQuery, EvaluationModel } from '@/model/follow.model';
import request from '@/request/alova';
import { fromHealthReportAnalysis } from '@/model/health-report.model';

// 计划搜索列表
export function planMethod(page: number, size: number, query?: PlanQuery) {
  return request.Post<List<PlanModel>>(
    '/fdhb-pc/followupPlanManage/pageFollowupPlan',
    query ?? {},
    {
      hitSource: /plan$/, // 匹配失效源
      params: { pageNum: page, pageSize: size },
    }
  );
}

// 随访计划状态改变
export function planUpdateStatusMethod(data: Partial<PlanModel>) {
  return request.Get(`/fdhb-pc/followupPlanManage/updateStatus`, {
    name: 'update-plan',
    params: { id: data.id, status: data.status },
    cacheFor: null,
  });
}

// 删除随访计划
export function planDeleteMethod(data: Partial<PlanModel>) {
  return request.Get(`/fdhb-pc/followupPlanManage/delete`, {
    name: 'update-PLAN',
    params: { id: data.id },
    cacheFor: null,
  });
}

// 搜索随访计划
export function plansSearchMethod(query?: PlanQuery) {
  return request.Post<List<TagModel>, TagModel[]>(`/fdhb-pc/tagManage/selectTag`, query ?? {}, {
    hitSource: /plan$/,
    transform(data) {
      return { total: data.length, data };
    },
  });
}

// 患者标签
export function tagsSearchMethod(query?: TagQuery) {
  return request.Post<List<TagModel>, TagModel[]>(`/fdhb-pc/tagManage/selectTag`, query ?? {}, {
    hitSource: /tag$/,
    transform(data) {
      return {
        total: data.length,
        data: data.map((item) => Object.assign(item, {
          category: item.type,
          disabled: item.status === '1',
          color: { 1: 'pink', 2: 'blue' }[item.type],
        })),
      };
    },
  });
}

// 全部标签
export function allTagsSearchMethod(query?: TagQuery) {
  return request.Post<List<TagModel>, TagModel[]>(`/fdhb-pc/tagManage/selectAllTag`, query ?? {}, {
    hitSource: /tag$/,
    transform(data) {
      return {
        total: data.length,
        data: data.map((item) => Object.assign(item, {
          category: item.type,
          disabled: item.status === '1',
          color: { 1: 'pink', 2: 'blue' }[item.type],
        })),
      };
    },
  });
}

// 就诊科室
export function departmentsMethod() {
  const departmentsData = reactive<any[]>([]);
  return request.Post<List<PlanModel>, PlanModel[]>(
    `/fdhb-pc/patientMedicalManage/getDeptOrDoctor`,
    { type: 1 },
    {
      transform(data) {
        data?.map((item, i) => {
          departmentsData.push({ id: `${i + 1}`, name: item });
        });
        return { total: departmentsData.length, data: departmentsData };
      },
    }
  );
}

// 就诊医生
export function doctorMethod() {
  const doctorData = reactive<any[]>([]);
  return request.Post<List<PlanModel>, PlanModel[]>(
    `/fdhb-pc/patientMedicalManage/getDeptOrDoctor`,
    { type: 2 },
    {
      transform(data) {
        if (data.length > 0) {
          data?.map((item, i) => {
            doctorData.push({ id: `${i + 1}`, name: item });
          });
        }
        return { total: doctorData.length, data: doctorData };
      },
    }
  );
}

// 新增和编辑随访计划
export function planEditMethod(data: Partial<PlanModel>) {
  return data.id
    ? request.Post(
        `/fdhb-pc/followupPlanManage/updateFollowupPlan`,
        { ...data, id: data.id },
        { name: 'edit-plan' }
      )
    : request.Post(`/fdhb-pc/followupPlanManage/addFollowupPlan`, data, { name: 'edit-plan' });
}

// 任务搜索列表
export function taskMethod(page: number, size: number, query?: TaskQuery) {
  return request.Post<List<TaskModel>>(
    '/fdhb-pc/followupTaskManage/pageFollowupTask',
    query ?? {},
    {
      hitSource: /task$/, // 匹配失效源
      params: { pageNum: page, pageSize: size },
    }
  );
}

// 随访任务提醒
export function taskRemindStatusMethod(data: Partial<TaskModel>) {
  return request.Post(`/fdhb-pc/followupTaskManage/taskRemind/${data.id}`);
}

export function FollowContentMethod(data: Partial<TaskModel>) {
  return request.Get<any[], any>(`/fdhb-pc/followupTaskManage/getFollowupTasksByGroupId`, {
    name: 'follow-modal',
    params: { id: data.followupTaskGroupId },
    cacheFor: null,
    transform(data) {
      return data;
    },
  });
}

//填写随访内容
export function FillFollowContentMethod(query: { id: string; fillin: any }) {
  return request.Post(`/fdhb-pc/followupTaskManage/updateFollowupTaskFillin/${query.id}`, {
    ...query.fillin,
  });
}

// 通用上传图片
export function UploadIFile(file: any) {
  const formData = new FormData();
  formData.append('file', file);
  return request.Post(`/file/upload`, formData, {
    // headers: { 'Content-Type': 'multipart/form-data', image: 'true' },
  });
}

// 获取随访评估列表
export function EvaluateMethod(page: number, size: number, query?: EvaluateQuery) {
  return request.Post<List<EvaluationModel>>(
    '/fdhb-pc/followupTaskManage/pageFollowupTaskGroup',
    query ?? {},
    {
      hitSource: /eva$/, // 匹配失效源
      params: { pageNum: page, pageSize: size },
    }
  );
}

// 填写随访结果评估
export function FillEvaluateMethod(data: Partial<EvaluationModel>) {
  return request.Post(`/fdhb-pc/followupTaskManage/followupEvaluate/${data.id}`, {
    ...data,
  });
}

// 查看评估详情
export function EvaluateDetailMethod(data: Partial<EvaluationModel>) {
  return request.Get(`/fdhb-pc/followupTaskManage/getFollowupTaskGroupDetailById`, {
    name: 'view-evaluation',
    params: { id: data.id },
    cacheFor: null,
    transform(data: any) {
      // if ()
      for (const task of data.tasks)  {

        task.analysis = fromHealthReportAnalysis(task.tonguefaceAnalysisReport ?? {})
      }

      return data;
    },
  });
}
