/*
* 录医嘱弹框
* */

import React, { Component } from 'react';
import { Form, Modal, Button, Table, Spin } from 'antd';
import CommonPagination from 'components/pagination/index';
import '../style/index.less';

class ShowCommonTableModal extends Component {

    constructor(props) {
        super(props);
        var date = new Date();
        var userData = JSON.parse(sessionStorage.getItem('userData'));
        var titleData = JSON.parse(sessionStorage.getItem('titleData'))
        this.state = {
            userData: userData,
            titleData: titleData,
            visible: false,
        }
    }
    componentDidMount() {
        // 提供给外部该组件的this
        this.props.onRef(this);
    }

    componentWillUnmount() {
        // 组件销毁前将静止让setState修改state的状态
        this.setState = (state, callback) => { return; }
    }

    // 打开弹框
    showModal = () => {
        this.setState({
            visible: true,
        });
    }

    // 关闭弹框
    handleCancel = () => {
        if (this.state.visible) {
            this.setState({
                visible: false,
            }, () => {
                var { handleFocus } = this.props;
                handleFocus && handleFocus()
            })
        }
    }

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

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

    // 显示分页总条数
    showTotal(total) {
        return `共 ${total ? total : 0} 条`;
    }

    render() {
        let { param } = this.props;
        let showPagination = param.showPagination ? true : false;
        let isOnRow = param.isOnRow ? true : false;
        let rowClassName = param.rowClassName ? true : false;
        let isFooter = param.isFooter ? true : false;
        return (
            <div className='doctor-advice-common-modal'><Modal
                title={param.title ? param.title : ''}
                visible={this.state.visible}
                onCancel={this.handleCancel}
                bodyStyle={{ padding: '0px' }}
                width={document.body.clientWidth < 1400 ? '600px' : '1100px'}
                footer={isFooter ? <div>
                    <Button onClick={this.handleCancel}>取消</Button>
                    <Button type="primary" onClick={this.props.handleOk ? this.props.handleOk : ''}>{param.okText ? param.okText : '确定'}</Button>
                </div> : null}
                mask={false}
                closable={param.title ? true : false}
                style={{ position: 'absolute', top: param.top ? param.top : '34%', left: param.left ? param.left : '28%', boxShadow: '0px 1px 4px rgba(0,0,0,0.3), 0px 0px 20px rgba(0,0,0,0.1) inset', paddingBottom: '0' }}
                className={'doctor-common-client' + (param.ModalClassName ? param.ModalClassName : '')}
            >
                {/**this.props.loading == true判断，处理其他地方引用该组件，默认为加载中问题 */}
                <Spin spinning={this.props.loading == true ? true : false}>
                    <div className='doctor-table-style' style={{ height: param.height ? param.height : 360 }} id={this.props.id ? this.props.id : ''}>
                        <Table
                            columns={param.columns}
                            dataSource={param.data}
                            pagination={false}
                            bordered={'bordered' in param ? param.bordered : true}
                            rowKey={(data) => {
                                return data.id
                            }}
                            onRow={isOnRow ? (record) => this.props.onClickRowPublic(record) : ''}
                            rowClassName={rowClassName ? (record) => this.props.setRowClassNamePublic(record) : ''}
                            className={param.TableClassName ? param.TableClassName : ''}
                            scroll={{
                                y: document.body.clientWidth < 1400 ? '295px' : (param.height ? param.height - 60 : 300),
                                x: document.body.clientWidth < 1400 ? '600px' : '800px'
                            }}
                        />
                        {
                            showPagination ? <div className='pagination-modal-style'>
                                <CommonPagination
                                    size={param.size ? param.size : 'small'}
                                    current={param.page ? param.page : 1}
                                    total={param.total ? param.total : 0}
                                    search={this.props.search}
                                    page={param.page ? param.page : 1}
                                    onShowSizeChange={this.onShowSizeChange.bind(this)}
                                    complete={true}
                                    dataSource={param.data}
                                    onChange={this.onChange.bind(this)}
                                /></div> : ''
                        }

                    </div></Spin>
            </Modal></div>
        )
    }
}

export default Form.create()(ShowCommonTableModal);