import React from 'react';
import { Modal, Table, Pagination, Button } from 'antd';
import { $http } from '../../containers/config/https'; 
import './style/nurseConfig.less';//引入样式

export default class SelectTable extends React.Component {
    constructor() {
        super();
        this.state = {
            visible: false,
            selectedData: [],
            selectedId: [],
            data: [],
            flag: ''
        }
    }

    componentDidMount() {
        // 提供给外部该组件的this
        this.props.onRef(this);
    }

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

    // 打开弹框
    showModal = (flag) => {
        let data = {
            "params": [{
            }]
        };
        $http.post('urlDeault', this, {
            code: "04010007",
            data: data,
            success: function (res) {
                this.setState({
                    visible: true,
                    data: (flag === "C") ? res.columnListData : res.buttonListData,
                    flag
                });
            },
            error: function (error) {
                message.error('失败：' + error);
            }
        });
    }

    // 关闭弹框
    handleCancel = () => {
        this.setState({
            visible: false,
        });
    }
    handleOk = () => {
        this.handleCancel();
        this.props.handleOk(this.state.selectedData, this.state.flag);
        this.setState({
            selectedId: []
        })
    }
    render() {
        let { param } = this.props;
        let showPagination = param.showPagination ? true : false;
        let rowClassName = param.rowClassName ? true : false;
        let isFooter = param.isFooter ? true : false;
        const rowSelection = {
            onChange: (selectedRowKeys, selectedRows) => {
                this.setState({
                    selectedData: selectedRows,
                    selectedId: selectedRowKeys
                })
            },
            selectedRowKeys: this.state.selectedId
        };
        return (
            <Modal
                title={param.title ? param.title : ''}
                visible={this.state.visible}
                onCancel={this.handleCancel}
                width={param.width ? param.width : ' 600px'}
                footer={isFooter ? <div>
                    <Button onClick={this.handleCancel}>取消</Button>
                    <Button type="primary" onClick={this.handleOk}>确定</Button>
                </div> : null}
                mask={false}
                closable={param.title ? true : false}
                style={{ position: 'absolute', top: param.top ? param.top : '40%', left: param.left ? param.left : '40%', overflow: 'auto', boxShadow: '0px 1px 4px rgba(0,0,0,0.3), 0px 0px 20px rgba(0,0,0,0.1) inset', paddingBottom: '0' }}
                className={param.ModalClassName ? param.ModalClassName : ''}
            >
                <Table
                    showHeader={false}
                    columns={param.columns}
                    rowKey={this.props.id}
                    dataSource={this.state.data}
                    pagination={false}
                    rowSelection={rowSelection}
                    rowClassName={rowClassName ? (record) => this.props.setRowClassNamePublic(record) : ''}
                    className={param.TableClassName ? param.TableClassName : ''}
                    style={{ height: showPagination ? param.height - 40 + 'px' : param.height, overflow: 'auto' }}
                />
            </Modal>
        )
    }
}
