/*
 * Author: 黎永顺
 * name: 员工明细列表
 * Description:
 * Date: 2023/5/24
 */
import React, { Component } from "react";
import { WeaLocaleProvider, WeaTable } from "ecCom";
import { Dropdown, Menu } from "antd";
import { statisticsEmployeeList } from "../../../apis/statistics";
import "../index.less";

const { getLabel } = WeaLocaleProvider;

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

  componentDidMount() {
    this.statisticsEmployeeList();
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.year !== this.props.year) this.statisticsEmployeeList(nextProps);
  }

  statisticsEmployeeList = (props) => {
    const { pageInfo } = this.state;
    const payload = {
      year: props ? props.year : this.props.year,
      keyword: props ? props.keyword : this.props.keyword,
      ...pageInfo
    };
    this.setState({ loading: true });
    statisticsEmployeeList(payload).then(({ status, data }) => {
      this.setState({ loading: false });
      if (status) {
        const { columns, list: dataSource, pageNum: current, total, pageSize } = data;
        this.setState({
          pageInfo: { ...pageInfo, current, pageSize, total },
          dataSource,
          columns: [...columns, {
            title: "操作",
            dataIndex: "operate",
            width: 120,
            render: (_, record) => {
              return <React.Fragment>
                <a target="_blank" style={{ marginRight: 10 }}
                   href={`${window.location.origin}/spa/hrmSalary/static/index.html#/main/hrmSalary/analysisOfSalaryStatistics/${record.id}?name=${record.name}&dept=${record.department || ""}`}>{getLabel(111, "查看")}</a>
                <Dropdown
                  overlay={
                    <Menu>
                      <Menu.Item>
                        <a href="javascript:void(0);"
                           onClick={() => this.props.onFilterLog("log", record.id)}>{getLabel(545781, "操作日志")}</a>
                      </Menu.Item>
                    </Menu>
                  }>
                  <a href="javascript:void(0)"><i className="icon-coms-more"/></a>
                </Dropdown>
              </React.Fragment>;
            }
          }]
        });
      }
    }).catch(() => this.setState({ loading: false }));
  };

  render() {
    const { dataSource, loading, columns, pageInfo } = this.state;
    const pagination = {
      ...pageInfo,
      showTotal: total => `${getLabel(111, "共")} ${total} ${getLabel(111, "条")}`,
      showQuickJumper: true,
      showSizeChanger: true,
      pageSizeOptions: ["10", "20", "50", "100"],
      onShowSizeChange: (current, pageSize) => {
        this.setState({
          pageInfo: { ...pageInfo, current, pageSize }
        }, () => {
          this.statisticsEmployeeList();
        });
      },
      onChange: current => {
        this.setState({
          pageInfo: { ...pageInfo, current }
        }, () => {
          this.statisticsEmployeeList();
        });
      }
    };
    return (
      <WeaTable
        rowKey="id"
        className="employeeTableWrapper"
        dataSource={dataSource}
        pagination={pagination}
        loading={loading}
        columns={columns}
        scroll={{ y: `calc(100vh - 182px)` }}
      />
    );
  }
}

export default EmployeeDetails;
