/*
 * Author: 黎永顺
 * name:考勤数据-导入
 * Description:
 * Date: 2024/3/14
 */
import React, { Component } from "react";
import { WeaLocaleProvider } from "ecCom";
import { Modal } from "antd";
import ImportDialog from "../../../../components/importDialog";
import HeaderSet from "../../../../components/importModal/headerSet";
import ImportFormOptions from "./importFormOptions";

const getLabel = WeaLocaleProvider.getLabel;

class Index extends Component {
  constructor(props) {
    super(props);
    this.state = {
      hasData: false,
      importDialog: {
        nextloading: false, link: null, importResult: {}, imageId: "", params: {},
        previewUrl: ""
      }
    };
  }

  componentWillReceiveProps(nextProps, nextContext) {
    const { hasData, importDialog } = this.state;
    const { params, previewUrl } = nextProps;
    this.setState({
      importDialog: {
        ...importDialog, importResult: {}, link: this.handleExportTemp,
        params, previewUrl, extraPreview: params
      }
    });
  }

  handleExportTemp = () => {
    const { hasData } = this.state;
    const { importFormPayload } = this.props;
    const { salarySobId, salaryYearMonth } = importFormPayload;
    if (!salarySobId || !salaryYearMonth) {
      Modal.warning({
        title: getLabel(131329, "信息确认"),
        content: getLabel(518702, "必要信息不完整，红色*为必填项！")
      });
      return;
    }
    window.open(`/api/bs/hrmsalary/attendQuote/downloadTemplate?salaryYearMonth=${salaryYearMonth}&salarySobId=${salarySobId}`);
  };
  handleImport = (payload) => {
    const { importDialog } = this.state, { importAttendQuoteData, params } = this.props;
    this.setState({ importDialog: { ...importDialog, nextloading: true } });
    importAttendQuoteData({ ...payload, ...params }).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 {
      visible, onCancel, importFormPayload, onChangeImportForm, onHeaderSettings, loading
    } = this.props;
    return (
      <ImportDialog
        visible={visible} onCancel={onCancel} {...importDialog} title={getLabel(24023, "数据导入")}
        onResetImportResult={() => this.setState({
          importDialog: { ...importDialog, importResult: {}, imageId: "", link: "" }
        })}
        importParams={<ImportFormOptions {...importFormPayload} onChangeImportForm={onChangeImportForm}/>}
        exportDataDom={<HeaderSet loading={loading} onSetClick={() => onHeaderSettings({ sourceType: "IMPORT" })}/>}
        nextCallback={imageId => this.setState({ importDialog: { ...importDialog, imageId } })}
        nextUplaodCallback={imageId => this.handleImport({ imageId })}
      />
    );
  }
}

export default Index;
