// 预住院查询
// @ByGJG
import React, { Component } from 'react';
import { Card, Row, Col, Input, Button, Table, message } from 'antd';
import { $http } from 'containers/config/https';
import store from 'store';
import iconListvisits from 'assets/images/icon_listvisits.png';
import { $loadTemplate } from '../lodop/Common/Print.js';


const formItemLayout = { // label 和input宽度
  labelCol: { span: 8 },
  wrapperCol: { span: 14 },
}
class PreInQuery extends Component {
  constructor(props) {
    super(props);
    this.state = {
      input: undefined,
      basicHeight: 0,
      tableData: [],
      loading: false,
      visible: false,
      userInfo: JSON.parse(window.sessionStorage.userData),
      tableObj: {},
      rowID: "",
      columns: []
    };

    this.columns = [
      {
        title: '患者ID',
        dataIndex: 'patID',
        width: 80,
        align: 'center'
      },
      {
        title: '登记号',
        dataIndex: 'patNo',
        width: 120,
        align: 'center'
      },
      {
        title: '患者姓名',
        dataIndex: 'patName',
        width: 100,
        align: 'center'
      },
      {
        title: '患者性别',
        dataIndex: 'patSexDesc',
        width: 80,
        align: 'center'
      },
      {
        title: '身份证号',
        dataIndex: 'patIDCardNo',
        width: 160,
        align: 'center'
      },
      {
        title: '住院号',
        dataIndex: 'patMedicalNo',
        width: 100,
        align: 'center'
      },
      {
        title: '手机号',
        dataIndex: 'patMobile',
        width: 120,
        align: 'center'
      },
      {
        title: '就诊号',
        dataIndex: 'admNo',
        width: 150,
        align: 'center'
      },
      {
        title: '就诊状态',
        dataIndex: 'admStatusDesc',
        width: 100,
        align: 'center'
      },
      {
        title: '就诊科室',
        dataIndex: 'admLocDesc',
        width: 120,
        align: 'center'
      },
      {
        title: '就诊病区',
        dataIndex: 'admWardDesc',
        width: 120,
        align: 'center'
      },
      {
        title: '就诊类型',
        dataIndex: 'admTypeDesc',
        width: 120,
        align: 'center'
      },
      {
        title: '预交金',
        dataIndex: 'deposit',
        width: 120,
        align: 'center'
      },
      {
        title: '审核状态',
        dataIndex: 'audiFlagDesc',
        width: 120,
        align: 'center'
      },
    ]
  }

  componentDidMount() {
    this.setState({ basicHeight: document.getElementById("diagnostic-query").clientHeight }, () => {
      this.tableList()
      this.getTableData()
    });
  }


  //输入框
  handleInputChange = (e) => {
    this.setState({
      input: e.target.value
    })
  }
  //输入框回车查询
  InputOnPressEnter = (e) => {
    this.setState({
      input: e.target.value
    }, () => {
      this.getTableData();
    })
  }

  // 打印预住院转门诊凭证
  print = (preInAuditingID) => {
    console.log("preInAuditingID",preInAuditingID)
    if (!preInAuditingID || preInAuditingID === '') {
      console.log("this.state.tableObj",this.state.tableObj)
      preInAuditingID=this.state.tableObj.preInAuditingID
    }
    let data = {};
    data = {
      params: [{
        preInAuditingID: preInAuditingID
      }]
    }
    let code = '05100405'
    let templateCode = 'PreInAuditing'
    $http.post('urlS', this, {
      code: code,
      data: data,
      success: function (res) {
        if (res.errorCode === '0') {
          //加载模板
          $loadTemplate(templateCode, res.result);
        }
      }
    })
  }

