import React, { Component } from 'react';
import {
  WeaSelect,
  WeaTools,
} from 'ecCom';

// 固定密集选择
export default class SafelevelValue extends Component {
  constructor(props) {
    super(props);
    this.state = {
      options: [],
    }
  }

  componentDidMount() {
    this.getDataOptions();
  }
    
  // 获取数据源的下拉选择项
  getDataOptions = () => {
    WeaTools.callApi('/api/integration/workflowtrigger/getTriggerOperation', 'POST', {operate: "getSafelevel"}).then(result => {
      this.setState({options: result.data || []});
    })
  }

  render() {
    const {
      record
    } = this.props;

    return (
      <WeaSelect
        ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@haoza6`}
        value={record.customsql}
        style={{width: 200}}
        options={this.state.options}
        onChange={this.handleChangeValue}
      />
    )
  }

  // 固定值变化时传值
  handleChangeValue = (value) => {
    const {
      record,
      datas=[],
      onChange,
    } = this.props;

    let changeAfterDatas = datas && datas.map(data => {
      if(data.fieldid === record.fieldid) {
        data.customsql = value;
      }
      return data;
    })

    // 调用父组件中的onchange方法改变值
    if(onChange && typeof(onChange) === "function") {
      onChange(changeAfterDatas);
    }
  }
}
