/*
 * Author: 黎永顺
 * name: 新增数据采集项
 * Description:
 */
import React, { Component } from "react";
import { WeaBrowser, WeaFormItem } from "ecCom";
import { getDomkes, getSearchs } from "../../util";
import "./index.less";

class AddItems extends Component {
  componentDidMount() {
    const { editId, condition, form } = this.props;
    if (!_.isEmpty(editId)) {
      getDomkes(condition).map(item => {
        if (item === "employeeId") {
          form.updateFields({
            [item]: {
              value: editId[item],
              valueSpan: editId["username"],
              valueObj: [{ id: editId[item], name: editId["username"] }]
            }
          });
        } else if (item === "taxAgentId") {
          form.updateFields({
            [item]: editId[item].toString()
          });
        } else {
          form.updateFields({
            [item]: editId[item] || ""
          });
        }
      });
    }
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.editId !== this.props.editId && !_.isEmpty(nextProps.editId)) {
      getDomkes(nextProps.condition).map(item => {
        if (item === "employeeId") {
          nextProps.form.updateFields({
            [item]: {
              value: nextProps.editId[item],
              valueSpan: nextProps.editId["username"],
              valueObj: [{ id: nextProps.editId[item], name: nextProps.editId["username"] }]
            }
          });
        } else if (item === "taxAgentId") {
          nextProps.form.updateFields({
            [item]: nextProps.editId[item].toString()
          });
        } else {
          nextProps.form.updateFields({
            [item]: nextProps.editId[item] || ""
          });
        }
      });
    }
  }

  render() {
    const { form, condition = [] } = this.props;
    return (
      <div className="addItemsWrapper form-dialog-layout">
        {getSearchs(form, condition, 2, false)}
        <Tips><span>若此员工数据已存在在同期列表中，则当前数据保存后会覆盖列表数据</span></Tips>
      </div>
    );
  }
}

export default AddItems;

export const Browser = payload => {
  const { label, onChange, viewAttr = 3, value, valueSpan, type = 1, isSingle = true } = payload;
  return (
    <WeaFormItem label={label} labelCol={{ span: 6 }} wrapperCol={{ span: 18 }}>
      <WeaBrowser
        viewAttr={viewAttr}
        type={type}
        isSingle={isSingle}
        value={value}
        valueSpan={valueSpan}
        onChange={(ids, names) => onChange({ ids, names })}/>
    </WeaFormItem>
  );
};
export const Tips = payload => {
  const { children } = payload;
  return (
    <div className="tipWrapper">
      <div className="title">小提示</div>
      <div className="content">{children}</div>
    </div>
  );
};
