/**
 * 药品查询
 */

import React from 'react';
import {
    Card, Table, Input, Form, Select, Row, Col, Checkbox, Tabs, Steps,
    DatePicker, Button, Popconfirm, Icon, Divider, message, Pagination, Tooltip,
} from 'antd';
import { Util, dayFormat } from 'tools/index';
import { $http } from 'containers/config/https';
import ColumnAuthoritysModel from 'pages/columnAuthority/ColumnAuthoritysModel';
import CommonPagination from 'components/pagination/index';
import RangeDatePicker from 'components/rangePicker/index';
import store from 'store';
import moment from 'moment';
import './style/index.less';


export default class DrugInquiryPage extends React.Component {
    constructor(props) {
        super(props)
        var userData = JSON.parse(sessionStorage.getItem('userData'));
        var patientData = JSON.parse(sessionStorage.getItem('patientData'));
        this.state = {
            total: 0,
            page: 1,
            pageSize: 10,
            size: 'small',
            userData: userData,
            patientData: patientData,
        }
    }

    componentDidMount() {
        this.initData()
        this.loadData(1)
    }

    initData() {
        let data = {
            params: [{
                compontName: "DrugInquiryPage",
                type: "C",
                reactCode: ['DrugInquiryPage']
            }]
        }
        $http.post('urlDeault', this, {
            code: "01040073",
            data: data,
            success: (res) => {
                if (+res.errorCode === 0) {
                    var columns = res.result.C;
                    var dynColumns = []
                    for (var i = 0; i < columns.length; i++) {
                        dynColumns.push({
                            title: this.state.userData.language == 'EN' ? columns[i].enDescripts : columns[i].title,
                            dataIndex: columns[i].dataIndex,
                            width: parseInt(columns[i].width),
                            align: columns[i].align || 'center'
                        })
                    }
                    this.setState({
                        column: dynColumns,
                        localeProvider: this.state.userData.language == 'EN' ? res.result.language.EN : res.result.language.CN
                    })
                    this.props.hanlderThis.setState({
                        reload: false,
                    })
                }
            }
        })

    }

    loadData(page, pageSize, e, status) {
        var params = {
            params: [{
                admID: this.state.patientData ? this.state.patientData.admID : '',
                formDate: this.state.dateFrom,
                toDate: this.state.dateTo
            }],
            pagination: [{
                pageSize: this.state.pageSize,
                currentPage: page
            }]
        }
        $http.post('urlDeault', this, {
            code: "03050268",
            data: params,
            success: function (res) {
                if (res.errorCode == '0') {
                    this.setState({
                        dataSource: res.result.Data,
                        total: res.result.TotalCount,
                        page: page,
                        search: e,
                    })
                }
            }
        });
    }

    startChange(date) {
        this.setState({
            dateFrom: date,
        }, () => {
            this.loadData(1)
        })
    }

    endChange(date) {
        this.setState({
            dateTo: date,
        }, () => {
            this.loadData(1)
        })
    }

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

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

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

    render() {
        var date = new Date();
        return <div className='patient-drug-body'>
            <ColumnAuthoritysModel
                clientWidth='800px'
                compontName="DrugInquiryPage"
                configType="C"
                onRef={this.onColumnRef}
                reloadData={this.initData}
            />
            <Card>
                <Col span={12}>
                    <RangeDatePicker label={'开始时间：'} format='YYYY-MM-DD' width={'80%'}
                        locale={this.state.userData.language}
                        startChange={this.startChange.bind(this)} endChange={this.endChange.bind(this)} />
                </Col>

                <Button type='primary' onClick={this.loadData.bind(this, 1, '', 'search')}>查询</Button>
            </Card>
            <Card style={{ marginTop: 5,height:'60vh' }}>
                <Table columns={this.state.column} dataSource={this.state.dataSource}
                    pagination={false} bordered
                    scroll={{ x: 1200, y: '50vh' }}
                    onHeaderRow={column => {
                        return {
                            onDoubleClick: () => {
                                this.columnRef.onOpenModel();
                            },
                        };
                    }}
                rowKey={(data) => {
                    return data.seqNo
                }}
                ></Table>
                <CommonPagination size={this.state.size} current={this.state.page} total={this.state.total}
                    search={this.state.search} complete={true}
                    onShowSizeChange={this.changePageSize.bind(this)} dataSource={this.state.dataSource}
                    onChange={this.onChange.bind(this)} />
            </Card>
        </div>
    }

}