import request from './request';
import type {
  InputTodayExamStatisticsInfo,
  TodayExamStatisticsResponse,
  InputRevenueStatisticsInfo,
  RevenueStatisticsResponse,
  InputTodayExamProgress,
  TodayExamProgressResponse,
  InputPhysicalExamCustomerList,
  PhysicalExamCustomerListResponse,
  InputPhysicalExamProgressDetail,
  PhysicalExamProgressDetailResponse,
  InputPhysicalExamProgress,
  PhysicalExamProgressResponse,
  InputCustomerDetail,
  CustomerDetailResponse,
  InputMedicalExamCenterSignIn,
  PhysicalExamSignInResponse,
  InputTongyishuInfo,
  TongyishuGetResponse,
  InputTongyishuSignSubmit,
  TongyishuSignSubmitResponse,
  InputDaojiandanInfo,
  DaojiandanGetResponse,
  InputDaojiandanSignSubmit,
  DaojiandanSignSubmitResponse,
  InputAddItemBillInfo,
  AddItemBillGetResponse,
  InputAddItemBillSignSubmit,
  AddItemBillSignSubmitResponse,
  InputDaojiandanPrintStatus,
  DaojiandanPrintStatusResponse,
  InputTijianPdfFileInfo,
  TijianPdfFileGetResponse,
  InputCustomerDetailEdit,
  CustomerDetailEditResponse,
  InputPhysicalExamAddItem,
  PhysicalExamAddItemListResponse,
  InputAddItemCustomerInfo,
  AddItemCustomerInfoResponse,
  InputChannelCompanyInfo,
  ChannelCompanyInfoListResponse,
  InputPhysicalExamAddOrder,
  PhysicalExamQrcodeCreateResponse,
  InputOrderPaymentInfo,
  PhysicalExamIsPaymentResponse,
  InputPhysicalExamDiningLog,
  PhysicalExamDiningLogResponse,
  InputTijianDiningLog,
  TijianDiningLogResponse,
  InputOperatorRemarkInfo,
  OperatorRemarkInfoResponse,
  InputOperatorRemarkInfoAdd,
  OperatorRemarkInfoAddResponse,
  InputVerificationCodeImage,
  VerificationCodeImageResponse,
  InputLoginByPassword,
  LoginByPasswordResponse,
  InputVerificationCodePhone,
  VerificationCodePhoneResponse,
  InputUserOwnedMenus,
  UserOwnedMenusResponse,
  InputB1ServiceBoard,
  B1ServiceBoardResponse,
  InputNorth3ServiceBoard,
  North3ServiceBoardResponse,
  InputReportSendQRcode,
  ReportSendQRcodeResponse,
  InputReportSendInfo,
  ReportSendInfoResponse,
  InputExpressContact,
  ReportSendAddressSaveResponse,
  InputExamOptionalItemList,
  OptionalItemInfoListResponse,
  InputOptionalPackageRemove,
  OptionalPackageRemoveResponse,
  InputTijianOptionItemRecordList,
  TijianOptionItemRecordListResponse,
  InputPhysicalExamTaipingInfo,
  IsTaipingVipResponse,
  InputCustomSettlementApplyApprove,
  CustomSettlementApproveStatusGetResponse,
  InputCustomSettlementApply,
  CustomSettlementApplyResponse,
  InputCustomSettlementApplyCancel,
  CustomSettlementApplyCancelResponse,
} from './types';

/**
 * 自助机HIS接口
 * 基础路径: /api/his-web/medical-exam-center-app/
 */
const MEDICAL_EXAM_BASE_PATH = '/api/his-web/medical-exam-center-app';

/**
 * 今日体检统计信息
 * @param data 请求参数（空对象）
 * @returns 今日体检统计信息
 */
export const getTodayExamStatistics = (
  data: InputTodayExamStatisticsInfo = {}
): Promise<TodayExamStatisticsResponse> => {
  return request.post<TodayExamStatisticsResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/today-exam-statistics`,
    data
  ).then(res => res.data);
};

/**
 * 营收数据统计
 * @param data 请求参数（空对象）
 * @returns 营收数据统计信息
 */
export const getRevenueStatistics = (
  data: InputRevenueStatisticsInfo = {}
): Promise<RevenueStatisticsResponse> => {
  return request.post<RevenueStatisticsResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/revenue-statistics`,
    data
  ).then(res => res.data);
};

/**
 * 今日体检进度信息
 * @param data 请求参数（空对象）
 * @returns 今日体检进度信息
 */
