/**
 * 传染病字典维护
 */
import React from 'react'
import { Form, message, Modal, Row, Col, Radio, Tabs, Select, DatePicker, Input, TimePicker, Button, Divider, Checkbox, Table } from 'antd';
import { $http } from '../../../containers/config/https';
// import RangeDatePicker from 'components/rangePicker/index';
import store from 'store';
import CommonPagination from 'components/pagination/index';
import AddInfectiousDictionary from './AddInfectiousDictionary'
import './style/InfectiousModel.less'

let user = {}
let patientData = {}
const { Option } = Select;
class InfectiousDictionary extends React.Component {
    //InfectiousDictionary
    constructor(props) {
        super(props);
        var date = new Date()
        user = sessionStorage.userData ? JSON.parse(sessionStorage.userData) : {};
        patientData = JSON.parse(sessionStorage.getItem('patientData'));
        this.state = {
            LocData: [],
            page: "1",
            pageSize: "10",
            total: 0,
            data: [],
            columns: [],
            ENObj: {},
            CNObj: {},
            diseaseID: "",
            ICDDiag: "",
            diseaseID:"",
            DiseaseArrData:[],
            clickDiseaseID:"",
        }
    }

    componentDidMount() {
        this.loadColumn();
        this.getOptionTypeName()
    }



    //查询列头数据
    loadColumn = () => {
        let data = {
            params: [{
                compontName: 'InfectiousDictionary',
                type: "C",
                reactCode: ['InfectiousDictionary'],
            }]
        }
        $http.post('urlDeault', this, {
            code: "01040073",
            data: data,
            success: (res) => {
                console.log(res);
                if (+res.errorCode === 0) {
                    let column = res.result.C;
                    if (user.language == 'EN') {
                        for (let i = 0; i < column.length; i++) {
                            column[i].title = column[i].enDescripts
                        }
                    }
                    this.setState({
                        columns: column,
                        tableWidth: res.totalWidth,
                        ENObj: res.result.language.EN,
                        CNObj: res.result.language.CN,
                    }, () => {
                        this.loadInfectiousData()
                    })
                }
            }
        })
    }

