/*
 * Author: 黎永顺
 * name: 非系统人员管理表单项
 * Description:
 * Date: 2023/3/13
 */
import React, { Component } from "react";
import { WeaSlideModal } from "ecCom";
import { message } from "antd";
import { getConditionDomkeys, getSearchs } from "../../../util";
import SlideModalTitle from "../../../components/slideModalTitle";
import { detail, save, update } from "../../../apis/externalPersonManage";

class ExternalPersonManageEditSlide extends Component {
  constructor(props) {
    super(props);
    this.state = {
      date: "",
      loading: false
    };
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible && nextProps.id) this.detail(nextProps);
  }

  detail = (props) => {
    const { form, id, condition } = props;
    detail({ id }).then(({ status, data }) => {
      if (status) {
        form.updateFields({
          "departmentId": {
            value: data["departmentId"],
            valueSpan: data["departmentOrgName"],
            valueObj: [{ id: data["departmentId"], name: data["departmentOrgName"] }]
          }
        });
        form.updateFields({
          "subcompanyId": {
            value: data["subcompanyId"],
            valueSpan: data["subcompanyOrgName"],
            valueObj: [{ id: data["subcompanyId"], name: data["subcompanyOrgName"] }]
          }
        });
        // form.updateFields({
        //   "jobtitleId": {
        //     value: jobtitleId,
        //     valueSpan: data["jobtitleOrgName"],
        //     valueObj: [{ id: jobtitleId, name: data["jobtitleOrgName"] }]
        //   }
        // });
        _.map(_.without(getConditionDomkeys(condition), "departmentId", "subcompanyId"), item => {
          form.updateFields({ [item]: { value: data[item] } }, false);
        });
      }
    });
  };

  handleSubmit = () => {
    const { form, id, onCancel, title } = this.props;
    form.validateForm().then(f => {
      if (f.isValid) {
        const tmpV = _.reduce(_.keys(form.getFormDatas()), (pre, cur) => {
          if (cur === "departmentId") {
            return _.assign(pre, {
              [cur]: form.getFormDatas()[cur].value,
              departmentName: form.getFormDatas()["departmentId"].valueSpan
            });
          } else if (cur === "subcompanyId") {
            return _.assign(pre, {
              [cur]: form.getFormDatas()[cur].value,
              subcompanyName: form.getFormDatas()["subcompanyId"].valueSpan
            });
          }
          return _.assign(pre, { [cur]: form.getFormDatas()[cur].value });
        }, {});
        const payload = _.omitBy(form.getFormParams(), _.isNil);
        this.setState({ loading: true });
        const APIFOX = !id ? save : update;
        APIFOX(id ? { ...payload, id } : payload).then(({ status, errormsg }) => {
          this.setState({ loading: false });
          if (status) {
            message.success(`${title}成功`);
            onCancel(true);
          } else {
            message.error(errormsg || `${title}失败`);
          }
        }).catch(() => this.setState({ loading: false }));
      } else {
        f.showErrors();
        this.setState({ date: new Date() });
      }
    });
  };
  handleFormChange = (res) => {
    const { form, id } = this.props;
    if (_.keys(res)[0] === "departmentId" || _.keys(res)[0] === "subcompanyId") {
      const key = _.replace(_.keys(res)[0], "Id", "Name");
      form.updateFields({ [key]: res[_.keys(res)[0]].valueSpan || "" });
    }
  };

  render() {
    const { loading } = this.state;
    const { visible, title, onCancel, showOperateBtn, form, condition } = this.props;
    return (
      <WeaSlideModal
        className="slideOuterWrapper"
        visible={visible}
        top={0}
        measureT="%"
        width={800}
        measureX="px"
        height={100}
        measureY="%"
        direction="right"
        title={
          <SlideModalTitle
            subtitle={title}
            editable={true}
            loading={loading}
            showOperateBtn={showOperateBtn}
            onSave={this.handleSubmit}
          />
        }
        content={getSearchs(form, condition, 1, false, this.handleFormChange)}
        onClose={onCancel}
      />
    );
  }
}

export default ExternalPersonManageEditSlide;
