/*
 * Author: 黎永顺
 * Description: 总览
 * Date: 2022-04-20 20:49:23
 * LastEditTime: 2022-05-09 15:07:56
 */
import React, { Component } from "react";
import { Icon, Spin, Tooltip } from "antd";
import { WeaLocaleProvider, WeaNewScroll, WeaTable } from "ecCom";
import { getOverViewList } from "../../../../apis/standingBook";
import "./index.less";
import { convertToUrlString } from "../../../../util/url";

const getLabel = WeaLocaleProvider.getLabel;
export default class OverViewIndex extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dataSource: [], columns: [], pageInfo: { current: 1, pageSize: 10, total: 0 }, loading: false
    };
  }

  componentDidMount() {
    this.getOverViewList();
  }

  getOverViewList = () => {
    const { pageInfo } = this.state;
    this.setState({ loading: true });
    getOverViewList({ ..._.pick(this.props, ["billMonth", "paymentOrganization"]), ...pageInfo })
      .then(({ status, data }) => {
        this.setState({ loading: false });
        if (status) {
          const { columns, list: dataSource, pageNum: current, pageSize, total } = data;
          this.setState({
            dataSource, pageInfo: { current, pageSize, total },
            columns: _.map(columns, (it) => ({
              ...it, title: (
                <span dangerouslySetInnerHTML={{ __html: it.title }}></span>
              )
            }))
          });
        }
      });
  };
  handleExport = () => {
    const url = `/api/bs/hrmsalary/welfare/overView/export?${convertToUrlString(_.pick(this.props, ["billMonth", "paymentOrganization"]))}`;
    window.open(url, "_blank");
  };

  render() {
    const { pageInfo, loading, dataSource, columns } = this.state, { billMonth } = this.props;
    const pagination = {
      ...pageInfo,
      showTotal: total => `${getLabel(18609, "共")} ${total} ${getLabel(18256, "条")}`,
      showQuickJumper: true,
      showSizeChanger: true,
      pageSizeOptions: ["10", "20", "50", "100"],
      onShowSizeChange: (current, pageSize) => {
        this.setState({ pageInfo: { ...pageInfo, current, pageSize } }, () => this.getOverViewList());
      },
      onChange: current => this.setState({ pageInfo: { ...pageInfo, current } }, () => this.getOverViewList())
    };
    return (
      <div className="normalWapper">
        <div className="topContent">
          <div className="month">
            <span>
              {getLabel(111, "账单月份")}
              <Tooltip placement="topLeft" title={getLabel(111, "提示：正常缴纳，账单月份即社保福利缴纳月份")}>
                <Icon type="question-circle"/>
              </Tooltip>
            </span>
            <span>{billMonth}</span>
          </div>
        </div>
        <div className="tabOption">
          <i className="iconfont icon-export" onClick={this.handleExport} title={getLabel(111, "导出")}/>
        </div>
        {/* table */}
        <div className="tableWrapper">
          <WeaNewScroll height="100%">
            <Spin spinning={loading}>
              <WeaTable rowKey="id" columns={columns} dataSource={dataSource} loading={loading} pagination={pagination}
                        scroll={{ x: 1200 }}/>
            </Spin>
          </WeaNewScroll>
        </div>
      </div>
    );
  }
}
