import React from 'react';

/**
 * 临床路径执行管理 API 服务层
 * 所有接口调用统一管理
 */

/**
 * 从 session 获取公共参数
 */
const getSessionData = () => {
  return {
    admID: '3',  // 写死用于测试
    userID: sessionStorage.getItem('userID') || '',
    hospID: sessionStorage.getItem('hospID') || '',
    deptID: sessionStorage.getItem('deptID') || '',
  };
};

/**
 * 通用 API 请求方法
 * @param {string} apiCode - 接口编号
 * @param {object} params - 请求参数
 * @returns {Promise}
 */
const request = async (apiCode, params) => {
  const sessionData = getSessionData();

  // 组合参数
  const requestData = {
    ...params,
    ...sessionData,
  };

  // 调用接口
  return new Promise((resolve, reject) => {
    React.$http.post('/api/clinicalPathway', {
      code: apiCode,
      data: requestData,
    })
    .then(response => {
      if (response.errorCode === '0') {
        resolve(response.data);
      } else {
        reject(new Error(response.errorMessage || '接口调用失败'));
      }
    })
    .catch(error => {
      reject(error);
    });
  });
};

// ========== API 方法定义 ==========

/**
 * 03110069 - 获取入径路径信息
 * @param {string} entranceId - 入径ID
 */
export const getInPatPathwayInfo = (entranceId) => {
  return request('03110069', { entranceId });
};

/**
 * 03110070 - 获取阶段时间轴
 * @param {string} entranceId - 入径ID
 */
export const getStageTimeline = (entranceId) => {
  return request('03110070', { entranceId });
};

/**
 * 03110071 - 获取阶段执行项目
 * @param {string} stageId - 阶段ID
 */
export const getStageExecuteList = (stageId) => {
  return request('03110071', { stageId });
};

/**
 * 03110072 - 完成当前阶段
 * @param {string} stageId - 阶段ID
 * @param {string} action - 操作类型: complete(完成) | rollback(退回)
 * @param {string} reason - 退回原因
 */
export const completeStage = (stageId, action = 'complete', reason = '') => {
  return request('03110072', { stageId, action, reason });
};

/**
 * 03110073 - 执行项目 / 撤销执行
 * @param {string} itemId - 项目ID
 * @param {boolean} isExecuted - 是否执行
 * @param {string} executeTime - 执行时间
 */
export const executeDetailItem = (itemId, isExecuted, executeTime) => {
  return request('03110073', {
    itemId,
    isExecuted: isExecuted ? '1' : '0',
    executeTime: executeTime || '',
  });
};

/**
 * 03110074 - 跳过项目
 * @param {string} itemId - 项目ID
 * @param {string} variationReasonId - 变异原因ID
 */
export const skipDetailItem = (itemId, variationReasonId) => {
  return request('03110074', { itemId, variationReasonId });
};

/**
 * 03110075 - 获取未执行项目列表
 * @param {string} entranceId - 入径ID
 */
export const getUnexecutedItems = (entranceId) => {
  return request('03110075', { entranceId });
};

/**
 * 03110076 - 获取路径外医嘱列表
 * @param {string} entranceId - 入径ID
 */
export const getExternalOrders = (entranceId) => {
  return request('03110076', { entranceId });
};

/**
 * 03110077 - 保存变异记录
 * @param {string} entranceId - 入径ID
 * @param {string} type - 变异类型: unexecuted(未执行) | external(路径外医嘱)
 * @param {string} itemId - 项目ID
 * @param {string} categoryId - 原因分类ID
 * @param {string} reasonId - 具体原因ID
 * @param {string} remark - 备注
 */
export const saveMutationRecord = (entranceId, type, itemId, categoryId, reasonId, remark) => {
  return request('03110077', {
    entranceId,
    type,
    itemId,
    categoryId,
    reasonId,
    remark,
  });
};

/**
 * 03110080 - 更改路径状态 (出径/作废/退回)
 * @param {string} entranceId - 入径ID
 * @param {number} exitType - 2-出径, 3-作废
 * @param {string} reason - 原因
 * @param {string} remark - 备注
 */
export const changePathwayStatus = (entranceId, exitType, reason, remark) => {
  return request('03110080', {
    entranceId,
    exitType,
    reason,
    remark,
  });
};

/**
 * 03110081 - 获取可切换路径列表
 * @param {string} entranceId - 入径ID
 */
export const getSwitchablePaths = (entranceId) => {
  return request('03110081', { entranceId });
};

/**
 * 03110082 - 切换路径
 * @param {string} entranceId - 当前入径ID
 * @param {string} targetPathwayId - 目标路径ID
 * @param {string} targetStageId - 目标阶段ID
 * @param {string} reason - 切换原因
 */
export const switchPathway = (entranceId, targetPathwayId, targetStageId, reason) => {
  return request('03110082', {
    entranceId,
    targetPathwayId,
    targetStageId,
    reason,
  });
};

/**
 * 03110083 - 获取合并症路径列表
 * @param {string} entranceId - 入径ID
 */
export const getComplicationPaths = (entranceId) => {
  return request('03110083', { entranceId });
};

/**
 * 03110084 - 添加合并症路径
 * @param {string} entranceId - 入径ID
 * @param {string} complicationPathwayId - 合并症路径ID
 */
export const addComplicationPathway = (entranceId, complicationPathwayId) => {
  return request('03110084', {
    entranceId,
    complicationPathwayId,
  });
};

/**
 * 03110085 - 删除合并症路径
 * @param {string} complicationId - 合并症ID
 */
export const removeComplicationPathway = (complicationId) => {
  return request('03110085', { complicationId });
};

/**
 * 03110088 - 重新入径
 * @param {string} patientId - 患者ID
 * @param {string} visitId - 就诊ID
 * @param {string} pathwayId - 路径ID
 * @param {string} reason - 入径原因
 */
export const reentryPathway = (patientId, visitId, pathwayId, reason) => {
  return request('03110088', {
    patientId,
    visitId,
    pathwayId,
    reason,
  });
};

/**
 * 03110096 - 保存阶段签名
 * @param {string} stageId - 阶段ID
 * @param {string} doctorSign - 医生签名
 * @param {string} nurseSign - 护士签名
 */
export const saveStageSign = (stageId, doctorSign, nurseSign) => {
  return request('03110096', {
    stageId,
    doctorSign,
    nurseSign,
  });
};

/**
 * 03110089 - 获取变异原因分类树
 * @param {string} queryFlag - 查询标识，默认 "MC_List"
 * @returns {Promise} 变异原因分类数据
 */
export const getVariationReasonTree = (queryFlag = 'MC_List') => {
  return request('03110089', { queryFlag });
};

export default {
  getInPatPathwayInfo,
  getStageTimeline,
  getStageExecuteList,
  completeStage,
  executeDetailItem,
  skipDetailItem,
  getUnexecutedItems,
  getExternalOrders,
  saveMutationRecord,
  changePathwayStatus,
  getSwitchablePaths,
  switchPathway,
  getComplicationPaths,
  addComplicationPathway,
  removeComplicationPathway,
  reentryPathway,
  saveStageSign,
  getVariationReasonTree,
};
