import React, { Component } from "react";
import style from "./index.less";
import { normalVal, filterDataArr, filterOtherDataArr,timestampToTime } from '@utils/tools';
import Notify from '@commonComp/Notify';
import Information from '../Information';
import $ from "jquery";
// import AssessResultHis from '@containers/AssessResultHis'

// import "@utils/jqprint";

class PreviewBody extends Component {
  constructor(props) {
    super(props)
    this.state = {
      dateTime: "",
    }
    this.getCurrentDate = this.getCurrentDate.bind(this);
    this.onPrint = this.onPrint.bind(this);
  }
  componentDidMount() {
    this.getCurrentDate();
  }
  onPrint() {
    $("#content").jqprint({
      debug: false,
      importCSS: true,
      printContainer: true,
      operaSupport: false,
    });
    this.props.onClose()
  }

  surePrint(jsonStr) {
    if (filterDataArr(JSON.parse(jsonStr.chief)) == '') {
      Notify.info('主诉不能为空');
      return false;
    } else if (!jsonStr.diag || jsonStr.diag.trim().length < 1) {
      Notify.info('诊断不能为空');
      return false;
    } else {
      this.onPrint();
      this.props.save(true)
    }
  }
  getCurrentDate() {
    let myDate = new Date();
    let year = myDate.getFullYear();       //获取完整的年份(4位,1970-????)
    let mon = myDate.getMonth() - 0 + 1;       //获取当前月份(0-11,0代表1月)
    let day = myDate.getDate();            //获取当前日(1-31)
    let date = year + '-' + (mon < 10 ? '0' + mon : mon) + '-' + (day < 10 ? '0' + day : day);
    this.setState({ dateTime: date })
  }
  showDetails(val) {
    if (val.questionDetailList.length > 0) {
      return val.questionDetailList.map((item) => {
        if (val.value == item.name) {
          return <td style={{ width: '16%', fontSize: '12px' }}><span style={{ color: item.abnormal != '0' ? "red" : null }}>{val.value}</span> {val.labelSuffix}</td>
        }
      })
    } else {
      return <td style={{ width: '16%', fontSize: '12px' }}><span style={{ color: (val.value - 0).toString() == 'NaN' ? "red" : (val.maxValue || val.minValue) ? (val.value > val.maxValue || val.value < val.minValue ? "red" : null) : null }}>{val.value}</span> {val.labelSuffix}</td>
    }
  }

