import React, { PureComponent } from "react";
import { connect } from "dva";
import { Select } from "antd";
import styles from "./index.less";

class SelectComp extends PureComponent {
  constructor(props) {
    super(props);
  }
  componentWillMount() {
    this.getEnumData();
  }
  componentDidMount() {
    //
  }
  getEnumData() {
    const { dispatch, getEnumUrl, urlParams } = this.props;
    dispatch({
      type: getEnumUrl,
      payload: urlParams ? urlParams : null
    });
  }
  selectChilds() {
    const { enumData } = this.props;
    if (!enumData) return;
    const enumDataArr = JSON.parse(enumData);
    const selectOptions = [];
    for (let i = 0; i < enumDataArr.length; i++) {
      selectOptions.push(
        <Select.Option value={enumDataArr[i].value} key={i}>
          {enumDataArr[i].name}
        </Select.Option>
      );
    }
    return selectOptions;
  }
  selectHandle(changedValue) {
    const onChange = this.props.onChange;
    if (onChange) {
      onChange(changedValue);
    }
    console.log(changedValue);
  }
  render() {
    return (
      <Select
        style={{ width: "220px" }}
        onChange={this.selectHandle.bind(this)}
      >
        {this.selectChilds()}
      </Select>
    );
  }
}
export default connect(state => ({
  enumData: state.select.enumData
}))(SelectComp);
