/*
 * Author: 黎永顺
 * name: 个税申报表详情-导入
 * Description:
 * Date: 2023/12/28
 */
import React, { Component } from "react";
import { WeaCheckbox, WeaLocaleProvider } from "ecCom";
import { message } from "antd";
import ImportDialog from "../../../components/importDialog";
import * as API from "../../../apis/declare";

const getLabel = WeaLocaleProvider.getLabel;

class TaxDeclareDetailImportDialog extends Component {
  constructor(props) {
    super(props);
    this.state = {
      importDialog: {
        visible: false, title: "", nextloading: false, hasData: "0",
        link: null, importResult: {}, imageId: "", taxDeclarationId: "",
        previewUrl: "/api/bs/hrmsalary/taxdeclaration/preview",
        extraPreview: { taxDeclarationId: "" }
      }
    };
  }

  handleImportTaxDeclare = (taxDeclarationId) => {
    this.setState({
      importDialog: {
        ...this.state.importDialog, link: this.handleExportTemp,
        taxDeclarationId, visible: true, title: getLabel(24023, "数据导入"),
        extraPreview: { taxDeclarationId }
      }
    });
  };
  handleImport = (payload) => {
    const { importDialog } = this.state;
    const { taxDeclarationId } = importDialog;
    this.setState({ importDialog: { ...importDialog, nextloading: true } });
    API.taxdeclarationImportData({ ...payload, taxDeclarationId })
      .then(({ data, status }) => {
        this.setState({ importDialog: { ...importDialog, nextloading: false } });
        if (status) {
          this.setState({
            importDialog: { ...importDialog, ...payload, importResult: data }
          }, () => this.props.onSuccess());
        }
      }).catch(() => this.setState({ importDialog: { ...importDialog, nextloading: false } }));
  };
  handleExportTemp = () => {
    const { importDialog: { taxDeclarationId, hasData } } = this.state;
    message.destroy();
    message.loading(getLabel(111, "下载中"), 0);
    const promise = API.taxdeclarationExportTemplate({ taxDeclarationId, hasData: hasData === "1" });
    message.destroy();
  };
  handleCancel = () => {
    this.setState({
      importDialog: {
        visible: false, title: "", nextloading: false, hasData: "0",
        link: null, importResult: {}, imageId: "", taxDeclarationId: "",
        previewUrl: "/api/bs/hrmsalary/taxdeclaration/preview"
      }
    });
  };

  render() {
    const { importDialog } = this.state;
    return (
      <ImportDialog
        {...importDialog} onCancel={this.handleCancel}
        onResetImportResult={() => this.setState(({
          importDialog: { ...importDialog, importResult: {}, imageId: "", hasData: "0" }
        }))}
        nextCallback={imageId => this.setState({ importDialog: { ...importDialog, imageId } })}
        nextUplaodCallback={imageId => this.handleImport({ imageId })}
        exportDataDom={
          <WeaCheckbox
            content={getLabel(543208, "导出现有数据")} value={importDialog.hasData}
            helpfulTip={getLabel(111, "提示：建议先导出现有最新数据，修改后再导入")}
            onChange={val => this.setState({ importDialog: { ...importDialog, hasData: val } })}
          />
        }
      />
    );
  }
}

export default TaxDeclareDetailImportDialog;
