import React from 'react'
import { Modal, Form, Row, Col, Input, Button, Select, Icon, Menu, Dropdown, Table, Checkbox, Switch, Divider } from 'antd'
const FormItem = Form.Item
const SelectOption = Select.Option
const OptionGroup = Select.OptGroup
const InputGroup = Input.Group
const SubMenu = Menu.SubMenu
const MenuItem = Menu.Item
const MenuItemGroup = Menu.ItemGroup;
import { connect } from 'dva'
import dataSource from '../../models/dataSource'

class DataSourceColumnConfig extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            
        }
    }

    onCheckAllChange() {

    }

    render() {
        const { dataSource, dispatch } = this.props;

        const columns = [{
            title: <div><Checkbox
                style={{ margin: '0 8px 0 0', display: dataSource.newOne.columns ? (dataSource.newOne.columns.length > 0 ? 'inline-block' : 'none') : 'none'}}
                indeterminate={dataSource.newOne.columns ? (dataSource.newOne.columns.filter(c => c.using).length > 0 && dataSource.newOne.columns.filter(c => c.using).length < dataSource.newOne.columns.length) : false}
                checked={dataSource.newOne.columns ? (dataSource.newOne.columns.filter(c => c.using).length == dataSource.newOne.columns.length) : false}
                onChange={(e) => {
                    let target = e.target;
                    let columns = dataSource.newOne.columns ? dataSource.newOne.columns.map(c => {
                        c.using = target.checked;
                        return c;
                    }) : [];

                    dispatch({ type: 'dataSource/setNewModelField', name: 'columns', value: columns });
                }}
            />启用</div>,
            dataIndex: 'using',
            key: 'using',
            width: 50,
            render: (v, r) => <Checkbox
                dataKey={r.key}
                onChange={(e) => {
                    let target = e.target;
                    let key = target.dataKey;
                    let columns = dataSource.newOne.columns.map(c => {
                        if(c.key == key) {
                            c.using = target.checked;
                        }
                        return c;
                    });

                    dispatch({ type: 'dataSource/setNewModelField', name: 'columns', value: columns });
                }}
                checked={v}
            />
        }, {
            title: '列名',
            dataIndex: 'name',
            key: 'name',
            width: 80
        }, {
            title: '别名',
            dataIndex: 'alias',
            key: 'alias',
            width: 100,
            render: (text, record) => {
                return(
                    <Input
                        value={text}
                        onChange={(e) => {
                            const value = e.target.value;
                            let columns = dataSource.newOne.columns.map(c => {
                                if(c.key == record.key) {
                                    c['alias'] = value;
                                }
                                return c;
                            });
                            
                            dispatch({ type: 'dataSource/setNewModelField', name: 'columns', value: columns });
                        }}
                    >
                    </Input>
                )
            }
        }, {
            title: '数据类型',
            dataIndex: 'dataType',
            key: 'dataType',
            width: 80
        }, {
            title: '分析类型',
            dataIndex: 'columnType',
            key: 'columnType',
            width: 80,
            render: (text, record) => {
                return (
                    <Select
                        style={{ width: '100%' }}
                        value={text}
                        onChange={(value) => {
                            let columns = dataSource.newOne.columns.map(c => {
                                if(c.key == record.key) {
                                    c.columnType = value;
                                    c.groupable = c.columnType == 'categorical';
                                    c.bucketizable = ['time', 'scale', 'ordinal'].indexOf(record.columnType) != -1;
                                }
                                return c;
                            });
                            dispatch({ type: 'dataSource/setNewModelField', name: 'columns', value: columns });
                        }}
                    >
                    {
                        [
                            <SelectOption value='index' key='index'>索引</SelectOption>,
                            <SelectOption value='time' key='time'>时间</SelectOption>,
                            <SelectOption value='categorical' key='categorical'>类别</SelectOption>,
                            <SelectOption value='scale' key='scale'>标量</SelectOption>,
                            <SelectOption value='ordinal' key='ordinal'>序值</SelectOption>,
                            <SelectOption value='string' key='string'>字符串</SelectOption>
                        ].map((s, i) => {
                            if(record.dataType=='VARCHAR2' && [0,2,5].indexOf(i) != -1) {
                                return s;
                            }else if(record.dataType=='DATE' && [1].indexOf(i) != -1) {
                                return s;
                            }else if(record.dataType=='NUMBER' && [0,2,3,4,5].indexOf(i) != -1) {
                                return s;
                            }else {
                                return null
                            }
                        }).filter((s)=>s!=null)
                    }
                    </Select>
                )
            }
        }, {
            title: '允许分组',
            dataIndex: 'groupable',
            key: 'groupable',
            width: 50,
            className: 'column-groupable',
            render: (value, record) => <Switch disabled={record.columnType!='categorical'} checked={value} onChange={(checked) => {
                let columns = dataSource.newOne.columns.map(c => {
                    if(c.key == record.key) {
                        c.groupable = checked;
                    }
                    return c;
                });
                dispatch({ type: 'dataSource/setNewModelField', name: 'columns', value: columns });
            }}/>
        }, {
            title: '允许分段',
            dataIndex: 'bucketizable',
            key: 'bucketizable',
            width: 50,
            className: 'column-bucketizable',
            render: (value, record) => <Switch
                disabled={['time', 'scale', 'ordinal'].indexOf(record.columnType)==-1}
                checked={value}
                defaultChecked={true}
                onChange={(checked) => {
                    let columns = dataSource.newOne.columns.map(c => {
                        if(c.key == record.key) {
                            c.bucketizable = checked;
                        }
                        return c;
                    });
                    dispatch({ type: 'dataSource/setNewModelField', name: 'columns', value: columns });
                }}
            />
        }, {
            title: '备注',
            dataIndex: 'description',
            key: 'description',
            width: 200,
            render: (text, record) => {
                return <Input
                    value={text}
                    onChange={(e) => {
                        let columns = dataSource.newOne.columns.map(c => {
                            if(c.key == record.key) {
                                c['description'] = e.target.value;
                            }
                            return c;
                        });
                        
                        dispatch({ type: 'dataSource/setNewModelField', name: 'columns', value: columns });
                    }}
                >
                </Input>
            }
        }];

        console.log(dataSource.newOne.type);
        return (
            <div>
                {
                    dataSource.newOne.type=='database'?(
                        <div>
                            <Form size='small'>
                                <Divider orientation="left">数据对象</Divider>
                                <FormItem className='textarea-target'>
                                    <Input.TextArea
                                        placeholder='输入表名或查询SQL'
                                        autosize={{ minRows: 3 }}
                                        value={dataSource.newOne.target}
                                        onChange={(e) => {
                                            dispatch({ type: 'dataSource/setNewModelField', name: 'target', value: e.target.value });
                                        }}
                                    />
                                </FormItem>
                                <div className='buttons'>
                                    <Button onClick={() => {
                                        dispatch({ type: 'dataSource/importNewModelColumns' })
                                    }}>刷新</Button>
                                </div>
                            </Form>
                            <Divider orientation="left">数据列</Divider>
                        </div>
                    ):null
                }
                <Table
                    className='table-columnconfig'
                    dataSource={dataSource.newOne.columns}
                    columns={columns}
                    locale={{
                        emptyText: '未连接到数据对象'
                    }}
                />
            </div>
        );
    }
}

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

export default connect(mapStateToProps)(DataSourceColumnConfig);