// 处方详情
import React from 'react';
import { Toast } from 'antd-mobile';
import './style/index.less';
import { $http } from "containers/config/https";

export default class PrescriptionDetails extends React.Component {
  constructor() {
    super();
    let userData = localStorage.getItem("userData") ? JSON.parse(localStorage.getItem("userData")) : {};
    this.state = {
      userData,
      prescriptionDetails: {},
      detailInfo: []
    };
  }

  componentDidMount() {
    let pDetail = this.props.location && this.props.location.state ? this.props.location.state : {};
    if (pDetail && pDetail.ordPrescNo) {
      this.setState({ pDetail }, () => {
        this.getPrescriptionDetails();
      })
    } else {
      this.setState({ prescriptionDetails: {} })
    }
  }

  // 查询处方明细
  getPrescriptionDetails = () => {
    let { pDetail } = this.state;
    let data = {
      params: [{
        // ordPrescNo: 'O2020061000001'
        ordPrescNo: pDetail && pDetail.ordPrescNo ? pDetail.ordPrescNo : ""
      }]
    }
    $http.post("urlS", this, {
      code: "02030020",
      data: data,
      namespace: "HIS",
      success: function (res) {
        if (+res.errorCode === 0) {
          let prescriptionDetails = res.result[0].mainInfo && res.result[0].mainInfo.length > 0 ? res.result[0].mainInfo[0] : {};
          let detailInfo = res.result[0].detailInfo && res.result[0].detailInfo.length > 0 ? res.result[0].detailInfo[0].rows : [];
          this.setState({ prescriptionDetails, detailInfo })
        } else {
          this.setState({ prescriptionDetails: {} }, () => {
            Toast.info(res.errorMessage, 3, () => { }, false);
          })
        }
      },
      error: function (error) {
        Toast.info(error, 3, () => { }, false);
      }
    });
  }

  render() {
    let { prescriptionDetails, userData, detailInfo } = this.state;
    return (
      <div className="prescription-details">
        <h3 className="pd-header">{userData && userData.hospDesc ? userData.hospDesc : ""}处方签<span>普通</span></h3>
        <div className="pd-basic-info border-bottom-after">
          <div className="pd-basic-info-header">
            处方号：{prescriptionDetails.ordPrescNo}
          </div>
          <div className="pd-basic-info-row">
            <div>科室：<span>{prescriptionDetails.ordLocDesc}</span></div>
            <div>费别：<span>{prescriptionDetails.admReasonDesc}</span></div>
          </div>
          <div className="pd-basic-info-row">
            <div>姓名：<span>{prescriptionDetails.patName}</span></div>
            <div>性别：<span>{prescriptionDetails.patSexDesc}</span></div>
          </div>
          <div className="pd-basic-info-row">
            <div>年龄：<span>{prescriptionDetails.patAge}</span></div>
            <div>日期：<span>{prescriptionDetails.ordDate}</span></div>
          </div>
          <div className="pd-basic-info-diagnosis">
            诊断：{prescriptionDetails.admDiag}
          </div>
        </div>
        <div className="pd-content border-bottom-after">
          <h3 className="pd-content-header">PR：</h3>
          <div className="pd-content-center">
            <div className="pd-content-drug">
              {detailInfo && detailInfo.map((item, index) => {
                return (
                  <span key={index}>{item.medDesc} ({item.ordQty + item.ordUnitDesc})</span>
                )
              })}
            </div>
            <div className="pd-content-usage">
              用法：{(prescriptionDetails && prescriptionDetails.cmFreqDesc ? prescriptionDetails.cmFreqDesc + '，' : '') + (prescriptionDetails && prescriptionDetails.cmExplainDesc ? prescriptionDetails.cmExplainDesc + '，' : "") + (prescriptionDetails && prescriptionDetails.cmUsageDesc ? prescriptionDetails.cmUsageDesc : "")}
              <span style={{ margin: '0 8px', display: prescriptionDetails && prescriptionDetails.cmFac ? "inline-block" : "none" }}>{'共：' + prescriptionDetails.cmFac + "剂"}</span>
              {prescriptionDetails && prescriptionDetails.cmCookTypeDesc ? prescriptionDetails.cmCookTypeDesc : ""}
            </div>
            <div className="pd-line">
              <span className="pd-line-left">--------------------------------------------------</span>
              <span style={{ fontWeight: '900' }}>以下空白</span>
              <span className="pd-line-left">--------------------------------------------------</span>
            </div>
          </div>
        </div>
        <div className="pd-footer pd-basic-info">
          <div className="pd-basic-info-row">
            <div><span>开方医生：</span>{prescriptionDetails.ordDocDesc}</div>
            <div><span>配药：</span>{prescriptionDetails.prepUserName}</div>
          </div>
          <div className="pd-basic-info-row">
            <div><span>复核：</span>{prescriptionDetails.auditUserName}</div>
            <div><span>发药：</span>{prescriptionDetails.dispUserName}</div>
          </div>
          <div className="pd-basic-info-row">
            <div><span>金额：</span>¥ {prescriptionDetails.prescAmt}</div>
          </div>
          <div className="pd-footer-desc">
            医师处方仅开具3日内有效（医师注明除外）
          </div>
        </div>
      </div>
    )
  }
}