import { post } from "./axios";
export async function useHlw(data) {
  const res = await post("hlw", data);
  return res;
}
export async function generateUserSig(data) {
  const res = await useHlw({
    type: "generateUserSig",
    corpId: sessionStorage.getItem("corpId"),
    ...data,
  });
  return res;
}

export async function getHlwDiagnosisList(data) {
  const res = await useHlw({
    type: "getDiagnosisList",
    ...data,
  });
  return res;
}

export async function updateDiagnosisCategory(data) {
  const res = await useHlw({
    type: "updateHlwDiagnosisCategory",
    ...data,
  });
  return res;
}

export async function getConsultOrder(data) {
  const res = await useHlw({
    type: "getConsultOrder",
    // corpId: sessionStorage.getItem("corpId"),
    ...data,
  });
  return res;
}

export async function updateConsultOrderStatus(data) {
  const res = await useHlw({
    type: "updateConsultOrderStatus",
    ...data,
  });
  return res;
}

export async function getDrugs(data) {
  const res = await useHlw({
    type: "getDrugInfo",
    ...data,
  });
  return res;
}
export async function hlwuploadprescription(data) {
  const res = await useHlw({
    type: "hlwuploadprescription",
    ...data,
  });
  return res;
}

export async function addConsultDiagnosis(params) {
  const res = await useHlw({
    type: "addConsultDiagnosis",
    params,
  });
  return res;
}
export async function sendSystemNotification(data) {
  const res = await useHlw({
    type: "sendSystemNotification",
    ...data,
  });
  return res;
}
export async function getAuditDiagnosisList(params) {
  const res = await useHlw({
    type: "getAuditDiagnosisList",
    ...params,
  });
  return res;
}

export async function getAuditedDiagnosisList(params) {
  const res = await useHlw({
    type: "getAuditedDiagnosisList",
    ...params,
  });
  return res;
}

export async function auditDiagnosis(params) {
  const res = await useHlw({
    type: "auditDiagnosis",
    ...params,
  });
  return res;
}

export async function getOrderDiagnosis(data) {
  return await useHlw({
    type: "getOrderDiagnosis",
    ...data,
  });
}

// 根据处方id(rpNo)获取处方详情（用于处方笺展示）
export async function getRxDetail(params) {
  return await useHlw({
    type: "getRxDetail",
    ...params,
  });
}

export async function getPatientOrder(data) {
  return await useHlw({
    type: "getPatientOrder",
    ...data,
  });
}

export async function refundConsultation(data) {
  return await useHlw({
    type: "refundConsultOrder",
    ...data,
  });
}

export async function finishConsultOrder(data) {
  return await useHlw({
    type: "finishConsultOrder",
    ...data,
  });
}

export async function acceptConsultOrder(data) {
  return await useHlw({
    type: "acceptConsultOrder",
    ...data,
  });
}

export async function getHlwDoctorList(data) {
  return await useHlw({
    type: "getHlwDoctorList",
    ...data,
  });
}

export async function updateHlwDoctor(data) {
  return await useHlw({
    type: "updateHlwDoctor",
    ...data,
  });
}

export async function getLatestSubmitTime(data) {
  return await useHlw({
    type: "getLatestSubmitTime",
    ...data,
  });
}

export async function orderHasDiagnosis(data) {
  return await useHlw({
    type: "orderHasDiagnosis",
    ...data,
  });
}

export async function doctorHasPendingOrder(data) {
  return await useHlw({
    type: "doctorHasPendingOrder",
    ...data,
  });
}

export async function getAllDoctorNos(data) {
  return await useHlw({
    type: "getAllDoctorNos",
    ...data,
  });
}

export async function reUploadDiagnosticRecord(_id) {
  return await useHlw({
    type: "reUploadDiagnosticRecord",
    _id,
  });
}

export async function getDrugStoreList(data) {
  return await useHlw({
    type: "getDrugStoreList",
    ...data,
  });
}

export async function addDrugStore(data) {
  return await useHlw({
    type: "addDrugStore",
    ...data,
  });
}

export async function editDrugStore(data) {
  return await useHlw({
    type: "editDrugStore",
    ...data,
  });
}
export async function getChargeItemList() {
  return await useHlw({
    type: "getChargeItemList",
  });
}

export async function setDrugStoreStatus(data) {
  return await useHlw({
    type: "setDrugStoreStatus",
    ...data,
  });
}

