import React from 'react'
import { connect } from 'dva'
import { Layout, Steps, Button, message, Tabs } from 'antd'
const { Header, Content } = Layout
const Step = Steps.Step
const TabPane = Tabs.TabPane
import { Link } from 'react-router-dom'
import DataConnectConfig from './dataConnectConfig'
import BaseConfig from './baseConfig'
import ColumnConfig from './columnConfig'
import AccessConfig from './accessConfig'
import OtherConfig from './otherConfig'
import './dataSourceDetail.less'

class DataSourceDetail extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            mode: props.match.params.code&&props.match.params.code=='create'?'create':'modify',
            type: props.match.params.type,
            code: props.match.params.code,
            tab: props.match.params.tab,
            current: ['base', 'column', 'access'].indexOf(props.match.params.tab)
        }
    }

    componentDidMount() {
        this.props.dispatch({ type: 'dataSource/setNewModelFields', fields: [
            { name: 'type', value: this.props.match.params.type },
            { name: 'code', value: this.props.match.params.code }
        ] });
    }

    next() {
        const { type, current } = this.state;
        this.setState({ current: current + 1 });
        let step = ['base', 'column', 'access'][current + 1];
        this.props.dispatch({ type: 'main/redirect', path: '/datasource/' + type + '/create/' + step })
    }

    prev() {
        const { type, current } = this.state;
        this.setState({ current: current - 1 });
        let step = ['base', 'column', 'access'][current - 1];
        this.props.dispatch({ type: 'main/redirect', path: '/datasource/' + type + '/create/' + step })
    }

    render() {
        const { dispatch } = this.props;
        const { type, mode, code, tab, current } = this.state;
        const steps = [{
            tabName: 'base',
            title: '数据连接配置',
            content: <DataConnectConfig mode={mode} />
        }, {
            tabName: 'column',
            title: '数据列配置',
            content: <ColumnConfig mode={mode} />
        }, {
            tabName: 'access',
            title: '权限配置',
            content: <AccessConfig mode={mode} />
        }, {
            tabName: 'other',
            title: '完成',
            content: <OtherConfig mode={mode} />
        }];
        const tabs = [{
            tabName: 'base',
            title: '属性配置',
            content: <BaseConfig mode={mode} />
        }, {
            tabName: 'column',
            title: '数据列配置',
            content: <ColumnConfig mode={mode} />
        }, {
            tabName: 'access',
            title: '权限配置',
            content: <AccessConfig mode={mode} />
        }];
        return (
            <Layout className='layout-datasource-detail'>
                <Content className='content'>
                    {mode == 'create' ? (<div>
                        <Steps className='steps-body' current={current}>
                            {steps.map((item,index) => <Step key={'step-' + index} title={item.title} />)}
                        </Steps>
                        <div className="steps-content">
                            {steps[current].content}
                        </div>
                        <div className="steps-action">
                            {
                                current > 0 && (
                                <Button onClick={() => this.prev()}>
                                    上一步
                                </Button>)
                            }
                            {
                                current < steps.length - 1
                                && <Button type="primary" onClick={() => this.next()}>下一步</Button>
                            }
                            {
                                current === steps.length - 1
                                && <Button type="primary" onClick={() => {
                                    dispatch({ type: 'dataSource/remoteAdd' })
                                    dispatch({ type: 'main/redirect', path: { pathname: '/datasource' } });
                                }}>完成</Button>
                            }
                        </div>
                    </div>) : (
                        <Tabs activeKey={tab} type="card"
                            onChange={(key) => {
                                dispatch({ type: 'main/redirect', path: '/datasource/' + type + '/' + code + '/' + key })
                                this.setState({
                                    tab: key
                                })
                            }}
                            tabBarExtraContent={<Button onClick={() => {
                                dispatch({ type: 'dataSource/remoteModify', code: this.props.match.params.code });
                            }}>保存修改</Button>}
                        >
                            {tabs.map((item, index) => {
                                return <TabPane className='tab-datasource' key={item.tabName} tab={item.title}>
                                    {item.content}
                                </TabPane>
                            })}
                        </Tabs>
                    )}
                </Content>
            </Layout>
        )
    }
}

export default connect()(DataSourceDetail);