/*
 * Author: 黎永顺
 * name: 薪資核算-线下对比导入
 * Description:
 * Date: 2023/9/27
 */
import React, { Component } from "react";
import { WeaLocaleProvider } from "ecCom";
import ImportDialog from "../../../../components/importDialog";
import { importComparisonExcelAcctResult } from "../../../../apis/calculate";

const getLabel = WeaLocaleProvider.getLabel;

class SalaryCalcOcImport extends Component {
  constructor(props) {
    super(props);
    this.state = {
      importDialog: {
        visible: false, title: "", nextloading: false, importResult: {}, imageId: "", link: "",
        previewUrl: "/api/bs/hrmsalary/salaryacct/comparisonresult/preview",
        extraPreview: { salaryAcctRecordId: "" }
      }
    };
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible) {
      const { importDialog } = this.state;
      this.setState({
        importDialog: {
          ...this.state.importDialog,
          link: `/api/bs/hrmsalary/salaryacct/comparisonresult/importtemplate/export?salaryAcctRecordId=${nextProps.salaryAcctRecordId}`,
          visible: nextProps.visible, importResult: {},
          title: nextProps.title, imageId: "",
          extraPreview: { salaryAcctRecordId: nextProps.salaryAcctRecordId }
        }
      });
    }
  }

  handleImport = (payload) => {
    const { importDialog } = this.state;
    const { salaryAcctRecordId } = this.props;
    this.setState({ importDialog: { ...importDialog, nextloading: true } });
    importComparisonExcelAcctResult({ ...payload, salaryAcctRecordId })
      .then(({ data, status }) => {
        this.setState({ importDialog: { ...importDialog, nextloading: false } });
        if (status) {
          this.setState({
            importDialog: { ...importDialog, ...payload, importResult: data }
          });
        }
      }).catch(() => this.setState({ importDialog: { ...importDialog, nextloading: false } }));
  };

  render() {
    const { importDialog } = this.state;
    return (
      <ImportDialog
        {...importDialog} onCancel={this.props.onCancel} exportDataDom={null}
        onResetImportResult={() => this.setState(({
          importDialog: { ...importDialog, importResult: {}, imageId: "" }
        }))}
        nextCallback={imageId => this.setState({ importDialog: { ...importDialog, imageId } })}
        nextUplaodCallback={imageId => this.handleImport({ imageId })}
      />
    );
  }
}

export default SalaryCalcOcImport;
