/*
 * Author: 黎永顺
 * name: 工资单
 * Description:
 * Date: 2023/11/13
 */
import React, { Component } from "react";
import { WeaLocaleProvider, WeaTable } from "ecCom";
import { Dropdown, Menu } from "antd";
import { mySalaryBillList, mySalaryBillList4Card } from "../../../../apis/mySalaryBenefits";
import moment from "moment";

const getLabel = WeaLocaleProvider.getLabel;
const API = {
  mySalaryBillList, mySalaryBillList4Card
};

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

  componentDidMount() {
    this.getMySalaryBillList(this.props);
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.salaryYearMonth !== this.props.salaryYearMonth) this.getMySalaryBillList(nextProps);
  }

  getMySalaryBillList = (props) => {
    this.setState({ loading: true });
    const { pageInfo } = this.state;
    const { salaryYearMonth, employeeId, type = "mySalaryBillList" } = props;
    API[type]({ salaryYearMonth, employeeId, ...pageInfo }).then(({ status, data }) => {
      this.setState({ loading: false });
      if (status) {
        const { columns, datas: dataSource, pageInfo: { pageNum: current, pageSize, total } } = data;
        this.setState({
          dataSource, pageInfo: { ...pageInfo, current, pageSize, total },
          columns: _.map(columns, it => {
            if (it.column === "salaryYearMonth" || it.column === "sendTime") {
              return {
                dataIndex: it.column, title: it.text, width: it.width,
                render: (__, record) => (<span>{record[it["column"]]}</span>)
              };
            }
            return {
              dataIndex: it.column, title: it.text, width: it.width
            };
          })
        });
      }
    }).catch(() => this.setState({ loading: false }));
  };

  render() {
    const { dataSource, loading, columns, pageInfo } = this.state;
    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.getMySalaryBillList(this.props));
      },
      onChange: current => {
        this.setState({
          pageInfo: { ...pageInfo, current }
        }, () => this.getMySalaryBillList(this.props));
      }
    };
    return (
      <WeaTable rowKey="id" dataSource={dataSource} pagination={pagination}
                scroll={{ y: `calc(100vh - 166px)` }}
                loading={loading} columns={[...columns, {
        dataIndex: "options",
        title: getLabel(30585, "操作"),
        width: 120,
        render: (_, record) => (<React.Fragment>
          <a style={{ marginRight: 10 }}
             href={`${window.location.origin}/spa/hrmSalary/static/index.html#/main/hrmSalary/mySalary/${record.id}`}
             target="_blank">{getLabel(33564, "查看")}</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>)
      }]}
      />
    );
  }
}

export default Index;
