/*
 * Author: 黎永顺
 * name: 社保福利档案页面重构-导入
 * Description:
 * Date: 2023/11/6
 */
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { WeaCheckbox, WeaLocaleProvider } from "ecCom";
import ImportDialog from "../../../../../components/importDialog";
import { convertToUrlString, getURLParameters } from "../../../../../util/url";
import { importBatch } from "../../../../../apis/welfareArchive";

const getLabel = WeaLocaleProvider.getLabel;

@inject("archivesStore")
@observer
class Index extends Component {
  constructor(props) {
    super(props);
    this.state = {
      importDialog: {
        nextloading: false, link: "", importResult: {}, imageId: "",
        previewUrl: "/api/bs/hrmsalary/scheme/preview"
      }
    };
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) {
      const { runStatuses, archivesStore: { welfareForm } } = nextProps;
      const payload = {
        [runStatuses !== "ext" ? "exportData" : "extWelArchiveList"]: "false",
        runStatuses, ...welfareForm.getFormParams()
      };
      this.setState({
        importDialog: {
          ...this.state.importDialog, importResult: {},
          link: `/api/bs/hrmsalary/scheme/template/export?${convertToUrlString(payload)}`
        }
      });
    } else {
      this.setState({
        importDialog: {
          nextloading: false, link: "", importResult: {}, imageId: "",
          previewUrl: "/api/bs/hrmsalary/scheme/preview"
        }
      });
    }
  }

  handleImport = (payload) => {
    const { importDialog } = this.state, { runStatuses: runStatus } = this.props;
    this.setState({ importDialog: { ...importDialog, nextloading: true } });
    importBatch({ ...payload, runStatus: runStatus.split(",")[0] }).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 { runStatuses, archivesStore: { welfareForm } } = this.props;
    const { importDialog } = this.state;
    const { link } = importDialog;
    return (
      <ImportDialog
        {...this.props} {...importDialog}
        onResetImportResult={() => this.setState({
          importDialog: {
            ...importDialog, importResult: {}, imageId: "", link: ""
          }
        })}
        exportDataDom={
          <WeaCheckbox
            value={getURLParameters(link)["exportData"] === "true" ? "1" : "0"}
            content={getLabel(543208, "导出现有数据")}
            helpfulTip={getLabel(111, "提示：建议先导出现有最新数据，修改后再导入")}
            onChange={val => {
              let payload = {
                exportData: val === "1", runStatuses, ...welfareForm.getFormParams()
              };
              runStatuses === "ext" && (payload = { ...payload, extWelArchiveList: true, runStatuses: "" });
              this.setState({
                importDialog: {
                  ...importDialog,
                  link: `/api/bs/hrmsalary/scheme/template/export?${convertToUrlString(payload)}`
                }
              });
            }}
          />
        }
        nextCallback={imageId => this.setState({ importDialog: { ...importDialog, imageId } })}
        nextUplaodCallback={imageId => this.handleImport({ imageId })}
      />
    );
  }
}

export default Index;
