/*
  *  编辑角色操作者
  *
  * @Author:    黎永顺
  * @Date:      2024/8/20
  * @Wechat:
  * @Email:     971387674@qq.com
  * @description:
*/
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { WeaDialog, WeaLocaleProvider, WeaTools } from "ecCom";
import { Button } from "antd";
import { roleOperatorConditions } from "../conditions";
import { getSearchs } from "../../../../util";

const getLabel = WeaLocaleProvider.getLabel;
const getKey = WeaTools.getKey;

@inject("taxAgentStore")
@observer
class EditRoleDialog extends Component {
  constructor(props) {
    super(props);
    this.state = {
      conditions: [], loading: false, targetSob: {}
    };
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) {
      this.setState({
        conditions: _.map(roleOperatorConditions, item => ({
          ...item, items: _.map(item.items, o => {
            o = { ...o, label: getLabel(o.lanId, o.label), value: String(nextProps.record[getKey(o)]) };
            switch (getKey(o)) {
              case "link":
                o = { ...o, options: nextProps.linkOptions, hide: _.isEmpty(nextProps.linkOptions) };
                break;
              case "sortedIndex":
                o = { ...o, hide: _.isEmpty(nextProps.linkOptions) };
                break;
              case "targetName":
                o = ["EMP", "DEPARTMENT", "JOB", "SUB_COMPANY", "ROLE"].includes(nextProps.record.targetType) ?
                  {
                    ...o, value: "", browserConditionParam: {
                      ...this.renderBrowserType(nextProps.record.targetType),
                      replaceDatas: [{ id: nextProps.record["target"], name: nextProps.record[getKey(o)] }]
                    }
                  } :
                  nextProps.record.targetType === "SQL" ? {
                    ...o, conditionType: "TEXTAREA", otherParams: { minRows: 3 }
                  } : nextProps.record.targetType === "LEVEL" ?
                    {
                      ...o, startValue: nextProps.record[getKey(o)].split("-")[0],
                      endValue: nextProps.record[getKey(o)].split("-")[1],
                      conditionType: "SCOPE", precision: 0
                    } : (nextProps.record.targetType === "SOB" ||
                      nextProps.record.targetType === "TAX") ? {
                      ...o, value: nextProps.record["target"],
                      conditionType: "CUSTOMBROWSER", browserConditionParam: {
                        completeURL: nextProps.record.targetType === "SOB" ?
                          "/api/bs/hrmsalary/salarysob/listAuth" : "/api/bs/hrmsalary/taxAgent/listAuth",
                        dataParams: { filterType: "QUERY_DATA" },
                        filterByName: true, isSingle: true,
                        tableProps: {}, searchParamsKey: "name",
                        replaceDatas: [{ id: nextProps.record["target"], name: nextProps.record["targetName"] }]
                      }
                    } : { ...o };
                break;
              default:
                break;
            }
            return o;
          })
        })),
        targetSob: { id: nextProps.record["target"], name: nextProps.record["targetName"] }
      }, () => nextProps.taxAgentStore.roleOperatorForm.initFormFields(this.state.conditions));
    }
    if (nextProps.visible !== this.props.visible && !nextProps.visible) {
      this.setState({ targetSob: {} });
      nextProps.taxAgentStore.initRoleOperatorForm();
    }
    if (nextProps.loading !== this.props.loading && !nextProps.loading) this.props.onCancel();
  }

  renderBrowserType = (enumType) => {
    let browserType = {};
    switch (enumType) {
      case "EMP":
        browserType = { ...browserType, type: 17, isSingle: true, title: getLabel(82246, "人员选择") };
        break;
      case "DEPARTMENT":
        browserType = { ...browserType, type: 57, isSingle: true, title: getLabel(111, "部门选择") };
        break;
      case "JOB":
        browserType = { ...browserType, type: 278, isSingle: true, title: getLabel(111, "岗位选择") };
        break;
      case "SUB_COMPANY":
        browserType = { ...browserType, type: 164, isSingle: true, title: getLabel(111, "分部选择") };
        break;
      case "ROLE":
        browserType = { ...browserType, type: 65, isSingle: true, title: getLabel(111, "角色选择") };
        break;
      default:
        break;
    }
    return browserType;
  };
  save = () => {
    const { targetSob } = this.state;
    const { taxAgentStore: { roleOperatorForm }, record, onChange } = this.props;
    roleOperatorForm.validateForm().then(f => {
      if (f.isValid) {
        const { targetName: __, link, sortedIndex } = roleOperatorForm.getFormParams();
        const targetName = roleOperatorForm.getFormDatas().targetName;
        onChange([_.assign(record, {
          editId: record.id, sortedIndex,
          id: record.targetType === "LEVEL" ? targetName.value.join("-") : targetName.value,
          name: record.targetType === "SQL" ? __ :
            record.targetType === "LEVEL" ? __.join("-") : (targetName.valueSpan || targetSob.name),
          link: link === "undefined" ? "OR" : link
        })]);
      } else {
        f.showErrors();
      }
    });
  };
  handleFormChange = (val) => {
    const { record } = this.props;
    const key = _.keys(val)[0];
    if (key === "targetName" && (record.targetType === "SOB" || record.targetType === "TAX"))
      this.setState({ targetSob: _.head(val[key]) });
  };

  render() {
    const { conditions } = this.state;
    const { taxAgentStore: { roleOperatorForm }, linkOptions, loading } = this.props;
    return (
      <WeaDialog
        {...this.props} style={{ width: 480, height: _.isEmpty(linkOptions) ? 129 : 223 }}
        initLoadCss title={getLabel(111, "编辑操作者")}
        buttons={[
          <Button type="primary" loading={loading} onClick={this.save}>{getLabel(111, "保存")}</Button>
        ]}
      >
        <div className="form-dialog-layout">
          {getSearchs(roleOperatorForm, conditions, 1, false, this.handleFormChange)}
        </div>
      </WeaDialog>
    );
  }
}

export default EditRoleDialog;
