import request from '@/utils/request'

// 查询药品信息列表
export function listDrugs(query) {
  return request({
    url: '/yfc-admin/drugs/drugs/list',
    method: 'get',
    params: query
  })
}

// 查询药品信息详细
export function getDrugs(id) {
  return request({
    url: '/yfc-admin/drugs/drugs/' + id,
    method: 'get'
  })
}

// 新增药品信息
export function addDrugs(data) {
  return request({
    url: '/yfc-admin/drugs/drugs',
    method: 'post',
    data: data
  })
}

// 修改药品信息
export function updateDrugs(data) {
  return request({
    url: '/yfc-admin/drugs/drugs',
    method: 'put',
    data: data
  })
}

// 删除药品信息
export function delDrugs(id) {
  return request({
    url: '/yfc-admin/drugs/drugs/' + id,
    method: 'delete'
  })
}

// 导出药品信息
export function exportDrugs(query) {
  return request({
    url: '/yfc-admin/drugs/drugs/export',
    method: 'get',
    params: query
  })
}

//出库
export function listAll(query) {
  return request({
    url: '/yfc-admin/drugs/drugs/listAll',
    method: 'get',
    params: query
  })
}

export function drugsMap(query) {
  const {pageNum, pageSize, date, ...data} = query;
  if (Array.isArray(date)) [data.startTime, data.endTime = data.startTime] = date;
  return request({
    url: '/yfc-admin/medicalHerbsManage/pageHerbsMapping',
    method: 'post',
    data,
    params: {pageNum, pageSize},
  })
}

export function drugsMapDelete(id) {
  return request({
    url: '/yfc-admin/medicalHerbsManage/deleteHerbsMapping',
    method: 'post',
    data: Array.isArray(id) ? id : [id],
  })
}

export function drugsMapUpdate(data, query) {
  return request({
    url: '/yfc-admin/medicalHerbsManage/addHerbsMapping',
    method: 'post',
    data: data.map(item => Object.assign({}, query, item))
  })
}

export function drugsMapImport(query) {
  const form = new FormData();
  for (const [key, value] of Object.entries(query)) form.append(key, value);
  return request({
    url: '/yfc-admin/medicalHerbsManage/importHerbsMapping',
    method: 'post',
    data: form,
  })
}

export function drugsMapImportTemplate() {
  return request({
    url: '/yfc-admin/medicalHerbsManage/downloadImportTemplate',
    method: 'post',
    responseType: 'blob',
    fileType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8',
  })
}

export function drugsMapExport(query) {
  return request({
    url: '/yfc-admin/medicalHerbsManage/exportHerbsMapping',
    method: 'post',
    data: query,
    responseType: 'blob',
    fileType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8',
  })
}

/**
 * 搜索系统药品
 * @param query
 * @param {string} query.name
 */
export function drugsSearchForSystem(query) {
  return request({
    url: '/yfc-admin/drugs/drugs/listAll',
    method: 'get',
    params: query
  })
}

/**
 * 搜索医疗机构药品
 * @param query
 * @param {string} query.herbsName
 * @param {string} query.communityId
 * @param {string} query.mechanismId
 */
export function drugsSearchForMechanism(query) {
  const {pageNum, pageSize, date, ...data} = query;
  return request({
    url: '/yfc-admin/medicalHerbsManage/getAllHerbs',
    method: 'post',
    data,
    params: {pageNum, pageSize},
  })
}
