import React, { Component } from 'react';
import {
  WeaBrowser,
  WeaLocaleProvider,
} from 'ecCom';
import {
  message
} from 'antd';

const {getLabel} = WeaLocaleProvider;

// 固定创建人选择
export default class FixedCreator extends Component {
  constructor(props) {
    super(props);
    props.getInstance && props.getInstance(this);
  }

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

    return (
      <WeaBrowser
        ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@tqx8f3`}
        type={1}
        fieldName="hrmid"
        viewAttr="3"
        value={record.hrmid}
        valueSpan={record.hrmidspan}
        textDecoration={true}
        inputStyle={{width: 200}}
        onChange={this.handleChangeValue}
      />
    )
  }

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

    let changeAfterDatas = datas && datas.map(data => {
      if(data.fieldid === record.fieldid) {
        data.hrmidspan = names;
        data.hrmid = ids;
      }
      return data;
    })

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

  // 校验必填
  onSave = () => {
    const {
      record,
    } = this.props;

    if(!record.hrmid) {
      message.error(getLabel('23155,30036', '固定创建人选择必填'));
      return false;
    }
    return true;
  }
}
