import request from '@/utils/request'
import {bignumber, multiply} from 'mathjs';

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

// 查询处方待审核详情
export function selectOrderDetail(query) {
  return request({
    url: '/yfc-admin/prescription/prescriptionCore/selectOrderDetail',
    method: 'post',
    params: query
  })
}
export function selectOrderDetail2(query) {
  return request({
    url: '/yfc-admin/prescription/prescriptionCore/selectOrderDetail',
    method: 'post',
    params: query
  }).then(res => {
    const data = res.data;
    const sign = (index) => { try {return data.operateList[index].operater;} catch (e) {} };
    const count = bignumber(data['number'] || 0);
    return {
      patient: {
        name: data['name'],
        gender: data['sex'],
        age: data['age'] ? `${data['age']}岁` : '',
        birthday: `${data['patientBirthday']}`,
        phone: `${data['contactNumber']}`,
      },
      recipe: {
        date: `${data['prescriptionTime']}`,
        type: {1: '中药处方', 2: '中药制剂'}[data.type] || '',
        count: data['number'],
        total: data['packageNumber'],
        category: data['dosageForm'],
        method: data['prescriptionusage'],
        volume: data['concentration'] && `每次${data['concentration']}`,
        totalVolume: data['packageDose'],
        frequency: data['frequency'],
        frequencyTime: data['medicationTime'],
        decoction: data['isBehalf'] === '1' ? '代煎' : '自煎',

        delivery: data['expressExecutor'],
        address: [/*data['province'], data['city'], data['region'],*/ data['address']].filter(Boolean).join(''),

        medicineFees: data['prescriptionSum'],
        decoctionFees: data['daijianCost'],
        deliveryFees: data['distributionCost'],
        totalFees: data['prescriptionTotalSum'],

        unitWeight: '',
        totalWeight: '',

        medicines: Array.isArray(data['detailList']) ? data['detailList'].map(item => {
          return {
            mark: item['locatorNum'],
            name: item['matName'],
            dosage: item['matDose'],
            unit: item['matUnitName'],
            usage: item['matUsageName'],
            total: +multiply(count, bignumber(item['matDose'] || 0)).toFixed(2),
          };
        }) : [],
      },
      department: [data['department']].filter(Boolean).join(' '),
      diagnosis: [data['disName']].filter(Boolean).join(' '),
      record: {
        title: data['yljgName'],
        date: data['prescriptionTime'],
        no: `${data['preNo']}`,
        bedNo: data['bedNo'],
        orderNo: data['orderNo'],
        recordNo: data['recipientCode'],
        serialNumber: data['recipeSerial'],
        category: data['preMzZy'] === '1' ? '门诊' : '住院',
        remark: data['remark'] || [data['entrust'], data['pharmacistsremarks']].filter(Boolean).join('，'),
      },
      sign: {
        /* 开方专用 */ field1: data['doctor'] || sign(1 - 1),
        /* 审核专用 */ field2: data['checkBy'] || sign(2 - 1),
        /* 调配专用 */ field3: sign(3 - 1),
        /* 复核专用 */ field4: sign(4 - 1),
        /* 浸泡专用 */ field5: sign(5 - 1),
        /* 煎煮专用 */ field6: sign(6 - 1),
        /* 打包专用 */ field7: sign(7 - 1),
        /* 发药专用 */ field8: sign(8 - 1),
      },
      field1: {1: '配药', 2: '煎药', 3: '发药', 4: '配送'}[data.state],
    }
  })
}

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


// 释放待审核处方
export function releaseOrder(query) {
  return request({
    url: '/yfc-admin/pharmacistStatus/releaseOrder',
    method: 'post',
    params: query
  })
}

//添加备注
export function remarkYS(data) {
  return request({
    url: '/yfc-admin/prescription/prescriptionCore/remark',
    method: 'put',
    data: data
  })
}

// 查询处方审核列表
// export function listPrescriptionAuditNew(query) {
//   return request({
//     url: '/prescription/prescriptionAudit/getPrescriptionAuditList',
//     method: 'get',
//     params: query
//   })
// }

//窗口发药
// export function listPrescriptionAuditFy(query) {
//   return request({
//     url: '/prescription/prescriptionAudit/fy',
//     method: 'get',
//     params: query
//   })
// }


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

// 查询处方审核详细
// export function getPrescriptionAudit(id) {
//   return request({
//     url: '/prescription/prescriptionAudit/' + id,
//     method: 'get'
//   })
// }

// 新增处方审核
// export function addPrescriptionAudit(data) {
//   return request({
//     url: '/prescription/prescriptionAudit',
//     method: 'post',
//     data: data
//   })
// }

// 修改处方审核
// export function updatePrescriptionAudit(data) {
//   return request({
//     url: '/prescription/prescriptionAudit',
//     method: 'put',
//     data: data
//   })
// }

// 删除处方审核
// export function delPrescriptionAudit(id) {
//   return request({
//     url: '/prescription/prescriptionAudit/' + id,
//     method: 'delete'
//   })
// }

// 导出处方审核
// export function exportPrescriptionAudit(query) {
//   return request({
//     url: '/prescription/prescriptionAudit/export',
//     method: 'get',
//     params: query
//   })
// }

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

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