import React from 'react';
import {Cascader, Form, Input, Select, Button, Checkbox, Radio, Tooltip, Icon } from 'antd';
import fetch from 'common/xFetch';
const FormItem = Form.Item;

const Option = Select.Option;
const T = React.PropTypes;

const MSelect = React.createClass({
  getInitialState() {
    return {
      options: this.props.options || []
    };
  },
  propTypes: {
    label: T.string,
    labelColSpan: T.number,
    wrapperColSpan: T.number,
    help: T.string,
    tip: T.string,
    value: T.string,
    rules: T.array,
    hidden: T.bool,
    form: T.object.isRequired,
    path: T.string.isRequired,
    dataSource: T.string,
    cascader: T.bool,
    multiple: T.bool
  },
  getDefaultProps() {
    return {
      label: '下拉选择',
      labelColSpan: 6,
      wrapperColSpan: 14,
      placeholder: '请选择',
      hidden: false,
      cascader: false,
      multiple: false
    }
  },
  componentWillReceiveProps(nextProps) {
    const dataSource = nextProps.dataSource

    if (dataSource) {
      if (dataSource !== this.props.dataSource) {
        this.loadAsyncOptions(dataSource);
      }
    } else {
      this.setState({
        options: nextProps.options || this.state.options
      });
    }

  },
  componentDidMount() {
    if (this.props.dataSource) {
      this.loadAsyncOptions(this.props.dataSource);
    }    
  },
  loadAsyncOptions(dataSource) {
    const f = (a) => a;
    const transform = this.props.transform || f;
    fetch(dataSource)
      .then((result) => {
        let data = transform(result);
        this.setState({
          options: data.options || this.state.options
        });
      }, (err) => {
        console.log('err', err);
      })
  },
  render() {

    const props = this.props;

    let itemProps = {
      label: props.label,
      className: props.hidden ? 'hide' : '',
      labelCol: { span: props.labelColSpan },
      wrapperCol: { span: props.wrapperColSpan }
    }

    if (props.help) {
      itemProps.help = props.help;
    }

    if (props.tip) {
      itemProps.label = (
        <span>
          {props.label}
          <Tooltip title={props.tip}>
            <Icon type="question-circle-o" />
          </Tooltip>
        </span>
      )
    }

    return (
      <FormItem {...itemProps}>
        {this.renderSelect()}
      </FormItem>
    )
  },

  renderSelect() {
    const props = this.props;
    const { getFieldProps} = this.props.form;

    const fieldProps = getFieldProps(props.path, {
      rules: props.rules,
      initialValue: props.initialValue,
      value: null,
      onChange: function() {
        props.onFieldChange({
          path: props.path,
          args: arguments
        })
      }
    });

    if (props.cascader) {
      return <Cascader
        {...fieldProps}
        placeholder={props.placeholder}
        style={{ width: '100%' }}
        options={this.state.options}
        disabled={props.disabled}/>
    }

    return (
      <Select 
        {...fieldProps} 
        multiple={props.multiple}
        placeholder={props.placeholder}
        disabled={props.disabled}
        style={{ width: '100%' }}>
        {this.state.options.map(r => {
          return (
            <Option 
              value={r.value} 
              key={r.value + r.label + props.id}>
              {r.label}
            </Option>
          )
        })}
      </Select>
    )
  }
});

export default MSelect;