/*
 * Author: 黎永顺
 * name: 退差列表
 * Description:
 * Date: 2022/11/23
 */
import React, { Component } from "react";
import { Spin } from "antd";
import { sysConfCodeRule } from "../../../../apis/ruleconfig";
import * as API from "../../../../apis/standingBook";
import "./index.less";

const APIFox = {
  "regression": API.recessionList,
  "regressionSum": API.recessionListSum,
  "difference": API.balanceList,
  "differenceSum": API.balanceListSum
};

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

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

  handleReceive = async ({ data }) => {
    const { onEdit, onChangeRowkey } = this.props;
    const { type, payload: { id, params } = {} } = data;
    if (type === "init") {
      this.recessionList();
    } else if (type === "turn") {
      if (id === "PAGEINFO") {
        const { pageNum: current, size: pageSize } = params;
        this.setState({ pageInfo: { ...this.state.pageInfo, current, pageSize } }, () => this.recessionList({}, true));
      } else if (id === "ROWSELECT") {
        const { selectedRowKeys } = params;
        this.setState({ selectedRowKeys });
        onChangeRowkey(selectedRowKeys);
      } else if (id === "EDIT") {
        onEdit(params);
      }
    }
  };
  postMessageToChild = () => {
    const childFrameObj = document.getElementById("atdTable");
    childFrameObj && childFrameObj.contentWindow.postMessage(JSON.stringify({
      ..._.pick(this.state, ["dataSource", "columns", "pageInfo", "selectedRowKeys", "sumRow"]),
      showOperates: !this.props.type
    }), "*");
  };

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible) this.recessionList();
  }

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

  handleResetSelectRowKeys = (selectedRowKeys) => {
    this.setState({ selectedRowKeys });
  };
  recessionList = (module, pageTurning = false) => {
    const { regType } = this.props, { pageInfo } = this.state;
    const paymentStatus = regType === "difference" ? "4" : "3";
    const payload = {
      ...pageInfo, ...module, paymentStatus,
      ..._.pick(this.props, ["billMonth", "paymentOrganization", "creator"])
    };
    this.setState({ loading: true });
    APIFox[regType](payload).then(({ status, data }) => {
      this.setState({ loading: false });
      if (status) {
        const { pageInfo: list } = data;
        const { columns, list: dataSource, pageNum: current, pageSize, total } = list;
        this.setState({
          pageInfo: { ...pageInfo, current, pageSize, total }, dataSource, columns
        }, () => this.postMessageToChild());
      }
    });
    sysConfCodeRule({ code: "OPEN_ACCT_RESULT_SUM" }).then(({ status, data }) => {
      if (status && data === "1" && !pageTurning) {
        APIFox[`${regType}Sum`](payload).then(({ status: sumStatus, data: sumData }) => {
          if (sumStatus) this.setState({ sumRow: sumData.sumRow }, () => this.postMessageToChild());
        });
      }
    });
  };

  render() {
    const { loading } = this.state;
    return (
      <Spin spinning={loading}>
        <iframe
          style={{ border: 0, width: "100%", height: "100%" }}
          // src="http://localhost:7607/#/standingbookTable"
          src="/spa/hrmSalary/hrmSalaryCalculateDetail/index.html#/standingbookTable"
          id="atdTable"
        />
      </Spin>
    );
  }
}

export default RegList;
