/*
 * Author: 黎永顺
 * name:社保福利台账总览-总计行
 * Description:
 * Date: 2023/10/17
 */
import React, { FunctionComponent, useCallback, useEffect, useMemo, useState } from "react";
import { ColumnType } from "antd/lib/table";
import { Spin, Table, Typography } from "antd";
import API from "@/api";

const { Text } = Typography;

interface OwnProps {
  columns: ColumnType<any>[];
  dataSourceUrl: string;
  payload: any;
}

type Props = OwnProps;

const welfareFixedTotal: FunctionComponent<Props> = (props) => {
  const [sumRow, setSumRow] = useState<Partial<{}>>({});//薪资核算总计行数据
  const [loading, setLoading] = useState<boolean>(false);
  const columns = useMemo(() => {
    return !_.isEmpty(props.columns) ? props.columns : [];
  }, [props.columns]);
  const dataSourceUrl = useCallback((payload) => {
    return API.CalculateService.getSyMixSum(payload);
  }, [props.dataSourceUrl]);
  useEffect(() => {
    if (!_.isEmpty(props.payload)) {
      setLoading(true);
      dataSourceUrl(props.payload).then(({ data }) => {
        setLoading(false);
        const { data: result, status } = data;
        if (status) setSumRow(result.sumRow || {});
      });
    }
  }, [props.payload]);
  return (<>
    {
      _.map(columns.slice(1), (item: any, index) => {
        return <Table.Summary.Cell index={index + 1} key={index + 1}>
          {
            loading ? <Spin spinning={loading} size="small"></Spin> :
              <Text type="danger">{sumRow[item.dataIndex] || "-"}</Text>
          }
        </Table.Summary.Cell>;
      })
    }
  </>);
};

export default welfareFixedTotal;