export const getTodayExamProgress = (
  data: InputTodayExamProgress = {}
): Promise<TodayExamProgressResponse> => {
  return request.post<TodayExamProgressResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/today-exam-progress`,
    data
  ).then(res => res.data);
};

/**
 * 体检客户列表
 */
export const getPhysicalExamCustomerList = (
  data: InputPhysicalExamCustomerList
): Promise<PhysicalExamCustomerListResponse> => {
  return request.post<PhysicalExamCustomerListResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/customer-list`,
    data
  ).then(res => res.data);
};

/**
 * 体检进度详情
 */
export const getPhysicalExamProgressDetail = (
  data: InputPhysicalExamProgressDetail
): Promise<PhysicalExamProgressDetailResponse> => {
  return request.post<PhysicalExamProgressDetailResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/progress`,
    data
  ).then(res => res.data);
};

/**
 * 体检进度
 */
export const getPhysicalExamProgress = (
  data: InputPhysicalExamProgress
): Promise<PhysicalExamProgressResponse> => {
  return request.post<PhysicalExamProgressResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/progress`,
    data
  ).then(res => res.data);
};

/**
 * 客户详情
 */
export const getCustomerDetail = (
  data: InputCustomerDetail
): Promise<CustomerDetailResponse> => {
  return request.post<CustomerDetailResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/customer-detail`,
    data
  ).then(res => res.data);
};

/**
 * iPad 体检中心签到
 */
export const signInMedicalExamCenter = (
  data: InputMedicalExamCenterSignIn
): Promise<PhysicalExamSignInResponse> => {
  const formData = new FormData();
  formData.append('id_no_pic', data.id_no_pic);

  return request.post<PhysicalExamSignInResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/sign-in`,
    formData,
    {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    }
  ).then(res => res.data);
};

/**
 * 获取体检知情同意书PDF
 */
export const getTongyishuPdf = (
  data: InputTongyishuInfo
): Promise<TongyishuGetResponse> => {
  return request.post<TongyishuGetResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/tongyishu-get`,
    data
  ).then(res => res.data);
};

/**
 * 提交体检知情同意书签名（上传签名图片）
 */
export const submitTongyishuSign = (
  data: InputTongyishuSignSubmit
): Promise<TongyishuSignSubmitResponse> => {
  const formData = new FormData();
  formData.append('sign_file', data.sign_file);

  return request.post<TongyishuSignSubmitResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/tongyishu-sign-submit?exam_id=${data.exam_id}&combination_code=${data.combination_code}`,
    formData,
    {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    }
  ).then(res => res.data);
};

/**
 * 客户信息编辑
 */
export const editCustomerDetail = (
  data: InputCustomerDetailEdit
): Promise<CustomerDetailEditResponse> => {
  return request.post<CustomerDetailEditResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/customer-detail-edit`,
    data
  ).then(res => res.data);
};

/**
 * 搜索体检加项
 */
export const searchPhysicalExamAddItem = (
  data: InputPhysicalExamAddItem
): Promise<PhysicalExamAddItemListResponse> => {
  return request.post<PhysicalExamAddItemListResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/add-item-list`,
    data
  ).then(res => res.data);
};

/**
 * 获取体检加项客户信息
 */
export const getAddItemCustomerInfo = (
  data: InputAddItemCustomerInfo
): Promise<AddItemCustomerInfoResponse> => {
  return request.post<AddItemCustomerInfoResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/add-item-customer-info`,
    data
  ).then(res => res.data);
};

/**
 * 获取挂账公司信息列表
 */
export const getChannelCompanyList = (
  data: InputChannelCompanyInfo = {}
): Promise<ChannelCompanyInfoListResponse> => {
  return request.post<ChannelCompanyInfoListResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/channel-company-list`,
    data
  ).then(res => res.data);
};

/**
 * Native-生成扫码支付二维码
 */
export const createNativePaymentQrcode = (
  data: InputPhysicalExamAddOrder
): Promise<PhysicalExamQrcodeCreateResponse> => {
  return request.post<PhysicalExamQrcodeCreateResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/qrcode-create`,
    data
  ).then(res => res.data);
};

/**
 * Native-查询支付是否成功
 */
export const checkNativePaymentStatus = (
  data: InputOrderPaymentInfo
): Promise<PhysicalExamIsPaymentResponse> => {
  return request.post<PhysicalExamIsPaymentResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/is-payment`,
    data
  ).then(res => res.data);
};

