/*
 * Author: 黎永顺
 * name:社保福利档案-操作日志
 * Description:
 * Date: 2023/11/6
 */
import React, { Component } from "react";
import { inject, observer } from "mobx-react";
import { WeaDialog, WeaLocaleProvider, WeaTable } from "ecCom";
import { getAdjustHistoryList } from "../../../../../apis/welfareArchive";
import { getLogSearchsForm, logConditions } from "../../config";
import moment from "moment";
import "./index.less";

const getLabel = WeaLocaleProvider.getLabel;

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

  componentDidMount() {
    const { archivesStore: { logForm } } = this.props;
    this.setState({
      conditions: _.map(logConditions, it => ({
        ...it, items: _.map(it.items, o => ({ ...o, label: getLabel(o.lanId, o.label) }))
      }))
    }, () => logForm.initFormFields(this.state.conditions));
  }

  componentWillReceiveProps(nextProps, nextContext) {
    if (nextProps.visible !== this.props.visible && nextProps.visible) this.getAdjustHistoryList();
    if (nextProps.visible !== this.props.visible && !nextProps.visible) {
      this.setState({
        dataSource: [], columns: [], pageInfo: { current: 0, pageSize: 10, total: 0 },
        loading: false
      });
    }
  }

  getAdjustHistoryList = (extra = {}) => {
    const { pageInfo } = this.state;
    const { archivesStore: { logForm } } = this.props;
    const payload = { ...pageInfo, ...logForm.getFormParams(), ...extra };
    this.setState({ loading: true });
    getAdjustHistoryList(payload).then(({ status, data }) => {
      this.setState({ loading: false });
      if (status) {
        const { columns, list: dataSource, pageNum: current, pageSize, total } = data;
        this.setState({
          pageInfo: { ...pageInfo, current, pageSize, total },
          dataSource, columns: _.map(columns, it => {
            if (it.dataIndex === "operateTime") {
              return { ...it, render: (text) => (<span>{moment(text).format("YYYY-MM-DD")}</span>) };
            }
            return { ...it };
          })
        });
      }
    }).catch(() => this.setState({ loading: false }));
  };

  render() {
    const { loading, dataSource, columns, pageInfo, conditions } = this.state;
    const { archivesStore: { logForm } } = 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.getAdjustHistoryList());
      },
      onChange: current => {
        this.setState({
          pageInfo: { ...pageInfo, current }
        }, () => this.getAdjustHistoryList());
      }
    };
    const scrollHeight = this.logRef ? this.logRef.state.height - 210 : 606.6;
    return (
      <WeaDialog
        {...this.props} title={getLabel(111, "基数调整记录")}
        ref={dom => this.logRef = dom} className="logDialog" initLoadCss
        style={{
          width: 1150,
          height: 606.6,
          minHeight: 200,
          minWidth: 380,
          maxHeight: "90%",
          maxWidth: "90%",
          overflow: "hidden",
          transform: "translate(0px, 0px)"
        }}
      >
        <div className="logDialogContent">
          {getLogSearchsForm(logForm, conditions, () => this.getAdjustHistoryList({ current: 1 }))}
          <WeaTable
            columns={columns} dataSource={dataSource}
            loading={loading} className="logTable"
            pagination={pagination} scroll={{ y: `${scrollHeight}px` }}
          />
        </div>
      </WeaDialog>
    );
  }
}

export default Index;
