import { Select, message } from "antd";
import { connect } from "dva/index";
import { Component } from "react";
import { businessStateEnum } from "@/utils/utils";
import styles from "./index.less";

@connect(({ store }) => ({
  store,
}))
export default class BusinessStateSelector extends Component {
  constructor(props) {
    super(props);
    this.state = {
      stateList: Object.keys(businessStateEnum).map((key) => ({
        name: businessStateEnum[key],
        value: Number(key),
      })),
      currentBusinessState: "",
    };
  }

  handleChange = (value) => {
    const { dispatch } = this.props;
    dispatch({
      type: "store/update_business_state",
      payload: {
        businessState: value
      },
      callback: (res) => {
        if (res.state === 200) {
          message.success('设置成功')
          this.setState({ currentBusinessState: value });
        } else {
          message.error(res.msg)
        }
      },
    });
  };

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: "store/get_business_state",
      callback: (res) => {
        if (res.state === 200) {
          this.setState({ currentBusinessState: res.data });
        } else {
          this.setState({ currentBusinessState: 2 });
          message.error(res.msg)
        }
      },
    });
  }

  render() {
    const { stateList, currentBusinessState } = this.state;
    return stateList.length ? (
      <div className={`${styles.state_warp}`}>
        <span className={`${styles.label}`}>经营状态</span>
        <Select
          value={currentBusinessState}
          style={{ width: 150 }}
          onChange={this.handleChange}
        >
          {stateList.map((state) => (
            <Select.Option key={state.name} value={state.value}>
              {state.name}
            </Select.Option>
          ))}
        </Select>
      </div>
    ) : null;
  }
}
