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

const APIFox = {
  "regression": API.recessionList,
  "difference": API.balanceList
};

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

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

  handleReceive = async ({ data }) => {
    const { onEdit, onChangeRowkey } = this.props;
    const { type, payload: { id, params } = {} } = data;
    if (type === "init") {
      this.postMessageToChild();
    } else if (type === "turn") {
      if (id === "PAGEINFO") {
        const { pageNum: current, size: pageSize } = params;
        this.setState({ pageInfo: { ...this.state.pageInfo, current, pageSize } }, () => this.recessionList());
      } else if (id === "ROWSELECT") {
        const { selectedRowKeys } = params;
        this.setState({ selectedRowKeys });
        onChangeRowkey(selectedRowKeys);
      } else if (id === "EDIT") {
        onEdit(params);
      }
    }
  };
  postMessageToChild = () => {
    const paymentStatus = this.props.type === "difference" ? "4" : "3";
    const creator = Number(getQueryString("creator"));
    const billMonth = getQueryString("billMonth");
    const paymentOrganization = getQueryString("paymentOrganization");
    const childFrameObj = document.getElementById("atdTable");
    const { pageInfo, dataSource, columns, selectedRowKeys, datalistPayload } = this.state;
    childFrameObj.contentWindow.postMessage(JSON.stringify({
      dataSource, columns, pageInfo,
      selectedRowKeys, selectedKey: this.props.type,
      sumpayload: { billMonth, paymentOrganization, creator, paymentStatus, ...datalistPayload },
      showOperates: !getQueryString("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) => {
    const { type } = this.props;
    const { loading, pageInfo } = this.state;
    const billMonth = getQueryString("billMonth");
    const paymentOrganization = getQueryString("paymentOrganization");
    const creator = Number(getQueryString("creator"));
    const paymentStatus = type === "difference" ? "4" : "3";
    const payload = {
      billMonth, paymentStatus,
      creator, paymentOrganization,
      ...pageInfo,
      ...module
    };
    this.setState({ loading: { ...loading, query: true } });
    APIFox[type](payload).then(({ status, data }) => {
      this.setState({ loading: { ...loading, query: 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,
          datalistPayload: module
        }, () => this.postMessageToChild());
      }
    }).catch(() => {
      this.setState({ loading: { ...loading, query: false } });
    });
  };

  render() {
    const { loading } = this.state;
    return (
      <Spin spinning={loading.query}>
        <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;
