import React from 'react'
import { Form, Select, Icon } from 'antd'
import { connect } from 'dva'
import CHART_TYPE from './chartType.json'
import './baseConfigForm.less'
const FormItem = Form.Item
const { Option } = Select

class baseConfigForm extends React.Component {
	render() {
		const props = this.props;
		const allDataSource = props.dataSource.list;
		const { formItemLayout } = props

		return (
			<Form hideRequiredMark={true}>
				<FormItem label='数据源' {...formItemLayout}>
					<Select
						disabled
						value={props.chartDesigner.baseConfig.dataSource}
						onChange={(value) => {
							props.dispatch({ type: 'chartDesigner/changeDataSource', name: 'baseConfig', value: {
								...props.chartDesigner.baseConfig, dataSource: value } });
						}}
					>
						{allDataSource.map((dataSource, i)=>{
							return (<Option key={`dataSource-${i}`} value={dataSource.code}>{dataSource.name}</Option>)
						})}
					</Select>
				</FormItem>
				<FormItem label='可视化模式' {...formItemLayout}>
					<Select
						className='viewtype-field'
						dropdownClassName='baseconfig-viewtype'
						value={props.chartDesigner.baseConfig.viewType}
						dropdownMatchSelectWidth={false}
						onChange={(value, item) => {
							props.dispatch({ type: 'chartDesigner/changeFields', fields: [
								{ name: 'baseConfig', value: { ...props.chartDesigner.baseConfig, viewType: value } },
								{ name: 'chartOption', value: { } }
							] });
						}}
					>
					{
						CHART_TYPE.map( c => (
							<Option key={c.type} value={c.type} title={c.label}>
								<div className='viewtype-box'>
									<Icon className='viewtype-icon' type={c.icon} />
									<div className='viewtype-text'>
										{c.label}
									</div>
								</div>
							</Option>
						))
					}
					</Select>
				</FormItem>
			</Form>
		);
	}
}

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

export default Form.create()(connect(mapStateToProps)(baseConfigForm));