/*
 * Author: 黎永顺
 * name: 社保福利台账-核算-导入
 * Description:
 * Date: 2024/3/5
 */
import React, { Component } from "react";
import { WeaLocaleProvider } from "ecCom";
import { Badge, Button, message } from "antd";
import ImportDialog from "../../../../components/importDialog";
import ImportHeaderSetDialog from "./importHeaderSetDialog";
import * as API from "../../../../apis/standingBook";
import { getQueryString } from "../../../../util/url";

const getLabel = WeaLocaleProvider.getLabel;

class StandingBookCalcImportDialog extends Component {
  constructor(props) {
    super(props);
    this.state = {
      headerFieldsDialog: { visible: false, itemsByGroup: [], selectItems: [] },
      importDialog: {
        visible: false, title: "", nextloading: false,
        link: null, importResult: {}, imageId: "",
        previewUrl: "/api/bs/hrmsalary/siaccount/welfare/preview"
      }
    };
  }

  async componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) {
      const { fieldUrl } = nextProps;
      const { data } = await API[fieldUrl]();
      this.setState({
        importDialog: {
          ...this.state.importDialog, importResult: {}, link: this.handleExportTemp, title: getLabel(24023, "数据导入")
        },
        headerFieldsDialog: {
          ...this.state.headerFieldsDialog, itemsByGroup: data,
          selectItems: _.map(_.filter(data, k => k.checked), o => o.fieldId)
        }
      });
    }
    this.setState({ importDialog: { ...this.state.importDialog, visible: nextProps.visible } });
  }

  handleImport = (payload) => {
    const { importDialog, headerFieldsDialog: { selectItems } } = this.state;
    if (_.isEmpty(selectItems)) {
      message.error(getLabel(111, "请选择表头字段"));
    } else {
      const { importUrl, importparams = {} } = this.props;
      this.setState({ importDialog: { ...importDialog, nextloading: true } });
      API[importUrl]({ ...payload, ...importparams }).then(({ data, status, errormsg }) => {
        this.setState({ importDialog: { ...importDialog, nextloading: false } });
        if (status) {
          this.setState({
            importDialog: { ...importDialog, ...payload, importResult: data }
          });
        } else {
          message.warning(errormsg);
        }
      }).catch(() => this.setState({ importDialog: { ...importDialog, nextloading: false } }));
    }
  };
  handleExportTemp = () => {
    const { tmpUrl } = this.props;
    const { headerFieldsDialog: { selectItems, itemsByGroup } } = this.state;
    if (_.isEmpty(selectItems)) {
      message.error(getLabel(111, "请选择表头字段"));
    } else {
      const billMonth = getQueryString("billMonth");
      const paymentOrganization = getQueryString("paymentOrganization");
      const payload = {
        billMonth,
        welfareNames: _.map(_.filter(itemsByGroup, k => selectItems.includes(k.fieldId)), o => o.salaryItemName),
        paymentOrganization: Number(paymentOrganization)
      };
      const promise = API[tmpUrl](payload);
    }
  };
  handleSelectedField = () => {
    this.setState({
      headerFieldsDialog: {
        ...this.state.headerFieldsDialog, visible: true
      }
    });
  };
  /*
   * Author: 黎永顺
   * Description:表单选项
   * Params:
   * Date: 2023/9/18
   */
  renderFormComponent = () => {
    const { selectItems } = this.state.headerFieldsDialog;
    return <div style={{ padding: "8px 16px", border: "1px solid #e5e5e5", margin: "4px 0" }}>
      <Badge
        count={!_.isEmpty(selectItems) ? selectItems.length : 0}>
        <Button onClick={this.handleSelectedField}>{getLabel(111, "请选择表头字段")}</Button>
      </Badge>
    </div>;
  };

  render() {
    const { importDialog, headerFieldsDialog } = this.state;
    const { cacheUrl } = this.props;
    return (
      <React.Fragment>
        <ImportDialog
          {...importDialog} onCancel={this.props.onCancel}
          importParams={this.renderFormComponent()}
          onResetImportResult={() => this.setState(({
            importDialog: { ...importDialog, importResult: {}, imageId: "" }
          }))}
          nextCallback={imageId => this.setState({ importDialog: { ...importDialog, imageId } })}
          nextUplaodCallback={imageId => this.handleImport({ imageId })}
        />
        {/*表头设置*/}
        <ImportHeaderSetDialog {...headerFieldsDialog}
                               onCancel={() => this.setState({
                                 headerFieldsDialog: { ...headerFieldsDialog, visible: false }
                               })}
                               onAdd={selectItems => this.setState({
                                 headerFieldsDialog: { ...headerFieldsDialog, visible: false, selectItems }
                               }, () => {
                                 const { selectItems: welfareNames } = this.state.headerFieldsDialog;
                                 const promise = API[cacheUrl]({ welfareNames });
                               })}
        />
      </React.Fragment>
    );
  }
}

export default StandingBookCalcImportDialog;