/**
 * 获取体检导检单PDF
 */
export const getDaojiandanPdf = (
  data: InputDaojiandanInfo
): Promise<DaojiandanGetResponse> => {
  return request.post<DaojiandanGetResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/daojiandan-get`,
    data
  ).then(res => res.data);
};

/**
 * 提交体检签名生成导检单PDF
 */
export const submitDaojiandanSign = (
  data: InputDaojiandanSignSubmit
): Promise<DaojiandanSignSubmitResponse> => {
  const formData = new FormData();
  formData.append('sign_file', data.sign_file);

  return request.post<DaojiandanSignSubmitResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/daojiandan-sign-submit?exam_id=${data.exam_id}`,
    formData,
    {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    }
  ).then(res => res.data);
};

/**
 * 获取体检加项单PDF
 */
export const getAddItemBillPdf = (
  data: InputAddItemBillInfo
): Promise<AddItemBillGetResponse> => {
  return request.post<AddItemBillGetResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/add-item-bill-get`,
    data
  ).then(res => res.data);
};

/**
 * 提交体检加项单签名生成PDF
 */
export const submitAddItemBillSign = (
  data: InputAddItemBillSignSubmit
): Promise<AddItemBillSignSubmitResponse> => {
  const formData = new FormData();
  formData.append('sign_file', data.sign_file);

  return request.post<AddItemBillSignSubmitResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/add-item-bill-sign-submit?exam_id=${data.exam_id}&pdf_sort=${data.pdf_sort}&combinationCode=${data.combinationCode}`,
    formData,
    {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    }
  ).then(res => res.data);
};

/**
 * 体检导检单打印状态修改
 */
export const editDaojiandanPrintStatus = (
  data: InputDaojiandanPrintStatus
): Promise<DaojiandanPrintStatusResponse> => {
  return request.post<DaojiandanPrintStatusResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/print-status-edit`,
    data
  ).then(res => res.data);
};

/**
 * 体检用餐登记列表
 */
export const getPhysicalExamDiningList = (
  data: InputPhysicalExamDiningLog
): Promise<PhysicalExamDiningLogResponse> => {
  return request.post<PhysicalExamDiningLogResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/dining-list`,
    data
  ).then(res => res.data);
};

/**
 * 体检用餐登记
 */
export const registerPhysicalExamDining = (
  data: InputTijianDiningLog
): Promise<TijianDiningLogResponse> => {
  return request.post<TijianDiningLogResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/dining-register`,
    data
  ).then(res => res.data);
};

/**
 * 获取操作员备注信息
 */
export const getOperatorRemarkInfo = (
  data: InputOperatorRemarkInfo
): Promise<OperatorRemarkInfoResponse> => {
  return request.post<OperatorRemarkInfoResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/remark-get`,
    data
  ).then(res => res.data);
};

/**
 * 保存操作员备注信息
 */
export const saveOperatorRemarkInfo = (
  data: InputOperatorRemarkInfoAdd
): Promise<OperatorRemarkInfoAddResponse> => {
  return request.post<OperatorRemarkInfoAddResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/remark-save`,
    data
  ).then(res => res.data);
};

/**
 * 获取登录验证码（图片）
 */
export const getVerificationCodeImage = (
  data: InputVerificationCodeImage
): Promise<VerificationCodeImageResponse> => {
  // 注意：此接口基础路径与其他 HIS 接口不同，直接使用 /api/auth
  return request.post<VerificationCodeImageResponse>(
    `/api/auth/verification-code/image`,
    data
  ).then(res => res.data);
};

/**
 * 登录（账号密码登录-图片验证码）
 */
export const loginByPassword = (
  data: InputLoginByPassword
): Promise<LoginByPasswordResponse> => {
  return request.post<LoginByPasswordResponse>(
    `/api/auth/login-by-password`,
    data
  ).then(res => res.data);
};

/**
 * 获取登录验证码（手机号）
 */
export const getVerificationCodePhone = (
  data: InputVerificationCodePhone
): Promise<VerificationCodePhoneResponse> => {
  return request.post<VerificationCodePhoneResponse>(
    `/api/auth/verification-code/phone`,
    data
  ).then(res => res.data);
};

/**
 * 获取用户菜单权限
 */
export const getUserOwnedMenus = (
  data: InputUserOwnedMenus
): Promise<UserOwnedMenusResponse> => {
  // 从 localStorage 获取 accessToken
  const accessToken = typeof window !== 'undefined'
    ? localStorage.getItem('accessToken')
    : null;

  const headers: Record<string, string> = {};
  if (accessToken) {
    headers.Authorization = `Bearer ${accessToken}`;
  }

  return request.post<UserOwnedMenusResponse>(
    `/api/auth/user/owned/menus`,
    data,
    {
      headers,
    }
  ).then(res => res.data);
};

/**
 * B1服务看板
 */
export const getB1ServiceBoard = (
  data: InputB1ServiceBoard = {}
): Promise<B1ServiceBoardResponse> => {
  return request.post<B1ServiceBoardResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/b1-service-board`,
    data
  ).then(res => res.data);
};

