// 诊疗记录
import React from 'react';
import {Steps} from 'antd-mobile';
import './style/index.less';
import { $http } from "containers/config/https";
// import {Util} from 'tools';
import moment from 'moment';

import zljlNoDataImg from 'assets/images/zljl-nodata.png';

let {Step} = Steps;

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

  componentDidMount() {
    this.getStepListData();
  }

	componentWillUnmount() { 
		// 组件销毁前将静止让setState修改state的状态
		this.setState = (state,callback)=>{ return; }
  }

  // 获取诊疗记录数据
  getStepListData = () => {
    let {userData} = this.state;
    let data = {
			params: [{
        patID: userData&&userData.hisPatID ? userData.hisPatID : "",
        startDate: "", //  Util.getLastMonth().last
        endDate: "", // Util.getLastMonth().now
      }]
		}
		$http.post("urlS", this, {
			code: "03070067",
      data: data,
      namespace: "HIS",
			success: function(res) {
				if (+res.errorCode === 0) {
          let stepListData = res.result&&res.result.data.length > 0 ? res.result.data : [];
          this.setState({stepListData})
				} else {
					this.setState({stepListData: []});
				}
			}
		});
  }

  handleOperableClick = (item, e) => {
    e.stopPropagation();
    if (item&&item.eventCode === "01") { // 诊疗记录
      this.history.push({
        pathname: "/0003/00030012",
        state: {...item}
      })
    } else if (item&&item.eventCode === "02") { // 体检
      this.history.push({
        pathname: "/0003/00030011",
        state: {...item}
      })
    } else if (item&&item.eventCode === "03") { // 随访
      this.history.push({
        pathname: "/0003/00030013",
        state: {...item}
      })
    }
  }

  render() {
    let {stepListData} = this.state;
    let activeSteps = stepListData&&stepListData.length ? stepListData.length : 1;
    return (
      <div className="diagnosis-and-treatment">
        {stepListData&&stepListData.length > 0 ? (
          <div className="dat-content">
            <Steps size="small" current={+activeSteps - 1}>
              {stepListData.map((item, index) => {
                return <Step key={index} 
                  title={<span className={item.value === (+activeSteps) ? "steps-active" : ""}><div onClick={(e) => this.handleOperableClick(item, e)}>{item.eventDesc}<div className="step-left-date">{item.dateTime ? item.dateTime.slice(0, 4) : ""}<span>{item.dateTime ? moment(item.dateTime).format("YYYY年MM月DD日").slice(5) : ""}</span></div></div></span>} 
                  description={<div>{item.admDiag ? <span className="desc-span">{item.admDiag}</span> : ""}<span>{item.operable ? item.operable : ""}</span></div>} 
                  icon={<i className="iconfont icon-dian"></i>} 
                  // onClick={() => this.handleStepsChildClick(item, index)}
                ></Step>
              })}
            </Steps>
          </div>
        ) : <div className="record-nodata"><img src={zljlNoDataImg} alt="" /><span>暂无就诊记录</span></div>}
      </div>
    )
  }
}