/*
 * Author: 黎永顺
 * name: 人员信息报送-导入
 * Description:
 * Date: 2023/12/28
 */
import React, { Component } from "react";
import { WeaCheckbox, WeaLocaleProvider, WeaSelect } from "ecCom";
import { message } from "antd";
import ImportDialog from "../../../components/importDialog";
import * as API from "../../../apis/declare";

const getLabel = WeaLocaleProvider.getLabel;

class EmployeeDeclareDetailImportDialog extends Component {
  constructor(props) {
    super(props);
    this.state = {
      importDialog: {
        visible: false, title: "", nextloading: false, checkType: "jobNum",
        link: null, importResult: {}, imageId: "", tempPayload: {},
        previewUrl: "/api/bs/hrmsalary/employeedeclare/preview"
      }
    };
  }

  handleImportEmployeeDeclare = (tempPayload) => {
    this.setState({
      importDialog: {
        ...this.state.importDialog, link: this.handleExportTemp,
        tempPayload, visible: true, title: getLabel(24023, "数据导入")
      }
    });
  };
  handleImport = (payload) => {
    const { importDialog } = this.state;
    const { tempPayload, checkType } = importDialog;
    this.setState({ importDialog: { ...importDialog, nextloading: true } });
    API.employeedeclareImportData({ ...payload, ...tempPayload, checkType })
      .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: { tempPayload } } = this.state;
    message.destroy();
    message.loading(getLabel(111, "下载中"), 0);
    const promise = API.employeedeclareExportTemplate(tempPayload);
    message.destroy();
  };
  handleCancel = () => {
    this.setState({
      importDialog: {
        visible: false, title: "", nextloading: false, checkType: "jobNum",
        link: null, importResult: {}, imageId: "", tempPayload: {},
        previewUrl: "/api/bs/hrmsalary/employeedeclare/preview"
      }
    });
  };
  renderFormComponent = () => {
    const { importDialog } = this.state;
    const { checkType } = importDialog;
    return <div style={{ padding: "8px 16px", border: "1px solid #e5e5e5", margin: "4px 0" }}>
      <div className="weapp-salary-tb-filter">
        <div className="tbf-item">
          <div className="tbfi-label">{getLabel(111, "校验字段")}</div>
          <WeaSelect
            detailtype={3} value={checkType} onChange={checkType => {
            this.setState(({ importDialog: { ...importDialog, checkType } }));
          }} options={[
            { key: "jobNum", showname: getLabel(1933, "工号") },
            { key: "sfz", showname: getLabel(1887, "身份证号码") }
          ]}
          />
        </div>
      </div>
    </div>;
  };

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

export default EmployeeDeclareDetailImportDialog;
