/*
 * Author: 黎永顺
 * name: 非系统人员导入
 * Description:
 * Date: 2023/8/1
 */
import React, { Component } from "react";
import ImportModal from "../../../components/importModal";
import { extEmpPreview, importExtEmp } from "../../../apis/externalPersonManage";

class ExternalPersonImport extends Component {
  constructor(props) {
    super(props);
    this.state = {
      previewDataSource: [],
      previewColumns: [],
      importResult: {}
    };
  }

  extEmpPreview = (params) => {
    extEmpPreview(params).then(({ status, data }) => {
      if (status) {
        const { headers, list } = data;
        this.setState({
          previewColumns: _.map(headers, (it, dataIndex) => ({ title: it, dataIndex })),
          previewDataSource: _.map(list, item => {
            return _.reduce(item, (pre, cur, key) => (_.assign(pre, { [key]: cur })), {});
          })
        });
      }
    });
  };
  handleImportFile = (params) => {
    importExtEmp(params).then(({ status, data }) => {
      if (status) {
        this.setState({
          importResult: data
        });
      }
    });
  };

  render() {
    const { previewDataSource, previewColumns, importResult } = this.state;
    return (
      <ImportModal
        isInit={false} {...this.props}
        columns={previewColumns}
        slideDataSource={previewDataSource}
        importResult={importResult}
        previewImport={this.extEmpPreview}
        importFile={this.handleImportFile}
        templateLink={`/api/bs/hrmsalary/extEmp/importtemplate/export`}
      />
    );
  }
}

export default ExternalPersonImport;
