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

class DataSourceDetailContent extends React.Component {

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

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

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

    render() {
        const { dispatch, dataSourceDetail, dataConnect } = this.props;
        const { type, code, tab, mode, current } = this.state;

        const steps = [{
            tabName: 'base',
            title: type === 'database' ? '数据链接配置' : '文件选择',
            content: <DataConnectConfig mode={mode} />,
        }, {
            tabName: 'column',
            title: '数据列配置',
            content: <ColumnConfig 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 className='datasource-steps-container'>
                        <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 disabled={
                                    // 第一步：没有选择数据连接或没有上传文件或上传文件有误
                                    ( current === 0 && ( ( type === 'database' && !dataSourceDetail.connectCode ) || ( type === 'file' && +1 === 2) ) ) ||
                                    // 第二步：数据列为空或者数据对象有变更
                                    (current === 1 && (!dataSourceDetail.columns || dataSourceDetail.columns.length === 0 || dataSourceDetail.targetDirty))
                                } type="primary" onClick={() => this.next()}>下一步</Button>
                            }
                            {
                                current === steps.length - 1
                                && <Button disabled={
                                    !dataSourceDetail.name || 
                                    !dataConnect.selected ||
                                    (!dataSourceDetail.columns || dataSourceDetail.columns.length === 0)
                                } type="primary" onClick={() => {
                                    dispatch({ type: 'dataSource/remoteAdd' })
                                }}>完成</Button>
                            }
                        </div>
                    </div>) : (
                        <Tabs activeKey={tab} type="card"
                            onChange={(key) => {
                                dispatch({ type: 'main/redirect', path: '/workshop/datasource/' + type + '/' + code + '/' + key })
                                this.setState({
                                    tab: key,
                                })
                            }}
                        >
                            {tabs.map((item, index) => {
                                return <TabPane className='tab-datasource' key={item.tabName} tab={item.title}>
                                    {item.content}
                                </TabPane>
                            })}
                        </Tabs>
                    )}
                </Content>
            </Layout>
        )
    }
}

export default connect(({ present: { dataSourceDetail, dataConnect } }) => ({ dataSourceDetail, dataConnect }))(DataSourceDetailContent);