export async function getChatRecord(data) {
  return await useHlw({
    type: "getChatRecord",
    ...data,
  });
}

export async function getDoctorPrescriptionCount(data) {
  return await useHlw({
    type: "getDoctorPrescriptionCount",
    ...data,
  });
}

export async function getStoreOrderStatistics(data) {
  return await useHlw({
    type: "getStoreOrderStatistics",
    ...data,
  });
}

export async function getTimePeriodStatistics(data) {
  return await useHlw({
    type: "getTimePeriodStatistics",
    ...data,
  });
}

export async function getHisCustomer(idCard, anotherIdNo) {
  return await useHlw({
    type: "getHisCustomer",
    idCard,
    anotherIdNo,
  });
}

export async function addHisCustomer(data) {
  return await useHlw({
    type: "addHisCustomer",
    ...data
  });
}

export async function getRecommendDoctor(data) {
  return await useHlw({
    type: "getRecommendDoctor",
    ...data,
  });
}

export async function hlwWebReg(data) {
  return await useHlw({
    type: "hlwWebReg",
    ...data,
  });
}

export async function hlwWebRegSettle(data) {
  return await useHlw({
    type: "hlwWebRegSettle",
    ...data,
  });
}

export async function getPatientHlwRegList(data) {
  return await useHlw({
    type: "getPatientHlwRegList",
    ...data,
  });
}

export async function hlwRegRefund(data) {
  return await useHlw({
    type: "hlwRegRefund",
    ...data,
  });
}

export async function getRegPayStatus(data) {
  return await useHlw({
    type: "getRegPayStatus",
    ...data,
  });
}

export async function getHlwDrugInfo(data) {
  return await useHlw({
    type: "getHlwDrugInfo",
    ...data,
  });
}

export async function editHlwDrugInfo(data) {
  return await useHlw({
    type: "editHlwDrugInfo",
    ...data,
  });
}

export async function addHlwDrugInfo(data) {
  return await useHlw({
    type: "addHlwDrugInfo",
    ...data,
  });
}

export async function setHlwDrugOnSale(data) {
  return await useHlw({
    type: "setHlwDrugOnSale",
    ...data,
  });
}

export async function importHlwDrugInfo(data) {
  return await useHlw({
    type: "importHlwDrugInfo",
    ...data,
  });
}

// 配送/线上购药药品库：使用 online-drug-info 集合的独立接口
export async function getOnlineDrugInfo(data) {
  return await useHlw({
    type: "getOnlineDrugInfo",
    ...data,
  });
}

export async function addOnlineDrugInfo(data) {
  return await useHlw({
    type: "addOnlineDrugInfo",
    ...data,
  });
}

export async function editOnlineDrugInfo(data) {
  return await useHlw({
    type: "editOnlineDrugInfo",
    ...data,
  });
}

export async function setOnlineDrugOnSale(data) {
  return await useHlw({
    type: "setOnlineDrugOnSale",
    ...data,
  });
}

export async function importOnlineDrugInfo(data) {
  return await useHlw({
    type: "importOnlineDrugInfo",
    ...data,
  });
}

// 药品配送订单列表（HLWSHIPPINGLIST）
export async function getMedicinePurchaseShippingList(params) {
  return await useHlw({
    type: "getMedicinePurchaseShippingList",
    ...params,
  });
}

// 购药订单物流详情（患者端同款：需要 accountId + id）
export async function getShippingDetail(params) {
  return await useHlw({
    type: "getShippingDetail",
    ...params,
  });
}

// 根据处方查询关联的购药订单（状态校验在服务端完成）
export async function getRxMedicinePurchaseOrderId(params) {
  const payload = { ...params };
  if (!payload.corpId) {
    const corpId = sessionStorage.getItem("corpId");
    if (corpId) {
      payload.corpId = corpId;
    }
  }
  return await useHlw({
    type: "getRxMedicinePurchaseOrderId",
    ...payload,
  });
}

// 单个购药订单详情
export async function getMedicinePurchaseOrder(params) {
  return await useHlw({
    type: "getMedicinePurchaseOrder",
    ...params,
  });
}

// 缺货登记列表（HLWOUTOFSTOCKLIST）
export async function getLackMedicineList(params) {
  return await useHlw({
    type: "getLackMedicineList",
    ...params,
  });
}

