import request from '@/utils/request'

// 查询处方取药列表
export function listMedicineChest(query) {
  return request({
    url: '/yfc-admin/prescription/medicineChest/query',
    method: 'post',
    params:query
  })
}

// 处方列表--设置行列
export function modifyLayout(query) {
  return request({
    url: '/yfc-admin/prescription/medicineChest/modifyLayout',
    method: 'post',
    params:query
  })
}

// 处方列表--取药
export function listTheMedicine(query) {
  return request({
    url: '/yfc-admin/prescription/medicineChest/getTheMedicine',
    method: 'post',
    params:query
  })
}

// 处方列表--解绑
export function listMedicineUnbundle(query) {
  return request({
    url: '/yfc-admin/prescription/medicineChest/unbundle',
    method: 'post',
    params:query
  })
}


// 查询处方审核列表
export function listPrescriptionCore(query) {
  return request({
    url: '/yfc-admin/prescription/prescriptionCore/list',
    method: 'get',
    params: query
  })
}

// 查询处方审核列表
export function listPrescriptionCoreNew(query) {
  return request({
    url: '/yfc-admin/prescription/prescriptionCore/getPrescriptionCoreList',
    method: 'get',
    params: query
  })
}

// 查询处方接收列表
export function listPrescriptionReception(query) {
  return request({
    url: '/yfc-admin/prescriptionManage/preOrderPage',
    method: 'post',
    data: query,
    params: {
      pageNum: query.pageNum,
      pageSize: query.pageSize,
    },
  })
}

export function getPrescriptionReception(id) {
  return request({
    url: `/yfc-admin/prescriptionManage/getPreOrderDetailById`,
    method: 'get',
    params: { id },
  }).then(res => {
    if (res.code !== 200) throw res.message;
    const {
      details = [],
      steep = {}, decoct = {}, pack = {},
      province, city, region,
      number, ...data
    } = res.data;
    for (const [key, value] of Object.entries(data)) {
      if (value == null || typeof value === 'object') delete data[key];
    }
    const location = [];
    if (province) {
      location.push(province);
      if (city) {
        location.push(city);
        if (region) location.push(region);
      }
    }
    return {
      ...data,
      location,
      steep: Object.assign({}, steep),
      decoct: Object.assign({}, decoct),
      pack: Object.assign({}, pack),
      prescription: Object.assign({number}, data),
      medicines: details.map(item => {
        return Object.assign(item, {
          drugsSpecsName: item.drugsSpecsName || item.ypggCenter || item.ypggHis,
        });
      }),
    };
  });
}

export function setPrescriptionReception(model) {
  return request({
    url: '/yfc-admin/prescriptionManage/receivePreOrder',
    method: 'get',
    params: { id: model.id },
  })
}

//窗口发药
export function listPrescriptionCoreFy(query) {
  return request({
    url: '/yfc-admin/prescription/prescriptionCore/fy',
    method: 'get',
    params: query
  })
}

//更新打印状态为已打印
export function updatePrintStatus(query) {
  return request({
    url: '/yfc-admin/prescription/prescriptionCore/updatePrintStatus',
    method: 'get',
    params: query
  })
}



//更改发药状态
export function fyState(data) {
  return request({
    url: '/yfc-admin/prescription/prescriptionCore/fyState',
    method: 'put',
    data: data
  })
}

// 查询处方审核详细
export function getPrescriptionCore(id) {
  return request({
    url: '/yfc-admin/prescription/prescriptionCore/' + id,
    method: 'get'
  })
}

export function getPrescriptionCore2(id) {
  return request({
    url: `/yfc-admin/prescription/prescriptionCore/${id}`,
    method: 'get',
  }).then(res => {
    if (res.code !== 200) throw res.message;
    const {
      oralPreItemList = [], oralPre = {},
      steep = {}, decoct = {}, pack = {},
      province, city, region,
      number, ...data
    } = res.data;
    for (const [key, value] of Object.entries(data)) {
      if (value == null || typeof value === 'object') delete data[key];
    }
    const location = [];
    if (province) {
      location.push(province);
      if (city) {
        location.push(city);
        if (region) location.push(region);
      }
    }
    return {
      ...data,
      location,
      steep: Object.assign({}, steep),
      decoct: Object.assign({}, decoct),
      pack: Object.assign({}, pack),
      prescription: Object.assign({number}, oralPre),
      medicines: oralPreItemList.map(item => {
        return Object.assign(item, {
          drugsSpecsName: item.drugsSpecsName || item.ypggCenter || item.ypggHis,
        });
      }),
    };
  });
}