  // 获取列表数据
  getTableData = () => {
    let { input } = this.state
    var params = {
      params: [{
        input: input || '',
      }],
    }
    $http.post('urlDeault', this, {
      code: "05100402",
      data: params,
      success: function (res) {
        if (res.errorCode == '0') {
          this.setState({
            tableData: res.result.rows,
            total: res.result.total,
            tableObj: {},
            loading: false,
            rowID: ''
          })
        } else {
          message.warning(res.errorMessage)
          this.setState({
            loading: false
          })
        }
      }
    });
  }
  //获取表头
  tableList = () => {
    var data = {
      params: [{
        "reactCode": ["PreHospitalizationQuery"],
        "compontName": "PreHospitalizationQuery",
        "type": "C"
      }]
    }
    $http.post('urlS', this, {
      code: '01040073',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          let resC = res.result.C
          for (var i = 0; i < resC.length; i++) {
            resC[i].key = i;
          }
          resC.map((item, i) => {
            item.title = item.descripts
          })
          let filterArr = resC.filter(function (item) { return item.display === 'Y' });
          this.setState({
            columns: filterArr,
          })
        }
      }
    })
  }

  //转住院
  turnInAdm = () => {
    let { rowID, tableObj } = this.state
    if (rowID == "") {
      message.info("请选中一条记录再进行操作！")
      return
    }
    var params = {
      params: [{
        admType: "I",
        admID: rowID
      }],
    }
    $http.post('urlDeault', this, {
      code: "05100403",
      data: params,
      success: function (res) {
        if (res.errorCode == '0') {
          message.success(res.errorMessage)
          this.getTableData()
        }
      }
    });
  }
  //转门诊审核
  turnOutAdm = () => {
    let { rowID, tableObj } = this.state
    if (rowID == "") {
      message.info("请选中一条记录再进行操作！")
      return
    }
    if (tableObj && tableObj.preInAuditingID != "") {
      message.info("该记录已审核，请勿继续操作！")
      return
    }
    var params = {
      params: [{
        status: 'A',
        admID: rowID,
        preInAuditingID: tableObj.preInAuditingID
      }],
    }
    $http.post('urlDeault', this, {
      code: "05100404",
      data: params,
      success: function (res) {
        if (res.errorCode == '0') {
          message.success(res.errorMessage)
          this.getTableData()
          this.print(res.result)
        }
      }
    });
  }
  //选中行
  onClickRowPublic = (record) => {
    return {
      // 单击行选中
      onClick: () => {
        this.setState({ rowID: record.admID, tableObj: record });
      }
    }
  }
  setRowClassNamePublic = (record) => {
    return (record.admID || record.admID) === this.state.rowID ? 'clickRowStyle' : '';
  }
  render() {
    let { input, basicHeight, tableData, loading, columns } = this.state;
    return (
      <div className="diagnostic-query">
        <Row id="diagnostic-query" style={{ padding: '0 24px' }}>
          <Col span={16} >
            <Row style={{ marginBottom: '20px' }}>
              病人标识：
              <Input
                style={{ width: "250px", margin: '22px 10px 0 0' }}
                placeholder="登记号/病人ID/病人姓名"
                allowClear
                onChange={this.handleInputChange} value={input}
                onPressEnter={this.InputOnPressEnter}
              />
              <Button style={{ marginTop: '8px' }} onClick={this.getTableData}>查询</Button>
            </Row>
          </Col>
          <Col span={8}  >
            <Row style={{ marginBottom: '20px' }}>
              <div style={{ padding: '22px 10px 0 0' }} >
                <Button type="primary" style={{ marginLeft: '8px' }} onClick={() => this.turnInAdm()}>转住院</Button>
                <Button type="primary" style={{ marginLeft: '8px' }} onClick={() => this.turnOutAdm()}>转门诊审核</Button>
                <Button type="primary" style={{ marginLeft: '8px' }} onClick={() => this.print()}>补打凭证</Button>
              </div>
            </Row>
          </Col>
        </Row>
        <div style={{ height: '6px', background: 'rgba(240, 242, 245, 1)', width: '100%' }}></div>
        <Card
          size="small"
          title={<div className="card-title-left-img"><img src={iconListvisits} alt="" />预住院列表</div>}
        >
          <div>
            <Table
              columns={columns ? columns : this.columns}
              dataSource={tableData}
              style={{ height: store.getState().tableHeight.y + 136 - basicHeight + 'px', }}
              scroll={{ x: 480, y: store.getState().tableHeight.y + 80 - basicHeight }}
              onRow={this.onClickRowPublic}
              rowClassName={this.setRowClassNamePublic}
              bordered
              rowKey={record => record.key}
              loading={loading}
              pagination={false}
              className="table-body-height"
            />
          </div>
        </Card>
      </div>
    );
  }
}

export default PreInQuery;
