/*
 * Author: 黎永顺
 * name:社保福利台账重构-列表
 * Description:
 * Date: 2024/1/23
 */
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { message, Modal, Spin } from "antd";
import { WeaLocaleProvider } from "ecCom";
import { calcPageNo, getIframeParentHeight } from "../../../../util";
import * as API from "../../../../apis/standingBook";
import { convertToUrlString } from "../../../../util/url";

const getLabel = WeaLocaleProvider.getLabel;

@inject("taxAgentStore")
@observer
class WelfareRecordList extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: false, dataSource: [], columns: [],
      pageInfo: { current: 0, pageSize: 10, total: 0 }
    };
  }

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

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

  batDelQueryList = (selectDelDataLen = 1) => {
    this.setState({
      pageInfo: {
        ...this.state.pageInfo,
        current: calcPageNo(this.state.pageInfo.total, this.state.pageInfo.current, 10, selectDelDataLen)
      }
    }, () => {
      this.getWelfareRecordList();
    });
  };
  handleReceive = ({ data }) => {
    const { pageInfo } = this.state;
    const { type, payload: { id, params } = {} } = data;
    if (type === "init") {
      this.getWelfareRecordList();
    } else if (type === "turn") {
      const module = {
        billMonth: params.billMonth,
        paymentOrganization: params.paymentOrganizationId
      };
      switch (id) {
        case "PAGEINFO":
          this.setState({ pageInfo: { ...pageInfo, ...params } }, () => this.getWelfareRecordList());
          break;
        case "CHECKBOX":
          const { selectedRowKeys } = params;
          this.props.onChangeSelectedRowKeys(selectedRowKeys);
          break;
        case "CALC":
        case "VIEW":
          const payload = {
            billMonth: params.billMonth,
            paymentOrganization: params.paymentOrganizationId,
            creator: params.creator,
            type: id === "VIEW" ? "detail" : ""
          };
          window.open(`/spa/hrmSalary/static/index.html#/main/hrmSalary/socialSecurityBenefits/standingBookDetail?${convertToUrlString(payload)}`);
          break;
        case "DELRC":
          Modal.confirm({
            title: getLabel(111, "确认信息"), content: getLabel(388758, "确认要删除吗?"),
            onOk: () => {
              API.siaccountDelete(module).then(({ status, errormsg }) => {
                if (status) {
                  message.success(getLabel(502230, "删除成功"));
                  this.batDelQueryList();
                } else {
                  message.error(errormsg);
                }
              });
            }
          });
          break;
        case "RECALC":
          Modal.confirm({
            title: getLabel(111, "确认信息"),
            content: getLabel(111, "重新核算后，此条台账数据将可以删除且可以进行此月份的核算，是否确认取消归档？"),
            onOk: () => {
              API.socialSecurityBenefitsRecalculate({ id: params.id })
                .then(({ status, errormsg }) => {
                  if (status) {
                    message.success(getLabel(30700, "操作成功！"));
                    this.getWelfareRecordList();
                  } else {
                    message.error(errormsg);
                  }
                });
            }
          });
          break;
        case "FILE":
          Modal.confirm({
            title: getLabel(111, "确认信息"),
            content: getLabel(111, "归档后，此条台账数据无法删除且不可以再进行此月份的核算，是否确认归档？"),
            onOk: () => {
              API.siaccountFile(module).then(({ status, errormsg }) => {
                if (status) {
                  message.success(getLabel(30700, "操作成功！"));
                  this.getWelfareRecordList();
                } else {
                  message.error(errormsg);
                }
              });
            }
          });
          break;
        case "log":
          this.props.onFilterLog(id, params.id);
          break;
        default:
          break;
      }
    }
  };
  postMessageToChild = (payload = {}) => {
    const i18n = {
      "共": getLabel(18609, "共"), "条": getLabel(18256, "条"),
      "总计": getLabel(523, "总计"), "已归档": getLabel(18800, "已归档"),
      "未归档": getLabel(17999, "未归档"), "操作": getLabel(30585, "操作"),
      "核算": getLabel(538780, "核算"), "归档": getLabel(251, "归档"),
      "查看": getLabel(33564, "查看"), "重新核算": getLabel(542637, "重新核算"),
      "删除": getLabel(535052, "删除"), "操作日志": getLabel(545781, "操作日志")
    };
    const childFrameObj = document.getElementById("unitTable");
    childFrameObj && childFrameObj.contentWindow.postMessage(JSON.stringify({ ...payload, i18n }), "*");
  };
  getWelfareRecordList = () => {
    const { queryForm, taxAgentStore: { showOperateBtn }, selectedRowKeys } = this.props;
    const { pageInfo } = this.state;
    const payload = { ...pageInfo, ...queryForm, taxAgents: queryForm.taxAgents ? queryForm.taxAgents.split(",") : [] };
    this.setState({ loading: true });
    API.getCommonList(payload).then(({ status, data }) => {
      this.setState({ loading: false });
      if (status) {
        const { pageInfo: result } = data;
        const { columns, list: dataSource, pageNum: current, pageSize, total } = result;
        this.setState({
          pageInfo: { ...pageInfo, current, pageSize, total },
          dataSource, columns
        }, () => this.postMessageToChild({
          scrollHeight: 108, dataSource, columns, pageInfo: this.state.pageInfo, showOperateBtn, selectedRowKeys,
          unitTableType: "welfareRecord"
        }));
      }
    }).catch(() => this.setState({ loading: false }));
  };

  render() {
    const { loading, dataSource } = this.state;
    return (
      <div className="salary-welfare-record-list"
           style={{ height: getIframeParentHeight(".wea-new-top-content", dataSource.length, 46) + "px" }}>
        <Spin spinning={loading}>
          <iframe
            style={{ border: 0, width: "100%", height: "100%" }}
            // src="http://localhost:7607/#/unitTable"
            src="/spa/hrmSalary/hrmSalaryCalculateDetail/index.html#/unitTable"
            id="unitTable"
          />
        </Spin>
      </div>
    );
  }
}

export default WelfareRecordList;