    // 获取传染病类别下拉数据
    getOptionTypeName = () => {
        let data = {
            params: [{
                categoryID: ""
            }]
        };
        $http.post('urlDeault', this, {
            code: "04190005",
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    this.setState({
                        DiseaseArrData: res.result.DiseaseArr,
                    })
                }
            },
            error: function (error) {
                message.error('失败：' + error);
            }
        });
    }


    loadInfectiousData = () => {
        let data = {
            params: [{
                diseaseID:this.state.diseaseID,
                ICDDiag:this.state.ICDDiag,
            }],
            pagination: [{
                pageSize: this.state.pageSize,
                currentPage: this.state.page,
              }]
        }
        $http.post('urlDeault', this, {
            code: "04190007",
            data: data,
            success: (res) => {
                if (res.errorCode == "0") {
                    this.setState({
                        data: res.result.diseaseContrastArr,
                        total: res.result.TotalCount,
                        data:res.result.diseaseContrastArr,
                        total:res.result.total,
                    })
                }
            }
        })
    }

    onColumnRef = (ref) => {
        this.columnRef = ref;
    }

    // // 页码改变的回调，参数是改变后的页码及每页条
    // onChange = (page, pageSize) => {
    //     this.compilePage(page, pageSize);
    // }

    // // 条数改变的回调
    // onShowSizeChange = (current, size) => {
    //     this.compilePage(1, size);
    // }

    onChange(e, pageSize) {
        this.setState({ page: e, pageSize: pageSize }, () => {
          this.loadInfectiousData();
        });
      }
    
      //分页切换
      changePageSize(page, pageSize) {
        this.setState({ pageSize: pageSize, page: page }, () => {
          this.loadInfectiousData();
        });
      }



    changeConditions(flag, e) {
        this.setState({
            [flag]: e.target.value
        })
    }

    onChangeDisease(flag, e){
        this.setState({
            [flag]: e
        },()=>{
            this.loadInfectiousData()
        })
    }
    //新增
    handlAddInfectiousDictionary = () => {
        this.child.handleShowModal();
    }

    onRef = ref => {
        this.child = ref;
    }

    //双击列触发事件
    onRow = (record) => {
        return {
            onDoubleClick: () => {
                this.setState({
                    clickDiseaseID:record.diseaseID
                },()=>{
                    this.handlAddInfectiousDictionary(record.diseaseID)
                })
            }
        }
    }

    render() {

        var language = user.language
        var languageObj = {}
        if (language == "EN") {
            languageObj = this.state.ENObj
        } else {
            languageObj = this.state.CNObj
        }

        let DiseaseArr = [];
        this.state.DiseaseArrData.map((item) => {
            DiseaseArr.push(
                <Option key={item.id} value={item.id}  seatchprop={item.descripts + '^' + item.descriptsSPCode} >
                    {item.descripts}
                </Option>
            )
        })


        return (
           <div style={{height:'100%',overflowY:'hidden'}} className="infectiousDictionary">
               <div>
                    <Row >
                        <Col span={6}  style={{margin:'10px 0px 7px 0px'}}>
                            <div> 

                            {languageObj.InfectiousDictionaryinfectious != null ? languageObj.InfectiousDictionaryinfectious.descripts : ""}
                            <Select 
                                defaultValue=""
                                allowClear={true}
                                showSearch={true}
                                optionFilterProp="seatchprop"
                               
                                onChange={this.onChangeDisease.bind(this, "diseaseID")}
                                style={{ width: '160px', margin: '0 80px 0 0' }}>
                                <Option key="" value="" >全部</Option>
                                {DiseaseArr}
                            </Select>
                            {/* {languageObj.InfectiousDictionaryinfectious != null ? languageObj.InfectiousDictionaryinfectious.descripts : ""}
                            <Input  style={{ width: '120px'}} onBlur={this.loadInfectiousData} onPressEnter={this.loadInfectiousData} onChange={this.changeConditions.bind(this,"diseaseID")} />*/}
                            </div> 
                        </Col>

                        <Col span={6} style={{margin:'10px 0px 7px 0px'}}>
                            <div> 
                            {languageObj.InfectiousDictionaryICD != null ? languageObj.InfectiousDictionaryICD.descripts : ""}
                            <Input onPressEnter={this.loadInfectiousData} onBlur={this.loadInfectiousData}  style={{ width: '120px'}} onChange={this.changeConditions.bind(this,"ICDDiag")}/>
                            </div>
                        </Col>

                        <Col span={6} style={{margin:'10px 0px 7px 0px'}}>
                            <div> 
                                <Button type='primary' onClick={this.loadInfectiousData}> {languageObj.InfectiousDictionaryquery != null ? languageObj.InfectiousDictionaryquery.descripts : ""}</Button>
                            </div>
                        </Col>

                        <Col span={6}  style={{margin:'10px 0px 7px 0px'}}>
                            <div> 
                                <Button type='primary' onClick={this.handlAddInfectiousDictionary}> {languageObj.InfectiousDictionaryinsert != null ? languageObj.InfectiousDictionaryinsert.descripts : ""}</Button>
                            </div>
                        </Col>
                    </Row>
                </div>
                <div style={{ width: '100%', height: '10px', background: '#f0f2f5' }}></div>
               <div style={{ width: '100%', height: 'calc(100% - 100px)', overflow: 'hidden' }} >
                    <Table
                        style={{ width: '100%', "padding": "5px",height:"100%" }}
                        dataSource={this.state.data}
                        columns={this.state.columns}
                        rowClassName="editable-row"
                        scroll={{ y: 'calc(100% - 60px)' }}
                        bordered
                        pagination={false}
                        onRow={this.onRow}
                    />
                </div>
                <div style={{ width: '100%', height: '45px', overflow: 'hidden' }}>
                    <CommonPagination
                        size='small'
                        current={this.state.page}
                        total={this.state.total}
                        search='search'
                        onShowSizeChange={this.changePageSize.bind(this)}
                        complete={true}
                        dataSource={this.state.data}
                        onChange={this.onChange.bind(this)}
                        scroll={{ y: store.getState().tableHeight.y + 'px' }}
                    />
                </div>
                <AddInfectiousDictionary
                    onRef={this.onRef}
                    diseaseID={this.state.clickDiseaseID}
                    handleQuery={this.loadInfectiousData}
                />
            </div>
        )
    }



} export default InfectiousDictionary