// 取药详情
import React from 'react';
import { List, Toast } from 'antd-mobile';
import './style/index.less';
import ListCheckboxItem from './common/ListCheckboxItem.jsx';
import { $http } from "containers/config/https";
import { Util } from 'tools';

const Item = List.Item;

export default class DetailsOfDrugTaking extends React.Component {
  constructor({ history }) {
    super();
    let userData = localStorage.getItem("userData") ? JSON.parse(localStorage.getItem("userData")) : {};
    this.state = {
      userData,
      pDetail: {},
      detailsOfDrugTakingData: {},
    };
    this.history = history;
  }

  componentDidMount() {
    let pDetail = this.props.location && this.props.location.state ? this.props.location.state : {};
    if (pDetail && pDetail.ordPrescNo) {
      this.setState({ pDetail }, () => {
        this.getDetailsOfDrugTakingData();
        window.sessionStorage.setItem(
          "detailsOfDrugTaking",
          JSON.stringify(
            { ...pDetail }
          )
        );
      })
    } else {
      let sessionPDetail = sessionStorage.getItem("detailsOfDrugTaking") ? JSON.parse(sessionStorage.getItem("detailsOfDrugTaking")) : {};
      if (sessionPDetail && sessionPDetail.ordPrescNo) {
        this.setState({ pDetail: sessionPDetail }, () => {
          this.getDetailsOfDrugTakingData();
        })
      } else {
        this.setState({ pDetail: {} })
      }
    }
  }

  // 查询处方取药详情
  getDetailsOfDrugTakingData = () => {
    let { pDetail, userData } = this.state;
    let data = {
      params: [{
        hospID: userData && userData.hisHospID ? userData.hisHospID : "",
        ordPrescNo: pDetail && pDetail.ordPrescNo ? pDetail.ordPrescNo : ""
      }]
    }
    $http.post("urlS", this, {
      code: "02030102",
      data: data,
      namespace: "HIS",
      success: function (res) {
        if (+res.errorCode === 0) {
          this.setState({ detailsOfDrugTakingData: res.result.data })
        } else {
          this.setState({ detailsOfDrugTakingData: {} }, () => {
            Toast.info(res.errorMessage, 3, () => { }, false);
          })
        }
      },
      error: function (error) {
        this.setState({ detailsOfDrugTakingData: {} }, () => {
          Toast.info(error, 3, () => { }, false);
        })
      }
    });
  }

  render() {
    let { detailsOfDrugTakingData } = this.state;
    let activeRadio = detailsOfDrugTakingData && detailsOfDrugTakingData.logisticsFlag === 'Y' ? '1' : '2';
    return (
      <div className="details-of-drug-taking">
        <List>
          <Item className="list-item-head" extra={<span>{(detailsOfDrugTakingData && detailsOfDrugTakingData.patName ? detailsOfDrugTakingData.patName + ' / ' : '') + (detailsOfDrugTakingData && detailsOfDrugTakingData.patSexDesc ? detailsOfDrugTakingData.patSexDesc + ' / ' : "") + (detailsOfDrugTakingData && detailsOfDrugTakingData.patAge ? detailsOfDrugTakingData.patAge + ' / ' : '') + (detailsOfDrugTakingData && detailsOfDrugTakingData.mobile ? detailsOfDrugTakingData.mobile : "")}</span>} arrow="horizontal">就诊人</Item>
        </List>
        {activeRadio === "1" ? (
          <List>
            <Item className="list-item-head">待签收</Item>
            <Item><span>暂无物流信息</span></Item>
          </List>
        ) : (
          <List>
            <Item className="list-item-head">待取药</Item>
            <Item extra={<span>二维码</span>}>取药码<span>{detailsOfDrugTakingData.takeDrugCode}</span></Item>
          </List>
        )}
        <div className="codt-list">
          <List className="no-check">
            <ListCheckboxItem history={this.history} listData={[detailsOfDrugTakingData]} />
          </List>
        </div>
        <List>
          <Item className="list-item-head" extra={<span>{activeRadio === "1" ? "物流配送" : "自提"}</span>}>取药方式</Item>
          {/* <Item>确认时间：{detailsOfDrugTakingData && detailsOfDrugTakingData.deliveryDate ? detailsOfDrugTakingData.deliveryDate + ' ' + detailsOfDrugTakingData.deliveryTime : ""}</Item> */}
        </List>
        {activeRadio === '1' ? (
          <div className="cdm-address">
            <List className="cdm-footer-list">
              <Item className="list-item-head">收货地址</Item>
              <Item>
                <div>{detailsOfDrugTakingData && detailsOfDrugTakingData.consignee ? detailsOfDrugTakingData.consignee : ''}<span style={{ marginLeft: '12px' }}>{detailsOfDrugTakingData && detailsOfDrugTakingData.telephone ? Util.phoneAddGap(detailsOfDrugTakingData.telephone) : ''}</span></div>
                <div>
                  {detailsOfDrugTakingData && detailsOfDrugTakingData.provinceDesc ? detailsOfDrugTakingData.provinceDesc : ''}
                  {detailsOfDrugTakingData && detailsOfDrugTakingData.cityDesc ? detailsOfDrugTakingData.cityDesc : ''}
                  {detailsOfDrugTakingData && detailsOfDrugTakingData.areaDesc ? detailsOfDrugTakingData.areaDesc : ''}
                  {detailsOfDrugTakingData && detailsOfDrugTakingData.villageDesc ? detailsOfDrugTakingData.villageDesc : ''}
                  {detailsOfDrugTakingData && detailsOfDrugTakingData.address ? detailsOfDrugTakingData.address : ""}
                </div>
              </Item>
            </List>
            <List className="cdm-footer-list">
              <Item className="list-item-head" extra={<span>到付</span>}>配送费</Item>
            </List>
          </div>
        ) : (activeRadio === '2' ? (
          <div className="cdm-address">
            <List className="cdm-footer-list">
              <Item className="list-item-head">自提地址</Item>
              <Item>
                <div>
                  {detailsOfDrugTakingData && detailsOfDrugTakingData.ordLocDesc ? detailsOfDrugTakingData.ordLocDesc : ''}
                  {detailsOfDrugTakingData && detailsOfDrugTakingData.build ? detailsOfDrugTakingData.build : ''}
                  {detailsOfDrugTakingData && detailsOfDrugTakingData.floor ? detailsOfDrugTakingData.floor + '层' : ''}
                  {detailsOfDrugTakingData && detailsOfDrugTakingData.locPhone ? (
                    <a style={{ marginLeft: '12px' }} href={`tel:${detailsOfDrugTakingData.locPhone}`} onClick={this.handleContact} alt="电话">{detailsOfDrugTakingData.locPhone}</a>
                  ) : ''}
                </div>
                <div>
                  {detailsOfDrugTakingData && detailsOfDrugTakingData.province ? detailsOfDrugTakingData.province : ''}
                  {detailsOfDrugTakingData && detailsOfDrugTakingData.city ? detailsOfDrugTakingData.city : ''}
                  {detailsOfDrugTakingData && detailsOfDrugTakingData.floor ? detailsOfDrugTakingData.floor : ''}
                  {detailsOfDrugTakingData && detailsOfDrugTakingData.area ? detailsOfDrugTakingData.area : ''}
                  {detailsOfDrugTakingData && detailsOfDrugTakingData.village ? detailsOfDrugTakingData.village : ''}
                  {detailsOfDrugTakingData && detailsOfDrugTakingData.address ? detailsOfDrugTakingData.address : ''}
                </div>
              </Item>
            </List>
          </div>
        ) : "")}
      </div>
    )
  }
}