
import React from 'react';//历史会诊
import { Button, Col, Row,Select,Pagination,Input,message,Tooltip,Checkbox,Modal, DatePicker,Table} from 'antd';
import { $http } from 'containers/config/https';
import store from 'store'; 
import moment from 'moment';
// import 'assets/css/system.css'

const { Option } = Select;
let userData ;
export default class HistoryConsultation extends React.Component {
            constructor(props) {
              super(props);
              userData = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
              this.state = {
                visible1: false,//弹框显示状态
                tableData:[],
                conTypeData: [],        //会诊类型下拉数据
                getDepartmentInformation:[],//科室下拉框
                checked: false, // 是否仅显示全部
               // type:'',
                conType:'',//会诊类型
                locDesc: '',
                stopDate: '',
                startDate: '',
                total: 0,// 历史会诊分页数据
                page: 1,
                pageSize: 10,
            
              },
              // 会诊查询的数据表头
                this.columns = [
                    {
                        key: 1,
                        title: '姓名',
                        dataIndex: 'patName',
                        align: "center",
                        width: '5%',
                    },
                    {
                        key: 2,
                        title: '就诊科室',
                        dataIndex: 'admLocDesc',
                        align: "center",
                        width: '5%'
                    },
                    {
                        key: 3,
                        title: '病区',
                        dataIndex: 'admWardDesc',
                        align: "center",
                        width: '5%'
                    },
                    {
                        key: 4,
                        title: '床号',
                        dataIndex: 'bedNo',
                        align: "center",
                        width: '6%'
                    },
                    {
                        key: 5,
                        title: '诊断',
                        dataIndex: 'admDiag',
                        align: "center",
                        width: '6.5%'
                    },
                    {
                        key: 6,
                        title: '会诊类型',
                        dataIndex: 'conType',
                        align: "center",
                        width: '6%'
                    },
                    {
                        key: 7,
                        title: '请会诊科室',
                        dataIndex: 'locDesc',
                        align: "center",
                        width: '6.5%'
                    },
                    {
                        key: 8,
                        title: '请会诊医师',
                        dataIndex: 'conItemRequireUserDesc',
                        align: "center",
                        width: '6%'
                    },
                    {
                        key: 9,
                        title: '申请时间',
                        dataIndex: 'conApplyTime',
                        align: "center",
                        width: '5.5%'
                    },
                    {
                        key: 10,
                        title: '会诊医院',
                        dataIndex: 'conHospDesc',
                        align: "center",
                        width: '8%'
                    },
                    {
                        key: 11,
                        title: '会诊科室',
                        dataIndex: 'conApplyLocDesc',
                        align: "center",
                        width: '5%'
                    },
                    {
                        key: 12,
                        title: '会诊医师',
                        dataIndex: 'conItemRequireUserDesc',
                        align: "center",
                        width: '5%'
                    },{
                        key: 13,
                        title: '接收时间',
                        dataIndex: 'conType',
                        align: "center",
                        width: '5%'
                    },{
                        key: 14,
                        title: '完成时间',
                        dataIndex: 'conNature',
                        align: "center",
                        width: '5%'
                    },
                    {
                        key: 15,
                        title: '会诊状态',
                        dataIndex: 'conStatus',
                        align: "center",
                        width: '5%'
                    },
                    {
                        key: 16,
                        title: '会诊地点',
                        dataIndex: 'conRequirePlace',
                        align: "center",
                        width: '5%'
                    },
                    {
                        key: 17,
                        title: '病情摘要',
                        dataIndex: 'conPatCondition',
                        align: "center",
                        width: '5%',
                        onCell: () => {
                            return {
                              style: {
                                maxWidth: 150,
                                overflow: 'hidden',
                                whiteSpace: 'nowrap',
                                textOverflow:'ellipsis',
                                cursor:'pointer'
                              }
                            }
                          },
                          render: (text) => <Tooltip placement="topLeft" title={text}>{text}</Tooltip>
                    },
                    {
                        key: 18,
                        title: '会诊目的',
                        dataIndex: 'conAims',
                        align: "center",
                        width: '5%',
                        onCell: () => {
                            return {
                              style: {
                                maxWidth: 150,
                                overflow: 'hidden',
                                whiteSpace: 'nowrap',
                                textOverflow:'ellipsis',
                                cursor:'pointer'
                              }
                            }
                          },
                          render: (text) => <Tooltip placement="topLeft" title={text}>{text}</Tooltip>
                    }
                ]
                    
            }
           
