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

const ref = ['province', 'city', 'area'] as const;
const ref2 = ['womenSpecialPeriod', 'foodAllergy', 'hobbyFlavor', 'job'] as const;

function to(params: App.Patient.Model) {
  const { address, ..._data } = params;
  const data: Record<string, any> = {}
  if (Array.isArray(address)) {
    address.forEach(({ name, code }, index) => {
      const key = ref[index];
      data[`${key}Name`] = name
      data[`${key}Code`] = code
    })
  }
  for (const key of ref2) {
    if (Array.isArray(_data[key])) _data[key] = _data[key]?.map(item => item?.value || item).join(',') as any;
  }
  return { ..._data, ...data, }
}

function from(params: Record<string, any>) {
  const address = [];
  for (const key of ref) {
    const name = params[`${key}Name`];
    const code = params[`${key}Code`];
    if (name && code) {
      address[address.length] = { name, code };
      delete params[`${key}Name`];
      delete params[`${key}Code`];
    } else {
      break;
    }
  }

  for (const key of ref2) {
    if (typeof params[key] === 'string' && params[key]) params[key] = params[key].split(',')
  }

  return { ...params, address } as App.Patient.Model


}

export function getUserInfoMethod() {
  return Post(`/patientInfoManage/getPatientInfoDetail`, {}, {
    transform({ data }) {
      return from(<any>data)
    }
  })
}

export function createUserInfoMethod(data: App.Patient.Model) {
  return Post(`/patientInfoManage/savePatientInfo`, { ...to(data), mobileAccountId: 0, }, {
    transform({ data }) { return { patientId: data } }
  });
}

export async function updateUserInfoMethod(data: App.Patient.Model) {

  const result = await Post(`/patientInfoManage/updatePatientInfo`, to(data));

}

export function verifyCardnoMethod(value: string) {
  return Get(`/patientInfoManage/isExistCardno`, { params: { cardno: value }, transform({ data }) { return data } })
}

export async function getUserRecordMethod() {
  const { data: $dictionaries } = await Get(`/dict/getDicts`);
  const $is: AnyArray = $dictionaries.find((item: AnyObject) => item.dictType === 'sys_yes_no')?.items ?? [];
  const $w: AnyArray = $dictionaries.find((item: AnyObject) => item.dictType === 'women_special_period')?.items ?? [];
  const $f: AnyArray = $dictionaries.find((item: AnyObject) => item.dictType === 'food_allergy')?.items ?? [];
  const $h: AnyArray = $dictionaries.find((item: AnyObject) => item.dictType === 'hobby_flavor')?.items ?? [];
  const $j: AnyArray = $dictionaries.find((item: AnyObject) => item.dictType === 'job')?.items ?? [];

  const get = (value: string, $dictionaries: AnyArray) => {
    return value?.split(',')
      .map(value => {
        const [v, l] = value.split(':');
        return l ? l.trim() : $dictionaries.find(item => item.dictValue === v)?.dictLabel
      })
      .filter(Boolean)
      .join(', ')
  }


  return Post(`/patientInfoManage/pageChangeRecord`, {}, {
    transform({ data }: AnyObject) {
      return {
        total: data.total,
        data: data.data?.map((item: AnyObject) => {
          return {
            reportTime: dayjs(item.createTime).format('YY/MM/DD'),
            height: item.height ? `${item.height}cm` : '',
            weight: item.weight ? `${item.weight}kg` : '',
            smokeState: get(item.smokeState, $is),
            drinkState: get(item.drinkState, $is),
            foodAllergy: get(item.foodAllergy, $f),
            hobbyFlavor: get(item.hobbyFlavor, $h),
            job: get(item.job, $j),
            womenSpecialPeriod: get(item.womenSpecialPeriod, $w),
          }
        })
      };
    }
  })
}