/**
 * 北3服务看板
 */
export const getNorth3ServiceBoard = (
  data: InputNorth3ServiceBoard = {}
): Promise<North3ServiceBoardResponse> => {
  return request.post<North3ServiceBoardResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/north3-service-board`,
    data
  ).then(res => res.data);
};

/**
 * 报告寄送登记二维码
 */
export const getReportSendQRcode = (
  data: InputReportSendQRcode
): Promise<ReportSendQRcodeResponse> => {
  return request.post<ReportSendQRcodeResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/report-send-qrcode`,
    data
  ).then(res => res.data);
};

/**
 * 查看报告寄送地址
 */
export const getReportSendInfo = (
  data: InputReportSendInfo
): Promise<ReportSendInfoResponse> => {
  return request.post<ReportSendInfoResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/report-send-info`,
    data
  ).then(res => res.data);
};

/**
 * 保存体检客户报告邮寄地址
 */
export const saveReportSendAddress = (
  data: InputExpressContact
): Promise<ReportSendAddressSaveResponse> => {
  return request.post<ReportSendAddressSaveResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/report-send-address-save`,
    data
  ).then(res => res.data);
};

/**
 * 获取客户体检同意书PDF（导检单、知情同意书、加项单）
 */
export const getTijianPdfFile = (
  data: InputTijianPdfFileInfo
): Promise<TijianPdfFileGetResponse> => {
  return request.post<TijianPdfFileGetResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/pdf-file-get`,
    data
  ).then(res => res.data);
};

/**
 * 通过身份证号获取体检选项项目列表
 */
export const getExamOptionalItemList = (
  data: InputExamOptionalItemList
): Promise<OptionalItemInfoListResponse> => {
  return request.post<OptionalItemInfoListResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/optional-item-list`,
    data
  ).then(res => res.data);
};

/**
 * 移除弃选体检套餐选项
 */
export const removeOptionalPackage = (
  data: InputOptionalPackageRemove
): Promise<OptionalPackageRemoveResponse> => {
  return request.post<OptionalPackageRemoveResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/optional-item-remove`,
    data
  ).then(res => res.data);
};

/**
 * 获取体检客户可选项操作记录信息
 */
export const getExamOptionRecordList = (
  data: InputTijianOptionItemRecordList
): Promise<TijianOptionItemRecordListResponse> => {
  return request.post<TijianOptionItemRecordListResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/optional-record-list`,
    data
  ).then(res => res.data);
};

/**
 * 获取太平VIP客户认证结果
 */
export const isTaipingVip = (
  data: InputPhysicalExamTaipingInfo
): Promise<IsTaipingVipResponse> => {
  return request.post<IsTaipingVipResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/is-taiping-vip`,
    data
  ).then(res => res.data);
};

/**
 * 获取体检加项自定义结算申请审批状态
 */
export const getCustomSettlementApproveStatus = (
  data: InputCustomSettlementApplyApprove
): Promise<CustomSettlementApproveStatusGetResponse> => {
  return request.post<CustomSettlementApproveStatusGetResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/custom-settlement-approve-status-get`,
    data
  ).then(res => res.data);
};

/**
 * 体检加项自定义结算申请
 */
export const customSettlementApply = (
  data: InputCustomSettlementApply
): Promise<CustomSettlementApplyResponse> => {
  return request.post<CustomSettlementApplyResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/custom-settlement-apply`,
    data
  ).then(res => res.data);
};

/**
 * 体检加项自定义结算申请取消
 */
export const customSettlementApplyCancel = (
  data: InputCustomSettlementApplyCancel
): Promise<CustomSettlementApplyCancelResponse> => {
  return request.post<CustomSettlementApplyCancelResponse>(
    `${MEDICAL_EXAM_BASE_PATH}/custom-settlement-apply-cancel`,
    data
  ).then(res => res.data);
};
