// 手术申请
import React, { Component } from 'react';
import { Row, Col, Table, Select, Button, Divider, message, Checkbox, Input, Icon, Tooltip, TimePicker } from 'antd';
import { $http } from '../../../containers/config/https';
import moment from 'moment';
import { Util } from 'tools/index';
import CommonPagination from 'components/pagination/index';
import CommonRangePicker from 'components/rangePicker/index';
import store from 'store';


// 用户数据
let userData;
export default class OperationPatients extends Component {
  constructor(props) {
    super(props);
    this.state = {
      columns: [],
      tableData: [],              // table数据
      total: 0,                  // table数据总量            
      applyLocData: [],			// 申请科室下拉数据
      operationRoomData: [],		// 手术间下拉数据
      operationTypeData: [],		// 手术类型下拉数据
      operationStatusData: [],	// 手术状态下拉数据
      patWardData: [],			// 患者病区下拉数据
      stDate: moment().format('YYYY-MM-DD'), 				// 开始日期
      endDate: moment().format('YYYY-MM-DD'),     // 结束日期
      applyDateFlag: '',			// 按申请日期
      applyLocID: '',				// 申请科室
      operationRoomID: '',		// 手术间
      operationStatusID: '',		// 手术状态
      operationTypeID: '',		// 手术类型
      patWardID: '',				// 患者病区
      number: '',					// 患者信息
      condition: '',				// 条件
      page: '1',
      pageSize: '10',
      clickFlag: false,
      checked: false,
      patNumber: '',
      errorList: [],
      clickCol: '',
      tableWidth: 0,
      selectedRows: [],//左边的勾选清除
      editingKey: '',
      opertaionData: [],
      operationStatus: '',
      DataList: [],
      operationType: '',
      TableListData: [],
     //tableType: 'C',
      ENObj: {},
			CNObj: {},
    }
    userData = sessionStorage.userData ? JSON.parse(sessionStorage.userData) : {};
  }

  componentDidMount() {
    this.getOperationPatients();
    this.getApplyLocData();
    this.getPatWardData();
    this.getOption();
    this.getselect();
    //	document.querySelector('.ant-table-body').style.height = store.getState().tableHeight.y + 'px';
  }