        //     componentWillReceiveProps(nextProps) {
              
           
        //   }
          componentDidMount() {
          // 数据异步请求，请求成功之后setState
            // this.handleQuery();
            this.getDepartmentInformation();
            this.getSelectData();
            this.setState({
                showLoading: false
            })
          }
          	// 3.13.23.获取科室信息
     getDepartmentInformation() {
	
			let data = {
				params: [
					{ hospID: userData.hospID}
				]
			}
			$http.post('urlS', this, {
				code: "04020008",
				data: data,
				success: function (res) {
					if (+res.errorCode === 0) {
						this.setState({ DepartmentInformation: res.result.Data})
					} else {
						this.setState({ DepartmentInformation: [] })
					}
				}
			})
		
    }
    // 查询条件 --- 科室名称
	onChangeLocDesc = (value) => {
        // console.log(e)
         this.setState({ locDesc: value },
             () =>{
             this.handleQuery();
         });
     }
     // 查询条件 --- 记录状态
     onChangeconType= (value) => {
        this.setState({ conType: value}
            , () =>{
                this.handleQuery();
            });
    }
   	// 获取Checkbox操作后的值
       handleCheckbox = (e) => {
        this.setState({checked: e.target.checked});
      }
          // 获取下拉框数据
        getSelectData() {
            let data = {
                params: [
                    {
                        hospID: userData.hospID,
                        locID: userData.locID,
                        groupID: userData.groupID,
                        userID: userData.userID,
                        type: 'apply'
                    }
                ]
            }
            $http.post('urlS', this, {
                code: "04090011",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.setState({ 
                             conTypeData: res.result.conType,   
                             handleStartDate:res.result.handleStartDate,
                             handleStopDate:res.result.handleStopDate
                    })
                    } else {
                        this.setState({ selectData: {} })
                    }
                }
            
            })
        }
               //查询
        handleQuery = () => {
            let { page, pageSize } = this.state;
            // if(this.state.checked === true) {
            //     checked = '可用'
            //   } else {
            //     checked = '不可用'
            //   }
            //console.log(this.state.type);
            let data = {
                params: [{
                    admID:'10029',
                    hospID: userData.hospID,
                    startDate:this.state.startDate,//开始时间
                    stopDate:this.state.stopDate,//结束时间
                    conApplyLocID: "",//申请科室
                    locID:"",//当前科室
                    conTypeCode:"",//会诊类型代码
                    conStatusCode:"",//会诊状态代码
                    type:this.state.checked ? 'All' : '',//默认查全部
                }],
                pagination: [{
                    pageSize: pageSize,
                    currentPage: page,
                    sortColumn: 'applyDate',
                    sortOrder: 'asc'
                }]
            }
            $http.post('urlS', this, {
                code: "04090018",
                data: data,
                success: function (res) {
                    if (+res.errorCode === 0) {
                        this.setState({tableData: res.result.Data, total: res.result.TotalCount, loading: false })
                    }
                }
            })
        }
        
          	// 显示弹框
        //   showModal = () => {
        //     // this.handleQuery('')

        //     this.setState({
        //       visible1: true,
        //     }, () => {
        //         console.log('默认查询列表')
        //     });
        //   }
          
            //历次会诊
            handleOk = () => {
                this.setState({
                    visible1: false,
            });
        }
             

            // 关闭弹框
          handleCancel = () => {
            this.setState({
              visible1: false,
            });
          }
          
            // 页码改变的回调，参数是改变后的页码及每页条数 历次会诊分页
            onChange = (page, pageSize) => {
                this.setState({ page: page, pageSize: pageSize }, () => {
                    // 重新调用查询方法
                    this.handleQuery("");
                });
            };

            onShowSizeChange = (current, size) => {
                this.setState({ page: 1, pageSize: size }, () => {
                    // 重新调用查询方法
                    this.handleQuery("");
                })
            };

            
            // 显示分页总条数
            showTotal(total) {
                return `共 ${total} 条`;
            }
            handleStDateChange = (date, datetimeString) => {
                this.setState({
                    startDate: datetimeString
                })
            }
            handleEndDateChange = (date, datetimeString) => {
                // console.log('结束时间1', datetimeString)
                this.setState({
                    stopDate: datetimeString
                })
            }

            componentWillReceiveProps (nextProps) {
                if (nextProps.visible !== this.props.visible && nextProps.visible) {
                    let currentData = moment(this.state.handleStartDate).format("YYYY-MM-DD");
                    let current = moment(this.state.handleStopDate).format("YYYY-MM-DD");
                    //console.log('当前日期', currentData)

                    this.setState({
                        stopDate: current,
                        startDate: currentData
                    }, () => {
                        this.handleQuery()
                    })

                }
            }

            render(){
                // console.log('结束时间', this.state.stopDate)
                  return(
                    <Modal
                    width={'80%'}
                    title="历史会诊记录"
                    visible={this.props.visible}
                    onOk={this.props.handleOk}
                    onCancel={this.props.handleOk}
                  >
                <div className='colors' style={{ width: '100%', height: '100%' }}>
                <div className="table-operations">
                    <Row style={{ minHeight: '60px', padding: '0 24px 10px 24px',marginTop:'-30px'}}>
                        <Col span={24}>
                            开始日期:<DatePicker
                                        style={{margin: '23px 15px 0 0' }}
                                        onChange={this.handleStDateChange} 
                                        value={this.state.startDate === "" ? moment(this.state.handleStartDate) : moment(this.state.startDate, 'YYYY-MM-DD')}
                                     />
                            结束日期:<DatePicker
                                        style={{margin: '23px 15px 0 0' }}
                                        onChange={this.handleEndDateChange}
                                         value={this.state.stopDate === "" ? moment(this.state.handleStopDate) : moment(this.state.stopDate, 'YYYY-MM-DD')} 
                                    />
                             默认查全部:<Checkbox style={{margin: '23px 15px 0 0' }} checked={this.state.checked} onChange={this.handleCheckbox}>
                                    </Checkbox>
                            请会诊科室:<Select  style={{ width: '160px', margin: '23px 15px 0 0' }}
                                       showSearch optionFilterProp="title" 
                                       allowClear  value={this.state.locDesc}
                                        onChange={this.onChangeLocDesc} 
                                >
                                {this.state.DepartmentInformation && this.state.DepartmentInformation.map(item => {
                                    return <Option key={item.id} value={item.id} title={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>
                                })}
                              </Select>
                            请会诊类型:<Select  style={{ width: '160px', margin: '23px 15px 0 0' }}
                                          value={this.state.conType} onChange={this.onChangeconType}
                                          >
                                         {this.state.conTypeData && this.state.conTypeData.map(item => {
                                                return <Option key={item.id} value={item.id} title={item.descripts + '^' + item.descriptsSPCode}>{item.descripts}</Option>
                                        })}
                              </Select>
                            <Button type="primary" style={{margin: '23px 15px 0 0' }} onClick={this.handleQuery}>查询</Button>
                            <Button type="primary"  style={{margin: '23px 15px 0 0' }}>打印</Button>
                        </Col>

                    </Row>
                </div>

                <div style={{ width: '100%', height: '10px'}}></div>
                <div style={{ width: '100%', padding: 12, overflow: 'hidden' }}>
                    <div style={{overflowX: 'auto',overflowY:'hidden' }}>
                    <Table
                        style={{height: '400px', maxHeight: '400px', overflow: 'scroll'}}
                        dataSource={this.state.tableData}
                        columns={this.columns}
                        pagination={false}
                        rowKey={(row) => {
                            return row.consultID
                        }}
                        loading={this.state.loading}
                        bordered
                    />
                    {/* 分页: total--总条数  defaultCurrent--默认的当前页 hideOnSinglePage--只有一页时隐藏分页器 */}
                    <Pagination
                        showTotal={() => this.showTotal(this.state.total)}
                        size="small"
                        showQuickJumper
                        showSizeChanger
                        defaultCurrent={1}
                        current={this.state.page}
                        total={this.state.total ? this.state.total : 0}
                        onChange={this.onChange}
                        onShowSizeChange={this.onShowSizeChange}
                        style={{ textAlign: "right",marginTop: '13px'}}
                    />
                     
                    </div>
                </div>
            </div>
                   
        </Modal>
          )
          }

      }