  toTime(time){
    let tmpTim = time.split(',').join('')-0
    if(time && tmpTim.toString() != 'NaN'){
      let date = new Date('1900-01-01');
      let dateTim = date.getTime();
      let times = (tmpTim-2)*24*60*60*1000;
      let result = timestampToTime(dateTim+times).split(' ')[0]
      return result;
    }else{
      return time;
    }
  }
  render() {
    const { show, preInfo, dataJson, dataStr, baseObj, flg } = this.props;
    const other_yjs = filterOtherDataArr(JSON.parse(dataStr.other),dataJson.other);
    const noData = JSON.stringify(preInfo) == '{}';
    return <div className={style['content']} style={{ width: flg ? '700' : '820' }}>
      <div className={style['contents']} id="content" style={{ margin: "0 auto", maxWidth: "600px" }}>
        <Information baseObj={baseObj} preInfo={preInfo}></Information>
        <table className={style['infos']}>
          <tr className={style['patInfoFst']}>
            <td className={style['patInfoSec']}>主诉：</td>
            <td className={style['patInfoSec']}>
              {
                filterDataArr(JSON.parse(dataStr.chief))
              }
            </td>
          </tr>
          <tr className={style['patInfoFst']}>
            <td className={style['patInfoSec']}>现病史：</td>
            <td className={style['patInfoSec']}>
              {
                filterDataArr(JSON.parse(dataStr.present))
              }
            </td>
          </tr>
          <tr className={style['patInfoFst']}>
            <td className={style['patInfoSec']}>其他史：</td>
            <td className={style['patInfoSec']}>
              {
                other_yjs.str1
              }
              {dataJson['yjs_1']||dataJson['yjs_2']||dataJson['yjs_3']||dataJson['yjs_4']?<table style={{textAlign:'center',verticalAlign: 'middle',display:'inline-block'}}>
                <tr>
                  <td rowSpan='2' style={{verticalAlign: 'middle'}}>月经史：（</td>
                  <td rowSpan='2' style={{verticalAlign: 'middle'}}>{dataJson['yjs_1']||'初潮年龄'}</td>
                  <td style={{borderBottom:'1px solid #000'}}>{dataJson['yjs_2']||'行经天数'}</td>
                  <td rowSpan='2' style={{verticalAlign: 'middle'}}>{dataJson['yjs_4']||'停经时间'}）</td>
                </tr>
                <tr>
                  <td style={{textAlign:'center'}}>{dataJson['yjs_3']||'周期'}</td>
                </tr>
              </table>:''}
              {
                other_yjs.str2
              }
            </td>
          </tr>
          <tr className={style['patInfoFst']}>
            <td className={style['patInfoSec']}>查体：</td>
            <td className={style['patInfoSec']}>
              {
                filterDataArr(JSON.parse(dataStr.vital))
              }
            </td>
          </tr>
          <tr className={style['patInfoFst']}>
            <td className={style['patInfoSec']}>化验：</td>
            <td className={style['patInfoSec']}>
              {
                dataJson.lis.getExcelDataList && dataJson.lis.getExcelDataList.map((items) => {
                  return items.lisExcelRes && items.lisExcelRes.map((item) => {
                    return <table style={{ marginBottom: '10px', width: '100%', fontSize: '12px' }}>
                      <tr style={{ fontSize: '14px', lineHeight: '1.5' }}><td><span style={{ borderBottom: '1px solid #666666' }}>{item.menus}</span></td></tr>
                      {
                        item.lisExcelItem && item.lisExcelItem.map((value) => {
                          return <tr style={{ lineHeight: '1.5', maxWidth: '650px' }}>
                            <td style={{ width: '32%', fontSize: '12px' }}>{value.itemName}</td>
                            <td style={{ width: '16%', fontSize: '12px' }}><span style={{ fontSize: '12px', color: value.type == 1 ? "red" : null }}>{value.value}</span> {value.unit}</td>
                            <td style={{ width: '24%', fontSize: '12px' }}>
                              {normalVal(value.min, value.max)}
                            </td>
                            <td style={{ width: '28%', fontSize: '12px' }}>{value.time == '' ? ('导入时间: ' + this.state.dateTime) : '化验时间: ' + this.toTime(value.time)
                          }</td>
                          </tr>
                        })
                      }
                    </table>
                  })
                })
              }
              {
                dataJson.lis.labelList && dataJson.lis.labelList.map((item, idx) => {
                  // if (item.show) {
                    return <table style={{ margin: '8px 0', width: '100%' }}>
                      {
                        item.show?<tr style={{ fontSize: '14px', lineHeight: '1.5' }}><td><span style={{ borderBottom: '1px solid #666666' }}> {item.name} </span></td></tr>:
                        <tr style={{ fontSize: '14px', lineHeight: '1.5' }}><td><span style={{ position:'relative',left:'-10px' }}>「{item.name}」</span></td></tr>
                      }
                      {
                        item.details.map((val) => {
                          if (val.value && val.value != '') {
                            return <tr style={{ lineHeight: '1.5', width: '100%', fontSize: '12px' }}>
                              <td style={{ width: '32%', fontSize: '12px' }}>{val.name}</td>
                              {this.showDetails(val)}
                              <td style={{ width: '24%', fontSize: '12px' }}>
                                {normalVal(val.minValue, val.maxValue)}
                              </td>
                              <td style={{ width: '28%', fontSize: '12px' }}>{'化验时间:' + item.time}</td>
                            </tr>
                          }
                        })
                      }
                    </table>
                  // }
                })
              }
            </td>
          </tr>
          <tr className={style['patInfoFst']}>
            <td className={style['patInfoSec']}>辅检：</td>
            <td className={style['patInfoSec']}>
              {
                dataStr.pacs && dataStr.pacs != '' && dataStr.pacs.split(';').map((item) => {
                  return <div style={{ fontSize: '14px',wordBreak:"break-all" }}>{item}</div>
                })
              }
            </td>
          </tr>
          <tr className={style['patInfoFst']}>
            <td className={style['patInfoSec']}>诊断：</td>
            <td className={style['patInfoSec']}>
              {
                dataStr.diag && dataStr.diag != '' && dataStr.diag.split(';').map((item) => {
                  return <div style={{ fontSize: '14px' }}>{item}</div>
                })
              }
            </td>
          </tr>
          <tr className={style['patInfoFst']}>
            <td className={style['patInfoSec']}>医嘱：</td>
            <td className={style['patInfoSec']} style={{ fontSize: '14px', padding: '1px 0px 8px 0px', lineHeight: '20px' }}>
              {dataJson.advice.commontreatment && dataJson.advice.commontreatment.length > 0 && <p style={{ fontSize: '14px', marginBottom: '8px', marginTop: '8px' }}><span>一般治疗</span></p>}
              {
                dataJson.advice.commontreatment && <div style={{ padding: '0px 0px 5px 0px',wordBreak:"break-all" }}>{dataJson.advice.commontreatment}</div>
              }
              {dataJson.advice.scheme && dataJson.advice.scheme.length > 0 && <p style={{ fontSize: '14px', marginBottom: '8px', marginTop: '8px' }}><span>治疗方案</span></p>}
              {
                dataJson.advice.scheme && dataJson.advice.scheme.map((item, index) => {
                  return <p>{item.treatment.map((it, ii) => {
                    return (it.treatmentStr && it.treatmentStr.length > 0 ?
                      <div style={{ padding: '0px 0px 5px 0px' }}>{it.treatmentStr}</div> : '')
                    })}</p>
                  })
              }
              {
                dataJson.advice.adviceInput && <div style={{ padding: '0px 0px 5px 0px',wordBreak:"break-all" }}>{dataJson.advice.adviceInput}</div>
              }
            </td>
          </tr>
        </table>
        <p style={{textAlign:'right',marginTop:'15px'}}>医生签名：<span style={{minWidth: '60px',padding: '0 5px 2px 5px',borderBottom: '1px solid #333',}}>{baseObj ? baseObj.doctorName : (noData ? '' : preInfo.doctorName)}</span></p>
        {/*<AssessResultHis inquiryId={baseObj&&baseObj.inquiryCode}></AssessResultHis>*/}
      </div>

      <div onClick={() => { this.surePrint(dataStr) }} style={{
        display: show ? 'inline-block' : 'none',
        padding: '10px 20px',
        float: 'right',
        marginTop: '15px',
        background: '#3B9ED0',
        color: '#fff',
        borderRadius: '4px',
        cursor: 'pointer',
        position: 'absolute',
        right: '40px'
      }}>打印</div>
    </div>
  }
}

export default PreviewBody;
