/*
 * Author: 黎永顺
 * name: 复制账套
 * Description:
 * Date: 2022/12/8
 */
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { copyConditions } from "../config";
import { duplicateLedger } from "../../../apis/ledger";
import { WeaDialog } from "ecCom";
import { Button, message } from "antd";
import { getSearchs } from "../../../util";
import "./index.less";

@inject("ledgerStore", "taxAgentStore")
@observer
class CopyLedgerModal extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false
    };
  }

  componentDidMount() {
    this.getTaxAgentSelectListAsAdmin();
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) {
      const { ledgerStore, name, taxAgentId } = nextProps;
      const { copyForm: form } = ledgerStore;
      form.updateFields({
        name: { value: name },
        taxAgentId: { value: taxAgentId.toString() }
      });
    }
  }

  getTaxAgentSelectListAsAdmin = () => {
    const { taxAgentStore, ledgerStore } = this.props;
    const { copyForm: form } = ledgerStore;
    const { getTaxAgentSelectListAsAdmin } = taxAgentStore;
    getTaxAgentSelectListAsAdmin().then(({ status, data }) => {
      if (status) {
        const conditions = _.map(copyConditions, it => {
          it.items = _.map(it.items, child => {
            if (child.domkey[0] === "taxAgentId") {
              return {
                ...child,
                options: _.map(data, it => ({ key: it.id, showname: it.content }))
              };
            } else {
              return { ...child };
            }
          });
          return { ...it };
        });
        form.initFormFields(conditions);
      }
    });
  };
  handleSubmit = () => {
    const { ledgerStore, id, onRefreshList, onCancel } = this.props;
    const { copyForm: form } = ledgerStore;
    form.validateForm().then(f => {
      if (f.isValid) {
        const payload = { id, ...form.getFormParams() };
        this.setState({ loading: true });
        duplicateLedger(payload).then(({ status, errormsg }) => {
          this.setState({ loading: false });
          if (status) {
            message.success(errormsg || "复制成功");
            onRefreshList();
            onCancel();
          } else {
            message.error(errormsg || "复制失败");
          }
        });
      } else {
        f.showErrors();
      }
    });
  };


  render() {
    const { onCancel, ledgerStore, ...extra } = this.props;
    const { loading } = this.state;
    const { copyForm: form } = ledgerStore;
    const buttons = [
      <Button type="primary" onClick={this.handleSubmit} loading={loading}>保存</Button>,
      <Button type="ghost" onClick={onCancel}> 取消 </Button>
    ];
    return (
      <WeaDialog
        {...extra}
        initLoadCss
        className="copyWrapper"
        style={{ width: 600 }}
        buttons={buttons}
        onCancel={onCancel}
      >
        {getSearchs(form, copyConditions, 1)}
      </WeaDialog>
    );
  }
}

export default CopyLedgerModal;
