/*
 * Author: 黎永顺
 * name:审批规则分类编辑
 * Description:
 * Date: 2024/4/25
 */
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { WeaDialog, WeaLocaleProvider, WeaTools } from "ecCom";
import { Button } from "antd";
import { getSearchs } from "../../../util";
import { classifyConditions } from "../config";

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

@inject("ledgerStore")
@observer
class LedgerAccountApprRuleClassifyNameEditDialog extends Component {
  constructor(props) {
    super(props);
    this.state = { conditions: [] };
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) this.init(nextProps);
    if (nextProps.visible !== this.props.visible && !nextProps.visible) this.props.ledgerStore.initAARClassifyForm();
  }

  init = (props) => {
    const { ledgerStore: { AARClassifyForm }, groupName, groupId } = props;
    this.setState({
      conditions: _.map(classifyConditions, item => ({
        ...item,
        items: _.map(item.items, o => ({ ...o, label: getLabel(o.lanId, o.label) }))
      }))
    }, () => {
      AARClassifyForm.initFormFields(this.state.conditions);
      groupId && AARClassifyForm.updateFields({ groupName });
    });
  };
  save = () => {
    const { ledgerStore: { AARClassifyForm }, groupId } = this.props;
    AARClassifyForm.validateForm().then(f => {
      if (f.isValid) {
        this.props.onCancel();
        this.props.onEdit(AARClassifyForm.getFormParams().groupName, groupId);
      } else {
        f.showErrors();
      }
    });
  };

  render() {
    const { conditions } = this.state;
    const { ledgerStore: { AARClassifyForm } } = this.props;
    return (
      <WeaDialog
        {...this.props} style={{ width: 480, height: 80 }} initLoadCss
        buttons={[
          <Button type="primary" onClick={this.save}>{getLabel(537558, "保存")}</Button>
        ]}
      >
        <div className="form-dialog-layout">{getSearchs(AARClassifyForm, conditions, 1, false)}</div>
      </WeaDialog>
    );
  }
}

export default LedgerAccountApprRuleClassifyNameEditDialog;
