/*
 * Author: 黎永顺
 * name: 导出模板保存框
 * Description:
 * Date: 2024/3/29
 */
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { WeaDialog, WeaLocaleProvider } from "ecCom";
import { Button, message } from "antd";
import { getSearchs } from "../../../../../util";
import { tempConditions } from "../../../../../common/conditions";
import * as API from "../../../../../apis/calculate";

const getLabel = WeaLocaleProvider.getLabel;

@inject("calculateStore")
@observer
class TempDialog extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false
    };
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) {
      nextProps.calculateStore.tempForm.initFormFields(tempConditions);
    } else {
      nextProps.calculateStore.initTempForm();
    }
  }

  save = () => {
    const { calculateStore: { tempForm }, salaryAcctRecordId, id, salaryItemIds } = this.props;
    tempForm.validateForm().then(f => {
      if (f.isValid) {
        const payload = tempForm.getFormParams();
        this.setState({ loading: true });
        API.saveExportTemplate({ ...payload, salaryAcctRecordId, id, salaryItemIds })
          .then(({ status, errormsg }) => {
            this.setState({ loading: false });
            if (status) {
              message.success(getLabel(111, "操作成功！"));
              this.props.onCancel(true);
            } else {
              message.error(errormsg);
            }
          }).catch(() => this.setState({ loading: false }));
      } else {
        f.showErrors();
      }
    });
  };

  render() {
    const { loading } = this.state;
    const { calculateStore: { tempForm } } = this.props;
    return (
      <WeaDialog
        {...this.props} style={{ width: 400, height: 80 }} initLoadCss title={getLabel(111, "存为模板")}
        buttons={[
          <Button type="primary" loading={loading} onClick={this.save}>{getLabel(111, "保存")}</Button>
        ]}
      >
        <div className="form-dialog-layout">{getSearchs(tempForm, tempConditions, 1, false)}</div>
      </WeaDialog>
    );
  }
}

export default TempDialog;