  //查询表头信息
  getOperationPatients = () => {
    let data = {
      params: [{
        reactCode: ['OperationPatList'],
				compontName: 'OperationPatList',
				type: "C",
        // compontName: 'OperationPatList',
        // type: this.state.tableType,
      }]
    }
    $http.post('urlS', this, {
      code: '01040073',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          let column = res.result.C;
					if (userData.language == 'EN') {
						for (let i = 0; i < column.length; i++) {
							column[i].title = column[i].enDescripts
						}
					}
          this.setState({
            columns: resData,
            tableWidth: res.totalWidth
          }, () => {
            this.getOperationInfo();
          })
        }
      }
    });
  }
  //患者病区
  handlePat = (e) => {
    this.setState({
      patWardID: e
    }, () => {
      this.getOperationInfo();
    });
  };
  //登记名/证件/姓名
  handlePatNumber = (e) => {
    this.setState({
      patNumber: e.target.value
    }, () => {
      this.getOperationInfo();
    })
  }
  //状态
  handleStatusData = (e) => {
    this.setState({
      operationStatus: e
    }, () => {
      this.getOperationInfo();
    })
  }
  //类型
  handleoperationType = (e) => {
    this.setState({
      operationType: e
    }, () => {
      this.getOperationInfo();
    })
  }

  // 获取下拉框数据
  getselect = () => {
    let data = {
      params: [{
        locID: userData.locID
      }]
    };
    $http.post('urlDeault', this, {
      code: "04160005",
      data: data,
      success: function (res) {
        //	console.log(res);
        if (+res.errorCode === 0) {
          this.setState({
            operationRoomData: res.result
          })
        }
      },
      error: function (error) {
        message.error('失败：' + error);
      }
    });
  }
  //获取科室下拉数据
  getApplyLocData = () => {
    let data = {
      params: [{
        hospID: userData.hospID,
        type: 'O^I^OI^E',
      }]
    }
    $http.post('urlS', this, {
      code: '04020008',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          this.setState({
            applyLocData: res.result.Data,
          })
        }
      }
    });
  }
  //获取病区下拉数据
  getPatWardData = () => {
    let data = {
      params: [{
        hospID: userData.hospID,
        type: 'W',
      }]
    }
    $http.post('urlS', this, {
      code: '04020008',
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          this.setState({
            patWardData: res.result.Data,
          })
        }
      }
    });
  }
  // 获取下拉数据源
  getSelectData = () => {
    let data = {
      params: [{

      }]
    };
    $http.post('urlDeault', this, {
      code: "**",
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          this.setState({
            conTypeData: res.result.conType
          })
        }
      },
      error: function (error) {
        message.error('失败：' + error);
      }
    });
  }

  // 获取下拉数据源
  getOption = () => {
    let data = {
      params: [{

      }]
    };
    $http.post('urlDeault', this, {
      code: "04160007",
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          this.setState({
            DataList: res.result,
            operationStatusData: res.result.operationStatusData,
            operationTypeData: res.result.operationTypeData
          })
        }
      },
      error: function (error) {
        message.error('失败：' + error);
      }
    });
  }
  // 查询手术信息
  getOperationInfo = () => {
    this.setState({
      clickFlag: true
    })
    let { page, pageSize, patWardID, patNumber, operationStatus, operationType } = this.state;
    let data = {
      "params": [{
        startDate: moment(this.state.stDate).format('YYYY-MM-DD'),
        stopDate: moment(this.state.endDate).format('YYYY-MM-DD'),
        applyFlag: this.state.checked ? 'Y' : 'N',
        applyLocID: "",
        operationRoomID: "",
        operationStatusID: operationStatus,
        operationType: operationType,
        patNumber: patNumber,
        patWardID: patWardID
      }],
      pagination: [{
        pageSize: pageSize,
        currentPage: page,

      }]
    };
    $http.post('urlDeault', this, {
      code: "04160008",
      data: data,
      success: function (res) {
        if (+res.errorCode === 0) {
          let tableData = res.result.Data;
          for (let i = 0; i < tableData.length - 1; i++) {
            tableData[i].key = i
          }
          this.setState({
            tableData,
            clickCol: '',
            total: res.result.TotalCount,
            clickFlag: false
          })
        } else {
          this.setState({
            clickFlag: false
          })
        }
      },
      error: function (error) {
        message.error('失败：' + error);
        this.setState({
          clickFlag: false
        })
      }
    });
  }

  // 翻页
  onChange = (page, pageSize) => {
    this.setState({ page: page, pageSize: pageSize }, () => {
      // 重新调用查询方法
      this.getOperationInfo();
    });
  };

  onShowSizeChange = (current, size) => {
    this.setState({ page: current, pageSize: size }, () => {
      // 重新调用查询方法
      this.onChange(current, size);
    })
  };
  // 获取Checkbox操作后的值
  handleCheckbox = (e) => {
    this.setState({ checked: e.target.checked });
  }
  render() {
    var language = userData.language
		var languageObj = {}
		if (language == "EN") {
			languageObj = this.state.ENObj
		} else {
			languageObj = this.state.CNObj
		}
    const formItemDate = {
      xs: { span: 24 },
      sm: { span: 12 },
      md: { span: 10 },
      lg: { span: 6 }
    };
    const iconStyle = {
      color: 'rgba(0,121,254,1)'
    }
    const spanStyle = {
      color: 'rgba(0,121,254,1)',
      marginRight: '5px'
    }
    return (
      <div style={{ width: '100%', height: '100%' }}>
        <div className="table-operations">
          <Row style={{ minHeight: '80px', padding: '0 24px 24px 24px' }}>
            <Col span={20}>
              <span style={{ display: 'inline-block', margin: '23px 20px 0 0', marginLeft: '24px' }}>
                <CommonRangePicker
                  width={300}
                  label='开始日期:'
                  startDate={moment()}
                  endDate={moment()}
                  format={'YYYY-MM-DD'}
                  startChange={(date) => this.setState({ stDate: date })}
                  endChange={(date) => this.setState({ endDate: date })}
                />
              </span>
              <Checkbox style={{ marginTop: '9px', marginRight: '20px' }} checked={this.state.checked} onChange={this.handleCheckbox}>申请日期</Checkbox>
              状态：	<Select
                allowClear
                style={{ width: '170px', margin: '23px 20px 0 0' }}
                value={this.state.operationStatus} onChange={this.handleStatusData}
              >
                {this.state.operationStatusData.map(data => (<Select.Option key={data.id} value={data.id}>{data.descripts}</Select.Option>))}
              </Select>
              类型：	<Select
                allowClear
                style={{ width: '170px', margin: '23px 20px 0 0' }}
                value={this.state.operationType} onChange={this.handleoperationType}
              >
                {this.state.operationTypeData.map(data => (<Select.Option key={data.id} value={data.id}>{data.descripts}</Select.Option>))}
              </Select>
              患者病区： <Select
                value={this.state.patWardID} onChange={this.handlePat}
                allowClear
                style={{ width: '170px', margin: '23px 20px 0 0' }}
              >
                {this.state.patWardData.map(data => (<Select.Option key={data.id} value={data.id}>{data.descripts}</Select.Option>))}
              </Select>
              患者： <Input
                style={{ width: '170px', margin: '23px 20px 0 0' }}
                allowClear
                placeholder='患者ID/证件号/姓名'
                value={this.state.patNumber}
                onChange={this.handlePatNumber}
              >
              </Input>
            </Col>
            <Col span={4} style={{ textAlign: 'right' }}>
              <Button type='primary' style={{ margin: '23px 20px 0 0' }} onClick={this.getOperationInfo}>查询</Button>
            </Col>
          </Row>
        </div>
        <div style={{ widht: '100%', height: '10px', background: '#f0f2f5' }}></div>
        <div style={{ padding: 24 }}>
          <div>
            <Table
              style={{ margin: '0 20px 0', height: store.getState().tableHeight.y + 45 + 'px' }}
              columns={this.state.columns}
              dataSource={this.state.tableData}
              pagination={false}
              scroll={{ x: 1000, y: store.getState().tableHeight.y }}
              bordered
              showHeader={true}
              className="table-body-height"
              rowKey={(row) => {
                return row.id
              }}
            />
            {/* 分页: total--总条数  defaultCurrent--默认的当前页 hideOnSinglePage--只有一页时隐藏分页器 */}
            <CommonPagination
              size='small'
              current={this.state.page}
              total={this.state.total}
              search='search'
              onShowSizeChange={this.onShowSizeChange.bind(this)}
              complete={true}
              dataSource={this.state.tableData}
              onChange={this.onChange.bind(this)}
            />
          </div>
        </div>

      </div>
    )
  }
}