/*
 * Author: 黎永顺
 * name: 调薪记录
 * Description:
 * Date: 2023/11/13
 */
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { WeaLocaleProvider, WeaTable } from "ecCom";
import Authority from "../../../mySalary/authority";
import { mySalaryRecordList4Card, recordList } from "../../../../apis/mySalaryBenefits";

const getLabel = WeaLocaleProvider.getLabel;
const API = {
  recordList, mySalaryRecordList4Card
};

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

  componentDidMount() {
    const { mySalaryStore: { initRecordData, getRecordData }, type } = this.props;
    !type ? initRecordData(this.getRecordList) : getRecordData({
      status: "1", token: "",
      callback: () => this.getRecordList()
    });
  }

  getRecordList = () => {
    const { pageInfo } = this.state;
    const { employeeId, type = "recordList" } = this.props;
    this.setState({ loading: true });
    API[type]({ ...pageInfo, employeeId }).then(({ status, data }) => {
      this.setState({ loading: false });
      if (status) {
        const { columns, list: dataSource, pageNum: current, pageSize, total } = data;
        this.setState({
          dataSource, pageInfo: { ...pageInfo, current, pageSize, total },
          columns: _.map(columns, it => ({ ...it, width: 150 }))
        });
      }
    }).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.getRecordList());
      },
      onChange: current => {
        this.setState({
          pageInfo: { ...pageInfo, current }
        }, () => this.getRecordList());
      }
    };
    return (
      <Authority ecId={`${this && this.props && this.props.ecId || ""}_Authority@lulowc`}
                 store={this.props.mySalaryStore}>
        <WeaTable rowKey="id" dataSource={dataSource} pagination={pagination}
                  loading={loading} columns={columns} scroll={{ x: 1200, y: `calc(100vh - 166px)` }}
        />
      </Authority>
    );
  }
}

export default Index;
