/*
 * Author: 黎永顺
 * name: 薪资档案页面重构-导入弹窗
 * Description:
 * Date: 2024/1/9
 */
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 { importSalaryArchive } from "../../../../apis/payrollFiles";

const getLabel = WeaLocaleProvider.getLabel;

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

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) {
      const { payrollFilesStore: { salaryFileQueryForm }, importType, isExtEmp } = nextProps;
      let payload = {}, extraPreview = {};
      if (importType !== "init" && importType !== "salaryItemAdjust") {
        extraPreview = {
          importType: "", listType: importType
        };
        payload = {
          importType: "", listType: importType, hasData: false,
          ...salaryFileQueryForm.getFormParams()
        };
      } else {
        extraPreview = {
          importType: importType, listType: "FIXED"
        };
        payload = {
          importType: importType, listType: "FIXED", hasData: false,
          ...salaryFileQueryForm.getFormParams()
        };
      }
      if (isExtEmp) payload = { ...payload, extSalaryArchiveList: true };
      this.setState({
        importDialog: {
          ...this.state.importDialog, extraPreview, importResult: {},
          link: `/api/bs/hrmsalary/salaryArchive/downloadTemplate?${convertToUrlString(payload)}`
        }
      });
    } else {
      this.setState({
        importDialog: {
          nextloading: false, link: "", importResult: {}, imageId: "",
          previewUrl: "/api/bs/hrmsalary/salaryArchive/preview",
          extraPreview: {}
        }
      });
    }
  }

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

export default Index;