export function getPrescriptionRS(id, medicines, {hidePregnant = false} = {}) {
  const hack = request({
    url: 'http://121.43.162.141/yf/knowlib/getYfMatInfo',
    method: 'post',
    data: medicines.map(item => item.matCode),
    params: {id},
    headers: {isToken: false},
  }).then(res => {
    if (res.ResultCode === 401) throw {message: res.ResultInfo};
    if (res.code !== 0) throw res;
    return res;
  });

  return hack.then(res => {
    const data = Array.isArray(res.data) ? res.data : [];
    return data.reduce((rs, item, index) => {
      if (item && item.matid && medicines[index]) {
        const name = medicines[index].matName || item.matname;
        const dosage = +medicines[index].matDose;
        const color = '#5AD8A6';
        if (dosage && item.matdosage) {
          const ref = {}, i = 1;
          const child = rs[i] || (rs[i] = {title: '', collection: []});
          child.collection.push(`<div>
            <span class="met-name" style="color: ${color};">${name}</span>
            <span class="met-value">${ref[item.matdosage]}</span>
          </div>`);
        }
        if (item.matsjj) {
          const ref = {1: '慎用', 2: '忌用', 3: '禁用'}, i = 2;
          const child = rs[i] || (rs[i] = {title: '慎忌禁用药', collection: []});
          child.collection.push(`<div>
            <span class="met-name" style="color: ${color};">${name}</span>
            <span class="met-value">${ref[item.matsjj]}</span>
          </div>`);
        }
        if (item.matyfsjj && !hidePregnant) {
          const color = item.matyfsjj === '3' ? 'red' : '#5AD8A6';
          const ref = {1: '孕妇慎用', 2: '孕妇忌用', 3: '孕妇禁用'}, i = 3;
          const child = rs[i] || (rs[i] = {title: '孕妇慎忌禁', collection: []});
          child.collection.push(`<div>
            <span class="met-name" style="color: ${color};">${name}</span>
            <span class="met-value">${ref[item.matyfsjj]}</span>
          </div>`);
        }
        if (item.matysjj) {
          const ref = {}, i = 4;
          const child = rs[i] || (rs[i] = {title: '服药饮食禁忌', collection: []});
          child.collection.push(`<div>
            <span class="met-name" style="color: ${color};">${name}</span>
            <span class="met-value">${item.matysjj || '无'}</span>
          </div>`);
        }
        if (item.matdxsm) {
          const color = 'red';
          const ref = {}, i = 5;
          const child = rs[i] || (rs[i] = {title: '药物毒性说明', collection: []});
          child.collection.push(`<div>
            <span class="met-name" style="color: ${color};">${name}</span>
            <span class="met-value">${item.matdxsm || '无'}</span>
          </div>`);
        }
        if (item.matbzjj) {
          const ref = {}, i = 6;
          const child = rs[i] || (rs[i] = {title: '病证用药禁忌', collection: []});
          child.collection.push(`<div>
            <span class="met-name" style="color: ${color};">${name}</span>
            <span class="met-value">${item.matbzjj || '无'}</span>
          </div>`);
        }
        if (item.matsbf) {
          const color = 'red';
          const ref = {}, i = 7;
          const child = rs[i] || (rs[i] = {title: '十八反', collection: []});
          child.collection.push(`<div>
            <span class="met-name" style="color: ${color};">${name}</span>
            <span class="met-value">反${item.matsbf}</span>
          </div>`);
        }
        if (item.matsjw) {
          const color = 'red';
          const ref = {}, i = 8;
          const child = rs[i] || (rs[i] = {title: '十九畏', collection: []});
          child.collection.push(`<div>
            <span class="met-name" style="color: ${color};">${name}</span>
            <span class="met-value">畏${item.matsjw}</span>
          </div>`);
        }
        if (item.matby) {
          const ref = {}, i = 9;
          const child = rs[i] || (rs[i] = {title: '用药不宜', collection: []});
          child.collection.push(`<div>
            <span class="met-name" style="color: ${color};">${name}</span>
            <span class="met-value">不宜与${item.matby}同用</span>
          </div>`);
        }
        if (dosage && (dosage < item.matmindosage || dosage > item.matmaxdosage)) {
          const ref = {}, i = 0;
          const child = rs[i] || (rs[i] = {title: '超剂量药品', collection: []});
          child.collection.push(`<div>
            <span class="met-name" style="color: ${color};">${name}</span>
            <span class="met-value" style="color: red;">(${item.matmindosage}-${item.matmaxdosage})</span>
          </div>`);
        }
      }
      return rs;
    }, []).filter(Boolean);
  });
}

// 新增处方审核
export function addPrescriptionCore(data) {
  return request({
    url: '/yfc-admin/prescription/prescriptionCore',
    method: 'post',
    data: data
  })
}

export function addPrescriptionCore2(model) {
  return request({
    url: '/yfc-admin/prescriptionManage/addPrescription',
    method: 'post',
    data: model
  });
}

// 修改处方审核
export function updatePrescriptionCore(data) {
  return request({
    url: '/yfc-admin/prescription/prescriptionCore',
    method: 'put',
    data: data
  })
}

// 删除处方审核
export function delPrescriptionCore(id) {
  return request({
    url: '/yfc-admin/prescription/prescriptionCore/' + id,
    method: 'delete'
  })
}

// 导出处方审核
export function exportPrescriptionCore(query) {
  return request({
    url: '/yfc-admin/prescription/prescriptionCore/export',
    method: 'get',
    params: query
  })
}

//审核状态
export function check(data) {
  return request({
    url: '/yfc-admin/prescription/prescriptionCore/check',
    method: 'put',
    data: data
  })
}

export function check2(model) {
  return request({
    url: '/yfc-admin/prescriptionManage/checkPrescription',
    method: 'post',
    data: model,
  });
}

//打印次数
export function addNum(id) {
  return request({
    url: `/prescription/prescriptionCore/addNum?id=${id}`,
    method: 'get',

  })
}

// 获取快递面单参数
export function getExpressRecordParams(id, type = 'sf') {
  return request({
    url: '/yfc-admin/prescriptionManage/expressSfPrint',
    method: 'post',
    data: {prescriptionCoreId: id}
  })
}
