import React from 'react'
import { Modal, Table, Radio, message, Input, Row, Col } from 'antd'
import { connect } from 'dva'
import './chooseDataSourceBox.less'
import { dateFormat } from '../../utils/baseUtils'
const { Search } = Input

class ChooseDataSourceBox extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            filterLabel: '',
            selectedRecord: null
        }
    }

    changeSelected = (record) => {
        this.setState({
            selectedRecord: Object.assign({}, record)
        });
    }

    okHandler = (model) => {
        const { selectedRecord } = this.state;
        const { dispatch, hideBox } = this.props;
        if(selectedRecord) {
            dispatch({ type: 'chartDesigner/remoteQucikAdd', dataSource: selectedRecord });
            hideBox();
        }else {
            message.warning('未选中数据源');
        }
    }

    render() {
        const { selectedRecord } = this.state;
        const { dataSource, visibleChooseDataSourceBox, hideBox } = this.props

        const reg = new RegExp('([+ \\- & | ! ( ) { } \\[ \\] ^ \" ~ * ? : ( ) \/])', 'g'); // 需要转义的字符
        let filterLabel = this.state.filterLabel.replace(new RegExp('(\\\\)', 'g'), '\\$1').replace(reg, '\\$1'); // 添加转义符号
        
        const columns = [{
            title: '选择',
            key: 'selected',
            width: 50,
            render: (text, record) => {
                return <Radio checked={selectedRecord ? selectedRecord.code === record.code : false}/>
            }
        }, {
            title: '名称',
            dataIndex: 'name',
            key: 'name',
            width: 100,
            render: (text, record) => {
                return <div className='datasource-name'>
                    <div className={`datasource-type type-${record.type.key}`}></div>
                    <div>
                        <span>
                            { filterLabel ?
                                (text.split(new RegExp(`(${filterLabel})`, 'i')).map((fragment, i) => {
                                    return (
                                        fragment.toLowerCase().replace(new RegExp('(\\\\)', 'g'), '\\$1').replace(reg, '\\$1') === filterLabel.toLowerCase() ?
                                        <span key={i} style={{fontWeight: 'bold', color: 'red'}} className="highlight">{fragment}</span> :
                                        fragment
                                    )
                                }
                                )) : text
                            }
                        </span>
                    </div>
                </div>
            }
        }, {
            title: '创建人',
            dataIndex: 'creator',
            key: 'creator',
            width: 100
        }, {
            title: '创建时间',
            dataIndex: 'createTime',
            key: 'createTime',
            width: 120,
            render: (text) => dateFormat(new Date(text), 'yyyy-MM-dd')
        }, {
            title: '说明',
            dataIndex: 'description',
            key: 'description',
            width: 200,
            render: (text, record) => {
                return (
                    <span>
                        { filterLabel ?
                            ((text || '').split(new RegExp(`(${filterLabel})`, 'i')).map((fragment, i) => {
                                return (
                                    fragment.toLowerCase().replace(new RegExp('(\\\\)', 'g'), '\\$1').replace(reg, '\\$1') === filterLabel.toLowerCase() ?
                                    <span key={i} style={{fontWeight: 'bold', color: 'red'}} className="highlight">{fragment}</span> :
                                    fragment
                                )
                            }
                            )) : text
                        }
                    </span>
                )
            }
        }];

        return (
            <Modal
                className='choosedatasource-box'
                title={
                    <Row>
                        <Col span={14}>选择数据源</Col>
                        <Col span={8}><Search 
                            placeholder="请输入关键字"
                            onChange={e => {
                                this.setState({
                                    filterLabel: e.target.value
                                });
                            }}
                        /></Col>
                    </Row>
                }
                visible={visibleChooseDataSourceBox}
                onOk={this.okHandler}
                onCancel={hideBox}
                maskClosable={false}
                destroyOnClose={true}
            >
                <Table
                    className='choosedatasource-table'
                    columns={columns}
                    dataSource={dataSource.list.filter(l => {
                        try{
                            let regLabel = new RegExp('(' + filterLabel + '){1}', 'ig');
                            return (l.name || '').search(regLabel) !== -1 || (l.description || '').search(regLabel) !== -1;
                        }catch(e) {
                            console.log(e);
                            return true;
                        }
                    })}
                    size='small'
                    scroll={{x: false, y: 471}}
                    pagination={false}
                    onRow={(record) => {
                        return {
                            onClick: () => {
                                this.changeSelected(record);
                            }
                        };
                    }}
                />
            </Modal>
        )
    }
}

function mapStateToProps({ present: { dataSource } }) {
    return { dataSource: dataSource };
}

export default connect(mapStateToProps)(ChooseDataSourceBox)