// 根据问诊订单聚合出的患者列表（用于补全患者档案信息）
export async function getOrderPatientList(params) {
  return await useHlw({
    type: "getOrderPatientList",
    ...params,
  });
}

// 缺货登记导出 Excel
export async function downloadLackMedicineExcel(filters) {
  const payload = {
    ...filters,
    type: "downloadLackMedicineReg",
  };
  if (!payload.corpId) {
    const corpId = sessionStorage.getItem("corpId");
    if (corpId) {
      payload.corpId = corpId;
    }
  }
  return downloadExcel(payload);
}

export async function getDrugStoreOptions() {
  return await useHlw({
    type: "getDrugStoreOptions"
  });
}

export async function getRefundOrderList(data) {
  return await useHlw({
    type: "getRefundOrderList",
    ...data,
  });
}

export async function handleRefundOrder(data) {
  return await useHlw({
    type: "handleRefundOrder",
    ...data,
  });
}

export async function setPharmacistOnlineStatus(data) {
  return await useHlw({
    type: "setPharmacistOnlineStatus",
    ...data,
  });
}

export async function getPharmacistOnlineStatus(data) {
  return await useHlw({
    type: "getPharmacistOnlineStatus",
    ...data,
  });
}

export async function getDoctorRxStats(data) {
  return await useHlw({
    type: "getDoctorRxStats",
    ...data,
  });
}

export async function getPharmacistRxStats(data) {
  return await useHlw({
    type: "getPharmacistRxStats",
    ...data,
  });
}

export async function getDoctorTimeStats(data) {
  return await useHlw({
    type: "getDoctorTimeStats",
    ...data,
  });
}

export async function getHlwOrderConfig(data) {
  return await useHlw({
    type: "getHlwOrderConfig",
    ...data,
  });
}

export async function updateHlwOrderConfig(data) {
  return await useHlw({
    type: "updateHlwOrderConfig",
    ...data,
  });
}

export async function getHlwPersonList(data) {
  return await useHlw({
    type: "getHlwPersonList",
    ...data,
  });
}

export async function updateHlwPerson(data) {
  return await useHlw({
    type: "updateHlwPerson",
    ...data,
  });
}

export async function addHlwPerson(data) {
  return await useHlw({
    type: "addHlwPerson",
    ...data,
  });
}

export async function deleteHlwPerson(data) {
  return await useHlw({
    type: "deleteHlwPerson",
    ...data,
  });
}

export async function getSealImage(uniqueId) {
  return await useHlw({
    type: "getSealImage",
    uniqueId
  });
}

export async function getHlwConfigGroup(group) {
  return await useHlw({
    type: "getHlwConfigGroup",
    group
  });
}

export async function getOnlineDrugList(data) {
  const res = await useHlw({
    ...data,
    type: "getOnlineDrugList",
  });
  return res;
}

export async function getHisDrugInfo(data) {
  const res = await useHlw({
    ...data,
    type: "getHisDrugInfo",
  });
  return res;
}

export async function downloadExcel(data) {
  // 创建URLSearchParams对象
  const params = new URLSearchParams();
  // 添加对象属性
  Object.entries(data).forEach(([key, value]) => {
    params.append(key, value);
  });
  // 获取 token 并添加到查询参数中
  const token = sessionStorage.getItem("accessToken");
  if (token) {
    params.append("token", token);
  }
  // 获取查询字符串
  const queryString = params.toString();
  const url = `${import.meta.env.VITE_API_URL}/zyt/2excel?${queryString}`;
  window.open(url, "_blank");
}

export async function getMrtcConfig(data) {
  return await useHlw({
    type: "getMrtcConfig",
    ...data,
  });
}

export async function getDoctorByNo(data) {
  return await useHlw({
    type: "getDoctorByNo",
    ...data,
  });
}

// 阿里音视频：视频房间创建完成后通知后端（用于下发给患者等）
export async function notifyVideoRoomCreated(data) {
  return await useHlw({
    type: "notifyVideoRoomCreated",
    ...data,
  });
}

// 阿里音视频：视频问诊结束通知后端
export async function notifyVideoRoomEnded(data) {
  return await useHlw({
    type: "notifyVideoRoomEnded",
    ...data,
  });
}

// 阿里音视频：记录/更新一次服务端录制事件
export async function saveMrtcRecordEvent(data) {
  return await useHlw({
    type: "saveMrtcRecordEvent",
    ...data,
  });
}

