import React, { Component } from 'react';
import { $http } from '../../../containers/config/https';
import {  Table, Button} from 'antd';
import store from 'store';
import CommonPagination from 'components/pagination/index';
import moment from 'moment';
import RangeDatePicker from 'components/rangePicker/index';

let userData = {}
class AnesthesiaMedicine extends Component{
    constructor(props) {
        var date = new Date()
        super(props);
        userData = sessionStorage.userData ? JSON.parse(sessionStorage.userData) : {};
        this.state = {
            anesthesiaData:[],
            stDate: moment(date).subtract(1, 'months').format('YYYY-MM-DD'), 				// 开始日期
			endDate: moment(date).format('YYYY-MM-DD'),     // 结束日期
            total: 0, 
            page: '1',
			pageSize: '10',
        }
    }

    componentDidMount() {
        this.getDetailTableColumns();
    }

    componentWillReceiveProps() {

    }
    componentWillUnmount() {

    }

    loadAnesthesia = () => {
        let data = {
            params: [{
              startDate:this.state.stDate,
              endDate:this.state.endDate
            }],
            pagination: [{
				pageSize: this.state.pageSize,
				currentPage: this.state.page,
			}]
        }
        $http.post('urlDeault', this, {
            code: "04160010",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    console.log(res)
                    this.setState({
                    anesthesiaData: res.result.anesthesiaArr,
                    total:res.result.total,
                    });
                }
            }
        })
    }

    //AnesthesiaMedicine
     // 获取明系列表头数据
     getDetailTableColumns = () => {
        let data = {
            params: [{
                reactCode: ['AnesthesiaMedicine'],
                compontName: 'AnesthesiaMedicine',
                type: 'C',
            }]
        }
        $http.post('urlS', this, {
            code: '01040073',
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    var columns = []
                    console.log(res)
                    columns = res.result.C
                    if (userData.language == 'EN') {
						for (let i = 0; i < columns.length; i++) {
							columns[i].title = columns[i].enDescripts
						}
					}
                    this.setState({
                        ENObj: res.result.language.EN,
                        CNObj: res.result.language.CN,
                        columns: columns,
                        tableWidth: res.totalWidth,
                    }, () => {
                        this.loadAnesthesia()
                    })
                }
            }
        })
    }

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

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

    startChange(date) {
        if(date>this.state.endDate){
            message.warning(userData.language="EN"?this.state.ENObj.AnesthesiaMedicinedate:this.state.CNObj.AnesthesiaMedicinedate)
			return;
        }
        this.setState({
            stDate: date,
        },()=>{
            if(this.state.endDate!=""){
                this.setState({
                    anesthesiaData:[],
                })
                this.loadAnesthesia();
            }
            
        }
        )
      }
    
      endChange(date) {
        if(this.state.stDate > date){
            message.warning(userData.language="EN"?this.state.ENObj.AnesthesiaMedicinedate:this.state.CNObj.AnesthesiaMedicinedate)
			return;
        }
        this.setState({
          endDate: date,
        },()=>{
            if(this.state.stDate!=""){
                this.setState({
                    anesthesiaData:[],
                })
                this.loadAnesthesia();
            }
        }
        )
      }



   render(){
       var languageObj = {}
       if(userData.language == "CN"){
            languageObj = this.state.CNObj
       }else{
            languageObj = this.state.ENObj
       }
        return(
            <div>
               <RangeDatePicker
					width={300}
					label={languageObj != null ? languageObj.AnesthesiaMedicineadmDate.descripts : ""}
					startDate={this.state.stDate}
					endDate={this.state.endDate}
					format={'YYYY-MM-DD'}
                    startChange={this.startChange.bind(this)} 
                    endChange={this.endChange.bind(this)}
				/>
                <Table
                    style={{ width: '100%',"padding":"5px", height: (store.getState().tableHeight.y - 230) }}
                    dataSource={this.state.anesthesiaData}
                    columns={this.state.columns}
                    rowClassName="editable-row"
                    pagination={false}
                    scroll={{ y: store.getState().tableHeight.y + 'px', x: this.state.tableWidth }}
                />
                <CommonPagination
						size='small'
						current={this.state.page}
						total={this.state.total}
						search='search'
						onShowSizeChange={this.onShowSizeChange.bind(this)}
						complete={true}
						dataSource={this.state.anesthesiaData}
						onChange={this.onChange.bind(this)}
						scroll={{ y: store.getState().tableHeight.y + 'px' }}
				/>
            </div>
        )
   }
}
export default AnesthesiaMedicine