/*
 * Creator:     ML
 * CreatDate:   2019-12-03
 * descripts：  门诊医生医嘱录入 一键打印 （处方，导诊，申请）
*/
import React from 'react';
import { Tabs, Row, Col, Icon, Divider, Modal, Button, Checkbox, message, Card } from 'antd';
import { $http } from 'containers/config/https';
import store from '../../../store';
import { $loadTemplate } from '../../lodop/Common/Print.js';

class PrintingOneStep extends React.Component {
  constructor(porps) {
    super(porps);
    this.state = {
      countArr: [],
      totalCount: 0,
      loading: false
    }

  }
  componentDidMount() {
    this.getOneStepData()
    this.props.onStep(this)
  }
  getOneStepData = () => {
    let data = {}
    data = {
      params: [{
        userID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID : "",
        admID: sessionStorage.patientData ? JSON.parse(sessionStorage.patientData).admID : "",
        hospID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).hospID : ""
      }],
    }
    $http.post('urlS', this, {
      code: '03050148',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          this.setState({
            countArr: res.count,
            totalCount: res.totalCount,
            loading: false
          })
        }
      },
    })
  }
  //点击打印
  handlePrit = () => {
    let data = {}
    this.setState({ loading: true })
    data = {
      params: [{
        userID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).userID : "",
        admID: sessionStorage.patientData ? JSON.parse(sessionStorage.patientData).admID : "",
        hospID: sessionStorage.userData ? JSON.parse(sessionStorage.userData).hospID : "",
        isPrint: "Y"
      }],
    }
    $http.post('urlS', this, {
      code: '03050148',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          if (res.result && res.result.length > 0) {
            res.result.map((item, i) => {
              $loadTemplate(item.template, item.printStr);
            })
            this.getOneStepData()
          } else {
            this.setState({
              loading: false
            })
          }
        }else{
          this.setState({
            loading: false
          })
        }
      },
    })
  }

  //跳转到相应的打印页面-策略模式
  handleToPrit = (code) => {
    let toObj = {
      '处方': () => this.props.callback("1"),
      '申请单': () => this.props.callback("2"),
      '导诊单': () => this.props.callback("4"),
      '检验告知单': () => this.props.callback("6"),
    }
    if (toObj[code]) {
      toObj[code]();
    }
  }
  render() {
    const { countArr, totalCount } = this.state
    let tabsHeight = store.getState().tableHeight.y + 110 + 'px'
    return (
      <div className="oneStepDom" style={{ height: tabsHeight }}>
        <div className="oneStep-item">
          <Row className="row-item">
            <Col span={9}></Col>
            <Col span={15}>本次打印共计：<span className="item-span">{totalCount}</span></Col>
          </Row>
          {
            countArr && countArr.map((item, i) => {
              return (
                <Row className="row-item">
                  <Col span={12}></Col>
                  <Col span={12} style={{
                    paddingLeft: (item.code == "presc" ? "26px" : ""),
                    position: (item.code == "presc" ? "relative" : "inherit")
                  }}>{item.descripts + "："}<span className="item-span">{item.count}</span>
                    <span onClick={() => this.handleToPrit(item.descripts)} style={{ color: "#096dd9", cursor: "pointer", fontSize: "14px", marginLeft: "2px" }}>(查看详情)</span>
                    <span style={{
                      display: (item.code == "presc" ? "inline-block" : "none"),
                      position: "absolute", top: "54px", left: "26px", height: "12px", lineHeight: "12px", fontSize: "12px"
                    }}>(当前为未打印处方,若需要打印所有请前往左侧处方打印)</span></Col>
                </Row>
              )
            })
          }
          <Row>
            <Col span={12}></Col>
            <Col span={12} style={{ paddingLeft: "136px" }}>
              <Button type={totalCount && totalCount > 0 ? "primary" : ""} loading={this.state.loading}
                disabled={totalCount && totalCount == 0} onClick={this.handlePrit}>一键打印</Button>
            </Col>
          </Row>
        </div>
      </div>
    )
  }
}
export default PrintingOneStep