import React, { PureComponent } from "react";
import { i18n } from "../public/i18n";
import { WeaTools } from "ecCom";

/**
 * 添加多级路径
 *
 * @param {*} url
 */
export const addContentPath = (url) => {
  const ecologyContentPath = window.ecologyContentPath || "";
  if (url && ecologyContentPath) {
    //避免重复添加ecologyContentPath
    //避免传入的参数不是链接
    if (url.startsWith("/") && !url.startsWith(ecologyContentPath)) {
      url = ecologyContentPath + url;
    }
  }
  return url;
};

export default class AttachToNumberField extends PureComponent {
  constructor(props) {
    super(props);

    this.state = {
      canRe: false,
      canCheck: false,
      canNew: false
    };
    this.typeMap = {
      subcompanycode: "SUBCOMPANY",
      departmentcode: "DEPARTMENT",
      job_no: "JOBTITLES",
      // work_code: "USER"
    };

  }

  componentWillMount() {
    const { field: { domkey: [targetKey] } } = this.props;
    this.targetKey = targetKey;
    this.type = this.typeMap[this.targetKey];
  }

  componentDidMount() {
    this.init(this.props.isEdit);
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps.form != this.props.form) {
      this.init(nextProps.isEdit);
    }
  }

  init = (isEdit) => {
    WeaTools.callApi("/api/hrm/codeSetting/getHasSelect", "GET", {}).then(res => {
      if (res) {
        //后台设置页面开关关闭，接口不会返回对应的权限，所以这里做下兼容
        const target = res[this.type] || { hasSelect: false, hasRight: false };
        const { hasSelect, hasRight } = target;
        this.setState({
          canRe: hasSelect && isEdit,
          canCheck: hasSelect,
          canNew: hasRight
        });
      }
    });
  };

  handleClick = (e) => {
    const urlMap = {
      select: {
        title: i18n.label.selectReserveNumber(),
        url: addContentPath(`/spa/hrm/engine.html#/hrmengine/reservedNumber?type=${this.type}&isSimple=1`)
      },
      add: {
        title: i18n.label.newReserveNumber(),
        url: addContentPath(`/spa/hrm/engine.html#/hrmengine/newReservedNumberContent?type=${this.type}`)
      }
    };

    const { title, url } = urlMap[e.target.id];

    ecCom.WeaTools.createDialog({
      title,
      url,
      moduleName: "hrm",
      style: { width: 800, height: 600 },
      callback: this.updateFormField,
      onCancel: () => { // 关闭通信
      }
    }).show();
  };
  updateFormField = (value) => {
    const { form } = this.props;
    form.updateFields({
      [this.targetKey]: value
    });
  };
  //重新生成编号
  reNumber = () => {
    const { form, useId } = this.props;
    const fieldMap = {
        subcompanycode: "subshowid",
        departmentcode: "showid",
        jobtitlecode: "jobactivityid",
        workcode: "useid"
      },
      fieldname = fieldMap[this.targetKey];

    WeaTools.callApi("/api/hrm/codeSetting/regenerateCode", "POST", {
      serialtype: this.type,
      [`${this.type.toLocaleLowerCase()}id`]: form.getFormParams()[fieldname] || useId
    }).then(res => {
      const { api_status, code } = res;
      if (api_status) {
        if (code) {
          this.updateFormField(code);
        } else {
          message.error(res.api_errormsg);
        }
      }
    });
  };


  render() {
    const { canRe, canCheck, canNew } = this.state;
    return (
      <div>
        {canRe && <a onClick={this.reNumber}>{i18n.label.reNumber()}</a>}
        {canCheck && <a onClick={this.handleClick} style={{ marginLeft: canRe && 10 }}
                        id="select">{i18n.label.selectReserveNumber()}</a>}
        {canCheck && canNew &&
          <a style={{ marginLeft: 10 }} onClick={this.handleClick} id="add">{i18n.label.newReserveNumber()}</a>}
      </div>
    );
  }
}