// 阿里音视频：查询某问诊订单的服务端录制列表（默认仅返回可播放的成功记录）
export async function getMrtcRecordList(data) {
  return await useHlw({
    type: "getMrtcRecordList",
    ...data,
  });
}


export async function getCancelReasons(orderNo) {
  return await useHlw({
    type: "getCancelReasons",
    orderNo,
  });
}

export async function refundMedicinePurchaseOrderYbFee({ id, orderNo, operator, operatorId }) {
  return await useHlw({
    type: "refundMedicinePurchaseOrderYbFee",
    id,
    orderNo,
    operator,
    operatorId,
  });
}

export async function refundMedicinePurchaseOrderSelfFee({ id, orderNo, operator, operatorId, reason, cancelCode, role }) {
  return await useHlw({
    type: "refundMedicinePurchaseOrderSelfFee",
    id,
    orderNo,
    operator,
    operatorId,
    reason,
    cancelCode,
    role,
  });
}

export async function getMedicinePurchaseOrderDetail({ id }) {
  return await useHlw({
    type: "getMedicinePurchaseOrderDetail",
    id,
  });
}


export async function updateAfterSaleInfo({ notes, waybillNo, id }) {
  return await useHlw({
    type: "updateAfterSaleInfo",
    id,
    notes,
    waybillNo,
  });
}

export async function getReUploadRxList(pharmacistNo) {
  return await useHlw({
    type: "getReUploadRxList",
    pharmacistNo
  });
}

export async function handleReUploadRx({ ids, pharmacistNo }) {
  return await useHlw({
    type: "handleReUploadRx",
    pharmacistNo,
    ids
  });
}

export async function preCancelFengniaoOrder({ orderNo, reasonCode }) {
  return await useHlw({
    type: "preCancelFengniaoOrder",
    orderNo, reasonCode
  });
}

export async function getRecent90daysDrugRecord(patientId) {
  return await useHlw({
    type: "getRecent90daysDrugRecord",
    patientId
  });
}
export async function getPatientPurchaseOrderList({ patientId, page, pageSize }) {
  return await useHlw({
    type: "getPatientPurchaseOrderList",
    patientId,
    page,
    pageSize
  });
}

export async function getOnlineDrugRepeatTags() {
  return await useHlw({
    type: "getOnlineDrugRepeatTags"
  });
}

export async function getMedicalWineList(data = {}) {
  return await useHlw({
    ...data,
    type: "getMedicalWineList"
  });
}

export async function getWineShippingList(params) {
  return await useHlw({
    type: "getWineShippingList",
    ...params,
  });
}

export async function getWineShippingDetail(shippingNo) {
  return await useHlw({
    type: "getWineShippingDetail",
    shippingNo,
  });
}

export async function updateWineAfterSaleInfo({ notes, waybillNo, id }) {
  return await useHlw({
    type: "updateWineAfterSaleInfo",
    id,
    notes,
    waybillNo,
  });
}
export async function getHisEmployeeInfo(data) {
  return await useHlw({
    type: "getHisEmployeeInfo",
    ...data,
  });
}

export async function searchDrugs(keyword) {
  return await useHlw({
    type: "searchDrugs",
    keyword,
  });
}

export async function refundWineOrderFee({ id, orderNo, operator, operatorId }) {
  return await useHlw({
    type: "refundWineOrderFee",
    id,
    orderNo,
    operator,
    operatorId,
  });
}

export async function refundWineOrderShippingFee({ id, orderNo, operator, operatorId, reason, cancelCode, role }) {
  return await useHlw({
    type: "refundWineOrderShippingFee",
    id,
    orderNo,
    operator,
    operatorId,
    reason,
    cancelCode,
    role,
  });
}

export async function getRxMedicalWineOrder({ id }) {
  return await useHlw({
    type: "getRxMedicalWineOrder",
    id
  });
}

export async function getHighFreqAddressOrders(data) {
  return await useHlw({
    type: "getHighFreqAddressOrders",
    ...data,
  });
}

export async function getWineOrderPatient({ patientId, orderNo }) {
  return await useHlw({
    type: "getWineOrderPatient",
    patientId,
    orderNo,
  });
}

export async function getYbReviewResult(params) {
  return await useHlw({
    type: "getYbReviewResult",
    params
  });
}

export async function reCreateWineEmsOrder({ orderNo }) {
  return await useHlw({
    type: "reCreateWineEmsOrder",
    orderNo,
  });
}
