/*
 * Author: 黎永顺
 * name: 批量更新-薪资项目的值
 * Description:
 * Date: 2023/12/6
 */
import React, { Component } from "react";
import { Button, message } from "antd";
import { inject, observer } from "mobx-react";
import { WeaDialog, WeaLocaleProvider } from "ecCom";
import { getSearchs, toDecimal_n } from "../../../../../util";
import { salaryacctBatchUpdate } from "../../../../../apis/calculate";

const getLabel = WeaLocaleProvider.getLabel;

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

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visibe && !nextProps.visible) nextProps.calculateStore.initBatchUpdateForm();
  }

  save = () => {
    const {
      calculateStore: { batchUpdateForm }, salaryAcctRecordId, salaryItemId,
      idList, pattern, dataType
    } = this.props;
    batchUpdateForm.validateForm().then(f => {
      if (f.isValid) {
        const { value } = batchUpdateForm.getFormParams();
        this.setState({ loading: true });
        const payload = {
          salaryAcctRecordId, salaryItemId, idList,
          value: dataType === "number" ? toDecimal_n(value, pattern) : value
        };
        this.setState({ loading: false });
        salaryacctBatchUpdate(payload).then(({ status, errormsg }) => {
          if (status) {
            message.success(getLabel(30700, "操作成功！"));
            this.props.onCancel(true);
          } else {
            message.error(errormsg);
          }
        }).catch(() => this.setState({ loading: false }));
      } else {
        f.showErrors();
      }
    });
  };

  render() {
    const { loading } = this.state;
    const { conditions, calculateStore: { batchUpdateForm } } = this.props;
    return (
      <WeaDialog
        {...this.props} title={getLabel(111, "批量更新")} initLoadCss
        style={{ width: 480, height: 127 }}
        buttons={[
          <Button type="primary" onClick={this.save} loading={loading}>{getLabel(537558, "保存")}</Button>
        ]}
      >
        <div className="calc-update-dialog-layout">{getSearchs(batchUpdateForm, conditions, 1, false)}</div>
      </WeaDialog>
    );
  }
}

export default BatchUpdateSalaryItemValDialog;
