/**
 * 数据视图
 */
import React from 'react'
import { Form, Select, Button } from 'antd'
import { connect } from 'dva'
import DisplayColumnBox from './displayColumnBox'
const FormItem = Form.Item
const { Option } = Select

class DataViewConfigForm extends React.Component {
	constructor(props) {
		super(props);
		this.state = {
			formItemLayout: {
                labelCol: { span: 8 },
                wrapperCol: { span: 16 },
            },
			showBox: false,
			visibleDisplayColumnBox: false
		}
	}
	
	render() {
		const { autoRefresh, dispatch, chartDesigner } = this.props;
		const { formItemLayout, visibleDisplayColumnBox } = this.state;
		return (
            <Form layout='horizontal'>
				<FormItem label="展示列" {...formItemLayout}>
					<Button onClick={() => {this.setState({visibleDisplayColumnBox:true})}}>设置展示列</Button>
				</FormItem>
				<FormItem label='排序列' {...formItemLayout}>
					<Select
						allowClear
						value={chartDesigner.dataViewConfig.sortColumn}
						labelInValue={true}
						placeholder={chartDesigner.dataViewConfig.viewColumns.length > 0 ? chartDesigner.dataViewConfig.viewColumns[0].label : '无'}
						showSearch
						filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
						onChange={(value) => {
							dispatch({ type: 'chartDesigner/changeField', name: 'dataViewConfig', value: { ...chartDesigner.dataViewConfig, sortColumn: value }, autoRefresh });
						}}
					>
						{chartDesigner.dataViewConfig.viewColumns.map((c, i)=>{
							return <Option key={c.name} value={c.name}>{c.label}</Option>
						})}
					</Select>
				</FormItem>
				<FormItem label='排序方式' {...formItemLayout}>
					<Select
						defaultValue='asc'
						value={chartDesigner.dataViewConfig.sortType}
						placeholder='升序'
						onChange={(value) => {
							dispatch({ type: 'chartDesigner/changeField', name: 'dataViewConfig', value: { ...chartDesigner.dataViewConfig, sortType: value }, autoRefresh });
						}}
					>
						<Option value="asc">升序</Option>
						<Option value="desc">降序</Option>
					</Select>
				</FormItem>
				{visibleDisplayColumnBox && <DisplayColumnBox
					visibleDisplayColumnBox={visibleDisplayColumnBox}
					hideBox={() => this.setState({visibleDisplayColumnBox:false})}
					allColumns={chartDesigner.columns.map((c, i) => ({ ...c, key: i }))}
					defaultSelectedColumns={chartDesigner.dataViewConfig.viewColumns}
					okHandler={(targetColumns) => {
						dispatch(
							{
								type: 'chartDesigner/changeField', 
								name: 'dataViewConfig', 
								value: { ...chartDesigner.dataViewConfig, viewColumns: targetColumns, sortColumn: targetColumns.length > 0 ? { key: targetColumns[0].name, label: targetColumns[0].label } : { key: '' } },
								autoRefresh 
							}
						);
					}}
				/>}
			</Form>
        );
	}
}

function mapStateToProps({ chartDesigner }) {
    return { chartDesigner: chartDesigner }
}

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