/*
 * Author: 黎永顺
 * name: 薪资核算-线下对比列表
 * Description:
 * Date: 2023/9/26
 */
import React, { Component } from "react";
import { WeaLocaleProvider } from "ecCom";
import { Alert, Spin } from "antd";
import { comparisonResultList } from "../../../../apis/calculate";

const getLabel = WeaLocaleProvider.getLabel;

class SalaryCalcOcList extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false, pageInfo: { current: 1, pageSize: 10, total: 0 },
      columns: [], dataSource: [], formulaDesc: ""
    };
  }

  componentDidMount() {
    window.addEventListener("message", this.handleReceive, false);
  }

  componentWillUnmount() {
    window.removeEventListener("message", this.handleReceive, false);
  }

  handleReceive = async ({ data }) => {
    const { type, payload: { id, params } = {} } = data;
    if (type === "init") {
      this.comparisonResultList(this.props);
    } else if (type === "turn") {
      switch (id) {
        case "PAGEINFO":
          const { size: pageSize, pageNum: current } = params;
          this.setState({
            pageInfo: { ...this.state.pageInfo, current, pageSize }
          }, () => this.comparisonResultList(this.props));
          break;
        case "FORMULA":
          const { formulaDesc } = params;
          this.setState({ formulaDesc });
          break;
        default:
          break;
      }
    }
  };

  componentWillReceiveProps(nextProps, nextContext) {
    if (
      (nextProps.form.onlyDiffEmployee !== this.props.form.onlyDiffEmployee) ||
      (nextProps.form.onlyDiffSalaryItem !== this.props.form.onlyDiffSalaryItem) ||
      (nextProps.searchBool !== this.props.searchBool)
    ) {
      this.comparisonResultList(nextProps);
    }
  }

  postMessageToChild = (payload = {}) => {
    const i18n = {
      "共": getLabel(18609, "共"), "条": getLabel(18256, "条"),
      "总计": getLabel(523, "总计"), "系统值": getLabel(543280, "系统值"),
      "线下值": getLabel(543281, "线下值"), "差值": getLabel(543282, "差值")
    };
    const childFrameObj = document.getElementById("atdTable");
    childFrameObj.contentWindow.postMessage(JSON.stringify({ ...payload, i18n }), "*");
  };
  comparisonResultList = (props) => {
    const { form, routeParams: { salaryAcctRecordId } } = props;
    const { pageInfo } = this.state;
    this.setState({ loading: true });
    const payload = { ...pageInfo, ...form, salaryAcctRecordId };
    comparisonResultList(payload).then(({ status, data }) => {
      this.setState({ loading: false });
      if (status) {
        const { columns, pageInfo: { pageNum: current, pageSize, total, list: dataSource } } = data;
        this.setState({
          dataSource, columns: this.convertColumns(this.removeItem(columns)),
          pageInfo: { ...pageInfo, current, pageSize, total }
        }, () => {
          const { pageInfo, dataSource, columns } = this.state;
          this.postMessageToChild({ dataSource, pageInfo, columns });
        });
      }
    }).catch(() => this.setState({ loading: false }));
  };
  removeItem = (cols) => {
    return _.filter(cols, o => {
      if (JSON.stringify(o.children) === "[]") {
        return false;
      } else {
        this.removeItem(o.children);
        return true;
      }
    });
  };
  convertColumns = (cols) => {
    return _.map(cols, (item, index) => {
      const col = { title: item.text, dataIndex: item.column };
      if (_.isNaN(parseInt(item.column))) {
        return { ...col, width: 150, fixed: item.fixed || false };
      } else {
        return { ...col, width: 180, children: this.convertColumns(item.children) };
      }
    });
  };

  render() {
    const { loading, formulaDesc } = this.state;
    return (
      <div className="wea-new-table ">
        <Alert message={`${getLabel(18125, "公式")}：${formulaDesc}`} type="info"/>
        <Spin spinning={loading}>
          <iframe
            style={{ border: 0, width: "100%", height: "100%" }}
            // src="http://localhost:7607/#/OCTable"
            src="/spa/hrmSalary/hrmSalaryCalculateDetail/index.html#/OCTable"
            id="atdTable"
          />
        </Spin>
      </div>
    );
  }
}

export default SalaryCalcOcList;
