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

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) {
    let min = val.minValue, max = val.maxValue, value = val.value, dom = '';
    dom = getDomUpDown(min,max,value)
    if (val.questionDetailList.length > 0) {
      return val.questionDetailList.map((item) => {
        if (val.value == item.name) {
          return <td className={style.assistTableTdSec}><span style={{ color: item.abnormal != '0' ? "red" : null }}>{val.value}</span> {val.labelSuffix}</td>
        }
      })
    } else {
      return <td className={style.assistTableTdSec}>{dom} {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 ,come,showAssessBtn,showHistoryCases} = this.props;
    let other_data={},lis_data={},pas_data={},other_yjs='',access='',adviceData={};
    if(!come){
      other_data = JSON.parse(baseObj.detailList[2].contentJson)
      lis_data = JSON.parse(baseObj.detailList[4].contentJson)
      pas_data = JSON.parse(baseObj.detailList[5].contentJson)
      adviceData = JSON.parse(baseObj.detailList[7].contentJson)
      other_yjs = other_data&&other_data.pfix;
      access = other_data&&other_data.haveAssess
    }else{
      other_yjs = dataStr.other?filterOtherDataArr(JSON.parse(dataStr.other),dataJson.other):'';
      access = dataJson.haveAssess
      adviceData=dataJson.advice
    }
    //const isChronic = (dataJson.diagChronicMagItem&&dataJson.diagChronicMagItem.name)||(dataJson.mainChronicDesease&&dataJson.mainChronicDesease.name);
    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: "620px" }}>
        <Information baseObj={baseObj} preInfo={preInfo}></Information>
        <table className={style['infos']}>
          <ItemPart dataStr={dataStr.chief} title={'主诉：'} type={1}></ItemPart>
          <ItemPart dataStr={dataStr.present} title={'现病史：'} type={1}></ItemPart>
          <ItemPart dataStr={dataStr.present} title={'其他史：'} dataJson={come?dataJson:other_data} other_yjs={other_yjs} type={3}></ItemPart>
          <ItemPart dataStr={dataStr.vital} title={'查体：'} type={1}></ItemPart>
          <PreviewInspect dataJson={come?dataJson.lis:lis_data} toTime={this.toTime} dateTime={this.state.dateTime} showDetails={this.showDetails}></PreviewInspect>
          <ItemPart dataStr={dataStr.pacs} title={'辅检：'} type={2}></ItemPart>
          <ItemPart dataStr={dataStr.diag} title={'诊断：'} type={2}></ItemPart>
          <tr className={style['patInfoFst']}>
            <td className={style['patInfoSec']}>医嘱：</td>
            <td className={`${style['patInfoSec']} ${style['pushMessage']} ${style['font14']}`}>
              {((adviceData&&adviceData.assay && adviceData.assay.length > 0) || ((adviceData&&adviceData.check)&& (adviceData&&adviceData.assay.check > 0))) && <p className={style.pushMessageTitle}><span>开单项目</span></p>}
              {
                ((adviceData&&adviceData.assay && adviceData.assay.length > 0) || ((adviceData&&adviceData.check) && (adviceData&&adviceData.assay.check > 0))) && <div className={style.pushMessageDes}>{adviceData.check?adviceData.check+',':''} {adviceData.assay}</div>
              }
              {adviceData&&adviceData.commontreatment && adviceData.commontreatment.length > 0 && <p  className={style.pushMessageTitle}><span>一般治疗</span></p>}
              {
                adviceData&&adviceData.commontreatment && <div dangerouslySetInnerHTML={{__html: adviceData.commontreatment}} className={style.pushMessageDes}></div>
              }
              {adviceData&&adviceData.followUp && adviceData.followUp.length > 0 && <p className={style.pushMessageTitle}><span>回访时间：<span className={style['bbtm']}>{adviceData.followUp}</span> 后回访，不适随诊</span></p>}
              {adviceData&&adviceData.scheme && adviceData.scheme.length > 0 && <p className={style.pushMessageTitle}><span>治疗方案</span></p>}
              {
                adviceData&&adviceData.scheme && adviceData.scheme.map((item, index) => {
                  return <p>{item.treatment.map((it, ii) => {
                    return (it.treatmentStr && it.treatmentStr.length > 0 ?
                      <div className={style.pushMessageDes}>{it.treatmentStr}</div> : '')
                    })}</p>
                  })
              }
              {
                adviceData&&adviceData.adviceInput && <div dangerouslySetInnerHTML={{__html: adviceData.adviceInput}} className={style.pushMessageDes}></div>
              }
            </td>
          </tr>
        </table>
        <p className={style.docName}>医生签名：<span>{baseObj ? baseObj.doctorName : (noData ? '' : preInfo.doctorName)}</span></p>
        {showAssessBtn&&access?<AssessResultHis showHistoryCases={showHistoryCases} inquiryId={baseObj&&baseObj.id} inquiryDate={baseObj&&baseObj.inquiryDate}></AssessResultHis>:""}
        <div onClick={() => { this.surePrint(dataStr) }} className={style.printBtn} style={{
          display: show ? 'inline-block' : 'none',
        }}>打印</div>
      </div>
    </div>
  }
}

export default PreviewBody;
